Here's how to send raw transaction BTC using Bitcoin-cli command
Asked Answered
P

3

37

Scenario:

Michael receives 0.05000000 BTC from Pablo and another 0.01000000 BTC from Kuradang. Michael also wants to send 0.02500000 BTC to Berteng. Each amount that Michael receives has the corresponding txid and other details. Lets check that out using listunspent command then create a raw transaction using createrawtransaction after that sign it using signrawtransaction and send that raw transaction using sendrawtransaction.

Given:

  • Michael's Bitcoin Address: mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe
  • Berteng's Bitcoin Address: mxh3H416KCRoBDiweSESew5YJyAk1nxLrN
  • Send to Berteng: 0.02500000 mBTC

Step 1)

listunspent

Format:

$ bitcoin-cli listunspent [misconf=1] [max_number_confirmation=99999999] '["<wallet_address>", ...]'

Lets check Michael's list of unspent using with his address.

Execute:

$ bitcoin-cli listunspent 1 99999999 '["mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe"]'

Result:

[{
    "txid": "12b8e7ede4992f4d30f93idj3085746951d945e39f40becebd7c290af8c2e7ad",
    "vout": 1,
    "address": "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe",
    "account": "micz",
    "scriptPubKey": "76a9143aa28e1740a6a5a2190975b6e7f1ad67aaec9a3988ac",
    "amount": 0.05000000,
    "confirmations": 94,
    "spendable": true
}, {
    "txid": "8443bc63b65d569ff9ekwm37sy3b67b9c7c6f8f386c3cdf372b260961b64ec9fc",
    "vout": 1,
    "address": "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe",
    "account": "micz",
    "scriptPubKey": "76a9143aa28e1740a6a5a2190975b6e7f1ad67aaec9a3988ac",
    "amount": 0.01000000,
    "confirmations": 93,
    "spendable": true
}]

What we see here is the results that assigned 50 and 10 mBTC to our address mkrz…. To spend this output we will create a new transaction.

Step 2)

createrawtransaction

We need to choose some blocks with sufficient amount from the result of listunspent. Since we only need to send 0.02500000 mBTC I think the first block has the enough amount to make the transaction.

Format:

$ bitcoin-cli createrawtransaction
    '[{
        "txid" : "<txid_of_selected_block>",
        "vout" : <vout>
    }]'
    '{"<recipient_address>": <amount_to_send>, "<sender_address>": <amount_change>}'

To pay the fee, we will reduce the change output by 0.5 millibits as you can see below.

Execute:

$ bitcoin-cli createrawtransaction
    '[{
        "txid" : "12b8e7ede4992f4d30f93idj3085746951d945e39f40becebd7c290af8c2e7ad",
        "vout" : 0
    }]'
    '{"mxh3H416KCRoBDiweSESew5YJyAk1nxLrN": 0.025, "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe": 0.0245}'

Result

0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c0000000000ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000

The createrawtransaction command produces a raw hex string that encodes the transaction details we supplied. If you want to decode the hex just use decoderawtransaction command.

Step 3)

signrawtransaction

signs the transaction in the serialized transaction format using private keys stored in the wallet or provided in the call.

Format:

$ bitcoin-cli signrawtransaction <hex_createrawtransaction>

Execute:

$ bitcoin-cli signrawtransaction 0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c0000000000ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000

Result:

{
   "hex" : "0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c000000006a47304402203e8a16522da80cef66bacfbc0c800c6d52c4a26d1d86a54e0a1b76d661f020c9022010397f00149f2a8fb2bc5bca52f2d7a7f87e3897a273ef54b277e4af52051a06012103c9700559f690c4a9182faa8bed88ad8a0c563777ac1d3f00fd44ea6c71dc5127ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000",
   "complete" : true
}

Now the signrawtransaction command returns another hex-encoded raw transaction.

Step 4)

sendrawtransaction

RPC validates a transaction and broadcasts it to the peer-to-peer network.

Format:

$ bitcoin-cli sendrawtransaction <hex_signrawtransaction>

Execute:

$ bitcoin-cli sendrawtransaction 0100000001e34ac1e2baac09c366fce1c2245536bda8f7db0f6685862aecf53ebd69f9a89c000000006a47304402203e8a16522da80cef66bacfbc0c800c6d52c4a26d1d86a54e0a1b76d661f020c9022010397f00149f2a8fb2bc5bca52f2d7a7f87e3897a273ef54b277e4af52051a06012103c9700559f690c4a9182faa8bed88ad8a0c563777ac1d3f00fd44ea6c71dc5127ffffffff02a0252600000000001976a914d90d36e98f62968d2bc9bbd68107564a156a9bcf88ac50622500000000001976a91407bdb518fa2e6089fd810235cf1100c9c13d1fd288ac00000000

Result:

ae74538baa914f3799081ba78429d5d84f36a0127438e9f721dff584ac17b346

The command sendrawtransaction returns a transaction hash (txid) as it submits the transaction on the network. To check the transaction hash you can go to tbtc.blockr.io

For more info see this site mastering bitcoin

Pacha answered 21/7, 2016 at 2:27 Comment(3)
listunspent with a bitcoin address doesn't work for me.. edit: now it worked! thanks! this feature is not even documented..Macrobiotics
Why is vout in input of the transaction you are crating 0, if it is 1 in the listed spendable transaction?Megen
@Megen I believe that's an error. The output number should match the one in the unspent transactionBertabertasi
H
4

Yet another simpler way with some extra fundrawtransaction. Docs say us what inputs and output all required fields. But don't forget empty list is object too:

