Bitcoin Scripting
Script opcodes, P2PKH, P2SH, P2WPKH, Timelock (CLTV, CSV), multisig scripts, and covenant proposals.
Overview
Bitcoin Script is the programmable language that enables Bitcoin's core functionality. Understanding it is essential for building on Bitcoin.
Script Opcodes
P2PKH (Pay-to-Public-Key-Hash)
The original Bitcoin transaction type.
P2PKH Script Flow
# Locking Script (scriptPubKey)
locking_script = [
"OP_DUP",
"OP_HASH160",
"pubkey_hash_20_bytes",
"OP_EQUALVERIFY",
"OP_CHECKSIG"
]
# Unlocking Script (scriptSig)
unlocking_script = [
"signature_71_bytes",
"public_key_33_bytes"
]
# Combined execution:
# Stack: [sig, pubkey]
# OP_DUP â [sig, pubkey, pubkey]
# OP_HASH160 â [sig, pubkey, pubkey_hash]
# <pubkey_hash> â [sig, pubkey, pubkey_hash, expected_hash]
# OP_EQUALVERIFY â [sig, pubkey] (verifies hashes match)
# OP_CHECKSIG â [TRUE] (verifies signature)
P2PKH Address Generation
Address = Base58Check(version + Hash160(pubkey) + checksum)
Version: 0x00 (mainnet), 0x6F (testnet)
P2SH (Pay-to-Script-Hash)
Enables complex scripts by hiding the script behind a hash.
P2SH Multisig Example
# Redemption Script (redeemScript)
redeem_script = [
"OP_2", # 2-of-3 multisig
"pubkey_A",
"pubkey_B",
"pubkey_C",
"OP_3",
"OP_CHECKMULTISIG"
]
# Locking Script
locking_script = [
"OP_HASH160",
hash160(redeem_script), # 20-byte hash
"OP_EQUAL"
]
# ScriptSig
script_sig = [
"OP_0", # dummy (CHECKMULTISIG bug)
"sig_A",
"sig_C" # 2 signatures required
]
P2SH Benefits
| Feature | P2PKH | P2SH |
|---|---|---|
| Script Complexity | Simple only | Any script |
| Sender Responsibility | Knows full script | Only knows hash |
| Fee Payment | Sender | Sender |
| Flexibility | Low | High |
P2WPKH (Pay-to-Witness-Public-Key-Hash)
Native SegWit transaction type.
P2WPKH Structure
# Witness Program
witness_program = [
"OP_0", # SegWit version 0
hash160(pubkey) # 20-byte public key hash
]
# Witness Data (in witness field, not scriptSig)
witness = [
"signature",
"public_key"
]
# Output Script (scriptPubKey)
output_script = [
"OP_0",
"20_bytes_hash" # Push 20 bytes directly
]
P2WPKH vs P2PKH
| Aspect | P2PKH | P2WPKH |
|---|---|---|
| Address Format | 1... | bc1q... |
| Signature Location | scriptSig | Witness |
| Malleability | Vulnerable | Fixed |
| Fee (typical) | ~226 bytes | ~141 vB |
P2WSH (Pay-to-Witness-Script-Hash)
SegWit version for complex scripts.
P2WSH Example
# Witness Script
witness_script = [
"OP_2",
"pubkey_A",
"pubkey_B",
"pubkey_C",
"OP_3",
"OP_CHECKMULTISIG"
]
# Output Script
output_script = [
"OP_0",
sha256(witness_script) # 32-byte hash
]
# Witness
witness = [
"OP_0",
"sig_A",
"sig_C",
witness_script # Full script revealed
]
Timelock Mechanisms
CLTV (CheckLockTimeVerify) - BIP-65
Absolute timelock: transaction cannot be mined before specific time/block.
# CLTV Script
cltv_script = [
"OP_CHECKLOCKTIMEVERIFY",
"OP_DROP",
"OP_DUP",
"OP_HASH160",
"pubkey_hash",
"OP_EQUALVERIFY",
"OP_CHECKSIG"
]
# Transaction nLockTime field
nLockTime = 1700000000 # Unix timestamp
# OR
nLockTime = 800000 # Block height
CSV (CheckSequenceVerify) - BIP-112
Relative timelock: transaction cannot be mined until N blocks after UTXO confirmation.
# CSV Script
csv_script = [
"OP_CHECKSEQUENCEVERIFY",
"OP_DROP",
"OP_DUP",
"OP_HASH160",
"pubkey_hash",
"OP_EQUALVERIFY",
"OP_CHECKSIG"
]
# nSequence field
nSequence = 0x00000001 # 1 block relative lock
# Bits 22-31: 0 = blocks, 1 = time
# Bits 0-15: lock value
Timelock Comparison
| Feature | CLTV | CSV |
|---|---|---|
| Lock Type | Absolute | Relative |
| Field | nLockTime | nSequence |
| Granularity | Block height or timestamp | Blocks or time units |
| Use Case | HTLCs, time-locked vaults | Lightning, RBF |
| BIP | BIP-65 | BIP-112 |
HTLC (Hash Time-Locked Contract)
# HTLC Script
htlc_script = [
"OP_IF",
"OP_SHA256",
"hash_of_secret",
"OP_EQUALVERIFY",
<recipient_pubkey>,
"OP_CHECKSIG",
"OP_ELSE",
<timelock_height>,
"OP_CHECKLOCKTIMEVERIFY",
"OP_DROP",
<sender_pubkey>,
"OP_CHECKSIG",
"OP_ENDIF"
]
Multisig Scripts
Bare Multisig (Deprecated)
# 2-of-3 Multisig (bare, deprecated)
bare_multisig = [
"OP_2",
"pubkey_A",
"pubkey_B",
"pubkey_C",
"OP_3",
"OP_CHECKMULTISIG"
]
# Problem: Non-standard, most nodes won't relay
P2SH Multisig (Standard)
# 2-of-3 Multisig (P2SH)
# Redeem Script
redeem_script = [
"OP_2",
"pubkey_A",
"pubkey_B",
"pubkey_C",
"OP_3",
"OP_CHECKMULTISIG"
]
# Output: OP_HASH160 <hash(redeem_script)> OP_EQUAL
# Input: OP_0 <sig_A> <sig_C> <redeem_script>
P2WSH Multisig (Best Practice)
# 2-of-3 Multisig (P2WSH)
# Witness Script
witness_script = [
"OP_2",
"pubkey_A",
"pubkey_B",
"pubkey_C",
"OP_3",
"OP_CHECKMULTISIG"
]
# Output: OP_0 <sha256(witness_script)>
# Witness: OP_0 <sig_A> <sig_C> <witness_script>
Multisig Security
| Configuration | Security | Flexibility | Complexity |
|---|---|---|---|
| 1-of-2 | Low | High | Low |
| 2-of-3 | Balanced | Medium | Medium |
| 3-of-5 | High | Low | High |
| 5-of-7 | Very High | Very Low | Very High |
Covenant Proposals
Covenants allow scripts to constrain how future transactions spend coins.
OP_CHECKTEMPLATEVERIFY (CTV) - BIP-119
# CTV allows committing to future spending conditions
ctv_script = [
"OP_CHECKTEMPLATEVERIFY",
"OP_DROP"
]
# Template hash includes:
# - Version
# - Locktime
# - Input count
# - Output count
# - Outputs hash
# - Input index
OP_VAULT (Vault Construction)
# Vault commit stage
vault_commit = [
"OP_SHA256",
"hash_of_vault_script",
"OP_EQUAL"
]
# Vault recovery script
vault_recovery = [
"OP_IF",
# Normal path: requires timelock
"OP_CHECKLOCKTIMEVERIFY",
"OP_DROP",
<recovery_pubkey>,
"OP_CHECKSIG",
"OP_ELSE",
# Theft path: requires timelock + revault
<timelock>,
"OP_CHECKLOCKTIMEVERIFY",
"OP_DROP",
<cold_storage_pubkey>,
"OP_CHECKSIG",
"OP_ENDIF"
]
Covenant Comparison
| Proposal | Functionality | Complexity | Status |
|---|---|---|---|
| CTV (BIP-119) | Template commitment | Low | Draft |
| APO (BIP-118) | Input-indexed signing | Medium | Draft |
| VAULT | Pre-confirmed outputs | High | Research |
| ELEMENTS | Pegged sidechains | High | Production |
Real-World Script Usage
Lightning Network Scripts
# HTLC Script (Lightning)
htlc = [
"OP_DUP",
"OP_HASH160",
<revocation_pubkey_hash>,
"OP_EQUAL",
"OP_IF",
"OP_CHECKSIG",
"OP_ELSE",
<remote_htlc_pubkey>,
"OP_SWAP",
"OP_SIZE",
32,
"OP_EQUAL",
"OP_NOTIF",
# Payment hash missing
"OP_DROP",
2,
"OP_SWAP",
<remote_htlc_pubkey>,
2,
"OP_CHECKMULTISIG",
"OP_ELSE",
"OP_HASH160",
<payment_hash>,
"OP_EQUALVERIFY",
"OP_CHECKSIG",
"OP_ENDIF",
"OP_ENDIF"
]
Pay-to-Anchor (P2A) - BIP-118
# P2A: Pay to Anchor
# Simple output that can only be spent via witness
p2a_output = [
"OP_1",
<32_byte_tweak>
]
Trade-offs
Script Complexity vs Security
| Complexity | Security | Use Case | Cost |
|---|---|---|---|
| Simple P2PKH | High | Basic payments | Low |
| P2SH Multisig | Medium-High | Business wallets | Medium |
| P2WSH Complex | Medium | DeFi protocols | High |
| Custom Script | Variable | Specialized | Variable |
Timelock Trade-offs
| Lock Type | Flexibility | Security | Privacy |
|---|---|---|---|
| CLTV (Absolute) | Low | High | Medium |
| CSV (Relative) | Medium | High | Medium |
| Both Combined | High | Medium | Low |
Interview Questions
-
How does P2SH enable flexible scripts without burdening senders?
- Answer: Senders only need to know the hash of the script, not the script itself. The full script is revealed only when spending, keeping transactions compact while enabling complex spending conditions.
-
What prevents OP_CHECKMULTISIG from accepting fewer signatures than required?
- Answer: A bug in Bitcoin's original implementation requires an extra OP_0 on the stack. OP_CHECKMULTISIG pops the m value, then m signatures, then n public keys, then n, then OP_0.
-
How does CSV (relative timelock) differ from CLTV in Lightning Network channel closures?
- Answer: CSV is used for HTLC outputs to allow time-based resolution without knowing the absolute block height when the channel was opened, enabling relative time-based security.
Quiz Questions
-
What opcode creates a signature verification in P2PKH?
- A) OP_HASH160
- B) OP_CHECKSIG â
- C) OP_EQUALVERIFY
- D) OP_DUP
-
In P2SH multisig, what must be included in the scriptSig?
- A) The full script only
- B) Signatures and the redeem script â
- C) Just the script hash
- D) Public keys only
-
Which BIP introduced OP_CHECKLOCKTIMEVERIFY?
- A) BIP-112
- B) BIP-141
- C) BIP-65 â
- D) BIP-119
Practice
- Write a P2SH 2-of-3 multisig locking and unlocking scripts
- Implement a simple HTLC script with both CLTV and hash locks
- Create a timelocked vault script using CLTV
- Analyze a real Bitcoin transaction script on blockstream.info