Bitcoin transaction composition (i)

Source: Internet
Author: User

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

Brief introduction

Trading (Transaction) is the information carrier of the bitcoin system, the smallest unit. Blocks, in turn, pack the base units, affix them to the seals, and concatenate them together. The huge amount of power guarantees the safety of the block and guarantees the security of the individual transaction.

Type

There are three common types of trading: Volume Trading (Generation), synthetic address trading (Script hash), and generic address trading (PubKey hash). The classification is not strictly meaningful, it is simply a distinction made based on the input and output of the transaction.

Generation TX

Each block corresponds to a production trade (Generation TX), which does not enter the transaction, and the dug-out SGD is the source of all the coins.

Script Hash TX

This type of trading is not very common at the moment, and most people may not have heard of it, but it makes sense. The future should be used frequently on certain occasions. The accepted address of this type of transaction is not the usual meaning of the address, but a synthetic address, starting with 3 (yes, 3 is also the Bitcoin address!) )。 A three-to-one public key can generate a composite address. When the build process n of 3 is specified in the N,n range is [1, 3] , if n=1, only need a private key signature to spend the address of the currency, if n=3, you need three private keys to sign in sequence.

PubKey Hash TX

This class is the most common type of transaction, consisting of n inputs and m outputs.

Data

The transaction is the transfer of the currency ownership information, the ownership is registered on the Bitcoin address (public Key). This information is public throughout the network, stored in plaintext (all data in the Bitcoin system is clear) and only needs to be verified with a private key signature if the transfer of currency ownership is required.

Field Size Description Data Type explain
4 Version, versions uint32_t Version number of the transaction data structure
1 + Tx_in count, number of inputs Var_int Enter the number of trades
41+ Tx_in Tx_in[] Enter an array of trades, each input >=41 bytes
1 + Tx_out count, Output quantity Var_int Number of output addresses
+ Tx_out Tx_out[] Enter an array of addresses, >=9 bytes per input
4 Lock_time, lockout time uint32_t See below for explanations

lock_timeis a multi-meaning field that indicates that the transaction is in a locked state before a block of height or before a certain point in time, and cannot be included in the block.

value meaning
0 Immediate effect
< 500000000 Block height, locked before the block (does not take effect)
>= 500000000 The meaning is a Unix timestamp, which is locked before that moment (does not take effect)

If the field for all input transactions for the transaction is sequence INT32 maximum (0xFFFFFFFF), the field is ignored lock_time . Otherwise, the transaction will not be included in the block until it reaches the block height or reaches a certain point.

Example

For illustrative purposes, we read the earlier block data, taking the height 116219 block as an example.

12345678910111213141516171819202122 # ~ Bitcoind getblock 0000000000007c639f2cbb23e4606a1d022fa4206353b9d92e99f5144bd74611{" Hash": "0000000000007c639f2cbb23e4606a1d022fa4206353b9d92e99f5144bd74611", "confirmations": 144667, "size": 1536, "height": 116219, "Version": 1, " merkleroot": "587fefd748f899f84d0fa1d8a3876fdb406a4bb8f54a31445cb72564701daea6", "TX": ["be8f08d7f519eb863a68cf292ca51dbab7c9b49f50a96d13f2db32e432db363e","A387039ECA66297BA51EF2DA3DCC8A0FC745BCB511E20ED9505CC6762BE037BB","2bd83162e264abf59f9124ca517050065f8c8eed2a21fbf85d454ee4e0e4c267","028cfae228f8a4b0caee9c566bd41aed36bcd237cdc0eb18f0331d1e87111743","3a06b6615756dc3363a8567fbfa8fe978ee0ba06eb33fd844886a0f01149ad62"],"Time": 1301705313, "nonce": 1826107553, "bits": "1b00f339", "difficulty": 68977.78463021, " Previousblockhash": "00000000000010D549135EB39BD3BBB1047DF8E1512357216E8A85C57A1EFBFB", " Nextblockhash": "000000000000e9fcc59a6850f64a94476a30f5fe35d6d8c4b4ce0b1b04103a77 "}

