What is a Locking Script (scriptPubKey)
What is a Locking Script (scriptPubKey)?
The locking script is the heart of Bitcoin's security model. Every bitcoin that exists on the blockchain is protected by a locking script. To spend those coins, you must satisfy its conditions.
Terminology
The locking script goes by several names in Bitcoin documentation and code:
-
scriptPubKey — the original technical name (since the most common type locks coins to a public key hash)
-
Locking script — descriptive term used in most educational materials
-
Output script — because it lives in transaction outputs
-
pk_script — used in some protocol-level documentation
All of these terms refer to the same thing: the script embedded in a transaction output that defines the spending conditions.
Where Does the Locking Script Live?
The locking script is part of a transaction output (TxOut). Every time you send bitcoin, you create an output. That output contains:
-
Value: The amount of satoshis being locked
-
scriptPubKey: The locking script defining who can spend them
TxOut structure:
{
value: 8 bytes (amount in satoshis, little-endian)
script_len: varint (length of the following script)
scriptPubKey: N bytes (the locking script bytecode)
}
When this output is later referenced as an input in a spending transaction, the node retrieves this scriptPubKey from its UTXO set and evaluates it against the spender's unlocking script.
The Standard Locking Script Types
Bitcoin Core recognizes five standard locking script patterns:
P2PK (Pay-to-Public-Key):
<pubKey> OP_CHECKSIG
Locks coins directly to a public key. Very old format, used in early Bitcoin and coinbase transactions.
P2PKH (Pay-to-Public-Key-Hash):
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
The most common Bitcoin address format (1xxx addresses). Locks to the hash of a public key.
P2SH (Pay-to-Script-Hash):
OP_HASH160 <scriptHash> OP_EQUAL
Locks to the hash of a redeem script. The actual spending conditions are hidden until spend time.
P2WPKH (Pay-to-Witness-Public-Key-Hash):
OP_0 <20-byte-pubKeyHash>
Native SegWit v0 address (bc1q...). Moves signature data to the witness field.
P2TR (Pay-to-Taproot):
OP_1 <32-byte-x-only-pubKey>
Taproot address (bc1p...). Can be spent via key path or script path.
Locking Script Design Principles
A good locking script is:
-
Correct: It accurately captures the intended spending conditions
-
Minimal: It uses only the opcodes necessary
-
Standard: It matches one of the recognized script patterns so miners will relay it
-
Unambiguous: There is exactly one way to satisfy it (or deliberately multiple ways for flexibility)
Non-Standard Locking Scripts
You can technically write any valid Script as a locking script, but if it doesn't match one of the standard templates, most nodes will not relay the transaction to other nodes (though miners can still include it in a block). These are called non-standard scripts.
// Non-standard: anyone who can find X such that SHA256(X) = <hash>
OP_SHA256 <hash> OP_EQUAL
// Non-standard: anyone can spend (no conditions)
OP_1
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: