Bitcoin-Core

部分簽署多重簽名 rawtransaction 返回redeemScript/witnessScript 與 scriptPubKey 不匹配

  • November 14, 2020

我想通過 RPC 使用 bitcoind 對 rawtransactions 進行部分簽名,但是當我嘗試對手動創建的原始交易進行簽名時,我幾乎被卡在了過程的最後,我花了很多時間試圖找到錯誤,但我不明白它。我將在這裡複製我所做的所有步驟。

  1. 首先我創建一個 2-3 多重簽名地址
$user1_pubkey = "03e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb2";
$user2_pubkey = "02df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea2946210";
$user3_pubkey = $this->bitcoin->getaddressinfo("2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv")['pubkey'];
$keys[] = $user1_pubkey;
$keys[] = $user2_pubkey;
$keys[] = $user3_pubkey;
$multisig_address = $this->bitcoin->addmultisigaddress(2, $keys);

Returned values:

["address"]=>
   string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
   ["redeemScript"]=>
   string(210) "522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae"
  1. 我已經向多重簽名地址發送了兩個小交易(一個是 0.005,另一個是 0.004)
ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4
8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5
  1. 現在我檢索與這些 txid 相關的所有資訊:
$transaction1 = $this->bitcoin->getrawtransaction("ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4", true);
$transaction2 = $this->bitcoin->getrawtransaction("8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5", true);

3.1。先前變數的結果:

$transaction1 =>

array(14) {
 ["txid"]=>
 string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
 ["hash"]=>
 string(64) "e586e88456cef32c01820d94541296aab85d776d890d8879184b8e70344a6353"
 ["version"]=>
 int(2)
 ["size"]=>
 int(247)
 ["vsize"]=>
 int(166)
 ["weight"]=>
 int(661)
 ["locktime"]=>
 int(1891405)
 ["vin"]=>
 array(1) {
   [0]=>
   array(5) {
     ["txid"]=>
     string(64) "9d0c2b8203b1b8f63ac8a8dc17a0e48b36efceee43d38fde50a6b3c92f811830"
     ["vout"]=>
     int(0)
     ["scriptSig"]=>
     array(2) {
       ["asm"]=>
       string(44) "0014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83"
       ["hex"]=>
       string(46) "160014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83"
     }
     ["txinwitness"]=>
     array(2) {
       [0]=>
       string(142) "3044022055bbad9ea94df56180ded2760dd77b0fc959e633b459bc906e6bd4456f72c71302201d9430ac2ed9debc9508442484accac54021c990e8906ebbf7a6b205ec9e827101"
       [1]=>
       string(66) "0326ae8d30a90a6926b81718dfa07b0b5058636f9c86465a1c3f37b0322af023c8"
     }
     ["sequence"]=>
     int(4294967294)
   }
 }
 ["vout"]=>
 array(2) {
   [0]=>
   array(3) {
     ["value"]=>
     float(0.005)
     ["n"]=>
     int(0)
     ["scriptPubKey"]=>
     array(5) {
       ["asm"]=>
       string(60) "OP_HASH160 c9ebaa3be1c12351c9e07ea5758a68eb20ee5098 OP_EQUAL"
       ["hex"]=>
       string(46) "a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887"
       ["reqSigs"]=>
       int(1)
       ["type"]=>
       string(10) "scripthash"
       ["addresses"]=>
       array(1) {
         [0]=>
         string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
       }
     }
   }
   [1]=>
   array(3) {
     ["value"]=>
     float(0.01040373)
     ["n"]=>
     int(1)
     ["scriptPubKey"]=>
     array(5) {
       ["asm"]=>
       string(60) "OP_HASH160 301c49c04ede304bdef0ab48e2b365a9eef737e4 OP_EQUAL"
       ["hex"]=>
       string(46) "a914301c49c04ede304bdef0ab48e2b365a9eef737e487"
       ["reqSigs"]=>
       int(1)
       ["type"]=>
       string(10) "scripthash"
       ["addresses"]=>
       array(1) {
         [0]=>
         string(35) "2MwdcJyxRAxqQ4tf6TQSubcLMZd21MrTskx"
       }
     }
   }
 }
 ["hex"]=>
 string(494) "020000000001013018812fc9b3a650de8fd343eeceef368be4a017dca8c83af6b8b103822b0c9d0000000017160014839c3b61637073d6aec4c74a5b2e8fe52fcc4d83feffffff0220a107000000000017a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887f5df0f000000000017a914301c49c04ede304bdef0ab48e2b365a9eef737e48702473044022055bbad9ea94df56180ded2760dd77b0fc959e633b459bc906e6bd4456f72c71302201d9430ac2ed9debc9508442484accac54021c990e8906ebbf7a6b205ec9e827101210326ae8d30a90a6926b81718dfa07b0b5058636f9c86465a1c3f37b0322af023c84ddc1c00"
 ["blockhash"]=>
 string(64) "00000000b1c75e4431f9c23a75779c964fa51cd714f9703781670f1a2c50a8c7"
 ["confirmations"]=>
 int(388)
 ["time"]=>
 int(1605141275)
 ["blocktime"]=>
 int(1605141275)
}


