Bitcoin transaction composition (ii)

Source: Internet
Author: User

Transferred from: http://618.io/2013/10/27/bitcoin-transaction-02/

Structuring, signing and broadcasting of transactions

The previous chapter introduces the transaction structure, signature and so on, in order to understand Bitcoin more intuitively, to construct and broadcast the complete process of the transaction manually by Bitcoind .

Normal trading 1. Find unused coins (unspent output)

By command: listunspent [minconf=1] [maxconf=9999999] ["address",...] lists the amount of money (transactions) that have not been spent on an address, minconf / maxconf indicates the range of confirmations for that revenue transaction, and, if necessary, to list transactions that are not yet confirmed, minconf set to 0.

Perform:

1 Bitcoind listunspent 0 ' ["1lab618uuwjlmva1q64thzxcloc4397zx3"]

Output:

1234567891011 [{"Txid": "296ea7bf981b44999d689853d17fe0ceb852a8a34e68fcd19f0a41e589132156","vout": 0 ,"address": "1lab618uuwjlmva1q64thzxcloc4397zx3","account": " "," Scriptpubkey ": " 76a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac ","Amount ": 0.19900000,"confirmations": 1}]

We found an unpaid deal for that address, located in the trading 296ea7bf981b4499 ... The No. 0 position of the 9f0a41e589132156.

2. Create a pending transaction

Create pending transactions, by command: createrawtransaction [{"txid":txid,"vout":n},...] {address:amount,...} to complete. We send 0.1BTC to 1q8s4qdrbcbfypg5afnr9tfc57pstkpx1x and pay 0.0001 BTC as a miner's fee. The amount of the input transaction is 0.199 , the output is 0.1 + 0.0001 = 0.1001 , then the remaining: 0.199-0.1001 = 0.0989 , send this as change back to yourself.

Perform:

123 Bitcoind createrawtransaction \' [{"Txid": " 296ea7bf981b44999d689853d17fe0ceb852a8a34e68fcd19f0a41e589132156"," Vout": 0}] ' \ ' {'1q8s4qdrbcbfypg5afnr9tfc57pstkpx1x': 0.1, '1lab618uuwjlmva1q64thzxcloc4397zx3': 0. 0989} '

Output:

1 010000000156211389e5410a9fd1fc684ea3a852b8cee07fd15398689d99441b98bfa76e290000000000ffffffff0280969800000000001976a914fdc 7990956642433ea75cabdcc0a9447c5d2b4ee88acd0e89600000000001976a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac00000000

By command: decoderawtransaction This segment of the hexadecimal string can be decoded.

Perform:

1 Bitcoind decoderawtransaction ' 010000000156211389e5410a9fd1fc684ea3a852b8cee07fd15398689d99441b98bfa76e290000000000ffffffff0280969800000000001976a914fdc 7990956642433ea75cabdcc0a9447c5d2b4ee88acd0e89600000000001976a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac00000000 '

Output:

