Pay-to-Script-Hash Mechanics
Pay-to-Script-Hash Mechanics (P2SH β BIP 16)
While native multisig works perfectly inside Bitcoinβs virtual machine, deploying raw multisig scripts directly into transaction outputs introduced massive real-world problems.
To solve this, developers activated Pay-to-Script-Hash (P2SH) via a soft fork in 2012 (formalized in BIP 16).
π The Problems with Raw Native Multisig
Before P2SH, if a merchant wanted to use a 2-of-3 multisig wallet for safety, anyone paying that merchant had to construct a massive, non-standard transaction output:
OP_2 <PubKey_A> <PubKey_B> <PubKey_C> OP_3 OP_CHECKMULTISIG
This model had three severe flaws:
- Sender Burden: Senders had to pay significantly higher transaction fees because the output (
scriptPubKey) was massive, and fee costs are determined by transaction data size. - Inconvenience: The sender had to know all three of the receiverβs public keys and understand the exact script layout just to send a basic payment.
- UTXO Bloat: Because these outputs contained large public keys, they took up a massive amount of space inside full nodes' RAM-cached
chainstatedatabases.
π‘ The Solution: Pay-to-Script-Hash (P2SH)
P2SH shifts the burden of complex scripts from the sender to the receiver.
Instead of sending coins to a massive, complex lock, the sender locks the coins to a simple 20-byte cryptographic hash of the spending script (which we call the Redeem Script).
* The sender's scriptPubKey becomes incredibly standard, short, and cheap:
OP_HASH160 <20-Byte-RedeemScriptHash> OP_EQUAL
- This compiles into a clean address starting with the number
3. To the sender, paying a complex 15-key multisig looks exactly like paying a standard single-key address!
NATIVE MULTISIG (SENDER PAYS FOR BLOAT)
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β scriptPubKey: OP_2 <PubKeyA> <PubKeyB> <PubKeyC> OP_3 OP_CHECKMULTISIG β
βββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β (Huge byte footprint in output)
βΌ
P2SH (RECEIVER PAYS FOR BLOAT)
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β scriptPubKey: OP_HASH160 <RedeemScriptHash> OP_EQUAL β
βββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β (Tiny, standard output footprint)
βΌ
βοΈ How P2SH Validation Works (Two-Step Execution)
When the receiver wants to spend the P2SH output, they must present the raw signatures AND the full serialized Redeem Script inside their inputβs scriptSig:
scriptSig(Input):<Sig_A> <Sig_C> <Serialized_RedeemScript>
A validating full node executes the verification process in two distinct, sequential phases:
P2SH VALIDATION CYCLE
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PHASE 1: THE HASH CHECK β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β’ Node executes: OP_HASH160 <RedeemScriptHash> OP_EQUAL β
β β’ It hashes the Serialized_RedeemScript from the spender. β
β β’ Checks if Hash(RedeemScript) == RedeemScriptHash in the output. β
βββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βββββΊ [FAIL] βββΊ Tx Rejected
β
βΌ [PASS]
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PHASE 2: REDEEM SCRIPT EXECUTION β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β’ Node deserializes the Serialized_RedeemScript bytes back into Opcodesβ
β β’ Executes the script: OP_2 <PubA> <PubB> <PubC> OP_3 OP_CHECKMULTISIG β
β β’ Runs this script using the remaining signatures: [0, Sig_A, Sig_C] β
βββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
Is Final Execution = 1?
[YES] βββΊ Spend Approved (Valid)
[NO] βββΊ Transaction Rejected (Invalid)
π Key Benefits of P2SH
- Fairness: The receiver of the complex script is the one who has to provide the massive signature and redeem script bytes during spending. Therefore, the receiver pays the high transaction fee, which is appropriate since they are the ones choosing to use multisig.
- Abstraction: Senders do not need to know whether they are paying a single user, a 2-of-3 multisig escrow, or a 15-of-15 corporate vault. To the sender, it is just a standard "3-address."
- Upgradability: P2SH paved the way for soft-forking complex scripts and structures (like SegWit) into the network without requiring hard-fork consensus changes.
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: