P2SH

(pay to script hash)

A Pay-to-Script-Hash (P2SH) script is the script standard in Legacy for more complex transaction types (such as multi-sigs). Instead of sending Bitcoin to a specific script directly, the output pubKeyScript contains the hash of the lock script; in the future accompanying input ScriptSig, must both prove they know the original lock script & provide data & op_codes to unlock it. This means the unlocking input ScriptSig contains both the unlock script *&* the original lock / redeem script. Let’s break this down into two clear steps:

1. Validate Input Hashed Lock Script Matches Previous Output Hashed Lock Script.
First, to spend Bitcoin sent to a hashed script, it must be proven that the script we’re unlocking matches the original hashed script. This means that the stack first consumes the entire lock/redeem script as a single array & hashes it with HASH160. This is then compared to the original hashed script using OP_EQUAL.

2. Execution Of Unlock Script & Lock Script.
If the last op_code (OP_EQUAL) of the previous validation step returns 1/true, then we can move on to actually unlocking the lock script by pushing both the lock script (now separated into the appropriate data & op_code bytes). This is a more normal execution in which all elements are pushed to the stack & then processed in typical LIFO behavior.

OP_Code(s) Review

P2SH requires two (2) pieces of data, the scripts, & three (3) op_codes.

# UnlockScript/LockScript

[lock-script]

[unlock-script]

# PubKeyScript

<OP_HASH160>

<OP_20>

[hash160[lock-script]

<OP_EQUAL>

Step 1