There are 5 transactions in the block, the first is generation TX, parsing out to see the specific content:

1234567891011121314151617181920212223242526272829303132 # ~ Bitcoind getrawtransaction be8f08d7f519eb863a68cf292ca51dbab7c9b49f50a96d13f2db32e432db363e 1{" hex": " 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff070439f3001b0134ffffffff014034152a01000 0004341045b3aaa284d169c5ae2d20d0b0673468ed3506aa8fea5976eacaf1ff304456f6522fbce1a646a24005b8b8e771a671f564ca6c03e484a1c39 4bf96e2a4ad01dceac00000000 ", " Txid": "be8f08d7f519eb863a68cf292ca51dbab7c9b49f50a96d13f2db32e432db363e", "Version": 1, "Locktime": 0, "vin": [{" Coinbase": "0439f3001b0134", "sequence": 4294967295 }],"Vout": [{"value": 50.01000000, "n": 0, "Scriptpubkey": {" asm": " 045b3aaa284d169c5ae2d20d0b0673468ed3506aa8fea5976eacaf1ff304456f6522fbce1a646a24005b8b8e771a671f564ca6c03e484a1c394bf96e2 A4ad01dce Op_checksig ", " hex": " 41045b3aaa284d169c5ae2d20d0b0673468ed3506aa8fea5976eacaf1ff304456f6522fbce1a646a24005b8b8e771a671f564ca6c03e484a1c394bf96 E2a4ad01dceac ", "Reqsigs": 1, "type": "PubKey", "Addresses": ["1lgztvotj6qujncurmbuajjkwwqzxkqndn"]}}]," Blockhash": "0000000000007c639f2cbb23e4606a1d022fa4206353b9d92e99f5144bd74611", "confirmations": 145029, "Time": 1301705313, "Blocktime": 1301705313 }

The input to the Generation TX is not a transaction, but a structure with a coinbase field. The value of this field is filled in by the person who dug up the block, which is a "privilege": you can write information to the currency system (people like to use the system data structure field names to name the site, such as blockchain, Coinbase, etc., these words of the various suffix domain names are squatting one empty). Satoshi the value written in the first transaction of Bitcoin coinbase is:

1 "Coinbase":" 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206 261696c6f757420666f722062616e6b73 "

Convert the paragraph 16 into ASCII characters, which is the famous Genesis block message:

1 The Times 03/jan/Chancellor on brink of second bailout for banks

The next show is a three-input, two-output normal transaction:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768 # ~ Bitcoind getrawtransaction 028cfae228f8a4b0caee9c566bd41aed36bcd237cdc0eb18f0331d1e87111743 1{"Hex":"0100000003c9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7000000008a473044022055bac1856ecbc377dd5e869b1a 84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba2a509b5fbd6cac97ff3af0141048aefd78bb A80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65097ce8d30d5a7e5ef8d1005eaafd4b3fbeff ffffffc9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7010000008a47304402206b993231adec55e6085e75f7dc5ca6c 19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22dc580ef1aa6cccef208d0141042ff65bd6b3ef04 253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c031449d31026afffffff fc98620a6c40fc7b3a506ad79af339541762facd1dd80ff0881d773fb72b230da010000008b483045022040a5d957e087ed61e80f1110bcaf4901b531 7c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff2d0efc83ad261da5b4f1d014104a7d1a57e650613d34 14ebd59e3192229dc09d3613e547bdd1F83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a829f3a8fe2fffffffff0200bd0105000000001976a914634228c2 6cf40a02a05db93f2f98b768a8e0e61b88acc096c7a6030000001976a9147514080ab2fcac0764de3a77d10cb790c71c74c288ac00000000 "," Txid": "028cfae228f8a4b0caee9c566bd41aed36bcd237cdc0eb18f0331d1e87111743", "Version": 1, "Locktime": 0, "vin": [{" Txid": "b79a4803014177ea0139c95f09c6903c01bb8f80d039631afd68cabf7eb0f3c9", "Vout": 0, "Scriptsig": {" asm": " 3044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba2a 509b5fbd6cac97ff3af01 048aefd78bba80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65097ce8d30d5a7e5ef8d1005ea Afd4b3fbe ", " hex": " 473044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba 2a509b5fbd6cac97ff3af0141048aefd78bba80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65 097ce8d30d5a7e5ef8d1005eaafd4b3fbe " },"sequence": 4294967295 },{" Txid": "b79a4803014177ea0139c95f09c6903c01bb8f80d039631afd68cabf7eb0f3c9", "Vout": 1, "Scriptsig": {" asm": " 304402206b993231adec55e6085e75f7dc5ca6c19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22d C580ef1aa6cccef208d01 042ff65bd6b3ef04253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c0314 49d31026a ", " hex": " 47304402206b993231adec55e6085e75f7dc5ca6c19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b2 2dc580ef1aa6cccef208d0141042ff65bd6b3ef04253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474 fb9159ab41653a9c1725c031449d31026a " },"sequence": 4294967295 },{" Txid": "Da30b272fb73d78108ff80ddd1ac2f76419533af79ad06a5b3c70fc4a62086c9", "Vout": 1, "Scriptsig": {" asm": " 3045022040a5d957e087ed61e80f1110bcaf4901b5317c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff 2d0efc83ad261da5b4f1d01 04a7d1a57e650613d3414ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a82 9F3A8FE2F ", " hex": " 483045022040a5d957e087ed61e80f1110bcaf4901b5317c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99b Ff2d0efc83ad261da5b4f1d014104a7d1a57e650613d3414ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7 da1c1f42606db15bf554dbe8a829f3a8fe2f " },"sequence": 4294967295 }],"Vout": [{"value": 0.84000000, "n": 0, "Scriptpubkey": {" asm": "op_dup op_hash160 634228c26cf40a02a05db93f2f98b768a8e0e61b op_equalverify op_checksig", " hex": "76a914634228c26cf40a02a05db93f2f98b768a8e0e61b88ac", "Reqsigs": 1, "type": "Pubkeyhash", "Addresses": ["1a3q9pdtr4h8wpvyb8svpinppt8znbhy8h"]}},{"value": 156.83000000, "n": 1, "Scriptpubkey": {" asm": "op_dup op_hash160 7514080ab2fcac0764de3a77d10cb790c71c74c2 op_equalverify op_checksig", " hex": "76a9147514080ab2fcac0764de3a77d10cb790c71c74c288ac", "Reqsigs": 1, "type": "Pubkeyhash", "Addresses": ["1bg44fzsoteyterykc1xhz8facwykhgvq8"]}}]," Blockhash": "0000000000007c639f2cbb23e4606a1d022fa4206353b9d92e99f5144bd74611", "confirmations": 147751, "Time": 1301705313, "Blocktime": 1301705313 }

The fields hex record all the relevant information, followed by the hex various types of field information that is parsed. The following decomposition hex of the content (hex can be seen directly from above):

