隔离见证交易签名计算示例

1.隔离见证交易的数据格式

隔离见证交易的数据格式如下:

[nVersion][marker][flag][txins][txouts][witness][nLockTime]

每一个tx_in 有相应的一个witness字段,witness字段以一个vint开头,表明栈元素的数量。每一个栈元素以vint开头表示其长度。

2.隔离见证签名计算方法

 签名为使用私钥对如下hash值,进行签名。

Double SHA256 of the serialization of:

     1. nVersion of the transaction (4-byte little endian)

     2. hashPrevouts (32-byte hash)

     3. hashSequence (32-byte hash)

     4. outpoint (32-byte hash + 4-byte little endian)

     5. scriptCode of the input (serialized as scripts inside CTxOuts)

     6. value of the output spent by this input (8-byte little endian)

     7. nSequence of the input (4-byte little endian)

     8. hashOutputs (32-byte hash)

     9. nLocktime of the transaction (4-byte little endian)

    10. sighash type of the signature (4-byte little endian)

3. 隔离见证签名计算示例

以下已隔离见证地址向另外一个隔离见证地址转账为例(test chain):

地址 tb1qn7fgrgxlshl95c9n63hcgsn8sau6hcsr34y7yg 转账给tb1q8z6wvtv0cj88rtd6s2wupdycpurgj4723wrkrl 和 tb1qdpgfmqv5u5dt8zhuu07wt873xqjjshul69tmmc

转账信息见 https://testnet.blockchain.info/tx/93bdfff3f9f36cfea4aab010d9d0289d1a5dec3c633a485d4bef953431129554?show_adv=true

3.1 签名后交易数据如下:

01000000000101966d7ffa9b293ac410d3bd26a99159adffe697223860e5ab3ed304a572a695f90100000000fdffffff02002d31010000000016001438b4e62d8fc48e71adba829dc0b4980f068957ca9ec2c9010000000016001468509d8194e51ab38afce3fce59fd13025285f9f0248304502210099c39c2801527700753cd74a1e9280fbc7a32ce935a1aaca8821876b7982f53b022075f47ed60906900e8f67be50580ec0e8d734603f4a35d3753d442eae1a428f98012102977de8036776186940a5485ed33db045e4685515e0fc352245dda1dd7cd554f55ce71300

 

3.2 交易基本信息

nVersion: 01000000

输入数量:01

输入列表:

  • 32字节 前一次交易的hash

         966d7ffa9b293ac410d3bd26a99159adffe697223860e5ab3ed304a572a695f9

(默认是大端,要去网上查询详情的话,用的是小端

f995a672a504d33eabe560382297e6ffad5991a926bdd310c43a299bfa7f6d96)

  • 4字节 前一次交易的输出index : 0100000000(序号从0开始)
  • Pkscript 长度:VI (1字节)0x16(22字节)
  • Pkscript:

0[] PUSHDATA(20)[9f9281a0df85fe5a60b3d46f8442678779abe203]

1600149f9281a0df85fe5a60b3d46f8442678779abe203(使用的时候需要转换下

1976a9149f9281a0df85fe5a60b3d46f8442678779abe20388ac)

  • Sequence: fdffffff

输出数量:02

输出列表:

  • Value: 8字节 002d310100000000
  • Pkscript长度 0x16 (22)
  • Pkscript:16001438b4e62d8fc48e71adba829dc0b4980f068957ca
  • Value:8字节 9ec2c90100000000
  • Pkscript长度 0x16 (22)
  • Pkscript:16001468509d8194e51ab38afce3fce59fd13025285f9f

锁定时间:5ce71300

HashType :01000000(四字节)

 

3.3 计算hash值

(1)nVersion:    01000000

(2)

hashPrevouts:

dSHA256(966d7ffa9b293ac410d3bd26a99159adffe697223860e5ab3ed304a572a695f901000000)

= e54e21c0b72aa6409423de5bf165cd73f8a73f81b0a6f16e62e34ad054961ec8

(3)hashSequence: 

dSHA256(fdffffff) =

caf35e5224de16efa3ccaf41070f6e7b9432b6f79551e629fca9d1c03b43bc52

(4)outpoint:   32-byte hash + 4-byte index

966d7ffa9b293ac410d3bd26a99159adffe697223860e5ab3ed304a572a695f901000000

(5)scriptCode: 对于 P2WPKH 隔离见证 the scriptCode 为0x1976a914{20-byte-pubkey-hash}88ac.

600149f9281a0df85fe5a60b3d46f8442678779abe203

====》

1976a9149f9281a0df85fe5a60b3d46f8442678779abe20388ac

(6)amount: tx_in 的数量,需要从上个交易的tx_out 获取

80f0fa0200000000

(7)nSequence: fdffffff

(8)hashOutputs:

dSHA256(002d31010000000016001438b4e62d8fc48e71adba829dc0b4980f068957ca9ec2c9010000000016001468509d8194e51ab38afce3fce59fd13025285f9f)=

0a868ceb2f1b555a25d489ca6920d2d57e70c767ee5ac9f6b82636f99aa28884

(9)nLockTime:  5ce71300

(10)nHashType:   0x01000000

 

dsha256(1-10)= 8fcff20cd3a63a32cb16ceb74bc893fbb2e59c3bf398c1eeafec0332c7cf8b8b

利用tb1qn7fgrgxlshl95c9n63hcgsn8sau6hcsr34y7yg地址对应的私钥计算出签名

R: 99c39c2801527700753cd74a1e9280fbc7a32ce935a1aaca8821876b7982f53b

S: 75f47ed60906900e8f67be50580ec0e8d734603f4a35d3753d442eae1a428f98

地址对应的公钥为

02977DE8036776186940A5485ED33DB045E4685515E0FC352245DDA1DD7CD554F5

参考文档:

https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki

猜你喜欢

转载自blog.csdn.net/bebest2010/article/details/81122184