Multi-Signature

使用 Openssl 簽署 P2SH 交易

  • February 25, 2020

我有一個 P2SH 1-3,我想使用 openssl 簽署交易。我的贖回腳本是

512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE

我的交易數據是:

02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e48010000000069512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac0000000001000000

在裡面你可以找到兌換腳本和 SIGHASH。

我做了兩次 SHA256 並將其保存在a.txt文件中

printf $A | xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b | xxd -r -p > a.txt

現在我可以用我的私鑰簽名了

FIRMA=`openssl pkeyutl -inkey chiave_priv_1.pem -sign -in a.txt -pkeyopt digest:sha256 | xxd -p -c 256`

附加到FIRMA字節 01 (SIGHASH_ALL) 並為 OP_CHECKMULTISIG 的錯誤添加長度和 OP_0。

00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f01

贖回腳本長度為 210 char hex,69 in base16。前置4c(OP_PUSHDATA1)。

4c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE

我的整個 scriptSig 是:

00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE

ScriptSig 長度是B5,我的整個交易是:

02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000

解碼交易

bitcoin-cli decoderawtransaction 02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
{
 "txid": "e3d1d4fba5ebd8e6042ece0e5ecc40caf7f81b35fd06e17f76dace8565e2f15f",
 "hash": "e3d1d4fba5ebd8e6042ece0e5ecc40caf7f81b35fd06e17f76dace8565e2f15f",
 "version": 2,
 "size": 266,
 "vsize": 266,
 "weight": 1064,
 "locktime": 0,
 "vin": [
   {
     "txid": "01481ee0b940ba2f0b9796512cf1630cc78aa9e29d43c568dd5b5dd48b693224",
     "vout": 0,
     "scriptSig": {
       "asm": "0 3045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f[ALL] 512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51ae",
       "hex": "00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51ae"
     },
     "sequence": 4294967295
   }
 ],
 "vout": [
   {
     "value": 49.98200000,
     "n": 0,
     "scriptPubKey": {
       "asm": "OP_DUP OP_HASH160 a6cdf0c6e4850e2114cbc96df01f155b8792ed8c OP_EQUALVERIFY OP_CHECKSIG",
       "hex": "76a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac",
       "reqSigs": 1,
       "type": "pubkeyhash",
       "addresses": [
         "mviwAtz3zQj4xzH5mqLhRFrMSHwEcz7rYn"
       ]
     }
   }
 ]
}

發送交易:

$ bitcoin-cli sendrawtransaction 02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
error code: -26
error message:
mandatory-script-verify-flag-failed (unknown error) (code 16)

我的目標是創建自定義腳本。我什至嘗試了 2-3 但沒有幸運。

安德魯回答後的更新問題

您簽署的交易數據有誤。對於 P2SH,您實際上必須簽署redeemScript,而不是scriptPubKey。你必須像那個redeemScript 是scriptPubKey 一樣簽署它。


您的redeemScript 中還有另一個錯誤。AnOP_CHECKMULTISIG要求您推送所需的簽名者數量,然後是密鑰,然後是密鑰數量。因此,對於您所擁有的 1-of-3,它應該是

1 <key1> <key2> <key3> 3 OP_CHECKMULTISIG

但是你有

1 <key1> <key2> <key3> 1 OP_CHECKMULTISIG

這使得整個redeemScript 本身無效。

你的redeemScript 真的應該是

512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c53ae

因為redeemScript 無效,所以您嘗試花費的輸出也是無效的,因此是不可花費的。

引用自:https://bitcoin.stackexchange.com/questions/93449