Bitcoin Script and Opcodes: A Developer Introduction
Bitcoin Script is a stack-based language used to express and evaluate spending conditions for transaction outputs. It is intentionally constrained: it is not a general-purpose application runtime, and its rules differ across script versions and output types.
Locking and unlocking conditions
A transaction output includes a scriptPubKey. Spending that output requires data and, where applicable, script execution satisfying the relevant rules. In older transaction types this involves scriptSig; SegWit spends also use witness data. Taproot key-path spending validates a Schnorr signature without exposing a script, while script-path spending reveals a committed script and control information.
Common opcodes
OP_DUPduplicates the top stack item.
OP_HASH160applies SHA-256 followed by RIPEMD-160.
OP_EQUALVERIFYcompares two items and fails if they differ.
OP_CHECKSIGchecks a signature under the applicable script rules.
OP_CHECKLOCKTIMEVERIFYhelps enforce an absolute locktime condition.
OP_CHECKSEQUENCEVERIFYhelps enforce a relative locktime condition.
Opcode behavior depends on the script context and version. A developer should not assume an opcode has identical semantics in legacy Script and Tapscript.
Example: the P2PKH pattern
A traditional pay-to-public-key-hash locking script has the form:
OP_DUP OP_HASH160 <20-byte-public-key-hash> OP_EQUALVERIFY OP_CHECKSIG
This is a teaching example, not a recommended template for every modern wallet. SegWit and Taproot have different output structures and validation paths.
Why Taproot changes the picture
BIP342 defines Tapscript, which modifies signature-checking semantics and resource rules for Taproot script-path spends. BIP340 defines Schnorr signatures, while BIP341 defines Taproot's overall spending structure. A wallet that can send to a P2TR address does not necessarily implement arbitrary Taproot script-path construction or spending.
Developer safety checklist
Use well-tested libraries and network-specific test vectors. Distinguish consensus validity from relay policy. Do not construct production scripts from an unreviewed text example, and test edge cases such as timelocks, signature encodings, and script-version differences.
Primary sources
- BIP340: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
- BIP341: https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
- BIP342: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki
