Transaction Structure
Overview
P2SH transactions have a unique structure where the redeem script is provided in the input rather than being embedded in the output.
Output (Locking Script - ScriptPubKey)
The ScriptPubKey for P2SH is standardized and contains only the script hash.
OP_HASH160
OP_PUSHBYTES_20
89abcdefabbaabbaabbaabbaabbaabbaabbaabba
OP_EQUAL
# Hex value: a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba87Input (Unlocking Script - ScriptSig)
The ScriptSig contains the data required to satisfy the redeem script, followed by the serialized redeem script itself.
OP_0
OP_PUSHBYTES_72 3044022041b2a8a8e6c0c8e5b1c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f022012a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708101
OP_PUSHBYTES_71 304402207e8495b6a9c8d3f1a2b4c5d6e7f8091a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f02201a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b01
OP_PUSHDATA1 69 522102a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f7a8b9c0d1e2f3a42103b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f7a8b9c0d1e2f3a4b52103c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f7a8b9c0d1e2f3a4b5c53aeComponents:
- OP_0: Dummy element required due to a bug in OP_CHECKMULTISIG.
- <signature 1>: First DER-encoded signature with SIGHASH suffix.
- <signature 2>: Second DER-encoded signature with SIGHASH suffix.
- <redeem script>: The serialized 2-of-3 multisig script.
Script Execution
When validating a P2SH transaction, the script execution occurs in two phases:
- Script Hash Verification
- The serialized redeem script is hashed.
- The computed hash is compared against the script hash in the ScriptPubKey.
- Redeem Script Execution
- If the hashes match, the redeem script is deserialized.
- The script is executed with the remaining ScriptSig elements as inputs.
- If the hashes do not match, the transaction fails immediately.
The execution trace varies depending on the type of redeem script used:
- A 2-of-3 multisig script requires signature verification against multiple public keys.
- A timelock script involves time comparison operations.
- A hashlock script performs hash verification.
Each script type defines its own set of opcodes and stack operations.
Execution Trace
The example below demonstrates execution for a 2-of-3 multisig script, which is one of the most common P2SH use cases.
Phase 1: Redeem Script Hash Verification
- Push Dummy Element
Push OP_0 onto the stack. This dummy element is required due to a bug in OP_CHECKMULTISIG that consumes an extra stack element. - Push First Signature
Push the first 72-byte DER-encoded signature with SIGHASH suffix onto the stack. - Push Second Signature
Push the second 71-byte DER-encoded signature with SIGHASH suffix onto the stack. - Push Redeem Script
Push the serialized redeem script onto the stack. - Save Redeem Script (P2SH Rule)
Before executing the ScriptPubKey, the P2SH consensus rules save a copy of the top stack element (the serialized redeem script) for later execution. This happens outside the stack. - Hash Redeem Script
Pop the redeem script from the stack, apply SHA-256 followed by RIPEMD-160, and push the resulting 20-byte hash. - Push Expected Hash
Push the 20-byte expected script hash from the ScriptPubKey onto the stack. - Compare Hashes
Compare the computed hash with the expected hash. If they do not match, the script fails immediately. Both hash values are consumed and the result is pushed onto the stack.
Phase 2: Redeem Script Execution
- Verify and Prepare
The P2SH consensus rules verify that Phase 1 completed successfully (stack top is 1). The 1 value is removed, and the previously saved redeem script is deserialized into its component opcodes:- OP_2
- <pubkey1>
- <pubkey2>
- <pubkey3>
- OP_3
- OP_CHECKMULTISIG
- Push Required Signature Count
Push the number 2 onto the stack, indicating the minimum number of signatures required. - Push Public Keys
Push all three public keys from the redeem script onto the stack. - Push Total Key Count
Push the number 3 onto the stack, indicating the total number of public keys. - Verify Multisig
Verify that 2 valid signatures exist for any 2 of the 3 public keys. The dummy OP_0 element is consumed due to the off-by-one bug. If verification succeeds, 1 is pushed onto the stack. - Result
If the final stack contains 1, the transaction is valid and the funds are unlocked.