交易类型
产量交易(Generation)
每个Block都对应一个产量交易(Generation TX),该类交易是没有输入交易的,挖出的新币是所有币的源头。
合成地址交易(Script Hash)
该类交易的接收地址不是通常意义的地址,而是一个合成地址,以3开头,需要几对公私钥一起生成合成地址,在生成过程中可以指定,几对公私钥中的几个签名以后,就可以消费该地址的比特币。
通用地址交易(Pubkey Hash)
该类是最常见的交易类型,由N个输入、M个输出构成。
输入和输出可以简单的理解成,发出币的地址就是输入,收到币的地址就是输出。
数据结构
字段
数据类型
字段大小
字段描述
versionuint32_t
4交易数据结构的版本号tx_in countvar_int1+输入交易的数量tx_intx_in[]41+输入交易的数组,每个输入=41字节
tx_out countvar_int1+输出地址的数量tx_outtx_out[]9+输入地址的数组,每个输入=9字节lock_timeuint32_t4
lock_time是一个多意字段,表示在某个高度的Block之前或某个时间点之前该交易处于锁定态,无法收录进Block。
值
含义
0立即生效 500000000含义为Block高度,处于该Block之前为锁定(不生效)= 500000000含义为Unix时间戳,处于该时刻之前为锁定(不生效)
若该笔交易的所有输入交易的sequence字段,均为INT32最大值(0xffffffff),则忽略lock_time字段。否则,该交易在未达到Block高度或达到某个时刻之前,是不会被收录进Block中的。
示例
为了演示方便,我们读取稍早期的块数据,以高度116219 Block为例。
# ~ 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
}
该Block里面有5笔交易,第一笔为Generation TX,解析出来看一下具体内容:
# ~ bitcoind getrawtransaction be8f08d7f519eb863a68cf292ca51dbab7c9b49f50a96d13f2db32e432db363e 1
{
hex : 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff070439f3001b0134ffffffff014034152a010000004341045b3aaa284d169c5ae2d20d0b0673468ed3506aa8fea5976eacaf1ff304456f6522fbce1a646a24005b8b8e771a671f564ca6c03e484a1c394bf96e2a4ad01dceac00000000,
txid : be8f08d7f519eb863a68cf292ca51dbab7c9b49f50a96d13f2db32e432db363e,
version : 1,
locktime : 0,
vin : [
{
coinbase : 0439f3001b0134,
sequence : 4294967295
}
],
vout : [
{
value : 50.01000000,
n : 0,
scriptPubKey : {
asm : 045b3aaa284d169c5ae2d20d0b0673468ed3506aa8fea5976eacaf1ff304456f6522fbce1a646a24005b8b8e771a671f564ca6c03e484a1c394bf96e2a4ad01dce OP_CHECKSIG,
hex : 41045b3aaa284d169c5ae2d20d0b0673468ed3506aa8fea5976eacaf1ff304456f6522fbce1a646a24005b8b8e771a671f564ca6c03e484a1c394bf96e2a4ad01dceac,
reqSigs : 1,
type : pubkey,
addresses : [
1LgZTvoTJ6quJNCURmBUaJJkWWQZXkQnDn
]
}
}
],
blockhash : 0000000000007c639f2cbb23e4606a1d022fa4206353b9d92e99f5144bd74611,
confirmations : 145029,
time : 1301705313,
blocktime : 1301705313
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
Generation TX的输入不是一个交易,而带有coinbase字段的结构。该字段的值由挖出此Block的人填写,这是一种“特权”:可以把信息写入货币系统(大家很喜欢用系统中的数据结构字段名来命名站点,例如blockchain、coinbase等,这些词的各种后缀域名都被抢注一空)。中本聪在比特币的第一个交易中的写入的coinbase值是:
coinbase:04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b731
将该段16进制转换为ASCII字符,就是那段著名的创世块留言:
The Times 03/Jan/2009 Chancellor on brink of second bailout for banks1
接下来展示的是一个三个输入、两个输出的普通交易:
# ~ bitcoind getrawtransaction 028cfae228f8a4b0caee9c566bd41aed36bcd237cdc0eb18f0331d1e87111743 1
{
hex : 0100000003c9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7000000008a473044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba2a509b5fbd6cac97ff3af0141048aefd78bba80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65097ce8d30d5a7e5ef8d1005eaafd4b3fbeffffffffc9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7010000008a47304402206b993231adec55e6085e75f7dc5ca6c19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22dc580ef1aa6cccef208d0141042ff65bd6b3ef04253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c031449d31026affffffffc98620a6c40fc7b3a506ad79af339541762facd1dd80ff0881d773fb72b230da010000008b483045022040a5d957e087ed61e80f1110bcaf4901b5317c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff2d0efc83ad261da5b4f1d014104a7d1a57e650613d3414ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a829f3a8fe2fffffffff0200bd0105000000001976a914634228c26cf40a02a05db93f2f98b768a8e0e61b88acc096c7a6030000001976a9147514080ab2fcac0764de3a77d10cb790c71c74c288ac00000000,
txid : 028cfae228f8a4b0caee9c566bd41aed36bcd237cdc0eb18f0331d1e87111743,
version : 1,
locktime : 0,
vin : [
{
txid : b79a4803014177ea0139c95f09c6903c01bb8f80d039631afd68cabf7eb0f3c9,
vout : 0,
scriptSig : {
asm : 3044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba2a509b5fbd6cac97ff3af01 048aefd78bba80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65097ce8d30d5a7e5ef8d1005eaafd4b3fbe,
hex : 473044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba2a509b5fbd6cac97ff3af0141048aefd78bba80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65097ce8d30d5a7e5ef8d1005eaafd4b3fbe
},
sequence : 4294967295
},
{
txid : b79a4803014177ea0139c95f09c6903c01bb8f80d039631afd68cabf7eb0f3c9,
vout : 1,
scriptSig : {
asm : 304402206b993231adec55e6085e75f7dc5ca6c19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22dc580ef1aa6cccef208d01 042ff65bd6b3ef04253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c031449d31026a,
hex : 47304402206b993231adec55e6085e75f7dc5ca6c19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22dc580ef1aa6cccef208d0141042ff65bd6b3ef04253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c031449d31026a
},
sequence : 4294967295
},
{
txid : da30b272fb73d78108ff80ddd1ac2f76419533af79ad06a5b3c70fc4a62086c9,
vout : 1,
scriptSig : {
asm : 3045022040a5d957e087ed61e80f1110bcaf4901b5317c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff2d0efc83ad261da5b4f1d01 04a7d1a57e650613d3414ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a829f3a8fe2f,
hex : 483045022040a5d957e087ed61e80f1110bcaf4901b5317c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff2d0efc83ad261da5b4f1d014104a7d1a57e650613d3414ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a829f3a8fe2f
},
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
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
字段hex记录了所有相关信息,后面显示的是hex解析出来的各类字段信息。下面把逐个分解hex内容(hex可以从上面的直接看到):
01000000 // 版本号,UINT32
03 // Tx输入数量,变长INT。3个输入。
/*** 第一组Input Tx ***/
// Tx Hash,固定32字节
c9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7
00000000 // 消费的Tx位于前向交易输出的第0个,UINT32,固定4字节
8a // 签名的长度, 0x8A = 138字节
// 138字节长度的签名,含有两个部分:公钥+签名
47 // 签名长度,0x47 = 71字节
3044022055bac1856ecbc377dd5e869b1a84ed1d5228c987b098c095030c12431a4d5249022055523130a9d0af5fc27828aba43b464ecb1991172ba2a509b5fbd6cac97ff3af01
41 // 公钥长度,0x41 = 65字节
048aefd78bba80e2d1686225b755dacea890c9ca1be10ec98173d7d5f2fefbbf881a6e918f3b051f8aaaa3fcc18bbf65097ce8d30d5a7e5ef8d1005eaafd4b3fbe
ffffffff // sequence,0xffffffff = 4294967295, UINT32, 固定4字节
/*** 第二组Input Tx。与上同理,省略分解 ***/
c9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7010000008a47304402206b993231adec55e6085e75f7dc5ca6c19e42e744cd60abaff957b1c352b3ef9a022022a22fec37dfa2c646c78d9a0753d56cb4393e8d0b22dc580ef1aa6cccef208d0141042ff65bd6b3ef04253225405ccc3ab2dd926ff2ee48aac210819698440f35d785ec3cec92a51330eb0c76cf49e9e474fb9159ab41653a9c1725c031449d31026affffffff
/*** 第三组Input Tx ***/
c98620a6c40fc7b3a506ad79af339541762facd1dd80ff0881d773fb72b230da010000008b483045022040a5d957e087ed61e80f1110bcaf4901b5317c257711a6cbc54d6b98b6a8563f02210081e3697031fe82774b8f44dd3660901e61ac5a99bff2d0efc83ad261da5b4f1d014104a7d1a57e650613d3414ebd59e3192229dc09d3613e547bdd1f83435cc4ca0a11c679d96456cae75b1f5563728ec7da1c1f42606db15bf554dbe8a829f3a8fe2fffffffff
02 // Tx输出数量,变长INT。两个输出。
/*** 第一组输出 ***/
00bd010500000000 // 输出的币值,UINT64,8个字节。字节序需翻转,~= 0x000000000501bd00 = 84000000 satoshi
19 // 输出目