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?

1 Answer 1


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


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

  • Where are the ScriptCodes defined? Aug 15, 2017 at 0:58
  • 1
    The scriptCodes are defined in BIP 143: github.com/bitcoin/bips/blob/master/…
    – Ava Chow
    Aug 15, 2017 at 1:09
  • @AndrewChow Are P2SH and P2PH defined similarly? Didn't see definitions for them in BIP 143 though.
    – qweruiop
    Apr 11, 2018 at 2:32
  • 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
    Apr 11, 2018 at 2:53

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.