Bitcoin Script Patterns
Common Bitcoin Script patterns provide reusable templates for building smart contracts. Understanding these patterns helps developers implement common use cases efficiently. Build these patterns in Stack Lab.
Common Patterns
1. Pay-to-Pubkey-Hash (P2PKH)
Standard single-signature:
ScriptPubKey:
OP_DUP OP_HASH160 <pubkey_hash> OP_EQUALVERIFY OP_CHECKSIG
ScriptSig:
<signature> <pubkey>
2. Multisig
Multiple signatures required:
ScriptPubKey:
<M> <pubkey1> <pubkey2> ... <pubkeyN> <N> OP_CHECKMULTISIG
ScriptSig:
OP_0 <sig1> <sig2> ... <sigM>
3. Hash Lock
Reveal secret to spend:
ScriptPubKey:
OP_HASH256 <hash> OP_EQUAL
ScriptSig:
<secret>
4. Time Lock
Absolute timelock:
ScriptPubKey:
<locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP <pubkey> OP_CHECKSIG
ScriptSig:
<signature>
5. Relative Time Lock
Relative timelock:
ScriptPubKey:
<blocks> OP_CHECKSEQUENCEVERIFY OP_DROP <pubkey> OP_CHECKSIG
ScriptSig:
<signature>
Advanced Patterns
Escrow Contract
Three-party escrow:
2-of-3 Multisig:
- Buyer + Seller
- Buyer + Escrow
- Seller + Escrow
Vault Contract
Time-delayed recovery:
Structure:
- Hot key: Immediate spend (small)
- Cold key: Delayed spend (large)
- Recovery: Longer delay
Inheritance Contract
Time-locked beneficiary:
Structure:
- Beneficiary key
- Time lock (absolute)
- Can claim after date
Code Examples
Creating Common Patterns
Miniscript
Miniscript is a structured language for expressing spending policies that compiles to Bitcoin Script. Instead of writing raw OP codes directly, you define what must hold (e.g., "2-of-3 keys" or "key A and after block N") and tools produce correct, analyzable Script.
Policy vs. Script
- Policy: High-level conditions (e.g.,
2 of [pk(A), pk(B), pk(C)]orand(pk(A), after(100))). - Script: The actual bytecode that nodes execute; Miniscript compiles policy → Script.
Fragments and Composition
Miniscript uses composable fragments (pk, thresh, and, or, after, older, hash, multi, etc.) that map to Script. This makes it easier to:
- Combine conditions (AND, OR, m-of-n) without hand-rolled scriptSig/witness.
- Analyze correctness and safety (no unintended spends, no dust).
- Estimate satisfaction size (witness size, number of signatures).
When to Use Miniscript
- Wallets: Multisig, timelocks, and recovery policies.
- Contracts: Vaults, Atomic Swaps-style hashlocks, and Taproot script trees.
- Protocols: Lightning, DLCs, and other smart contract templates.
See Miniscript for the full specification, fragment set, and Tapscript support.
Best Practices
For Developers
- Use established patterns: Don't reinvent
- Prefer Miniscript for complex policies: Compile from policy when possible
- Test thoroughly: Script bugs are costly
- Consider Taproot: Better privacy and efficiency
- Document patterns: Explain what contracts do
Related Topics
- Bitcoin Script - Script system
- OP Codes - Available operations
- Miniscript - Policy-to-script compiler
- Smart Contracts - Contract patterns
Resources
- Bitcoin Script Patterns
- Miniscript - Playground and reference