$transaction2 =>

array(14) {
 ["txid"]=>
 string(64) "8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5"
 ["hash"]=>
 string(64) "0e8dfd742d3ea5f12ee7c033112f5e6f585b41e317b3f9d3fef61db4200c4bbd"
 ["version"]=>
 int(2)
 ["size"]=>
 int(247)
 ["vsize"]=>
 int(166)
 ["weight"]=>
 int(661)
 ["locktime"]=>
 int(1891405)
 ["vin"]=>
 array(1) {
   [0]=>
   array(5) {
     ["txid"]=>
     string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
     ["vout"]=>
     int(1)
     ["scriptSig"]=>
     array(2) {
       ["asm"]=>
       string(44) "0014c7f49ae39660b15632f46a3c69a67ccb4de7dc20"
       ["hex"]=>
       string(46) "160014c7f49ae39660b15632f46a3c69a67ccb4de7dc20"
     }
     ["txinwitness"]=>
     array(2) {
       [0]=>
       string(142) "304402207cc7558274cd9458c44bf7554d0b049bba1388cd82057b3f8d0786bc28690aad022057eea44e01ff11c608e254a844b620f428fb515db6ef33cd199c57d3008ce89c01"
       [1]=>
       string(66) "0303c01176f2db9efaa18e169e0ea7da87784208e05fcb1e0258ccb834805ed02d"
     }
     ["sequence"]=>
     int(4294967294)
   }
 }
 ["vout"]=>
 array(2) {
   [0]=>
   array(3) {
     ["value"]=>
     float(0.004)
     ["n"]=>
     int(0)
     ["scriptPubKey"]=>
     array(5) {
       ["asm"]=>
       string(60) "OP_HASH160 c9ebaa3be1c12351c9e07ea5758a68eb20ee5098 OP_EQUAL"
       ["hex"]=>
       string(46) "a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887"
       ["reqSigs"]=>
       int(1)
       ["type"]=>
       string(10) "scripthash"
       ["addresses"]=>
       array(1) {
         [0]=>
         string(35) "2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT"
       }
     }
   }
   [1]=>
   array(3) {
     ["value"]=>
     float(0.00640207)
     ["n"]=>
     int(1)
     ["scriptPubKey"]=>
     array(5) {
       ["asm"]=>
       string(60) "OP_HASH160 0f670955696f3835ef9204dad881b1c9f3002cc7 OP_EQUAL"
       ["hex"]=>
       string(46) "a9140f670955696f3835ef9204dad881b1c9f3002cc787"
       ["reqSigs"]=>
       int(1)
       ["type"]=>
       string(10) "scripthash"
       ["addresses"]=>
       array(1) {
         [0]=>
         string(35) "2MtefZG3K2Zs38dYntJwJKg1WqSEqNoW9sF"
       }
     }
   }
 }
 ["hex"]=>
 string(494) "02000000000101c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0100000017160014c7f49ae39660b15632f46a3c69a67ccb4de7dc20feffffff02801a06000000000017a914c9ebaa3be1c12351c9e07ea5758a68eb20ee509887cfc409000000000017a9140f670955696f3835ef9204dad881b1c9f3002cc7870247304402207cc7558274cd9458c44bf7554d0b049bba1388cd82057b3f8d0786bc28690aad022057eea44e01ff11c608e254a844b620f428fb515db6ef33cd199c57d3008ce89c01210303c01176f2db9efaa18e169e0ea7da87784208e05fcb1e0258ccb834805ed02d4ddc1c00"
 ["blockhash"]=>
 string(64) "00000000b1c75e4431f9c23a75779c964fa51cd714f9703781670f1a2c50a8c7"
 ["confirmations"]=>
 int(388)
 ["time"]=>
 int(1605141275)
 ["blocktime"]=>
 int(1605141275)
}
  1. 我想創建一個原始交易來花費兩個 utxos,並且我想在兩個不同的地址中花費它。因此,考慮到交易費用,我繼續手動創建交易。
       $fee = number_format(floatval($this->bitcoin->estimateSmartFee(6)['feerate'])/5, 8);
       $raw_amount = 0.00000000;
       $txids = [];
       $vouts = [];
       $amounts = [];
       $scriptPubs = [];
       for($i=0; $i<count($transactions); $i++){
           $total_amount+=$transactions[$i]["vout"][0]["value"];
           $txids[] = $transactions[$i]["txid"];
           $vouts[] = $transactions[$i]["vout"][0]["n"];
           $amounts[] = $transactions[$i]["vout"][0]["value"];
           $scriptPubs[] = $transactions[$i]["vout"][0]["scriptPubKey"]["hex"];
       }

       $inputs = '[';
       for($i=0; $i<count($vouts); $i++){
           $inputs.='{
                       "txid": "'.$txids[$i].'",
                       "vout": '.$vouts[$i].'
                     }';
           if($i+1!=count($vouts)){
               $inputs.=',';
           }
       }
       $inputs.=']';

       $minus_fee = $raw_amount-floatval($fee);
       $amount1 = $minus_fee*0.6;
       $amount2 = $minus_fee*0.4;

       $address1 = "tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk";
       $address2 = "tb1q2nrysxf8nwxv8euwtu470wgjruw25guxkal3wd";
       $outputs = '{
                     "'.$address1.'": '.$amount1.',
                     "'.$address2.'": '.$amount2.'
                   }';

       $raw_transaction = $this->bitcoin->createrawtransaction(json_decode($inputs), json_decode($outputs));

