7

I want to create and sign bitcoin transaction using only 'bitcoind' and 'bitcoin-cli'.

I can create a transaction using createrawtransaction RPC API, but signing it using 'signrawtransaction' fails.

UPDATE: I figured it out and answered myself below. Take care to have scriptPubKey correct and leave redeemScript empty for simple transactions.

The private key is not stored in bitcoind's built-in wallet. That is, private / public key pair was created by vanity utility (for example).

Setup

  • "from" address": 1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD (From_Addr_1As)
  • "to" address: 1Mu5S5fuztimKZizvYZjKwpYfM9ZFf7zmo (To_Addr_1Mu)
  • transaction that sent funds to From_Addr_1As: 4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e (Prev_Tx_4325)
  • new transaction is New_Tx

Funding

The transaction (Prev_Tx_4325) sent 0.00187 BTC to From_Addr_1As: https://blockchain.info/tx/4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e - also seen in the address transaction view https://blockchain.info/address/1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD

I want to send 0.00186 BTC from From_Addr_1As to To_Addr_1Mu. The 0.00001 BTC fee included.

Bash Shell: Create Raw Transaction (successful):

$ ./bitcoin-cli -rpcpassword=123456 createrawtransaction '[{"txid":"4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e","vout":0}]' '{"1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD":0.00186}'

Command output:

01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba525430000000000ffffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000

Bash Shell: Decode Raw Transaction (successful):

Pay attention: 'scriptSig' is empty!

$ ./bitcoin-cli -rpcpassword=123456 decoderawtransaction 01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba525430000000000ffffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000

Command output:

{ "txid" : "b1ff3da36a6d7c3aaaee41428a7e239baa6243e37d7e1db60ea022d9f994ec52", "version" : 1, "locktime" : 0, "vin" : [ { "txid" : "4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e", "vout" : 0, "scriptSig" : { "asm" : "", "hex" : "" }, "sequence" : 4294967295 } ], "vout" : [ { "value" : 0.00186000, "n" : 0, "scriptPubKey" : { "asm" : "OP_DUP OP_HASH160 01ddbca1a39b60b54fb671297a4a20a7681e0171 OP_EQUALVERIFY OP_CHECKSIG", "hex" : "76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac", "reqSigs" : 1, "type" : "pubkeyhash", "addresses" : [ "1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD" ] } } ] }

Bash Shell: Sign Raw Transaction (successful):

Pay attention:

  • scriptPubKey is copied from the Prev_Tx_4325
  • redeemScript is left empty string: ""

./bitcoin-cli -rpcpassword=123456 signrawtransaction "01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba525430000000000ffffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000" '[{"txid":"4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e","vout":0,"scriptPubKey":"76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac","redeemScript":""}]' '["5J34hLVaP9UAj4BcdbgYoPia93zzDfo8JhTzUN2tf8qqRiZqtjY"]' "ALL"

Command output:

{ "hex" : "01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba52543000000008a47304402206f40f4eb8c6cab7c6dd45a132d437e736a6a0dddfb2b78b10e6efbcaf61592f602200d329a57d7a0c969cc349f41852da4ba1a3bc2245e656bdd41780455b97bf84a014104aa49fbe6608076318ff09171e3c2b4a2effa52d53a417371140642996693ae3ac53ce300fff7fef650d0a2418b087a237aa6838eed3bdfad0ec0069df7209f4affffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000", "complete" : true }

Bash Shell: Decode signed transaction (successful):

$ ./bitcoin-cli -rpcpassword=123456 decoderawtransaction 01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba52543000000008a47304402206f40f4eb8c6cab7c6dd45a132d437e736a6a0dddfb2b78b10e6efbcaf61592f602200d329a57d7a0c969cc349f41852da4ba1a3bc2245e656bdd41780455b97bf84a014104aa49fbe6608076318ff09171e3c2b4a2effa52d53a417371140642996693ae3ac53ce300fff7fef650d0a2418b087a237aa6838eed3bdfad0ec0069df7209f4affffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000

Command output:

