<p>The execution order is:</p>

<ul>
<li>scriptSig/witness to construct the initial stack</li>
<li>scriptPubKey (which may invoke redeemScript evaluation in case of P2SH, and witness script invocation in case of P2WSH)</li>
</ul>

<p>The scriptCode is simply a name for "the code being executed". Inside scriptPubKey evaluation, it is equal to the scriptPubKey. Inside redeemScript evaluation, it is equal to the redeemScript. Inside witness script evaluation, it is equal to the witness script.</p>

CC BY-SA 4.0
2018-11-06T20:48:29.613Z
Okay, I kind of understand the execution order a bit better. But I still don't understand what scriptCode is. Is it just scriptPubKey when signing an input? You might have to break it down for me in simplistic terms of why it is used. I know it checks the signature of the resulting stack containing pubkey and signature but what is the duplication (OP_DUP) and the push of data for? I thought this matching keyhash OP_HASH160 business was already done by the witness + scriptsig + scriptPubKey.
cryptodescriptor
CC BY-SA 4.0
2018-11-06T21:13:41.537Z
The reason it is used is historical. It just happens to be there. Yes, when you're signing an input it is it equal to the script which contains the CHECKSIG opcode which is going to verify your signature.
Pieter Wuille
CC BY-SA 4.0
2019-03-18T22:35:43.327Z
I'm not totally understanding this either, from my understanding the scriptCode in the bip143 p2sh(p2pkh) example is never used in execution at runtime? My understanding is that the witness (<signature> <pubkey>) and scriptSig (<0 <20-byte-key-hash-compressed-pub-key>>) are pushed onto the stack, then the scriptPubKey: HASH160 <20-byte-script-hash> EQUAL is executed, eventually resulting in the execution of  <signature> <pubkey> CHECKSIG. At what point does this scriptCode executed, or does it?
Malone
CC BY-SA 4.0
2018-11-06T20:25:11.603Z
2018-11-06T20:25:11.603Z
Pieter Wuille
<p>I've been banging my head on a desk for a few days trying to understand the segwit protocol properly.</p>

<p>Another roadblock has thrown me off-guard and I am struggling to grasp the concept of "scriptCode:" used in the signature examples of bip0143 <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification</a></p>

<p>I understand that a scriptPubKey is basically the locking conditions put on an output when spent. And I also understand that scriptSig is to redeem a previously locked output (destined for my pubkey/pubkeyhash).</p>

<p>I had come to pretty good grips with what I have just stated above, with the scriptSig data items being pushed onto the stack then getting evaluated by the pubKeyHash ect. But now there is this weird "scriptCode" field delcared that looks suspiciously identical to a p2pkh pubKeyScript.</p>

<p>So I came to the conclusion that it is just used the same in every hashPreImage:</p>

<pre><code>DUP  HASH160  &lt;keyhash&gt;  EQUALVERIFY  CHECKSIG
</code></pre>

<p>But then I started thinking about stack evaluation order and got confused again. What is the order of stack evaluation? Is it: scriptSig data push > pubKeyScript evaluation > scriptCode? What about if there is witness data invloved? When is that pushed onto the stack? Or am I totally missing the point here?</p>

<p>Another thing I am confused about is p2sh-p2wpkh evaluation. I do not understand why the scriptSig has to be a push of the redeemScript:</p>

<pre><code> 0 &lt;20-byte-key-hash&gt;
</code></pre>

<p><strong>re-emphasising my main question. I am struggling with the concept of scriptCode and script evaluation order in general. I would like to know if the scriptCode is always the same format if I am only planning to sign p2sh-p2wpkh, pk2pkh, pk2pkh</strong> </p>

<script><p2pkh><p2wpkh>
scriptcode, scriptsig, pubkeyscript confusion
cryptodescriptor
# scriptcode, scriptsig, pubkeyscript confusion

(Asked by: username_0 on 2018-11-06)

I've been banging my head on a desk for a few days trying to understand the segwit protocol properly.

Another roadblock has thrown me off-guard and I am struggling to grasp the concept of "scriptCode:" used in the signature examples of bip0143 <https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification>

I understand that a scriptPubKey is basically the locking conditions put on an output when spent. And I also understand that scriptSig is to redeem a previously locked output (destined for my pubkey/pubkeyhash).

I had come to pretty good grips with what I have just stated above, with the scriptSig data items being pushed onto the stack then getting evaluated by the pubKeyHash ect. But now there is this weird "scriptCode" field delcared that looks suspiciously identical to a p2pkh pubKeyScript.

So I came to the conclusion that it is just used the same in every hashPreImage:

```
DUP  HASH160  <keyhash>  EQUALVERIFY  CHECKSIG

```

But then I started thinking about stack evaluation order and got confused again. What is the order of stack evaluation? Is it: scriptSig data push > pubKeyScript evaluation > scriptCode? What about if there is witness data invloved? When is that pushed onto the stack? Or am I totally missing the point here?

Another thing I am confused about is p2sh-p2wpkh evaluation. I do not understand why the scriptSig has to be a push of the redeemScript:

```
 0 <20-byte-key-hash>

```

**re-emphasising my main question. I am struggling with the concept of scriptCode and script evaluation order in general. I would like to know if the scriptCode is always the same format if I am only planning to sign p2sh-p2wpkh, pk2pkh, pk2pkh**

**Tags:** script, p2pkh, p2wpkh


---

## 1 answers

### Answer by username_1 on 2018-11-06. Score: 2 | Accepted answer

The execution order is:

* scriptSig/witness to construct the initial stack
* scriptPubKey (which may invoke redeemScript evaluation in case of P2SH, and witness script invocation in case of P2WSH)

The scriptCode is simply a name for "the code being executed". Inside scriptPubKey evaluation, it is equal to the scriptPubKey. Inside redeemScript evaluation, it is equal to the redeemScript. Inside witness script evaluation, it is equal to the witness script.

> **Comments:**
> **username_0**: Okay, I kind of understand the execution order a bit better. But I still don't understand what scriptCode is. Is it just scriptPubKey when signing an input? You might have to break it down for me in simplistic terms of why it is used. I know it checks the signature of the resulting stack containing pubkey and signature but what is the duplication (OP_DUP) and the push of data for? I thought this matching keyhash OP_HASH160 business was already done by the witness + scriptsig + scriptPubKey.  
> **username_1**: The reason it is used is historical. It just happens to be there. Yes, when you're signing an input it is it equal to the script which contains the CHECKSIG opcode which is going to verify your signature.  
> **username_2**: I'm not totally understanding this either, from my understanding the scriptCode in the bip143 p2sh(p2pkh) example is never used in execution at runtime? My understanding is that the witness (<signature> <pubkey>) and scriptSig (<0 <20-byte-key-hash-compressed-pub-key>>) are pushed onto the stack, then the scriptPubKey: HASH160 <20-byte-script-hash> EQUAL is executed, eventually resulting in the execution of  <signature> <pubkey> CHECKSIG. At what point does this scriptCode executed, or does it?  

---

