为什么 bitcore-lib 不能正确解码我的比特币交易十六进制?
Why does bitcore-lib not decode my bitcoin transaction hex correctly?
我正在使用 bitcore-lib
to decode transactions encoded in hexadecimal, but I'm getting a weird result. For comparison, I've used bitcoinjs-lib
,它似乎工作正常。
例子
交易
网络:比特币测试网
哈希:1eadc4a09c6abc8f024f04031334941ee455cd4fb112850788214da5f631a3d5
交易十六进制:
01000000000101a0bc55f51d3da3c30b0e92cd37effff17264c3d89057e1e041644606d2a703b5010000001716001482681fa5f944a92d53deacb38850ebba2dd44d44ffffffff0240d2df03000000001976a9147ab6ee8c168f32077c1cc83facb49a91d7ee5dae88aca5a2ecae0800000017a914ca21182b9eca3c99175dd89ce548b72db3244db08702483045022100d4485d23e5d0d3d14a40f0fa9c404f24d55d3db77091bbfb96232610e6b3a5220220712e7736a4229ade64a69e712e842f0a3020cf8209b5f2641114c39a4e9a669201210236443d966162716f76a632e487db80511ce795b68e192e9890bc435ca3e50c1f00000000
在这里试试十六进制代码:https://live.blockcypher.com/btc/decodetx/
片段:
const bitcore = require('bitcore-lib')
const bitcoinjs = require('bitcoinjs-lib')
const TX_HEX = '01000000000101a0bc55f51d3da3c30b0e92cd37effff17264c3d89057e1e041644606d2a703b5010000001716001482681fa5f944a92d53deacb38850ebba2dd44d44ffffffff0240d2df03000000001976a9147ab6ee8c168f32077c1cc83facb49a91d7ee5dae88aca5a2ecae0800000017a914ca21182b9eca3c99175dd89ce548b72db3244db08702483045022100d4485d23e5d0d3d14a40f0fa9c404f24d55d3db77091bbfb96232610e6b3a5220220712e7736a4229ade64a69e712e842f0a3020cf8209b5f2641114c39a4e9a669201210236443d966162716f76a632e487db80511ce795b68e192e9890bc435ca3e50c1f00000000'
const bitcoreTx = new bitcore.Transaction(Buffer.from(TX_HEX, 'hex'))
const bitcoinjsTx = bitcoinjs.Transaction.fromHex(Buffer.from(TX_HEX, 'hex'))
console.log(bitcoreTx.toJSON())
console.log(bitcoinjsTx)
结果
bitcore(不正确)
{ hash: '5f10810f9c514ab3f69b940c3dd7163af2f3a993a84e0e782fa5c39f5d0667a8',
version: 1,
inputs: [],
outputs:
[ { satoshis: 11762590741304222000,
script: '0b0e92cd37effff17264c3d89057e1e041644606d2a703b5010000001716001482681fa5f944a92d53deacb38850ebba2dd44d44ffffffff0240d2df03000000001976a9147ab6ee8c168f32077c1cc83facb49a91d7ee5dae88aca5a2ecae0800000017a914ca21182b9eca3c99175dd89ce548b72db3244db08702483045022100d4485d23e5d0d3d14a40f0fa9c404f24d55d3db77091bbfb96232610e6b3a5220220712e7736a4229ade64a69e712e842f0a3020cf8209b5f2641114c39a4e9a66' } ],
nLockTime: 35717522 }
bitcoinjs(正确)
Transaction {
version: 1,
locktime: 0,
ins:
[ { hash: <Buffer a0 bc 55 f5 1d 3d a3 c3 0b 0e 92 cd 37 ef ff f1 72 64 c3 d8 90 57 e1 e0 41 64 46 06 d2 a7 03 b5>,
index: 1,
script: <Buffer 16 00 14 82 68 1f a5 f9 44 a9 2d 53 de ac b3 88 50 eb ba 2d d4 4d 44>,
sequence: 4294967295,
witness: [Array] } ],
outs:
[ { value: 65000000,
script: <Buffer 76 a9 14 7a b6 ee 8c 16 8f 32 07 7c 1c c8 3f ac b4 9a 91 d7 ee 5d ae 88 ac> },
{ value: 37294482085,
script: <Buffer a9 14 ca 21 18 2b 9e ca 3c 99 17 5d d8 9c e5 48 b7 2d b3 24 4d b0 87> } ] }
您的交易是 segwit* 交易和 bitcorejs does not support it.
来自reddit:
SegWit input scriptSigs will have one of these patterns:
- 0x160014<20 bytes> (P2WPKH)
- 0x220020<32 bytes> (P2WSH)
在我看来,bitcorejs 的一个很好的替代品是 bcoin:它具有相同的功能(运行 一个完整的节点)加上 segwit 支持,并且它具有相似的体系结构,因此很容易迁移到它来自 bitcore。
我在这里发布这个是因为这是我搜索解码 script
和地址的地方。
解码“正常”脚本
一个正常的交易输出“脚本”是这样的:
76a9141a2e668007a28dbecb420a8e9ce8cdd1651f213d88ac
将其分解后,您会得到:
76 a9 14 1a2e668007a28dbecb420a8e9ce8cdd1651f213d 88 ac
0x76 (118) OP_DUP
0x19 (25) OP_HASH160
0x14 (20) is the length of the pubKeyHash (20 bytes)
... P2PKH (the pubKeyHash)
0x88 (136) OP_EQUALVERIFY
0xac (172) OP_CHECKSIG
地址错误?
P2PKH 地址只是您可能打算使用的地址的一部分。
您可能需要一个 Base58Check 地址,其中包括一个 version
前缀字节以及末尾的 4 个 check
哈希字节。
例如:
4c 1a2e668007a28dbecb420a8e9ce8cdd1651f213d 6496ad2a
如果您随后使用 BaseX Base58 编码,您将得到您熟悉的友好地址:
Xd5GzCN6mp77BeVEe6FrgqQt8MA1ge4Fsw
我正在使用 bitcore-lib
to decode transactions encoded in hexadecimal, but I'm getting a weird result. For comparison, I've used bitcoinjs-lib
,它似乎工作正常。
例子
交易
网络:比特币测试网
哈希:1eadc4a09c6abc8f024f04031334941ee455cd4fb112850788214da5f631a3d5
交易十六进制:
01000000000101a0bc55f51d3da3c30b0e92cd37effff17264c3d89057e1e041644606d2a703b5010000001716001482681fa5f944a92d53deacb38850ebba2dd44d44ffffffff0240d2df03000000001976a9147ab6ee8c168f32077c1cc83facb49a91d7ee5dae88aca5a2ecae0800000017a914ca21182b9eca3c99175dd89ce548b72db3244db08702483045022100d4485d23e5d0d3d14a40f0fa9c404f24d55d3db77091bbfb96232610e6b3a5220220712e7736a4229ade64a69e712e842f0a3020cf8209b5f2641114c39a4e9a669201210236443d966162716f76a632e487db80511ce795b68e192e9890bc435ca3e50c1f00000000
在这里试试十六进制代码:https://live.blockcypher.com/btc/decodetx/
片段:
const bitcore = require('bitcore-lib')
const bitcoinjs = require('bitcoinjs-lib')
const TX_HEX = '01000000000101a0bc55f51d3da3c30b0e92cd37effff17264c3d89057e1e041644606d2a703b5010000001716001482681fa5f944a92d53deacb38850ebba2dd44d44ffffffff0240d2df03000000001976a9147ab6ee8c168f32077c1cc83facb49a91d7ee5dae88aca5a2ecae0800000017a914ca21182b9eca3c99175dd89ce548b72db3244db08702483045022100d4485d23e5d0d3d14a40f0fa9c404f24d55d3db77091bbfb96232610e6b3a5220220712e7736a4229ade64a69e712e842f0a3020cf8209b5f2641114c39a4e9a669201210236443d966162716f76a632e487db80511ce795b68e192e9890bc435ca3e50c1f00000000'
const bitcoreTx = new bitcore.Transaction(Buffer.from(TX_HEX, 'hex'))
const bitcoinjsTx = bitcoinjs.Transaction.fromHex(Buffer.from(TX_HEX, 'hex'))
console.log(bitcoreTx.toJSON())
console.log(bitcoinjsTx)
结果
bitcore(不正确)
{ hash: '5f10810f9c514ab3f69b940c3dd7163af2f3a993a84e0e782fa5c39f5d0667a8',
version: 1,
inputs: [],
outputs:
[ { satoshis: 11762590741304222000,
script: '0b0e92cd37effff17264c3d89057e1e041644606d2a703b5010000001716001482681fa5f944a92d53deacb38850ebba2dd44d44ffffffff0240d2df03000000001976a9147ab6ee8c168f32077c1cc83facb49a91d7ee5dae88aca5a2ecae0800000017a914ca21182b9eca3c99175dd89ce548b72db3244db08702483045022100d4485d23e5d0d3d14a40f0fa9c404f24d55d3db77091bbfb96232610e6b3a5220220712e7736a4229ade64a69e712e842f0a3020cf8209b5f2641114c39a4e9a66' } ],
nLockTime: 35717522 }
bitcoinjs(正确)
Transaction {
version: 1,
locktime: 0,
ins:
[ { hash: <Buffer a0 bc 55 f5 1d 3d a3 c3 0b 0e 92 cd 37 ef ff f1 72 64 c3 d8 90 57 e1 e0 41 64 46 06 d2 a7 03 b5>,
index: 1,
script: <Buffer 16 00 14 82 68 1f a5 f9 44 a9 2d 53 de ac b3 88 50 eb ba 2d d4 4d 44>,
sequence: 4294967295,
witness: [Array] } ],
outs:
[ { value: 65000000,
script: <Buffer 76 a9 14 7a b6 ee 8c 16 8f 32 07 7c 1c c8 3f ac b4 9a 91 d7 ee 5d ae 88 ac> },
{ value: 37294482085,
script: <Buffer a9 14 ca 21 18 2b 9e ca 3c 99 17 5d d8 9c e5 48 b7 2d b3 24 4d b0 87> } ] }
您的交易是 segwit* 交易和 bitcorejs does not support it.
来自reddit:
SegWit input scriptSigs will have one of these patterns:
- 0x160014<20 bytes> (P2WPKH)
- 0x220020<32 bytes> (P2WSH)
在我看来,bitcorejs 的一个很好的替代品是 bcoin:它具有相同的功能(运行 一个完整的节点)加上 segwit 支持,并且它具有相似的体系结构,因此很容易迁移到它来自 bitcore。
我在这里发布这个是因为这是我搜索解码 script
和地址的地方。
解码“正常”脚本
一个正常的交易输出“脚本”是这样的:
76a9141a2e668007a28dbecb420a8e9ce8cdd1651f213d88ac
将其分解后,您会得到:
76 a9 14 1a2e668007a28dbecb420a8e9ce8cdd1651f213d 88 ac
0x76 (118) OP_DUP
0x19 (25) OP_HASH160
0x14 (20) is the length of the pubKeyHash (20 bytes)
... P2PKH (the pubKeyHash)
0x88 (136) OP_EQUALVERIFY
0xac (172) OP_CHECKSIG
地址错误?
P2PKH 地址只是您可能打算使用的地址的一部分。
您可能需要一个 Base58Check 地址,其中包括一个 version
前缀字节以及末尾的 4 个 check
哈希字节。
例如:
4c 1a2e668007a28dbecb420a8e9ce8cdd1651f213d 6496ad2a
如果您随后使用 BaseX Base58 编码,您将得到您熟悉的友好地址:
Xd5GzCN6mp77BeVEe6FrgqQt8MA1ge4Fsw