Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitCoin - how to build raw transaction?

According to the list from other question, to build a raw transaction I need:

  1. listunspent - you get the list of all unspent transactions
  2. You determine which ones you want to spend
  3. You use createrawtransaction to create your inputs / outputs, taking txid and vout from the list of transactions that you have that are playing, as well as a list of transactions that constitute a "money supply" for you.
  4. Use signrawtransaction to sign the created transaction
  5. Use sendrawtransaction to send the signed transaction

But there is one big problem - listunspent shows unspent amounts that are actually already spent, thus, I will have insufficient funds error in future or negative balance...

E.g.:

Created address 15d8uY8uubNFnn3DXaoGN123ZCRkm3HJK9.
Sent to it 0.01 BTC.
Later sent from this address 0.00400000 with fee 0.00010714:

    [
      {
        "account": "login_67",
        "address": "15d8uY8uubNFnn3DXaoGN123ZCRkm3HJK9",
------> "category": "receive",
        "amount": 0.01000000,
        "label": "login_67",
        "vout": 0,
        "confirmations": 628,
        "blockhash": "0000000000000000006bf20ac85a50d3e014a636b51231c78c6b27fe0a8e9223",
        "blockindex": 2,
        "blocktime": 1463753010,
        "txid": "f37679b68c8ee3cd362b5bb15b2d8007af4eab173eee2d6269a132b9491a55ad",
        "walletconflicts": [
        ],
        "time": 1463752690,
        "timereceived": 1463752690,
        "bip125-replaceable": "no"
      },
      {
        "account": "login_67",
        "address": "1BeqRNunznfwuFHnufXLFRhaCDm1mrvMS6",
------> "category": "send",
        "amount": -0.00400000,
        "vout": 1,
        "fee": -0.00010714,
        "confirmations": 609,
        "blockhash": "000000000000000000699cd44c4aed1eeb9c66181f6260bfe41473e66e1ece06",
        "blockindex": 1614,
        "blocktime": 1463767099,
        "txid": "9a39b070d8f21ddf9bd16e641c4b252c2da701336fd36b707ca072fd1ca11d58",
        "walletconflicts": [
        ],
        "time": 1463766747,
        "timereceived": 1463766747,
        "bip125-replaceable": "no",
        "abandoned": false
      }
    ]

Getbalance shows correct balance of 0.00589286

And listunspent shows wrong info about available funds:

[
  {
    "txid": "f37679b68c8ee3cd362b5bb15b2d8007af4eab173eee2d6269a132b9491a55ad",
    "vout": 0,
    "address": "15d8uY8uubNFnn3DXaoGN123ZCRkm3HJK9",
    "account": "login_67",
    "scriptPubKey": "76a91432b613c311d4c9c72f3ed1ba5e2c1d96645bd2da88ac",
    "amount": 0.01000000,
    "confirmations": 628,
    "spendable": true
  }
]

Like there wasn't any send transaction.

So how can I collect up all unspent funds from TXs?

Thanks for answers.

like image 231
Alexander M. Avatar asked Jul 01 '16 19:07

Alexander M.


People also ask

What is raw transaction in Bitcoin?

A Bitcoin raw transaction is a chunk of bytes that contains the info about a Bitcoin transaction. That raw transaction will become part of the blockchain when a miner adds it to a block. We are going to create a Raw Transaction step by step using bitcoind (Bitcoin Core server).

How do I send a raw transaction?

First decode the raw transaction hex to bytes format. Now initialize a new types. Transaction pointer and call DecodeBytes from the go-ethereum rlp package passing it the raw transaction bytes and the pointer to the ethereum transaction type. RLP is an encoding method used by Ethereum to serialized and derialized data.

How are Bitcoin transactions made?

Using the private key associated with their bitcoin, a user can sign transactions and thereby transfer the value to a new owner. The transaction is then broadcast to the network to be included in the blockchain. Anyone who possesses your private keys has access to your bitcoin.


1 Answers

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 T 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 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 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 blockchain.info or testnet.blockchain.info for Testnet network.

like image 133
Michael Blanza Avatar answered Sep 20 '22 01:09

Michael Blanza