btc铸币交易解析



1. 交易

1.1 区块内交易输入通用格式

Field Description Size
Previous Transaction hash doubled SHA256-hashed of a (previous) to-be-used transaction 32 bytes
Previous Txout-index non negative integer indexing an output of the to-be-used transaction 4 bytes
Txin-script length non negative integer VI = VarInt 1 - 9 bytes
Txin-script / scriptSig Script <in-script length>-many bytes
sequence_no normally 0xFFFFFFFF; irrelevant unless transaction’s lock_time is > 0 4 bytes

1.2 区块内交易输出通用格式

Field Description Size
value non negative integer giving the number of Satoshis(BTC/10^8) to be transfered 8 bytes
Txout-script length non negative integer 1 - 9 bytes VI = VarInt
Txout-script / scriptPubKey Script <out-script length>-many bytes

参考链接

2. 举例

2.1 创世区块 Raw Data

0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000

查看 HEX

查看 JSON

2.1.1 Raw Data 拆分

# version
01000000
# prev block header hash
0000000000000000000000000000000000000000000000000000000000000000
# merkle root hash
# 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A
# timestamp => 0x495fab29 = 1231006505
29AB5F49
# bits => 0x1d00ffff = 486,604,799
FFFF001D
# nonce => 0x7c2bac1d = 2,083,236,893
1DAC2B7C
# number of transaction
01

# The following Raw Transaction
# The following Raw Transaction
# The following Raw Transaction

# version
01000000
# number of transaction inputs
01
# outpoint txid - fake transaction ID for coinbase transaction
0000000000000000000000000000000000000000000000000000000000000000
# outpoint index - fake index
FFFFFFFF
# script length = 0x4D = 77 bytes (04ffff...E6B73)
4D
# coinbase - any data entered by the miner
04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73
# sequence
FFFFFFFF
# number of transaction outputs
01
# Amount of the first output in little-endian integer
# 0x012A05F200 = 5000000000 Satoshis = 50 BTC
00F2052A01000000
# script length = 0x43 = 67 bytes (434104...1d5fac)
43
# public key script
4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac
# lock time
00000000

2.1.2 Coinbase 解析

Coinbase 域可以当作 extra nonce 增大 POW 方法的范围。矿工可以通过修改 nonce(4 bytes), timestamp(微调) 和 extra nonce(2 到 100bytes)来解题。

04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73

# 8 bytes for extra nonce ???
04FFFF001D010445
echo 5468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73 | xxd -r -p
function hex_to_ascii(str1) {
  var hex = str1.toString();
  var str = "";
  for (var n = 0; n < hex.length; n += 2) {
    str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
  }
  return str;
}

const str =
  "5468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73";
console.log(hex_to_ascii(str));
// The Times 03/Jan/2009 Chancellor on brink of second bailout for banks

转换工具

2.1.3 Public Key Script 解析

4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac

# pushes 0x41 = 65 bytes (04678a...f11d5f) onto the stack.
# 0x01-0x4b => The next opcode bytes is data to be pushed onto the stack
41
# pubic key of miner
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f
# OP_CHECKSIG
ac

操作码查询

2.1.4 Public Key To Address

CoinBase 中写入了矿工的公钥,因此矿工算出的 target 被盗取了也无用。

# 1. input
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f
# 2. SHA-256
261C1EB21FC4708C6ACBE1CFC6D4565652E9E768B620782898936B93000A6C02
# 3. RIPEMD-160
62E907B15CBF27D5425399EBF6F0FB50EBB88F18
# 4. Adding network bytes 00
0062E907B15CBF27D5425399EBF6F0FB50EBB88F18
# 5. SHA-256
9B90F16DE7F0E580C07735DAC15FFE23E2F8F8E103914E509AA91913FFDB9FB6
# 6. SHA-256
C29B7D937E3049E279391E62FDF00C12DEF7444013DDF6215808D10E9F2D5996
# 7. First fisrt four bytes
C29B7D93
# 8. 将7的结果拼在4后面
0062E907B15CBF27D5425399EBF6F0FB50EBB88F18C29B7D93
# 9. Base58
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

快捷转换工具

