BitcoinDev Logo

Bitcoin Script Patterns

Bitcoin Script defines spending conditions in locking scripts; Smart Contracts & Advanced Scripting describes how those conditions act as contracts. The patterns here are reusable templates you can turn into scriptPubKeys and use in address generation and transaction construction. You can build and experiment with them in Stack Lab. For a policy language that compiles to Script, see Miniscript.

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)] or and(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

See Miniscript for the full specification, fragment set, and Tapscript support.


Best Practices

For Developers

  1. Use established patterns: Don't reinvent
  2. Prefer Miniscript for complex policies: Compile from policy when possible
  3. Test thoroughly: Script bugs are costly
  4. Consider Taproot: Better privacy and efficiency
  5. Document patterns: Explain what contracts do

  • Bitcoin Script - Script system
  • OP Codes - Available operations
  • Miniscript - Policy-to-script compiler
  • Smart Contracts - Contract patterns

Resources