TeachMeBitcoin

What is a Locking Script (scriptPubKey)

From TeachMeBitcoin, the free encyclopedia Reading time: 3 min

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:

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:

  1. Value: The amount of satoshis being locked

  2. 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:

  1. Correct: It accurately captures the intended spending conditions

  2. Minimal: It uses only the opcodes necessary

  3. Standard: It matches one of the recognized script patterns so miners will relay it

  4. 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
☕ Help support TeachMeBitcoin

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:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!