Multi-Signature

P2SH地址解碼

  • September 8, 2016

P2SH地址的解碼過程是怎樣的?到目前為止,

我們已經掌握了這些資訊。

讓我們通過一個範例 tx bea1e7ae7ecdc502215717f30d62a12085bb2866d8db730dfb626b60c337534c

十六進制腳本(用於第一個輸出)是:514104398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2210250504b2d4245544120506565722d506565722d6e6574776f726b2062657461212102553432353135362e31323234202020202020202020202020202020202020202053ae

腳本解碼給出:

{
   "asm" : "1 04398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2 0250504b2d4245544120506565722d506565722d6e6574776f726b206265746121 02553432353135362e313232342020202020202020202020202020202020202020 3 OP_CHECKMULTISIG",
   "reqSigs" : 1,
   "type" : "multisig",
   "addresses" : [
       "1TestLVFK8DsRYFQkDDwvfsPHqUqWPBe7",
       "1H16KgZg3wHgApvHvZkSocxN6ibzNT9Cc7",
       "1N31mRc4tiGumXdRmb1Bzk9BoG4Bc7Ctbi"
   ],
   "p2sh" : "3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8"
}

我們可以分解這個腳本嗎?

這是問題中的交易範例

贖回腳本是:514104398184a2cef0d7b73ed7a3a1d4ad16296c3c6986bed0bd72775060aae9891979eaea1efb28d7eb1da3304ec38a98b42086e3be2ceba82b0e932128ec422a6fc2210250504b2d4245544120506565722d506565722d6e6574776f726b2062657461212102553432353135362e31323234202020202020202020202020202020202020202053ae

要導出 P2SH 地址,我們執行以下操作: 使用 PHP的

sha256(redeemScript) :

$sha256= hash('sha256',hexStringToByteString($redeemScript));
SHA256: 8b1c6ece53385d6ae3cf3c40fc6239389a7006d9f46047d5d67c3fa78dd2afcc

Ripmd160 生成的雜湊

$ripemd160=hash('ripemd160',hexStringToByteString($sha256));
RIPEMD160: c25497b8a9bcc393f47abd19a435d1af440916ac

在前面加上“05”並散列兩次

$hashTwice = hash('sha256',hexStringToByteString (hash('sha256',hexStringToByteString ("05" .  $ripemd160))));
double hash: 479ce4efc25c4195ef3cc015d74c45034a2c72f21e322ba30f5c26d872e58b38

前 4 個字節是 chscksum

$checksum=substr($hashTwice,0,8);
checksum: 479ce4ef

將地址組成為:

base58-encode:[單字節版本][20 字節雜湊][4 字節校驗和]

$addr= "05" . $ripemd160 . $checksum;
address: 05c25497b8a9bcc393f47abd19a435d1af440916ac479ce4ef

base58 編碼

$p2sh=base58_encode($to_encode);
base58: 3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8

我們得到正確的 P2SH 地址:3KQYMMqMBTv8254UqwmaLzW5NDT879KzK8

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