2.2 交易版本为2 Raw Data

交易版本为2的区块中,coinbase域的开头必须是区块高度(BIP34规定)。

04600020257fc3a597ab931f5328f89d794607c2250be71aceba09000000000000000000726505dd546a0b3481ae1243ea3bd4f0d3ddb9bd4a0bbd1fc35210e0ea369967f701e560ce981317992a7e150102000000010000000000000000000000000000000000000000000000000000000000000000ffffffff58032c870a04f701e560fabe6d6da1af4b3a6aa97c5e7f85bb5313446c7ea465d8d7d2aecef57c54bd60320359f1020000004204cb9a62696e616e63652f6f7238303638894061954404589778f21c81646304010000000000ffffffff0340be4025000000001600143156afc4249915008020f932783319f3e610b97d0000000000000000266a24b9e11b6da9cba7e65e870647423053e1b2faf3680462f6bb4b4ccaacf557ad950ae98a2f00000000000000002b6a2952534b424c4f434b3a54a821112221c52f0761cf387925e551e6fa8147b692d051e3c4ae1d0035556b00000000

查看 HEX

查看 JSON

BIP-34

2.2.1 拆分

# version -> 0x20006004
04600020
# prev block header hash
257fc3a597ab931f5328f89d794607c2250be71aceba09000000000000000000
# merkle root hash
726505dd546a0b3481ae1243ea3bd4f0d3ddb9bd4a0bbd1fc35210e0ea369967
# timestamp
f701e560
# bits
ce981317
# nonce
992a7e15
# number of transaction
01

# The following Raw Transaction
# The following Raw Transaction
# The following Raw Transaction

# version
02000000
# number of transaction inputs
01
# outpoint txid -> fake transaction ID for coinbase transaction
0000000000000000000000000000000000000000000000000000000000000000
# outpoint index -> fake index
ffffffff
# 0x58 = 88 bytes(032c87...000000)
58
# coinbase start -> any data entered by the miner
# height of bytes -> 0x03-byte little-endian integer 
03
# height -> 0x0a872c = 689964
2c870a
04f701e560fabe6d6da1af4b3a6aa97c5e7f85bb5313446c7ea465d8d7d2aecef57c54bd60320359f1020000004204cb9a62696e616e63652f6f7238303638894061954404589778f21c81646304010000000000
# coinbase end
# sequence
FFFFFFFF
# number of transaction outputs
03
# value -> 0x2540be40 = 625000000 satoshis = 6.25 btc
40be402500000000
# 0x16 = 22 bytes(001431...0b97d)
16
# output script 0
00143156afc4249915008020f932783319f3e610b97d
# 0x26 = 38 bytes
000000000000000026
# output script 1
6a24b9e11b6da9cba7e65e870647423053e1b2faf3680462f6bb4b4ccaacf557ad950ae98a2f
# 0x2b = 43 bytes
00000000000000002b
# output script 2
6a2952534b424c4f434b3a54a821112221c52f0761cf387925e551e6fa8147b692d051e3c4ae1d0035556b
# lock time
00000000

2.2.2 输出脚本解析

# number of transaction outputs
03
40be40250000000016
# output script 0
00143156afc4249915008020f932783319f3e610b97d
000000000000000026
# output script 1
6a24b9e11b6da9cba7e65e870647423053e1b2faf3680462f6bb4b4ccaacf557ad950ae98a2f
00000000000000002b
# output script 2
6a2952534b424c4f434b3a54a821112221c52f0761cf387925e551e6fa8147b692d051e3c4ae1d0035556b
00000000
Type Operation Content
P2WPKH_V0 OP_0 3156afc4249915008020f932783319f3e610b97d
NULL_DATA OP_RETURN b9e11b6da9cba7e65e870647423053e1b2faf3680462f6bb4b4ccaacf557ad950ae98a2f
NULL_DATA OP_RETURN 52534b424c4f434b3a54a821112221c52f0761cf387925e551e6fa8147b692d051e3c4ae1d0035556b

P2WPKH介绍


上篇: 记维护uniapp网站时的一个bug 下篇: npm 淘宝镜像到期的后遗症?

站内搜索

分 类

标 签

相关链接