4.1。$raw_transaction 的結果如下:

0200000002c449b7874d903584c54e7bb63a1ce475ba536da800259d7bdda208fb719e66ef0000000000ffffffffe557ba77f7cdbac37e2b37b832dcb5556da8579ba2314610cd39a6947e839e8a0000000000ffffffff02e83c080000000000160014bcb00991436740df01fe7bf65d1f31670185ef97f07d050000000000160014b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e00000000
  1. 我解碼 $raw_transaction
$decoded = $this->bitcoin->decoderawtransaction($raw_transaction);

$decoded 的內容:

array(9) {
 ["txid"]=>
 string(64) "ba11b2caf11d79c1f4bc349f40895c380218e9ab91ec4c1d52aaee8284749549"
 ["hash"]=>
 string(64) "ba11b2caf11d79c1f4bc349f40895c380218e9ab91ec4c1d52aaee8284749549"
 ["version"]=>
 int(2)
 ["size"]=>
 int(154)
 ["vsize"]=>
 int(154)
 ["weight"]=>
 int(616)
 ["locktime"]=>
 int(0)
 ["vin"]=>
 array(2) {
   [0]=>
   array(4) {
     ["txid"]=>
     string(64) "ef669e71fb08a2dd7b9d2500a86d53ba75e41c3ab67b4ec58435904d87b749c4"
     ["vout"]=>
     int(0)
     ["scriptSig"]=>
     array(2) {
       ["asm"]=>
       string(0) ""
       ["hex"]=>
       string(0) ""
     }
     ["sequence"]=>
     int(4294967295)
   }
   [1]=>
   array(4) {
     ["txid"]=>
     string(64) "8a9e837e94a639cd104631a29b57a86d55b5dc32b8372b7ec3bacdf777ba57e5"
     ["vout"]=>
     int(0)
     ["scriptSig"]=>
     array(2) {
       ["asm"]=>
       string(0) ""
       ["hex"]=>
       string(0) ""
     }
     ["sequence"]=>
     int(4294967295)
   }
 }
 ["vout"]=>
 array(2) {
   [0]=>
   array(3) {
     ["value"]=>
     float(0.0053988)
     ["n"]=>
     int(0)
     ["scriptPubKey"]=>
     array(5) {
       ["asm"]=>
       string(42) "0 bcb00991436740df01fe7bf65d1f31670185ef97"
       ["hex"]=>
       string(44) "0014bcb00991436740df01fe7bf65d1f31670185ef97"
       ["reqSigs"]=>
       int(1)
       ["type"]=>
       string(18) "witness_v0_keyhash"
       ["addresses"]=>
       array(1) {
         [0]=>
         string(42) "tb1qhjcqny2rvaqd7q0700m968e3vuqctmuhhyk9fk"
       }
     }
   }
   [1]=>
   array(3) {
     ["value"]=>
     float(0.0035992)
     ["n"]=>
     int(1)
     ["scriptPubKey"]=>
     array(5) {
       ["asm"]=>
       string(42) "0 b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e"
       ["hex"]=>
       string(44) "0014b92b9f7f7bf6e4e1cdd73d3d23a1dbc98aa57d4e"
       ["reqSigs"]=>
       int(1)
       ["type"]=>
       string(18) "witness_v0_keyhash"
       ["addresses"]=>
       array(1) {
         [0]=>
         string(42) "tb1qhy4e7lmm7mjwrnwh857j8gwmex922l2wwc830y"
       }
     }
   }
 }
}
  1. 現在我繼續對之前創建的交易進行部分簽名。
       $private_key = $this->bitcoin->dumpprivkey("2Msqe8jLEReQpD4CYnRj29cAqQS8c81zidv"); // cS6e5CojfNy2ELWcsB4tLFVQV9wSSQUw1PU4YrSwRSjYLaC8ikrU
       $redeemScript = "522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae";
       $vin = $raw_transaction["vin"];
       $vout = $raw_transaction["vout"];

       $prevtxs = '[';
       for($i=0; $i<count($txids); $i++){
           $prevtxs .= '{
                   "txid": "'.$txids[$i].'",
                   "vout": '.$vouts[$i].',
                   "scriptPubKey": "'.$scriptPubs[$i].'", 
                   "redeemScript": "'.$redeemScript.'",
                   "amount": '.$amounts[$i].'
                 }';
           if($i+1!=count($txids)){
               $json2.=',';
           }
       }
       $prevtxs.=']';

       $partially_signed = $this->bitcoin->signrawtransactionwithkey($raw_transaction, json_decode($private_key), json_decode($prevtxs));
  1. 並且在上一步中是該過程失敗的地方。交易未簽名,bitcoind 返回此錯誤:
