交易生成和验证的原理(赎回脚本和签名脚本)

区块链 比特币

比特币交易中常见有P2PKH、P2SH两种模式

1、P2PKH(pay to public key hash)支付到公钥地址模式

赎回脚本:OP_DUP OP_HASH160 (0×14)[一个20字节的哈希值] OP_EQUALVERIFY OP_CHECKSIG

签名脚本:[签名的字节数][签名]0×01 [公钥的字节数] [公钥]

2、P2SH(pay to script hash) 支付到脚本模式,使用多重签名就需要用到这种模式

赎回脚本:OP_HASH160 (0×14)  [一个20字节的哈希值] OP_EQUAL “签名脚本”:(输入脚本)

签名脚本:0×00  [字节数]  [签名1] 0×01 …[字节数] [签名m] 0×01 [支付合同脚本的字节数]  [m]  [字节数][公钥1]… [字节数][公钥n][n] OP_CHECKSIGVERIFY

在BitconCore的命令行执行如下命令:

测试环境下4bdf36da28324ea2ceb111763d34269b999100674fe6fb5c5bb88b9593a94f2d交易 

./bitcoin-cli getrawtransaction 4bdf36da28324ea2ceb111763d34269b999100674fe6fb5c5bb88b9593a94f2d

010000000422488707bb77e0b3822274cb039289f71a35df8925e71e4eb473f6db1a0bf6b0000000006b483045022100f399632053419336fb2b833cadaa02d565713f0575d39e34c397734973e4b1dd02204fbf3bc61f9e47049fbb99c44b7ce747aca9283105f83df23f81a74a435866b5812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffffa87829f31727a9a5335df263e83864bc7c9105405988bbb3ba9061ef20b1f18a000000006a47304402206ad9b0376e0d8238101738dd3217edeccf0e249e48041d93ea72288d6b872c4902205e37ae3ede6fcd01fd1185944643629957033fedc51624a0a0818a6f7cda5785812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffffbbfef5fc97297cb490be8a61def989388f805f7a4281961620fd36e3aa595787010000006a473044022039eebcb2340be94a6bb9acedec57d5ddd90d725686fdc7bca9edafc8cc093df7022071d74ebd0ea914eb78a0aa69507911f37639379644a5975ba7ce95ae1ad67d3a812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffffd939111621217ecd86ff2132368f0da96d074f0877848815032b0622a02c1728010000006a47304402204d992344e427b4f650012ac5db2a8a3793f18124af2ad72e0ad9bebd538eba6d02200b9fcd76cdcbcb5b388a2e0a1a72c05c0774b3cc110d9e407cf690a04c8fa50b812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffff0200943577000000001976a9143db730c82ed950a73e371720312bc0b561de8c9f88aca29f3d25000000001976a914a31805c29bca9034539bd38505e632542062141088ac00000000
./bitcoin-cli decoderawtransaction  010000000422488707bb77e0b3822274cb039289f71a35df8925e71e4eb473f6db1a0bf6b0000000006b483045022100f399632053419336fb2b833cadaa02d565713f0575d39e34c397734973e4b1dd02204fbf3bc61f9e47049fbb99c44b7ce747aca9283105f83df23f81a74a435866b5812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffffa87829f31727a9a5335df263e83864bc7c9105405988bbb3ba9061ef20b1f18a000000006a47304402206ad9b0376e0d8238101738dd3217edeccf0e249e48041d93ea72288d6b872c4902205e37ae3ede6fcd01fd1185944643629957033fedc51624a0a0818a6f7cda5785812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffffbbfef5fc97297cb490be8a61def989388f805f7a4281961620fd36e3aa595787010000006a473044022039eebcb2340be94a6bb9acedec57d5ddd90d725686fdc7bca9edafc8cc093df7022071d74ebd0ea914eb78a0aa69507911f37639379644a5975ba7ce95ae1ad67d3a812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffffd939111621217ecd86ff2132368f0da96d074f0877848815032b0622a02c1728010000006a47304402204d992344e427b4f650012ac5db2a8a3793f18124af2ad72e0ad9bebd538eba6d02200b9fcd76cdcbcb5b388a2e0a1a72c05c0774b3cc110d9e407cf690a04c8fa50b812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7ffffffff0200943577000000001976a9143db730c82ed950a73e371720312bc0b561de8c9f88aca29f3d25000000001976a914a31805c29bca9034539bd38505e632542062141088ac00000000

{
  "txid": "4bdf36da28324ea2ceb111763d34269b999100674fe6fb5c5bb88b9593a94f2d",
  "hash": "4bdf36da28324ea2ceb111763d34269b999100674fe6fb5c5bb88b9593a94f2d",
  "version": 1,
  "size": 667,
  "vsize": 667,
  "locktime": 0,
  "vin": [
    {
      "txid": "b0f60b1adbf673b44e1ee72589df351af7899203cb742282b3e077bb07874822",
      "vout": 0,
      "scriptSig": {
        "asm": "3045022100f399632053419336fb2b833cadaa02d565713f0575d39e34c397734973e4b1dd02204fbf3bc61f9e47049fbb99c44b7ce747aca9283105f83df23f81a74a435866b5[ALL|ANYONECANPAY] 02fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7",
        "hex": "483045022100f399632053419336fb2b833cadaa02d565713f0575d39e34c397734973e4b1dd02204fbf3bc61f9e47049fbb99c44b7ce747aca9283105f83df23f81a74a435866b5812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7"
      },
      "sequence": 4294967295
    },
    {
      "txid": "8af1b120ef6190bab3bb88594005917cbc6438e863f25d33a5a92717f32978a8",
      "vout": 0,
      "scriptSig": {
        "asm": "304402206ad9b0376e0d8238101738dd3217edeccf0e249e48041d93ea72288d6b872c4902205e37ae3ede6fcd01fd1185944643629957033fedc51624a0a0818a6f7cda5785[ALL|ANYONECANPAY] 02fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7",
        "hex": "47304402206ad9b0376e0d8238101738dd3217edeccf0e249e48041d93ea72288d6b872c4902205e37ae3ede6fcd01fd1185944643629957033fedc51624a0a0818a6f7cda5785812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7"
      },
      "sequence": 4294967295
    },
    {
      "txid": "875759aae336fd20169681427a5f808f3889f9de618abe90b47c2997fcf5febb",
      "vout": 1,
      "scriptSig": {
        "asm": "3044022039eebcb2340be94a6bb9acedec57d5ddd90d725686fdc7bca9edafc8cc093df7022071d74ebd0ea914eb78a0aa69507911f37639379644a5975ba7ce95ae1ad67d3a[ALL|ANYONECANPAY] 02fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7",
        "hex": "473044022039eebcb2340be94a6bb9acedec57d5ddd90d725686fdc7bca9edafc8cc093df7022071d74ebd0ea914eb78a0aa69507911f37639379644a5975ba7ce95ae1ad67d3a812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7"
      },
      "sequence": 4294967295
    },
    {
      "txid": "28172ca022062b0315888477084f076da90d8f363221ff86cd7e2121161139d9",
      "vout": 1,
      "scriptSig": {
        "asm": "304402204d992344e427b4f650012ac5db2a8a3793f18124af2ad72e0ad9bebd538eba6d02200b9fcd76cdcbcb5b388a2e0a1a72c05c0774b3cc110d9e407cf690a04c8fa50b[ALL|ANYONECANPAY] 02fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7",
        "hex": "47304402204d992344e427b4f650012ac5db2a8a3793f18124af2ad72e0ad9bebd538eba6d02200b9fcd76cdcbcb5b388a2e0a1a72c05c0774b3cc110d9e407cf690a04c8fa50b812102fe7ea772409ffc0980d945bace2e01d085a574e589d13da5905aedf7ebbb9cf7"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 20.00000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 3db730c82ed950a73e371720312bc0b561de8c9f OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a9143db730c82ed950a73e371720312bc0b561de8c9f88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mm9Gy4MDFq3zK9iJxw4xK2tHRjQ8aR3Gmw"
        ]
      }
    },
    {
      "value": 6.24795554,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 a31805c29bca9034539bd38505e6325420621410 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914a31805c29bca9034539bd38505e632542062141088ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mvPKDSBZuGC4BxF1kPvrCjG6ZPKzc1H1vx"
        ]
      }
    }
  ]
}

发表评论