{ "txid" : "267550934eb413a623459464d88cb14ff47fa192df720cf8485b590812db8fba", "version" : 1, "locktime" : 0, "vin" : [ { "txid" : "4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e", "vout" : 0, "scriptSig" : { "asm" : "304402206f40f4eb8c6cab7c6dd45a132d437e736a6a0dddfb2b78b10e6efbcaf61592f602200d329a57d7a0c969cc349f41852da4ba1a3bc2245e656bdd41780455b97bf84a01 04aa49fbe6608076318ff09171e3c2b4a2effa52d53a417371140642996693ae3ac53ce300fff7fef650d0a2418b087a237aa6838eed3bdfad0ec0069df7209f4a", "hex" : "47304402206f40f4eb8c6cab7c6dd45a132d437e736a6a0dddfb2b78b10e6efbcaf61592f602200d329a57d7a0c969cc349f41852da4ba1a3bc2245e656bdd41780455b97bf84a014104aa49fbe6608076318ff09171e3c2b4a2effa52d53a417371140642996693ae3ac53ce300fff7fef650d0a2418b087a237aa6838eed3bdfad0ec0069df7209f4a" }, "sequence" : 4294967295 } ], "vout" : [ { "value" : 0.00186000, "n" : 0, "scriptPubKey" : { "asm" : "OP_DUP OP_HASH160 01ddbca1a39b60b54fb671297a4a20a7681e0171 OP_EQUALVERIFY OP_CHECKSIG", "hex" : "76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac", "reqSigs" : 1, "type" : "pubkeyhash", "addresses" : [ "1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD" ] } } ] }

Bash Shell: Send Raw Transaction (successful):

./bitcoin-cli -rpcpassword=123456 sendrawtransaction 01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba52543000000008a47304402206f40f4eb8c6cab7c6dd45a132d437e736a6a0dddfb2b78b10e6efbcaf61592f602200d329a57d7a0c969cc349f41852da4ba1a3bc2245e656bdd41780455b97bf84a014104aa49fbe6608076318ff09171e3c2b4a2effa52d53a417371140642996693ae3ac53ce300fff7fef650d0a2418b087a237aa6838eed3bdfad0ec0069df7209f4affffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000 267550934eb413a623459464d88cb14ff47fa192df720cf8485b590812db8fba

Command output: No output on command success!

Conclusion :-)

By explaining and formulating the question I found a solution. It is really worth asking on StackExchange, and I got to the answer by myself!

UPDATE: Thanks for comments - I answered myself below. Ensure scriptPubKey is correct and leave redeemScript empty for simple transactions.

  • 2
    What is the question? – Pieter Wuille Aug 9 '14 at 18:14
  • 2
    Ask the question and then answer it by yourself instead of putting the question and the answer in the "question" – Jan Moritz Aug 9 '14 at 20:07
  • As @JanMoritzLindemann already suggested, your post doesn't fit our format. Please separate it into a question and an answer. You can find a visual explanation of the Stackexchange format in our About. – Murch Aug 10 '14 at 1:31
  • why is your vout->value in btc while mine shows in satoshis ? – AMB Sep 2 '17 at 12:17
4

Answering my own question

Two things basically got into way of creating and signing a raw bitcoin transaction using 'bitcoind' program:

  • Leaving 'redeemScript' to be the empty string
    • It has to be empty for simple bitcoin transaction!
  • Using single-quote mark (') to make the bitcoin RPC call looking nice and not messy.

That's all.

  • when i do sendrawtransaction 01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba52543000000008a47304402206f40f4eb8c6cab7c6dd45a132d437e736a6a0dddfb2b78b10e6efbcaf61592f602200d329a57d7a0c969cc349f41852da4ba1a3bc2245e656bdd41780455b97bf84a014104aa49fbe6608076318ff09171e3c2b4a2effa52d53a417371140642996693ae3ac53ce300fff7fef650d0a2418b087a237aa6838eed3bdfad0ec0069df7209f4affffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000 i get error code: -25 error message: Missing inputs – AMB Sep 2 '17 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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