Bitcoin Script Validation: Signatures, Locktimes, and Spending Conditions
A Bitcoin output defines spending conditions. When an input spends that output, validating nodes execute the applicable spending rules using the transaction, its witness or script data, and the output being spent. The outcome is not merely a wallet decision: a failed consensus Script check invalidates the spending transaction.
The UTXO determines the rules
The previous output's locking script (scriptPubKey) determines how the input is interpreted. Legacy outputs may use scriptSig and scriptPubKey. Native SegWit outputs use witness data under version-specific rules. Taproot outputs use a v1 witness program with either a key-path signature or a script-path reveal.
Signature verification commits to transaction data
OP_CHECKSIG-style checks verify a signature against a message derived from the transaction and the chosen sighash mode. The exact digest algorithm depends on the spending context. SegWit v0 uses BIP143 hashing for covered witness programs; Taproot uses BIP341 and BIP342 rules and Schnorr signatures as specified by BIP340. It is incorrect to assume that every Bitcoin signature signs the same byte sequence.
Absolute and relative timelocks
OP_CHECKLOCKTIMEVERIFY (BIP65) enforces an absolute height or timestamp constraint together with transaction locktime and appropriate input sequence conditions. OP_CHECKSEQUENCEVERIFY (BIP112) enforces a relative age constraint using BIP68 sequence semantics. These are different from a wallet merely delaying broadcast.
SegWit and Taproot execution
SegWit separates witness material from the legacy transaction identifier and adds witness-program-specific validation. Taproot allows a key-path spend without revealing a script, or a script-path spend proving a committed leaf. Tapscript changes some signature and opcode semantics; its behavior must not be conflated with legacy Script.
Common failure cases
- A signature is valid for another transaction but not the transaction being checked.
- An input attempts to spend an output whose locking condition is not satisfied.
- A timelock condition has not matured.
- A witness program or control block fails structural or commitment checks.
- The transaction spends an already-spent output, regardless of script success.
Developer testing approach
Use isolated Regtest transactions to exercise success and failure paths. Record the previous output amount and script when computing SegWit signature digests. Test at boundary heights for locktimes and after replacement or fee-bumping changes that can affect signed transaction fields.
Primary references
Bitcoin Developer Reference — Transactions: https://developer.bitcoin.org/reference/transactions.html
BIP65 — CHECKLOCKTIMEVERIFY: https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
BIP112 — CHECKSEQUENCEVERIFY: https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki
BIP143 — Transaction Signature Verification for Version 0 Witness Program: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki
BIP341 — Taproot: https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
BIP342 — Tapscript: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki
