7
2
In a standard P2PKH configuration, the scripts look like:
Pubkey script: OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Signature script: <sig> <pubkey>
When redeeming P2PKH outputs, OP_DUP is done because the pubkey is used twice, once in the hash and then verifying the hashes match, and then again in verifying the signature. So, here it seems like OP_HASH160 is consuming the top stack element and replacing it with the HASH160() of what was there.
In P2SH transactions, the scripts look like:
Pubkey script: OP_HASH160 <Hash160(redeemScript)> OP_EQUAL
Signature script: <sig> [sig] [sig...] <redeemScript>
When redeeming P2SH outputs, there is no OP_DUP. Won't this mean that the redeemScript will be consumed by the OP_HASH160? How can the redeemScript be executed if it was consumed by the OP_HASH160? I think I am missing something here.
1Also: good question! Any question that forces me to look the answer up is good. :) – Nick ODell – 2014-10-20T21:31:48.117
Thanks! So it seems like there just custom code that says, if P2SH {do this extra checking, interpreting the top element of scriptSig as a script}. Is there no way to just do all this P2SH specific stuff with the OP codes that exist? Seems like no. Would it be possible to build in such an opcode, like OP_SCRIPT_DATA to signal that this is both data to be pushed and a script in itself to be run and verified? – morsecoder – 2014-10-20T22:13:34.097
Where would I see this process you referenced shown in the source code? – morsecoder – 2014-10-20T22:15:47.560
@StephenM347 (responding to first comment) No. The original Bitcoin spec did not allow for looping or evaluating subscripts, in order to make them easy to statically analyse.
Would it be possible to build in such an opcode, like OP_SCRIPT_DATASure, but the existing push-thing-on-stack commands do the trick just fine. – Nick ODell – 2014-10-20T22:37:50.127@StephenM347 (second comment) Added. The embarrassing thing is that I found out that I was wrong when I looked at the source code. Oh well, fixed. – Nick ODell – 2014-10-20T22:39:20.330