12345678910111213141516171819202122232425262728293031323334353637383940414243 01000000 //version number, UINT32 Number of input/TX inputs, variable length int. 3 inputs. /*** first set of input Tx ***///Tx Hash, fixed 32 bytesC9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab700000000 //Consumed TX is located in the forward trade output of No. 0, UINT32, fixed 4 bytes 8a //length of the signature, 0x8A = 138 bytes //138-byte length signature with two parts: public key + Signature //Signature length, 0x47 = 71 bytes 3044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172 Ba2a509b5fbd6cac97ff3af01 //Public key length, 0x41 = 65 bytes 048AEFD78BBA80E2D1686225B755DACEA890C9CA1BE10EC98173D7D5F2FEFBBF881A6E918F3B051F8AAAA3FCC18BBF65097CE8D30D5A7E5EF8D1 005eaafd4b3fbeFfffffff//Sequence,0xffffffff = 4294967295, UINT32, fixed 4 bytes/*** The second set of input Tx. In the same vein, omit the decomposition ***/c9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7010000008a47304402206b993231adec55e6085e75f7dc5ca6c19e42e 744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22dc580ef1aa6cccef208d0141042ff65bd6b3ef04253225 405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c031449d31026affffffff/*** Third set of input Tx ***/c98620a6c40fc7b3a506ad79af339541762facd1dd80ff0881d773fb72b230da010000008b483045022040a5d957e087ed61e80f1110bcaf4901b5317 c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff2d0efc83ad261da5b4f1d014104a7d1a57e650613d341 4ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a829f3a8fe2fffffffff the number of//TX outputs, variable length int. Two outputs. /*** first set of output ***/00bd010500000000 //output currency, uint64,8 bytes. The byte order needs to be flipped, ~= 0x000000000501bd00 = 84000000 Satoshi //Output Destination Address bytes, 0x19 = 25 bytes, consisting of some opcode and numeric values //Destination address//0x76-op_dup (Stack ops)//0XA9-op_hash160 (crypto)//0x14-length, 0x14 = 20 bytesA9 HASH160 value of//address, 20 bytes634228c26cf40a02a05db93f2f98b768a8e0e61b//0x88-op_equalverify (bit logic)//0xac-Op_checksig (crypto)AC/*** Second set of output ***/c096c7a603000000 +A9 7514080ab2fcac0764de3a77d10cb790c71c74c2 ac 00000000 //lock_time,uint32, fixed 4 bytes

Tx Hash, commonly known as transaction ID, is hex derived from: Tx Hash = SHA256(SHA256(hex)) . Since each transaction can only be the next input, and there is only one time, then there is no transaction with the same input, then there is no same TX Hash (SHA256 collision probability is very small, so there is no need to consider the problem of Hash collision, like no need to consider the address of the private key is hit by others).

Even so, in the system still produces the same Tx Hash, is a miner brother dug block, packaging block when forget to modify the value of the Generation Tx Coinbase field, the same amount of currency and output to the same address, then the construction of two exactly the same transaction, Located in the first position of the two blocks respectively. This is not a problem for the system, but if one is spent, the other is spent. The same generation TX is equivalent to covering the other, losing the dug-out coins in vain. The transaction ID is e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468, first appears in #91722, and the second appears in #91880.

Qq20131027-2

Trade signature

Signature is the verification of ownership, the node receives the transaction broadcast, the transaction will be verified, through the memory, packaged into the block, otherwise, discarded. The signature is similar to the traditional paper contract seal, the signing process, the legal transfer ownership guarantee means.

Signature type

Since the input and output of a transaction may have more than one, there are several types of signatures, currently a total of three categories: Sighash_all, Sighash_none, Sighash_single.

Sighash_all

The signature type is the default type, is also the majority of transactions currently used, as the name implies that the entire single transaction signed. First, the organization of all output, input, as the above decomposition of the hex process, each input corresponds to a signature, temporarily left blank, and other fields including sequence must be filled, so that the formation of a complete trading hex (only missing signature field). Then, each input is required to use the private key to sign the data, the signature is filled in the corresponding location, n input n signatures. The simple understanding is that the input, the output, approved and approved for the list, and agreed to spend my input.

Sighash_none

The signature type is the most freely loose, only the input signature, not the output signature, the output can be arbitrarily specified. After someone has signed a token to you, you can fill in any accepted address at any time and broadcast it to make it effective. The simple understanding is: I agree to spend my money, as to whom, I do not care.

Sighash_single

The signature type is next free and loose, only for its own input, output signature, and leave the sequence field blank. Its input order corresponds to the order of its output, such as the input is the 3rd, then the signature output is also a third. The simple understanding is: I agree to spend my money, and only to spend on my approved output, as for the other input, output, I do not care.

Bitcoin transaction composition (i)

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.