- Blockchain By Example
- Bellaj Badr Richard Horrocks Xun (Brian) Wu
- 255字
- 2021-06-10 18:53:28
Creating the transaction
At this level, it's time to create a transaction that spends the received transaction. From the listunspent output, we have the necessary ingredients (prevout_hash and prevout_n) to construct our raw transaction. Let's see how.
First, you need to convert the hello world message into hexadecimal, using an online converter (such as https://codebeautify.org/string-hex-converter). The hexadecimal encoded form will be 68656c6c6f20776f726c64.
Then we have to use the createrawtransaction command, which creates a transaction spending the given inputs and creating new outputs. We have to pass as an argument (from the previous output) an object with the following parameters:
- The txid of one of the available outputs
- The vout index (prevout_n for Electrum) of the selected output
- The hexadecimal form of the message
- The destination address (created earlier)
- The total number of satoshis (the smallest unit of the bitcoin currency) to send
Here we are sending one bitcoin, although you can set it to 0:
bitcoin-cli createrawtransaction "[{\"txid\":\"0791521362528725683caedf998006cf68b1cd817be1694ef0daca265d9b4252\", \"vout\": 1}]" "{\"data\":\"68656c6c6f20776f726c64\",\"2MsHsi4CHXsaNZSq5krnrpP4WShNgtuRa9U\":1.0000000}"
You'll get the following serialized long hex-encoded string, representing our raw transaction:
020000000152429b5d26cadaf04e69e17b81cdb168cf068099dfae3c6825875262135291070100000000ffffffff0200000000000000000d6a0b68656c6c6f20776f726c6400e1f5050000000017a914008051b4d96aa26269dfd36af0eb9c2b2fa894568700000000