Does createrawtransaction always use the following script?

scriptSig: <sig> <pubKey>

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?

2 Answers 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

  • 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
    Commented Oct 21, 2013 at 17:51
  • Also, what does "Yes, that's correct" refer to?
    – bvpx
    Commented Oct 21, 2013 at 17:54
  • 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 Commented Oct 21, 2013 at 18:02
  • 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
    Commented Oct 21, 2013 at 18:47
  • 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 en.bitcoin.it/wiki/Protocol_specification#tx Commented Oct 21, 2013 at 20:23

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.


#!/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([
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)


  "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 <hex>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.