How to convert a transaction into hex before using the sendrawtransaction API?

0

0

I've been trying to broadcast a transaction using sendrawtransaction of bitcoind. I keep getting an error : "TX decode failed.", Code -22.

I suspect I'm not properly formatting the transaction into hex before attempting to send.

 btc.sendrawtransaction(new_tx.to_json.unpack("H*")[0])

Here is a representation of my transaction in hex : 7b0a20202268617368223a22333235343463303630316532323932626566303039353265396636303437 38373665383637656336373664643036306163323365356563666361386135656335222c0a202022766572223a312c0a20202276696e5f737a223a312c0a202022766f75745f737a223a312c0a2020226c6f636b5f74696d65223a302c0a20202273697a65223a3139332c0a202022696e223a5b0a202020207b0a20202020202022707265765f6f7574223a7b0a20202020202020202268617368223a2239636239643035363965373135346662383565343538343162633564336663633734393961636431633831326264666565366535653961386433323933313664222c0a2020202020202020226e223a300a2020202020207d2c0a20202020202022736372697074536967223a22333034363032323130303935393761353761373036326537303762323830323439663662653831663434356263636663313038336264643231326465643231353961316534653137616330323231303065346163383535366362653237316163363631653965383364306439663564616333633032393330303263613836336230386431386236386434323361636461303120303333396535613566383031343165643664663566343436326461623831323161353461333365386163633161656235326639626264616465613138663033393963220a202020207d0a20205d2c0a2020226f7574223a5b0a202020207b0a2020202020202276616c7565223a22302 3031303030303030222c0a202020202020227363726970745075624b6579223a224f505f445550204f505f48415348313630203662343633623439373835393930386439646236\ 3562393734623434333532386232396132613932204f505f455155414c564552494659204f505f434845434b534947220a202020207d0a20205d0a7ee

  • Is this how I transform a transaction into hex in ruby ?
  • Is there anything else I'm missing ?

Attilah

Posted 2014-02-04T09:23:40.280

Reputation: 103

Could you give more info? For example, what is returned by new_tx.to_json.unpack("H*")[0]?Zergatul 2014-02-04T09:30:04.547

new_tx.to_json.unpack("H*")[0] is supposed to return the hex representation of the string.Attilah 2014-02-04T09:32:35.483

How we can analyze your problem, if we don't see your transaction in hex?Zergatul 2014-02-04T09:35:11.900

Looks like your transaction is totally invalid. Every transaction must start with 4 bytes version integer: 01000000 (in hex)Zergatul 2014-02-04T09:40:40.380

this transaction is to be sent on testnet. should those also start with 01000000 ?Attilah 2014-02-04T09:47:08.207

yes, there are no differences in version dataZergatul 2014-02-04T09:50:53.650

hmmm, what is the best way to transform a transaction in json format into hex ?Attilah 2014-02-04T09:53:52.447

Answers

4

You cannot convert json string to hex representation. Transactions have their own binary format (you can read in wiki: https://en.bitcoin.it/wiki/Transactions). To create transaction in hex format, you can use bitcoin-qt (or bitcoind) function createrawtransaction. Example of using:

createrawtransaction
'[
    { "txid": "10d5b7f61be2fa2e5979043a7071d3dbd371bc72cefea22e43b2dd54a6e27a99", "vout": 1 }
]'
'{
    "14rbFswzZfkPGkbFZ7Ffj2qhQA1omvgiUx": 0.099
}'

This call will return raw transaction in hex:

0100000001997ae2a654ddb2432ea2fece72bc71d3dbd371703a0479592efae21bf6b7d5100100000000ffffffff01e00f9700000000001976a9142a495afa8b8147ec2f01713b18693cb0a85743b288ac00000000

After that, you must call signrawtransaction:

signrawtransaction 0100000001997ae2a654ddb2432ea2fece72bc71d3dbd371703a0479592efae21bf6b7d5100100000000ffffffff01e00f9700000000001976a9142a495afa8b8147ec2f01713b18693cb0a85743b288ac00000000

This call will return raw transaction in hex, which you can send to network using sendrawtransaction.

Note: be very careful, you can make mistake in amount, and lose your bitcoins.

Zergatul

Posted 2014-02-04T09:23:40.280

Reputation: 948