<p>The <code>ScriptCode</code> is the script that is evaluated for a witness program type. Each witness program type has a specific template for the <code>ScriptCode</code>. That is simply part of its definition.</p>

<p>For a Pay-to-Witness-Pubkey-Hash output the ScriptCode is</p>

<pre><code>OP_DUP OP_HASH160 &lt;pubkey hash&gt; OP_EQUALVERIFY OP_CHECKSIG
</code></pre>

<p>The <code>&lt;pubkey hash&gt;</code> for the ScriptCode is taken from the witness program.</p>

CC BY-SA 3.0
2017-08-15T00:58:53.447Z
Where are the ScriptCodes defined?
meshcollider
CC BY-SA 3.0
2017-08-15T01:09:05.570Z
The scriptCodes are defined in BIP 143: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification
Ava Chow
CC BY-SA 3.0
2018-04-11T02:32:25.990Z
@AndrewChow Are P2SH and P2PH defined similarly? Didn't see definitions for them in BIP 143 though.
qweruiop
CC BY-SA 3.0
2018-04-11T02:53:54.480Z
For P2PKH, the scriptCode is the scriptPubKey. For P2SH, it's typically the redeemScript. They aren't defined in BIP 143 because they are not segwit output types; BIP 143 defines segwit stuff. AFAIK, there is no "official" documentation or specification which lays out what actually the scriptCode is. You will need to look in the code for that.
Ava Chow
CC BY-SA 3.0
2017-08-13T05:37:43.457Z
2017-08-13T05:37:43.457Z
Ava Chow
<p>I don't understand what the 'ScriptCode' thing is. Is it a part of transaction format? The wiki <a href="https://en.bitcoin.it/wiki/Transaction" rel="noreferrer">https://en.bitcoin.it/wiki/Transaction</a> describes scriptPubKey and scriptSig, mentioning nothing about 'ScriptCode'. </p>

<p>When viewing an example P2WPKH transaction here <a href="http://n.bitcoin.ninja/checktx?txid=d869f854e1f8788bcff294cc83b280942a8c728de71eb709a2c29d10bfe21b7c" rel="noreferrer">http://n.bitcoin.ninja/checktx?txid=d869f854e1f8788bcff294cc83b280942a8c728de71eb709a2c29d10bfe21b7c</a></p>

<p>The "Script Code" field is displayed as "OP_DUP OP_HASH160 1d7cd6c75c2e86f4cbf98eaed221b30bd9a0b928 OP_EQUALVERIFY OP_CHECKSIG"(so the standard script used by ordinary P2PKH transactions), but these are not given by scriptSig or scriptPubKey(obviously, since those two have a very specific form required for SegWit transactions). Can someone explain where this stuff comes from?</p>

<script><segregated-witness>
What is 'ScriptCode'?
user1633011
# What is 'ScriptCode'?

(Asked by: username_0 on 2017-08-13)

I don't understand what the 'ScriptCode' thing is. Is it a part of transaction format? The wiki <https://en.bitcoin.it/wiki/Transaction> describes scriptPubKey and scriptSig, mentioning nothing about 'ScriptCode'.

When viewing an example P2WPKH transaction here <http://n.bitcoin.ninja/checktx?txid=d869f854e1f8788bcff294cc83b280942a8c728de71eb709a2c29d10bfe21b7c>

The "Script Code" field is displayed as "OP\_DUP OP\_HASH160 1d7cd6c75c2e86f4cbf98eaed221b30bd9a0b928 OP\_EQUALVERIFY OP\_CHECKSIG"(so the standard script used by ordinary P2PKH transactions), but these are not given by scriptSig or scriptPubKey(obviously, since those two have a very specific form required for SegWit transactions). Can someone explain where this stuff comes from?

**Tags:** script, segregated-witness


---

## 1 answers

### Answer by username_1 on 2017-08-13. Score: 5

The `ScriptCode` is the script that is evaluated for a witness program type. Each witness program type has a specific template for the `ScriptCode`. That is simply part of its definition.

For a Pay-to-Witness-Pubkey-Hash output the ScriptCode is

```
OP_DUP OP_HASH160 <pubkey hash> OP_EQUALVERIFY OP_CHECKSIG

```

The `<pubkey hash>` for the ScriptCode is taken from the witness program.

> **Comments:**
> **username_2**: Where are the ScriptCodes defined?  
> **username_1**: The scriptCodes are defined in BIP 143: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification  
> **username_3**: @AndrewChow Are P2SH and P2PH defined similarly? Didn't see definitions for them in BIP 143 though.  
> **username_1**: For P2PKH, the scriptCode is the scriptPubKey. For P2SH, it's typically the redeemScript. They aren't defined in BIP 143 because they are not segwit output types; BIP 143 defines segwit stuff. AFAIK, there is no "official" documentation or specification which lays out what actually the scriptCode is. You will need to look in the code for that.  

---

