Overview
A locking script (formally called scriptPubKey) is the part of a Bitcoin transaction output that sets the conditions under which the bitcoin can be spent. Every output in a Bitcoin transaction contains an amount and a locking script. To spend that output in a future transaction, the spender must provide an unlocking script (scriptSig) or witness data that satisfies these conditions.
Common Locking Script Types
P2PKH (Pay-to-Public-Key-Hash):
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
P2SH (Pay-to-Script-Hash):
OP_HASH160 <scriptHash> OP_EQUAL
P2WPKH (Pay-to-Witness-Public-Key-Hash):
OP_0 <20-byte pubKeyHash>
P2TR (Pay-to-Taproot):
OP_1 <32-byte tweaked pubkey>
How Spending Works
When someone wants to spend an output, they construct a new transaction with an input that references the output. The input includes an unlocking script that provides the data (signatures, public keys, preimages) needed to make the locking script evaluate to true. Bitcoin nodes execute both scripts together to verify the spend.
Unlocking Script (input) + Locking Script (output)
<sig> <pubKey> + OP_DUP OP_HASH160 ...
│ │
└──── Evaluated ───────┘
together
Must return TRUE
Edge Cases
An OP_RETURN locking script creates a provably unspendable output — no unlocking script can satisfy it. This is used intentionally for embedding data. Conversely, scripts like OP_TRUE would make outputs spendable by anyone, which is why careful script construction is critical for security.