1234567891011121314151617181920212223242526272829303132333435363738394041424344 {"Txid ": " 54f773a3fdf7cb3292fc76b46c97e536348b3a0715886dbfd2f60e115fb3a8f0 ", "Version ": 1, "locktime ": 0, "vin ": [ {"Txid ": " 296ea7bf981b44999d689853d17fe0ceb852a8a34e68fcd19f0a41e589132156 ", "vout ": 0, "scriptsig ": { "asm ":" , "hex ": " "},"sequence ": 4294967295 }],"vout ": [ {"value ": 0.10000000, "n ": 0, "Scriptpubkey ": { "asm ": " op_dup op_hash160 fdc7990956642433ea75cabdcc0a9447c5d2b4ee op_equalverify op_checksig ", "hex ": " 76a914fdc7990956642433ea75cabdcc0a9447c5d2b4ee88ac ", "reqsigs ": 1, "type ": " Pubkeyhash ", "addresses ": [ "1q8s4qdrbcbfypg5afnr9tfc57pstkpx1x"]}},{"value ": 0.09890000, "n ": 1, "Scriptpubkey ": { "asm ": " op_dup op_hash160 d6c492056f3f99692b56967a42b8ad44ce76b67a op_equalverify op_checksig ", "hex ": " 76a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac ", "reqsigs ": 1, "type ": " Pubkeyhash ", "addresses ": [ "1lab618uuwjlmva1q64thzxcloc4397zx3"]}}]}

At this point, a "blank transaction" has been constructed, the transaction has not been signed with the private key, the field scriptSig is left blank, unsigned transactions are invalid. The TX ID at this time is not the final TX ID, and the TX ID will change after filling in the signature.

When creating trades manually, it is important to pay attention to the values of inputs and outputs 非常容易犯错的是忘记构造找零输出 (if unnecessary, do not construct trades manually). There was a man who had forgotten the change during the structuring of the deal, and the human tragedy that had paid for the miner's fee for the BTC , fortunately, the block, which was included in the deal, was dug up by the famous mining team, "Friedcat," and the team was very kind to return the extra cost.

3. Signature

Trade Signature Using command:

123 Signrawtransaction "Txid": Txid,"Vout": N,"Scriptpubkey": Hex,"Redeemscript": Hex},...] [<privatekey1>,...] \ [Sighashtype="All"]
    • The first parameter is the hexadecimal string of the created pending transaction;
    • The second parameter is a bit like the argument when the transaction was created, but a public key field is required, and the scriptPubKey other node verifies that the transaction was done by public key and signature, so the public key is provided, and if it is a composite address, it needs to be provided redeemScript ;
    • The third parameter is the private key of the address where the currency is to be spent, which is used to sign the transaction, and if the private key has been imported into bitcoind, it does not need to be explicitly provided;
    • The last parameter indicates the type of signature, in the previous article, three types of transaction signatures were introduced.

The signature needs to be found before scriptPubKey the input transaction information can be retrieved (or computed according to its public key), by command: getrawtransaction <txid> [verbose=0] done.

Perform:

1 Bitcoind getrawtransaction 296ea7bf981b44999d689853d17fe0ceb852a8a34e68fcd19f0a41e589132156 1

Output:

123456789101112131415161718192021222324252627282930313233343536 {"hex ": " 01000000010511331f639e974283d3909496787a660583dc88f41598d177e225b5f352314a000000006c493046022100be8c796122ec598295e6dfd66 64a20a7e20704a17f76d3d925c9ec421ca60bc1022100cf9f2d7b9f24285f7c119c91f24521e5483f6b141de6ee55658fa70116ee04d4012103cad07f 6de0b181891b5291a5bc82b228fe6509699648b0b53556dc0057eeb5a4ffffffff0160a62f01000000001976a914d6c492056f3f99692b56967a42b8a d44ce76b67a88ac00000000 ", "Txid ": " 296ea7bf981b44999d689853d17fe0ceb852a8a34e68fcd19f0a41e589132156 ", "Version ": 1, "locktime ": 0, "vin ": [ {"Txid ": " 4a3152f3b525e277d19815f488dc8305667a78969490d38342979e631f331105 ", "vout ": 0, "scriptsig ": { "asm ": " 3046022100be8c796122ec598295e6dfd6664a20a7e20704a17f76d3d925c9ec421ca60bc1022100cf9f2d7b9f24285f7c119c91f24521e5483f6b141 de6ee55658fa70116ee04d401 03cad07f6de0b181891b5291a5bc82b228fe6509699648b0b53556dc0057eeb5a4 ", "hex ": " 493046022100be8c796122ec598295e6dfd6664a20a7e20704a17f76d3d925c9ec421ca60bc1022100cf9f2d7b9f24285f7c119c91f24521e5483f6b1 41de6ee55658fa70116ee04d4012103cad07f6de0b181891b5291a5bc82b228fe6509699648b0b53556dc0057eeb5a4 " },"sequence ": 4294967295 }],"vout ": [ {"value ": 0.19900000, "n ": 0, "Scriptpubkey ": { "asm ": " op_dup op_hash160 d6c492056f3f99692b56967a42b8ad44ce76b67a op_equalverify op_checksig ", "hex ": " 76a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac ", "reqsigs ": 1, "type ": " Pubkeyhash ", "addresses ": [ "1lab618uuwjlmva1q64thzxcloc4397zx3"]}}],"Blockhash ": " 000000000000000488f18f7659acd85b2bd06a5ed2c4439eea74a8b968d16656 ", "confirmations ": "Time ": 1383235737, "blocktime ": 1383235737 }

scriptPubKeyLocated at "Vout" [0]-> "Scriptpubkey", "hex", namely: 76a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac .

Signature using the ECDSA algorithm, on which the "Blank Transaction" is signed, executed:

123 Bitcoind signrawtransaction \" 010000000156211389e5410a9fd1fc684ea3a852b8cee07fd15398689d99441b98bfa76e290000000000ffffffff0280969800000000001976a914fdc 7990956642433ea75cabdcc0a9447c5d2b4ee88acd0e89600000000001976a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac00000000 "\"[{"Txid": "296ea7bf981b44999d689853d17fe0ceb852a8a34e68fcd19f0a41e589132156", "Vout" : 0, "Scriptpubkey": "76a914d6c492056f3f99692b56967a42b8ad44ce76b67a88ac"}] '

Output:

1234 {"hex": " 010000000156211389e5410a9fd1fc684ea3a852b8cee07fd15398689d99441b98bfa76e29000000008c493046022100f9da4f53a6a4a8317f6e7e9cd 9a7b76e0f5e95dcdf70f1b1e2b3548eaa3a6975022100858d48aed79da8873e09b0e41691f7f3e518ce9a88ea3d03f7b32eb818f6068801410477c075 474b6798c6e2254d3d06c1ae3b91318ca5cc62d18398697208549f798e28efb6c55971a1de68cca81215dd53686c31ad8155cdc03563bf3f73ce87b4a affffffff0280969800000000001976a914fdc7990956642433ea75cabdcc0a9447c5d2b4ee88acd0e89600000000001976a914d6c492056f3f99692b 56967a42b8ad44ce76b67a88ac00000000 "," complete": true}

After signing, the signature value is filled into the empty field described above, resulting in a complete transaction. This can be viewed by decoding the commands described above decoderawtransaction .

The final step is to broadcast it out, waiting for the network to propagate to all nodes, about 10-60 seconds to broadcast to the global node, depending on the network connection with your node. Some time later, it will enter the block. The broadcast is sendrawtransaction done by command. If the node is not running, it can be broadcast through the API of the public node, for example: BLOCKCHAIN.INFO/PUSHTX.

Perform:

12 Bitcoind sendrawtransaction \ "010000000156211389e5410a9fd1fc684ea3a852b8cee07fd15398689d99441b98bfa76e29000000008c493046022100f9da4f53a6a4a8317f6e 7e9cd9a7b76e0f5e95dcdf70f1b1e2b3548eaa3a6975022100858d48aed79da8873e09b0e41691f7f3e518ce9a88ea3d03f7b32eb818f606880141047 7c075474b6798c6e2254d3d06c1ae3b91318ca5cc62d18398697208549f798e28efb6c55971a1de68cca81215dd53686c31ad8155cdc03563bf3f73ce 87b4aaffffffff0280969800000000001976a914fdc7990956642433ea75cabdcc0a9447c5d2b4ee88acd0e89600000000001976a914d6c492056f3f9 9692b56967a42b8ad44ce76b67a88ac00000000 "

Output:

1 b5f8da1ea9e02ec3cc0765f9600f49945e94ed4b0c88ed0648896bf3e213205d

Returns the transaction hash value, which is the ID of the transaction. Thus, the complete process of structuring, signing, and sending the transaction is complete.

Synthetic address Trading

The composite address starts with 3, enables multi-party management of assets, greatly improves security, and easily enables the payment of guaranteed three-way transactions based on the Bitcoin native. One M-of-N of the modes:

1 m {PubKey}... {PubKey} n op_checkmultisig

m and N need to meet:

    • 1<=N<=3
    • 1<=M<=N

Can be 1 of 1 , 1 of 2 ,, 2 of 3 etc. combinations, usually selected N=3 :

    • 1 of 3, the maximum private key redundancy. Lost private key loss, 3 of the private key can be signed any one of the coins, even if the loss of 2 will be protected from loss;
    • 2 of 3To improve the redundancy of the private key while solving the single point of trust problem. 3 private key Any 2 the private key can be signed, the three parties do not fully trust the situation, that is, intermediary transactions, very applicable;
    • 3 of 3, to maximize the solution to the problem of money trust, no private key redundancy. Must 3 private key all signed in order to send coins, applicable to multi-party management of important assets, but either side lost the private key caused serious loss;

The transaction construction, signature, and sending process of the composite address is similar to a normal transaction, where only a composite address is created. The Great God Gavin Andresen has demonstrated that the following is excerpted from its gist.

First, three pairs of public keys and private keys are required. The public key creates the address, and the private key is used for signing.

123456 # the most 0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b 1D8334F86/ 5jatxbaumfpyzfrwryaalk48fn6sfjp4rhqq2qsxs8ucfpe4yqu # No.2 04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccff ef4ec6874/5jb7fceh1wtm4ybbg3q3xbt6b525i17kvhy3vmc9aqfr6fh2qgk # No.3 048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1 863d46213/5JFJMGO5FWW9P8GVX48QBYDJNAZR9PMH5S389AXMTDYPT8DDQMW

Use command: createmultisig <nrequired> <‘["key","key"]‘> to synthesize, where the key public key is used, and only the public key is required to create the address. The creation type is 2 of 3 .

Input:

12 Bitcoind createmultisig 2 \' [" 0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b 1d8334f86"," 04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccff ef4ec6874"," 048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1 863d46213"] '

Output:

1234 {"address": "3qjmv3qfvl9suyo34yihaf3srcw3qsinyc","redeemscript": " 52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6 126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f872208 4861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbf B1e754e35fa1c7844c41f322a1863d4621353ae "}

The resulting composite address is: 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC , the address does not have a public key, only, the function is the same as the redeemScript public key. The subsequent construction, signature, and sending process is similar to the above-mentioned ordinary address transaction, omitted.

Bitcoin transaction composition (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.