I'm trying to understand the meaning of scriptCode
in the context of spending a P2SH(P2WPKH) UTXO.
I've already read the following answer to a similar question, but it does not fully address my misunderstanding.
After reading BIP141 for P2WPKH nested in BIP16 P2SH, we're told that the structure is:
witness: <signature> <pubkey>
scriptSig: <0 <20-byte-key-hash>>
(0x160014{20-byte-key-hash})
scriptPubKey: HASH160 <20-byte-script-hash> EQUAL
(0xA914{20-byte-script-hash}87)
I noticed that the scriptCode
gets committed to the hash to be signed for each input. Can someone explain why 1976a91479091972186c449eb1ded22b78e40d009bdf008988ac
(dup hash160 [79091972186c449eb1ded22b78e40d009bdf0089] equalverify checksig
) is used as the script code (from my understanding the scriptPubKey or locking script) for a p2sh(p2wpkh)
UTXO? That script corresponds to a p2pkh
scriptPubKey (surely this is out of context here)?
I understand the sequence of execution as:
witness <signature> <pubkey>
andscriptSig <0 <20-byte-key-hash-compressed-pub-key>>
are pushed onto the stack.- Then the scriptPubKey:
HASH160 <20-byte-script-hash> EQUAL
is executed with redeemScript (<0 <20-byte-key-hash-compressed-pub-key>>
) as the arg, eventually resulting in the execution of CHECKSIG.
Why include a scriptCode
that corresponds to a script that is seemingly out of context and not used?
Why is the following true?
For P2WPKH witness program, the scriptCode is 0x1976a914{20-byte-pubkey-hash}88ac.
It is referenced as prevOutScript
in this Javascript library: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/transaction.js#L396
1976a91479091972186c449eb1ded22b78e40d009bdf008988ac
when it is clearly not used? It's counterintuitive, as far as I know that script is only relevant of a p2pkh locking scriptPubKey...