<p>The one that you are referring to is sighashing. There is no good reason to use <code>1976a91479091972186c449eb1ded22b78e40d009bdf008988ac (dup hash160 [79091972186c449eb1ded22b78e40d009bdf0089] equalverify checksig</code> as the <code>scriptCode</code> 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.</p>

CC BY-SA 4.0
2019-03-19T13:01:45.367Z
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
CC BY-SA 4.0
2019-03-19T14:54:29.537Z
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
CC BY-SA 4.0
2019-03-19T15:18:59.327Z
Why is this true? -> For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac` (as per bip143)
Malone
CC BY-SA 4.0
2019-03-19T15:19:32.290Z
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
CC BY-SA 4.0
2019-03-19T15:25:48.813Z
@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
CC BY-SA 4.0
2019-03-19T11:27:22.030Z
2019-03-19T11:27:22.030Z
Ugam Kamat
<p>I'm trying to understand the meaning of <code>scriptCode</code> in the context of spending a P2SH(P2WPKH) UTXO.</p>

<p>I've already read the <a href="https://bitcoin.stackexchange.com/questions/80767/scriptcode-scriptsig-pubkeyscript-confusion">following answer</a> to a similar question, but it does not fully address my misunderstanding.</p>

<p>After reading BIP141 for P2WPKH nested in BIP16 P2SH, we're told that the structure is:</p>

<pre><code>witness:      &lt;signature&gt; &lt;pubkey&gt;
scriptSig:    &lt;0 &lt;20-byte-key-hash&gt;&gt;
                  (0x160014{20-byte-key-hash})
scriptPubKey: HASH160 &lt;20-byte-script-hash&gt; EQUAL
                  (0xA914{20-byte-script-hash}87)

</code></pre>

<p>I noticed that the <code>scriptCode</code> gets committed to the hash to be signed for each input. Can someone explain why <code>1976a91479091972186c449eb1ded22b78e40d009bdf008988ac</code> (<code>dup hash160 [79091972186c449eb1ded22b78e40d009bdf0089] equalverify checksig</code>) is used as the script code (from my understanding the scriptPubKey or locking script) for a <code>p2sh(p2wpkh)</code> UTXO? That script corresponds to a <code>p2pkh</code> scriptPubKey (surely this is out of context here)?</p>

<p>I understand the sequence of execution as: </p>

<ol>
<li><code>witness &lt;signature&gt; &lt;pubkey&gt;</code> and <code>scriptSig &lt;0 &lt;20-byte-key-hash-compressed-pub-key&gt;&gt;</code> are pushed onto the stack.</li>
<li>Then the scriptPubKey: <code>HASH160 &lt;20-byte-script-hash&gt; EQUAL</code> is executed with redeemScript (<code>&lt;0 &lt;20-byte-key-hash-compressed-pub-key&gt;&gt;</code>) as the arg, eventually resulting in the execution of   CHECKSIG.</li>
</ol>

<p>Why include a <code>scriptCode</code> that corresponds to a script that is seemingly out of context and not used?</p>

<p>Why is the following true? </p>

<pre><code>For P2WPKH witness program, the scriptCode is 0x1976a914{20-byte-pubkey-hash}88ac.
</code></pre>

<p>It is referenced as <code>prevOutScript</code> in this Javascript library: <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/transaction.js#L396" rel="nofollow noreferrer">https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/transaction.js#L396</a></p>

CC BY-SA 4.0
2019-03-19T09:53:59.957Z
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
CC BY-SA 4.0
2019-03-19T10:04:43.630Z
@BlockGuru This is exactly my point, why does the test vector here https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#p2sh-p2wpkh 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
<hash><script><bip143>
ScriptCode field in P2SH-P2WPKH BIP-143 Test Vector
Malone
# ScriptCode field in P2SH-P2WPKH BIP-143 Test Vector

(Asked by: username_0 on 2019-03-19)

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](https://bitcoin.stackexchange.com/questions/80767/scriptcode-scriptsig-pubkeyscript-confusion) 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:

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>

**Tags:** hash, script, bip143

> **Comments:**
> **username_1**: 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  
> **username_0**: @BlockGuru This is exactly my point, why does the test vector here https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#p2sh-p2wpkh 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...  

---

## 1 answers

### Answer by username_1 on 2019-03-19. Score: 0 | Accepted answer

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.

> **Comments:**
> **username_0**: 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...?  
> **username_1**: 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.  
> **username_0**: Why is this true? -> For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac` (as per bip143)  
> **username_1**: 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  
> **username_1**: @username_0 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.  

---

