Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: victorkimba17 on November 11, 2017, 08:06:27 AM



Title: Pay to Script Hash
Post by: victorkimba17 on November 11, 2017, 08:06:27 AM
For P2SH, i understand it lets the sender funds a transaction using 20 byte hash. The script supplied to redeem must hash to the scriptHash.

scriptPubKey: OP_HASH160 <scriptHash> OP_EQUAL
scriptSig: 0 <sig1> ... <script> OP_m <pubKey1> ... OP_n OP_CHECKMULTISIG

For this example, the processing is:

The checking process:
Stack   Script   Description
Empty.0 <sig1> <sig2> OP_2 <pubKey1> <pubKey2> <pubKey3> OP_3 OP_CHECKMULTISIG OP_HASH160 <scriptHash> OP_EQUAL   locking script combined with redeem script
0 <sig1> <sig2> OP_2 <pubKey1> <pubKey2> <pubKey3> OP_3   OP_CHECKMULTISIG OP_HASH160 <scriptHash> OP_EQUAL   Constants are added to the stack.
trueOP_HASH160 <scriptHash> OP_EQUAL   Signatures validated in the order of the keys in the script.

what happens after OP_CHECKMULTISIG, what value does OP_HASH160 hash to?





Title: Re: Pay to Script Hash
Post by: pebwindkraft on November 11, 2017, 11:29:04 AM
Looks like the formatting is a bit shuffled, can't get it clearly.
This is what I understand happens on the stack for a multisig P2SH tx (I ignore the 0 at the beginning for the CHECKMULTISIG bug):

Quote
Redeem Script     OP_2 PubKey1 PubKey2 PubKey3 OP_3 CHECKMULTISIG
Locking Script      HASH160 <20-byte hash of redeem script> EQUAL
Unlocking Script   Sig1 Sig2 <redeem script>
 

So when I want to unlock the tx, I drop Sig1, Sig2 and <redeem script> on the stack. Then the part of the locking script, and it gives:

Quote
Sig1
Sig2
<redeem script>
HASH160
<20-byte hash of redeem script>
EQUAL

(from Andreas' book "Mastering Bitcoin"):
The two scripts are combined in two stages. First, the redeem script is checked against the locking script to make sure the hash matches.

Replacing the <redeem script> and assuming the 20-byte hash of redeem script is "54c557e07dde5bb6cb791c7a540e0a4796f5e97e", we get this:

Quote
OP_2 PubKey1 PubKey2 PubKey3 OP_3 CHECKMULTISIG
HASH160
54c557e07dde5bb6cb791c7a540e0a4796f5e97e
EQUAL

And then, if the redeem script hash matches, this will be executed:
    <Sig1> <Sig2> OP_2 PK1 PK2 PK3 OP_3 CHECKMULTISIG

oh, the reference is on pages 151ff on the second edition of the book I mentioned.






Title: Re: Pay to Script Hash
Post by: victorkimba17 on November 11, 2017, 01:25:43 PM
Looks like the formatting is a bit shuffled, can't get it clearly.
 
I try to draw the table row and column line, but i don't know how to draw the line using the post reply formatter.

Quote
(from Andreas' book "Mastering Bitcoin"):
The two scripts are combined in two stages. First, the redeem script is checked against the locking script to make sure the hash matches.
I found his book online, the scripting chapter is at
https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/ch07.asciidoc



Title: Re: Pay to Script Hash
Post by: victorkimba17 on November 11, 2017, 03:14:24 PM
where are the bitcoin core files that handle P2SH transaction ?


Title: Re: Pay to Script Hash
Post by: victorkimba17 on November 12, 2017, 02:24:38 AM
where are the bitcoin core files that handle P2SH transaction ?

i figure it out.