redeemScript/witnessScript does not match scriptPubKey

我正在使用比特幣測試網對其進行測試,我認為我添加了所有資訊。如果有人知道錯誤在哪裡,請告訴我。我搜尋了資訊以修復它,但我找不到我的問題的解決方案。

已編輯:我按照下一個教程進行操作,在那裡我注意到我使用了錯誤的 scriptpub,並且我使用 getrawtransaction 檢索了資金輸入,並傳遞了第二個參數 (true) 以獲取解碼的交易。好吧,現在以前的錯誤消失了,但現在它拋出了一個新錯誤:redeemScript/witnessScript does not match scriptPubKey

謝謝

您的兌換腳本雜湊應該是:

RIPEMD160(的SHA256(0x522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae))= 0bf37781383277cbb4e544c402f5265a51f4d828

這導致了這個地址:

2MtLR5mKgWRZBYtCYiSuaKpTUSuney9Cg2V

然而,你不知何故最終得到了這個雜湊:

c9ebaa3be1c12351c9e07ea5758a68eb20ee5098

這導致了這個地址:

2NBet88aY57CSYCkH7nXKb2Bxae4K3xdRBT

2MtLR5mKgWRZBYtCYiSuaKpTUSuney9Cg2V是您從 scriptHash 派生的新地址0x0bf37781383277cbb4e544c402f5265a51f4d8289。一直回到第 2 步並將 0.005 和 0.004 發送到2MtLR5mKgWRZBYtCYiSuaKpTUSuney9Cg2V。這些交易的 scriptPubKey 都將是OP_HASH160 0bf37781383277cbb4e544c402f5265a51f4d828 OP_EQUAL.

像在第 3 步中所做的那樣檢索這些 txid 以建構新的支出交易。支出交易將有兩個輸入。一個解鎖 0.005 BTC,另一個解鎖 0.004 BTC。您需要包含滿足解鎖條件的每個輸入的相應 scriptSigs。您的解鎖條件是 2-3 多重簽名,因此您的 scriptSig 應該如下所示:

OP_0 <First Signature> <Second Signature> < OP_2 <First PublicKey> <Second PublicKey> <Third PublicKey> OP_3 OP_CHECKMULTISIG >
  1. OP_0被編碼為0x00十六進制。
  2. 假設你的第一個 DER 編碼的簽名是 72 字節,最後加上一個字節的雜湊碼類型,加上開頭的一個字節長度前綴如下:

49 30460221啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

  1. 假設你的第二個 DER 編碼的簽名也是 72 字節,最後加上一字節的雜湊碼類型,加上開頭的一字節長度前綴如下:

49 30460221CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC0221DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD 01

  1. < OP_2 <First PublicKey> <Second PublicKey> <Third PublicKey> OP_3 OP_CHECKMULTISIG >加上一個字節長度的前綴以十六進制編碼:

69 522103E33AF80B92E4EFD64852166D6126E10AAFDCC741579C67C6AAA102DF4BF6A54579333B7796C626S20CEBEB22102DF4BF4DB1755C6908080CEBEB22102DF4BF4B1755C29333B7755C626SA29462AD102B42C099A1279B693B

您的十六進制 scriptSig 將如下所示:

0x004930460221AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0221BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB014930460221CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC0221DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD0169522103e33af80b92e4efd64852166d6126e10aafdcc741579c67c6aa9eb89080cebeb22102df4bf6a54f79b333b7796d2941de13b1755c00c09f4dbf696c7729aea29462102102c099a12b6cba679ad56d8e793847f422634bc84b42cb3fae0b00a6943f56921253ae

也為其他輸入構造 scriptSig。您將需要自定義您的 php 程式碼以實現這一切。

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