What is Bitcoin Script?
Bitcoin Script is a stack-based, Forth-like programming language used to define spending conditions for Bitcoin outputs. Every Bitcoin transaction contains scripts that specify who can spend the coins and under what conditions.
Scripts are intentionally not Turing-complete to prevent infinite loops and ensure predictable execution. They consist of opcodes (operation codes) that manipulate a stack of data elements, perform cryptographic operations, and enforce conditions.
Common Script Patterns
P2PKH (Pay to Public Key Hash)
The original and most common script type. It locks funds to the hash of a public key. To spend, you must provide a valid signature and the public key that hashes to the specified value.
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIGP2SH (Pay to Script Hash)
Allows complex spending conditions to be hidden behind a hash. The actual script (called the redeem script) is revealed only when spending. Used for multisig and wrapped SegWit.
OP_HASH160 <scriptHash> OP_EQUALP2WPKH (Pay to Witness Public Key Hash)
Native SegWit version of P2PKH. The signature and public key are moved to the witness section, reducing transaction size and fees. Addresses start with "bc1q".
OP_0 <20-byte pubKeyHash>P2TR (Pay to Taproot)
The newest script type from the Taproot upgrade. Uses Schnorr signatures and allows complex scripts to appear as simple key spends when conditions are met. Addresses start with "bc1p".
OP_1 <32-byte x-only pubkey>Multisig (M-of-N)
Requires M signatures from N possible public keys. Used for shared custody, corporate wallets, and enhanced security. The native format uses OP_CHECKMULTISIG.
OP_M <pubKey1> ... <pubKeyN> OP_N OP_CHECKMULTISIGOpcode Reference Guide
Bitcoin Script includes several categories of opcodes:
- Push Operations - Push data or constants onto the stack (OP_0, OP_1-16, OP_PUSHDATA)
- Flow Control - Conditional execution (OP_IF, OP_ELSE, OP_ENDIF, OP_VERIFY)
- Stack Operations - Manipulate stack items (OP_DUP, OP_DROP, OP_SWAP, OP_ROT)
- Arithmetic - Basic math operations (OP_ADD, OP_SUB, comparisons)
- Cryptographic - Hashing and signature verification (OP_HASH160, OP_CHECKSIG)
- Locktime - Time-based conditions (OP_CHECKLOCKTIMEVERIFY, OP_CHECKSEQUENCEVERIFY)
Frequently Asked Questions
What is Bitcoin Script used for?
Bitcoin Script defines the conditions that must be met to spend bitcoins. Every output has a locking script (scriptPubKey) that specifies requirements like signatures, timelocks, or hash preimages. The spender provides an unlocking script (scriptSig) that satisfies these conditions.
How do I read a Bitcoin script?
Bitcoin scripts are written in hexadecimal and executed left-to-right on a stack. Use this debugger to parse the hex into readable opcodes and trace the stack execution. Each opcode either pushes data, manipulates the stack, or performs cryptographic operations.
Why is Bitcoin Script stack-based?
Stack-based languages are simple to implement and verify. Operations pop inputs from the stack and push results back. This design makes script execution deterministic and prevents complex control flow that could enable denial-of-service attacks.
What is an opcode?
An opcode (operation code) is a single instruction in Bitcoin Script. Each opcode is represented by a single byte (0x00-0xff). Examples include OP_DUP (duplicate top stack item), OP_HASH160 (hash with SHA-256 then RIPEMD-160), and OP_CHECKSIG (verify a signature).
What is the difference between scriptPubKey and scriptSig?
scriptPubKey (locking script) is in the output and defines spending conditions. scriptSig (unlocking script) is in the input and provides data to satisfy those conditions. For SegWit outputs, the unlocking data is in the witness instead of scriptSig.
What are disabled opcodes?
Several opcodes (like OP_CAT, OP_MUL, OP_DIV) were disabled early in Bitcoin's history due to potential vulnerabilities. Any transaction using them is invalid. Some may be re-enabled through soft forks with proper safety measures.
What is OP_RETURN?
OP_RETURN immediately marks a script as invalid, making the output provably unspendable. It's used to embed arbitrary data (up to 80 bytes) in the blockchain without creating unspendable UTXOs that waste node resources.
How do timelocks work in Bitcoin Script?
OP_CHECKLOCKTIMEVERIFY (CLTV) enforces an absolute time/block height before spending. OP_CHECKSEQUENCEVERIFY (CSV) enforces a relative time since the output was created. These enable HTLCs, payment channels, and other time-dependent contracts.
Build on Bitcoin
Spark enables instant Bitcoin transactions with programmable scripts. Create HTLCs, payment channels, and more with our developer tools.