<p>Yes that is correct.</p>

<p>What you can do is:</p>

<ul>
<li>listunspent</li>
<li>createrawtransaction with receiver:amount and your change:amount 
<ul>
<li>(make the amount differ to pay the fee) </li>
</ul></li>
</ul>

<p>Take the raw hex transaction and use it with <a href="http://brainwallet.org/#tx" rel="nofollow">http://brainwallet.org/#tx</a> to be able to edit in JSON format. Don't worry, when your done you can sign the raw transaction in your local client.</p>

<p>About scriptPubKey, that is a little harder, and you should only experiment with this on testnet. You can try to understand this transaction <a href="https://en.bitcoin.it/wiki/Script#Transaction_puzzle" rel="nofollow">https://en.bitcoin.it/wiki/Script#Transaction_puzzle</a> and also try to understand how the stack works by playing around with <a href="http://wiki.forthfreak.net/index.cgi?jsforth" rel="nofollow">jsforth</a></p>

CC BY-SA 3.0
2013-10-21T17:51:57.377Z
I have a good idea of how the stack works, and I don't want to use an external site to create my transactions. I'm assuming brainwallet is open source and I can view how they modify the `scriptPubKey` there? There is no way to simply write `{address:amount,scriptPubKey:stuffhere}` in the second field of `createrawtransaction`?
bvpx
CC BY-SA 3.0
2013-10-21T17:54:31.877Z
Also, what does "Yes, that's correct" refer to?
bvpx
CC BY-SA 3.0
2013-10-21T18:02:20.060Z
about createrawtransaction, yes that's correct, it only uses the basic script. about trusting an external site... you dont, its only for convenience... you should always check with **decoderawtransaction** that it will do what you really want
Jonas Oestman
CC BY-SA 3.0
2013-10-21T18:47:45.570Z
It's not about trust, I want to learn how to do it myself, I guess at this point the best way is to read the sourcecode of brainwallet and understand how they are doing it. Unless there is documentation somewhere outlining the process.
bvpx
CC BY-SA 3.0
2013-10-21T20:23:58.610Z
brainwallet is only one implementation of how to make a raw transaction, here you can learn about the raw hex and what every bit means https://en.bitcoin.it/wiki/Protocol_specification#tx
Jonas Oestman
CC BY-SA 3.0
2013-10-21T20:32:45.823Z
Okay, so I would construct a raw transaction using that specification, then sign it and send it with bitcoind after I am sure the data is correct using `decoderawtransaction`?
bvpx
CC BY-SA 3.0
2013-10-21T21:37:19.577Z
Yes that is exactly right! You can check an already signed transaction with **signrawtransaction** with empty arguments like so [] [], and if all is good it will respond with the same raw transaction and complete:true
Jonas Oestman
CC BY-SA 3.0
2013-10-21T21:50:39.420Z
I'm a bit confused about the transaction header. I've dug up some raw transactions I've signed previously with `createrawtransaction` then `signrawtransaction`, I am finding that the first 4 bytes are always the same (as expected). However the next 12 bytes that represent the "tx command" are different. How is that possible? Also, what exactly is the 4 byte payload checksum, and how is it derived?
bvpx
CC BY-SA 3.0
2013-10-21T22:26:33.560Z
The transaction starts with 01 00 00 00, so you dont need to care about how the transaction is broadcasted and how the payload checksum is calculated or [why](http://bitcoin.stackexchange.com/questions/2788/what-are-the-february-20-changes-to-the-bitcoin-protocol)
Jonas Oestman
CC BY-SA 3.0
2013-10-21T22:30:57.963Z
Sorry to keep this thread going, but... what do you mean "I don't need to care"? If I'm writing this entire transaction by hand, how do I know what to write for those bits? You're saying I can write anything there and it will be fine? It's extremely frustrating to try to learn this protocol because the documentation is very poor.
bvpx
CC BY-SA 3.0
2013-10-22T00:44:02.537Z
Are you going to create software that can broadcast your transaction to the bitcoin network? If you are, go to the [github](https://github.com/bitcoin/bitcoin) repository. If you will use **sendrawtransaction** from bitcoin-qt or bitcoind, then you do not need to use the Message header.
Jonas Oestman
CC BY-SA 3.0
2013-10-22T04:46:48.883Z
Something is very unclear to me: if I can't use `createrawtransaction` to write a transaction with a custom script, how do I build the raw transaction that I eventually sign and broadcast with `sendrawtransaction`?
bvpx
CC BY-SA 3.0
2013-10-22T05:51:33.913Z
Your script need to collect "prev_outputs" that you know you can sign later. Then create a JSON transaction. Convert that JSON to a raw hex transaction and sign it with **signrawtransaction** (or you can write your own code). I found how you can send it with [python](https://github.com/slush0/sendtx)
Jonas Oestman
CC BY-SA 3.0
2013-10-22T06:00:51.320Z
I mean, I know how to create a standard raw transaction with `createrawtransaction`, sign it and broadcast it. My question refers to adding custom OPcodes to the `scriptPubKey`, if I can't do that with `createrawtransaction` then how is it done? I want to create this transaction however possible then send it with regular bitcoind.
bvpx
CC BY-SA 3.0
2013-10-22T06:11:01.430Z
**createrawtransaction** is only a first step, to be able to play around with transactions. I don't think they will ever add the ability to do custom OPcodes. I want the same thing, make transactions by code, but I dont know how. Ask at [bitcointalk](https://bitcointalk.org/index.php?board=39.0)
Jonas Oestman
CC BY-SA 3.0
2013-10-21T17:08:22.773Z
2013-10-21T17:08:22.773Z
Jonas Oestman
<p>After reading: <a href="https://bitcoindev.network/guides/bitcoinjs-lib/bitcoin-script-puzzles/" rel="nofollow noreferrer">https://bitcoindev.network/guides/bitcoinjs-lib/bitcoin-script-puzzles/</a> and reverting versions of some libraries I think I managed it with <a href="https://github.com/bitcoinjs/bitcoinjs-lib" rel="nofollow noreferrer">https://github.com/bitcoinjs/bitcoinjs-lib</a> Unfortunately the transaction wasn't accepted by the network, possibly because non-standard? Not sure. One day I'll debug it.</p>
<p>main.js</p>
<pre><code>#!/usr/bin/env node

// inputs
from_txid = '0f2084be761e258f9bdb00d546b44a173cc31951a7f8d1f64b63c4c5da73cdb4'
from_vout = 0
to_ammount = 63800

to_addr = '3KRk7f2JgekF6x7QBqPHdZ3pPDuMdY3eWR'
to_ammount = 63800
// https://bitcoinfees.gitlab.io/
to_fee = 27 * 255
to_net = to_ammount - to_fee

const bitcoin = require('bitcoinjs-lib')
const network = bitcoin.networks.bitcoin
const redeemScript = bitcoin.script.compile([
  bitcoin.opcodes.OP_ADD,
  bitcoin.opcodes.OP_5,
  bitcoin.opcodes.OP_EQUAL])
const p2wpkhAlice0 = bitcoin.payments.p2sh({ address: to_addr, network })
const txb = new bitcoin.TransactionBuilder(network)
txb.addInput(from_txid, from_vout)
txb.addOutput(p2wpkhAlice0.address, to_net)
console.error('p2wpkhAlice0.address: ' + require('util').inspect(p2wpkhAlice0.address));
const tx = txb.buildIncomplete()
const InputScriptP2SH = bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3])
tx.setInputScript(0, InputScriptP2SH)
console.log(tx.toHex())
</code></pre>
<p>package.json</p>
<pre><code>{
  &quot;name&quot;: &quot;tmp&quot;,
  &quot;version&quot;: &quot;1.0.0&quot;,
  &quot;license&quot;: &quot;AGPL&quot;,
  &quot;dependencies&quot;: {
    &quot;bip32&quot;: &quot;2.0.6&quot;,
    &quot;bip39&quot;: &quot;2.6.0&quot;,
    &quot;bitcoinjs-lib&quot;: &quot;5.2.0&quot;
  }
}
</code></pre>
<p>You have to use these versions of libraries with that code because interfaces changed a lot.</p>
<p>main.js produces a hex raw transaction which you can then try to submit with:</p>
<pre><code>bitcoin-cli sendrawtransaction &lt;hex&gt;
</code></pre>

CC BY-SA 4.0
2024-02-08T09:07:26.357Z
2024-02-08T09:07:26.357Z
Ciro Santilli OurBigBook.com
<p>Does <code>createrawtransaction</code> always use the following script?</p>

<pre><code>scriptPubKey: OP_DUP OP_HASH160 &lt;pubKeyHash&gt; OP_EQUALVERIFY OP_CHECKSIG
scriptSig: &lt;sig&gt; &lt;pubKey&gt;
</code></pre>

<p>If so, how do you create a transaction that uses a custom <code>scriptPubKey</code> value? If not, what is the syntax for writing the JSON that enables custom scriptPubKey values for the transaction using <code>createrawtransaction</code>? </p>

<transactions><script>
Does createrawtransaction have the ability to create a transaction with custom scripts?
bvpx
# Does createrawtransaction have the ability to create a transaction with custom scripts?

(Asked by: username_0 on 2013-10-21)

Does createrawtransaction always use the following script?
scriptPubKey: OP_DUP OP_HASH160  OP_EQUALVERIFY OP_CHECKSIG
scriptSig:  

If so, how do you create a transaction that uses a custom scriptPubKey value? If not, what is the syntax for writing the JSON that enables custom scriptPubKey values for the transaction using createrawtransaction?

**Tags:** transactions, script


---

## 2 answers

### Answer by username_1 on 2013-10-21. Score: 2

Yes that is correct.

What you can do is:

* listunspent
* createrawtransaction with receiver:amount and your change:amount
  + (make the amount differ to pay the fee)

Take the raw hex transaction and use it with <http://brainwallet.org/#tx> to be able to edit in JSON format. Don't worry, when your done you can sign the raw transaction in your local client.

About scriptPubKey, that is a little harder, and you should only experiment with this on testnet. You can try to understand this transaction <https://en.bitcoin.it/wiki/Script#Transaction_puzzle> and also try to understand how the stack works by playing around with [jsforth](http://wiki.forthfreak.net/index.cgi?jsforth)

> **Comments:**
> **username_0**: I have a good idea of how the stack works, and I don't want to use an external site to create my transactions. I'm assuming brainwallet is open source and I can view how they modify the `scriptPubKey` there? There is no way to simply write `{address:amount,scriptPubKey:stuffhere}` in the second field of `createrawtransaction`?  
> **username_0**: Also, what does "Yes, that's correct" refer to?  
> **username_1**: about createrawtransaction, yes that's correct, it only uses the basic script. about trusting an external site... you dont, its only for convenience... you should always check with **decoderawtransaction** that it will do what you really want  
> **username_0**: It's not about trust, I want to learn how to do it myself, I guess at this point the best way is to read the sourcecode of brainwallet and understand how they are doing it. Unless there is documentation somewhere outlining the process.  
> **username_1**: brainwallet is only one implementation of how to make a raw transaction, here you can learn about the raw hex and what every bit means https://en.bitcoin.it/wiki/Protocol_specification#tx  
> **username_0**: Okay, so I would construct a raw transaction using that specification, then sign it and send it with bitcoind after I am sure the data is correct using `decoderawtransaction`?  
> **username_1**: Yes that is exactly right! You can check an already signed transaction with **signrawtransaction** with empty arguments like so [] [], and if all is good it will respond with the same raw transaction and complete:true  
> **username_0**: I'm a bit confused about the transaction header. I've dug up some raw transactions I've signed previously with `createrawtransaction` then `signrawtransaction`, I am finding that the first 4 bytes are always the same (as expected). However the next 12 bytes that represent the "tx command" are different. How is that possible? Also, what exactly is the 4 byte payload checksum, and how is it derived?  
> **username_1**: The transaction starts with 01 00 00 00, so you dont need to care about how the transaction is broadcasted and how the payload checksum is calculated or [why](http://bitcoin.stackexchange.com/questions/2788/what-are-the-february-20-changes-to-the-bitcoin-protocol)  
> **username_0**: Sorry to keep this thread going, but... what do you mean "I don't need to care"? If I'm writing this entire transaction by hand, how do I know what to write for those bits? You're saying I can write anything there and it will be fine? It's extremely frustrating to try to learn this protocol because the documentation is very poor.  
> **username_1**: Are you going to create software that can broadcast your transaction to the bitcoin network? If you are, go to the [github](https://github.com/bitcoin/bitcoin) repository. If you will use **sendrawtransaction** from bitcoin-qt or bitcoind, then you do not need to use the Message header.  
> **username_0**: Something is very unclear to me: if I can't use `createrawtransaction` to write a transaction with a custom script, how do I build the raw transaction that I eventually sign and broadcast with `sendrawtransaction`?  
> **username_1**: Your script need to collect "prev_outputs" that you know you can sign later. Then create a JSON transaction. Convert that JSON to a raw hex transaction and sign it with **signrawtransaction** (or you can write your own code). I found how you can send it with [python](https://github.com/slush0/sendtx)  
> **username_0**: I mean, I know how to create a standard raw transaction with `createrawtransaction`, sign it and broadcast it. My question refers to adding custom OPcodes to the `scriptPubKey`, if I can't do that with `createrawtransaction` then how is it done? I want to create this transaction however possible then send it with regular bitcoind.  
> **username_1**: **createrawtransaction** is only a first step, to be able to play around with transactions. I don't think they will ever add the ability to do custom OPcodes. I want the same thing, make transactions by code, but I dont know how. Ask at [bitcointalk](https://bitcointalk.org/index.php?board=39.0)  

---

### Answer by username_2 on 2024-02-08. Score: 0

After reading: https://bitcoindev.network/guides/bitcoinjs-lib/bitcoin-script-puzzles/ and reverting versions of some libraries I think I managed it with https://github.com/bitcoinjs/bitcoinjs-lib Unfortunately the transaction wasn't accepted by the network, possibly because non-standard? Not sure. One day I'll debug it.
main.js
#!/usr/bin/env node

// inputs
from_txid = '0f2084be761e258f9bdb00d546b44a173cc31951a7f8d1f64b63c4c5da73cdb4'
from_vout = 0
to_ammount = 63800

to_addr = '3KRk7f2JgekF6x7QBqPHdZ3pPDuMdY3eWR'
to_ammount = 63800
// https://bitcoinfees.gitlab.io/
to_fee = 27 * 255
to_net = to_ammount - to_fee

const bitcoin = require('bitcoinjs-lib')
const network = bitcoin.networks.bitcoin
const redeemScript = bitcoin.script.compile([
  bitcoin.opcodes.OP_ADD,
  bitcoin.opcodes.OP_5,
  bitcoin.opcodes.OP_EQUAL])
const p2wpkhAlice0 = bitcoin.payments.p2sh({ address: to_addr, network })
const txb = new bitcoin.TransactionBuilder(network)
txb.addInput(from_txid, from_vout)
txb.addOutput(p2wpkhAlice0.address, to_net)
console.error('p2wpkhAlice0.address: ' + require('util').inspect(p2wpkhAlice0.address));
const tx = txb.buildIncomplete()
const InputScriptP2SH = bitcoin.script.compile([bitcoin.opcodes.OP_2, bitcoin.opcodes.OP_3])
tx.setInputScript(0, InputScriptP2SH)
console.log(tx.toHex())

package.json
{
  "name": "tmp",
  "version": "1.0.0",
  "license": "AGPL",
  "dependencies": {
    "bip32": "2.0.6",
    "bip39": "2.6.0",
    "bitcoinjs-lib": "5.2.0"
  }
}

You have to use these versions of libraries with that code because interfaces changed a lot.
main.js produces a hex raw transaction which you can then try to submit with:
bitcoin-cli sendrawtransaction


---