Step 1)

$ bitcoin-cli createrawtransaction '[]' \
    '{"mxh3H416KCRoBDiweSESew5YJyAk1nxLrN": 0.025}'

Step 2)

$ bitcoin-cli fundrawtransaction <hex_createrawtransaction> \
    '{"changeAddress": "mkrzDhhZtzQm8zgckSs4fMNrvtNJ66zaFe"}'

After second step you can look at the fee generated by your wallet. And change it with feeRate option of fundrawtransaction command. This all can be done before sign and send to network your transaction.

Happening answered 8/10, 2017 at 23:24 Comment(1)
Thank you... this has saved me a lot of work.... I have a question: How does it generate the fee? Is it a high priority fee or a low priority fee? Can the priority be changed? Will the fee generated in this way reflect the current congestion on the network?Ayakoayala
W
4

Multisig implementation of send rawtransaction in regtest environment)

1)Create a rawtransaction

syntax:

bitcoin-cli -regtest createrawtransaction '[{"txid":"","vout":}]' '{"receive_address":amount}'

Example:

 > bitcoin-cli -regtest createrawtransaction
    > '[{"txid":"eee0de90e9878c039f87c9eedbdf5b9a5da157b19e5354a51ff3b2f84c8a901b","vout":0},]'
    > '{"2MxieCJNTKiiBj6U3SjghQaatZYbM7Qn6GW":30}'

2)Sign the raw transaction

syntax

<hexstring> [{"txid":txid,"vout":n,"scriptPubKey":hex},...] [<privatekey1>,...]
bitcoin-cli -regtest signrawtransaction "0200000001a1c33ebb12d94f96effc70c9dd4488faf32dc15269d6a41d115ac956033cadbb0100000000ffffffff0100e1f505000000001976a9145768a869521bb01af8bdd787e6e1e65ec3d9870e88ac00000000" '''
        [
          {
            "txid":"bbad3c0356c95a111da4d66952c12df3fa8844ddc970fcef964fd912bb3ec3a1",
            "vout":1,
            "scriptPubKey":"a91401a8f0509da2396f58d3f9948a76331964524c9687",
            "redeemScript": "5221022d6f957dd76773432d2493edb5601b2d0791286e31c83483ab299672c3d44fc921022dd9c36eece99cc308986a8334c3a0bf24d7ef8b842dacebf56f2477b0f6daab21030544c9613bf27c9773e6fcd79c7786a297188a2647d346da0c3305e22d3e28da53ae"
          }
        ] 
    ''' '''
    [
    "cUdhE3NvePyjqksgJTc5BQfXGDJckPgEKfoRT72x2BRoG2uMS81H"
    ]
    '''

o/p will be a hex which i store as

signed_raw_txn=0200000001a1c33ebb12d94f96effc70c9dd4488faf32dc15269d6a41d115ac956033cadbb01000000fdfe0000483045022100f9561640d9cf6c7fa7decdd2a3e0c40b0f05d167ee96f22dc11b7ef120c8483902201e53915eed3399e07a4e2648ab76c665fca6486d872d10964ad1926e792d2d6001483045022100a0a084182fb84efdf21757b9caa723cb013a469dc47c5bd8007c23f6201260c8022014fa11cb241c2e63ed0a043aceb1c3f89344c22b4ea6662f93da30820ef18796014c695221022d6f957dd76773432d2493edb5601b2d0791286e31c83483ab299672c3d44fc921022dd9c36eece99cc308986a8334c3a0bf24d7ef8b842dacebf56f2477b0f6daab21030544c9613bf27c9773e6fcd79c7786a297188a2647d346da0c3305e22d3e28da53aeffffffff0100e1f505000000001976a9145768a869521bb01af8bdd787e6e1e65ec3d9870e88ac00000000

3)Send rawtransaction

bitcoin-cli -regtest sendrawtransaction $signed_raw_txn
Weissman answered 19/1, 2018 at 9:26 Comment(1)
How can I get the transaction ID ?Abbie
T
2

I don't have 50 rep points to be able to comment to user3931836, but regarding "how does it generate the fee? What's its priority? Does network congestion affect the fee?"

  1. The fee is whatever amount is left over after you subtract the total output amounts from the total UTXO input amounts:

fee = (sum of UTXO) - (sum of total being sent)

i.e. the fee amount is not overtly specified; the miner simply keeps whatever is unspecified in the output(s), the leftover amount. So you should reduce your "change" output to ensure there is enough to cover the fee.

  1. The fee is neither high priority nor low priority. The transaction itself become high priority (to miners) by you opting to leave a higher fee amount to the miner (miners are greedy and choose the transactions that pay more - which the payer themselves chooses).
  2. The fee is based on the amount of data you are sending in the transaction. The miner looks at the number of bytes in your transaction, then the fee you are paying for the transaction, then computes the fee/byte to prioritise the transaction.

Remember, if your transaction uses 50 UTXOs, then it has a LOT of bytes so they will expect a large payment. But if your transaction uses only 1 UTXO, then it uses a LOT fewer bytes, so you can pay less. Make sure you optimise your selection of UTXOs to reduce the number of bytes in the transaction - then you can provide a smaller fee without impacting its queue "priority". Currently miners expected around 40 satoshis per byte, but check an online tool to see what the estimated processing times are for the various amounts. If you pay them less (per byte), then they'll leave you in the mempool for longer (as a lower "priority" in their context).

Tacye answered 13/4, 2021 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.