关于冷钱包构造交易及签名的问题
发布于 1 年前 作者 路人-戊 2175 次浏览 来自 比特币

创建一笔交易

下面是我造的一笔交易, 2vin=>3vout

luren5@ubuntu:/gopath/src/github.com/luren5/cold-wallet$ bitcoin-cli -rpcpassword=123456 createrawtransaction '[{"txid":"4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e","vout":0}, {"txid": "bbe8ad2b59cc619c8c78e54150c7a064150bf75823266c8451854d267edc6efe", "vout":1}]' '{"1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD":0.00186, "12BLQiMmc3cbwejnV2NhtvhW9n3fRWor64": 0.0245,"1UgyYavyYJWdMspRep84y5s3XU4jxAw7N": 0.0234}'

结果

02000000023ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba525430000000000fffffffffe6edc7e264d8551846c262358f70b1564a0c75041e5788c9c61cc592bade8bb0100000000ffffffff0390d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac50622500000000001976a9140cec7cb1f571909150fcec5ffdde48854b53d29388aca0b42300000000001976a914053c99afdc60ccd260bafdb344e1b503d51f46c188ac00000000
decode
luren5@ubuntu:/gopath/src/github.com/luren5/cold-wallet$ bitcoin-cli decoderawtransaction 02000000023ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba525430000000000fffffffffe6edc7e264d8551846c262358f70b1564a0c75041e5788c9c61cc592bade8bb0100000000ffffffff0390d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac50622500000000001976a9140cec7cb1f571909150fcec5ffdde48854b53d29388aca0b42300000000001976a914053c99afdc60ccd260bafdb344e1b503d51f46c188ac00000000
{
  "txid": "7e1eaa74e1e3295548696754906de897870b391001c97fa1503226ae98063c5e",
  "hash": "7e1eaa74e1e3295548696754906de897870b391001c97fa1503226ae98063c5e",
  "version": 2,
  "size": 194,
  "vsize": 194,
  "locktime": 0,
  "vin": [
    {
      "txid": "4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    },
    {
      "txid": "bbe8ad2b59cc619c8c78e54150c7a064150bf75823266c8451854d267edc6efe",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.00186000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 01ddbca1a39b60b54fb671297a4a20a7681e0171 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD"
        ]
      }
    },
    {
      "value": 0.02450000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 0cec7cb1f571909150fcec5ffdde48854b53d293 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a9140cec7cb1f571909150fcec5ffdde48854b53d29388ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "12BLQiMmc3cbwejnV2NhtvhW9n3fRWor64"
        ]
      }
    },
    {
      "value": 0.02340000,
      "n": 2,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 053c99afdc60ccd260bafdb344e1b503d51f46c1 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914053c99afdc60ccd260bafdb344e1b503d51f46c188ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1UgyYavyYJWdMspRep84y5s3XU4jxAw7N"
        ]
      }
    }
  ]
}

一切正常!

签名

如这个示例 https://bitcoin.stackexchange.com/questions/29955/how-to-sign-bitcoin-transaction-with-bitcoind-and-non-bitcoind-wallet-private, 它的签名部分如下

./bitcoin-cli -rpcpassword=123456 signrawtransaction "01000000013ea2097dbb3bf6932b97d2469b4fa78e28a2e8d05c586affe9c8cb66dba525430000000000ffffffff0190d60200000000001976a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac00000000" '[{"txid":"4325a5db66cbc8e9ff6a585cd0e8a2288ea74f9b46d2972b93f63bbb7d09a23e","vout":0,"scriptPubKey":"76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac","redeemScript":""}]' '["5J34hLVaP9UAj4BcdbgYoPia93zzDfo8JhTzUN2tf8qqRiZqtjY"]' "ALL"

但是它是 1vin=>1vout的,那么问题来了 1、我上述造的交易,在对它进行签名的过程,signrawtransactoin的第二个参数 prevtxs 应该怎么构造,它的规则是怎样的 2、上面示例里面的 signrawtransaction 第二个参数 prevtxs 的 scriptPubKey参数貌似是, decoderawtransaction 结果中 vout中 hex 值,它们之间有什么关系 1511772738374.jpg 5A2438E3-66B5-4A73-AD73-F86B503B207B.png

3 回复

1、我上述造的交易,在对它进行签名的过程,signrawtransactoin的第二个参数 prevtxs 应该怎么构造,它的规则是怎样的

比特币的交易可以组成链式结构,任何一笔交易的输入都是上一笔交易的输出,prevout 就是上一笔交易的输出,你找到你这个prevout 花费的是哪笔交易的vout,这里prevout就笔交易的txid,vout索引号,至于这个scriptPubKey 76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac 就是上一笔的这个vout对应的锁定脚本,一般也就是所谓的地址,这个也是在上一笔交易中能知道的。

2、上面示例里面的 signrawtransaction 第二个参数 prevtxs 的 scriptPubKey参数貌似是, decoderawtransaction 结果中 vout中 hex 值,它们之间有什么关系

至于 为啥scriptPubKey 76a91401ddbca1a39b60b54fb671297a4a20a7681e017188ac 为啥跟prevtxs里一致,是因为上一笔交易的vout里是转给这个地址1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD,你这笔交易依然是要转给这个地址 1AsJjnWg5QKBThM6mK9jZ8mmo6KUzDjRD

总结: 本质上scriptPubKey是交易的锁定脚本,决定着你转向的是什么格式的脚本,而76a914053c99afdc60ccd260bafdb344e1b503d51f46c188ac这种是一般的P2PKH格式,也就是转给普通地址,解码成OP_CODE "asm": "OP_DUP OP_HASH160 053c99afdc60ccd260bafdb344e1b503d51f46c1 OP_EQUALVERIFY OP_CHECKSIG", 也就是说 053c99afdc60ccd260bafdb344e1b503d51f46c1 是地址的hash160

@maiiz 换句话说,signrawtransaction 的第二个参数 prevtxs 是直接跟这笔交易使用哪些utxo(也就是vin里面的信息) 一一对应的是吗

@路人-戊 是的,用的哪些unspent,就与哪些unspent的utxo一一对应

回到顶部