The Redeem Script
The Redeem Script and Unlocking P2SH
The Redeem Script is the heart of a P2SH transaction. It is the actual set of instructions that governs the spending of the coins. In a P2SH spend, the spender must provide both the signatures and the full Redeem Script itself.
1. What is a Redeem Script?
A Redeem Script is any valid Bitcoin script. The most common use case is a multisig script:
OP_2 [PubKey1] [PubKey2] [PubKey3] OP_3 OP_CHECKMULTISIG
To create a P2SH address, you simply take the double-hash (Hash160) of this entire byte sequence.
2. The Unlocking Script (ScriptSig)
When spending, the ScriptSig must follow this pattern:
[Signatures...] [Full Redeem Script]
3. The Two-Phase Execution
The Bitcoin Virtual Machine (BVM) processes P2SH in two distinct phases:
Phase A: Hash Verification
The BVM takes the Full Redeem Script from the end of the ScriptSig and hashes it. It compares this to the hash in the ScriptPubKey. If they match, the first part of the verification is successful.
Phase B: Script Execution
If Phase A passes, the BVM "unwraps" the Full Redeem Script and executes it as if it were the actual locking script. It uses the Signatures provided earlier in the ScriptSig to satisfy the conditions.
4. The Critical Rule
The Full Redeem Script must be the last item pushed in the ScriptSig. If it is placed anywhere else, the hashing step will fail, and the coins will be unspendable.
| Step | Data | Logic |
|---|---|---|
| 1 | [Full Script] |
Must hash to the Target |
| 2 | [Signatures] |
Must satisfy the Full Script |
In the next section, we will discuss the Benefits of P2SH in more detail.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: