TeachMeBitcoin

The Locking Script (scriptPubKey): The Anchor Guide to Programmable Money

From TeachMeBitcoin, the free encyclopedia Reading time: 5 min

The Locking Script (scriptPubKey): The Anchor Guide to Programmable Money

IMPORTANT

Executive Summary: Every Bitcoin output is secured by a computer program called a scriptPubKey. This "Locking Script" defines the specific conditions that must be met to spend the unspent output (UTXO) in the future. By using Bitcoin's native, stack-based language (Script), these locks can range from simple single-key signatures to complex multi-signature escrows and time-delayed smart contracts.


🔍 Why This Module Matters

In the traditional banking system, "ownership" is determined by a legal name on a database. In Bitcoin, "ownership" is determined by a Mathematical Challenge. Every piece of bitcoin you own is actually just a UTXO locked with a scriptPubKey challenge that only you can solve. Understanding the structure of these locks is essential for understanding how addresses work, how multisig security is implemented, and how Layer 2 protocols like the Lightning Network function.


🏛️ The Architecture of Bitcoin Script

Bitcoin Script is a specialized, assembly-like language designed for one purpose: Non-Turing Complete Financial Contracts.

1. The Stack-Based Execution

Script works like a stack of plates.

2. The Components of a Lock

A scriptPubKey consists of:


⚙️ Standard Locking Patterns: Decoding the Addresses

Depending on which "Address Type" you use, the scriptPubKey lock changes its internal logic.

Address Type Technical Name Typical Lock Pattern
Legacy (1...) P2PKH OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Nested (3...) P2SH OP_HASH160 <ScriptHash> OP_EQUAL
SegWit (bc1q..) P2WPKH 0 <PubKeyHash> (Version 0 Witness)
Taproot (bc1p..) P2TR 1 <32-Byte-PubKey> (Version 1 Witness)

Deep Dive: The P2PKH "Legacy" Lock

This is the most famous script in Bitcoin history:

  1. OP_DUP: Copy the public key provided by the spender.

  2. OP_HASH160: Hash it.

  3. <PubKeyHash>: Check if the hash matches the one in the lock.

  4. OP_EQUALVERIFY: If no match, stop immediately (invalid).

  5. OP_CHECKSIG: Check if the digital signature provided matches the public key.

graph TD
 A[Stack Start] --> B[Push PubKey]
 B --> C[Push Signature]
 C --> D[OP_DUP]
 D --> E[OP_HASH160]
 E --> F{Match Hash in Lock?}
 F -- Yes --> G[OP_CHECKSIG]
 G --> H[Final: TRUE/FALSE]

🛠️ Programmable Sovereignty: Beyond Simple Keys

Because scriptPubKey is a language, we can create advanced conditions for spending:

[!TIP] Smart Contracts: People often think Bitcoin doesn't have smart contracts. In reality, every single transaction is a small, secure smart contract executed by the scriptPubKey.


🛡️ The Validation Process: Combining ScriptSig and ScriptPubKey

When a node validates a transaction, it performs a Concatenation:

  1. It takes the scriptSig (Unlocking Script) from the current transaction input.

  2. It appends the scriptPubKey (Locking Script) from the previous output.

  3. It runs the combined script.

  4. The Result: If the stack ends with a value of TRUE (non-zero), the transaction is valid and the coins are "unlocked."


🎯 Learning Objectives for this Module

By the end of this module, you will be able to:

  1. Define scriptPubKey and its role in securing Bitcoin outputs.

  2. Describe the stack-based execution model of Bitcoin Script.

  3. Recognize the standard locking patterns for Legacy and SegWit addresses.

  4. Explain why Bitcoin Script is not Turing Complete and why this is a safety feature.

  5. Understand the difference between a Locking Script (output) and an Unlocking Script (input).


🗺️ Module Roadmap: What's Next?

We will now explore the specific functional components of Bitcoin's programmable layer:

  1. Unlocking Script (scriptSig): How signatures provide the keys to the locks.

  2. Bitcoin Script Language Reference: A deep dive into Opcodes and Math.

  3. Multisig Locking Mechanics: How groups secure shared funds.

  4. Pay-to-Script-Hash (P2SH): The foundation for complex escrow and smart contracts.


🎓 Summary

The scriptPubKey is the definitive "No" in the Bitcoin network. It is the wall that prevents anyone from moving your funds unless they can solve the mathematical puzzle you have set. By understanding how to write and read these locks, you gain the ability to build sophisticated, trustless financial tools on top of the world's most secure ledger.

☕ 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!