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>>
scriptPubKey: HASH160 <20-byte-script-hash> EQUAL

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:

  1. witness <signature> <pubkey> and scriptSig <0 <20-byte-key-hash-compressed-pub-key>> are pushed onto the stack.
  2. 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

  • Can you be more clear in your question? In one part you mention OP_EQUALVERIFY and OP_DUP operators are used in the P2SH(P2WPKH), which is clearly not the case
    – Ugam Kamat
    Mar 19, 2019 at 9:53
  • @BlockGuru This is exactly my point, why does the test vector here github.com/bitcoin/bips/blob/master/… contain the scriptCode field: 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...
    – Malone
    Mar 19, 2019 at 10:04

1 Answer 1


The one that you are referring to is sighashing. There is no good reason to use 1976a91479091972186c449eb1ded22b78e40d009bdf008988ac (dup hash160 [79091972186c449eb1ded22b78e40d009bdf0089] equalverify checksig as the scriptCode in Segwit but to limit the number of changes SegWit makes to the original sighashing. In designing SegWit, only intended changes were made. The hash structure remains the same, and contains all the same data. The sighash flags still have the same effect. The only thing that changes in BIP-143 is that some data is organized under precomputed hashes, and the input amounts go into it.

  • So with that in mind, the scriptCode is not executed at any point? It's just included to reduce changes to the protocol. If I remove this scriptCode field, I should still be able to generate a valid hash...?
    – Malone
    Mar 19, 2019 at 13:01
  • From what I get, If you change the scriptCode, it will change the hash of the message that you sign. Other nodes verifying the transaction will deem the signature as invalid.
    – Ugam Kamat
    Mar 19, 2019 at 14:54
  • Why is this true? -> For P2WPKH witness program, the scriptCode is 0x1976a914{20-byte-pubkey-hash}88ac (as per bip143)
    – Malone
    Mar 19, 2019 at 15:18
  • I don't know the origin of scriptcode, but related to your second question I assume it can be explained well in this scenario. Imagine you have a coffee shop and you want to create all transactions in SegWit so as to reduce transaction fees. However, it is uncertain whether your customers have wallets that support SegWit. When you create P2SH(P2WPKH), other customers' wallets see this as a normal P2SH transaction and can make payments to it without being aware that it is a SegWit transaction. When all wallets get updated to SegWit, it is presumed that we will gravitate to native BIP173 address
    – Ugam Kamat
    Mar 19, 2019 at 15:19
  • @Malone the scriptCode is the same in both P2WPKH and P2SH(P2WPKH). 0x1976a914{20-byte-pubkey-hash}88ac. Just that different public keys have been used for both transactions shown.
    – Ugam Kamat
    Mar 19, 2019 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.