— —
Follow on X
LESSONS

Bitcoin Script: locks and keys

Every output carries a locking script; every input provides an unlocking script. Nodes run them on a stack machine. Script has no loops, which keeps it predictable.

7 MIN READ
A P2PKH script executes: push signature and public key, duplicate, hash, compare, check signature.
LEVEL · Advanced
IN THIS LESSON · 4 OF 9
BY THE END YOU WILL BE ABLE TO ANSWER
  • Why does Script have no loops?
  • In P2PKH, what does OP_EQUALVERIFY check?
  • What is the difference between standardness and consensus?
  • A standard P2PKH locking script is OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG. How many opcodes is that, excluding the pushed hash?

Then prove it: a 3-question quiz · a challenge. The lesson marks itself complete at 100%.

After this lesson you can: read a simple locking script.

Bitcoin does not know about accounts or owners. It knows about scripts. Each output has a scriptPubKey (the lock) and each spending input provides a scriptSig or witness (the key). A node concatenates them, runs the result, and accepts the spend if it ends with TRUE.

The machine

Script is a stack-based language with about a hundred opcodes: push data, duplicate, hash, compare, check a signature, check a timelock. There are no loops and no way to write one, so every script finishes in bounded time and cannot hang a node. That limitation is deliberate; it is why Bitcoin never has "smart contract" exploits that drain the network.

Pay-to-public-key-hash, step by step

Locking script: OP_DUP OP_HASH160 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG

Unlocking script: <signature> <public key>

Execution: push the signature and key; duplicate the key; hash it; push the expected hash; verify they are equal (fail if not); check the signature against the key and the transaction. TRUE means spend.

Five steps of executing a pay-to-public-key-hash script on the stack.
The stack machine, running P2PKH.

Other standard scripts

  • Multisig: OP_2 <keyA> <keyB> <keyC> OP_3 OP_CHECKMULTISIG, usually behind a P2SH or P2WSH hash.
  • Timelocks: OP_CHECKLOCKTIMEVERIFY (absolute) and OP_CHECKSEQUENCEVERIFY (relative), which Lightning and inheritance schemes use.
  • Hash locks: reveal a preimage whose hash matches, the basis of HTLCs.
  • OP_RETURN: marks an output unspendable and carries up to 80 bytes of data.

Standardness versus consensus

Table of standard script types and what each requires.
The standard locks.

Nodes relay only "standard" script forms to limit abuse; non-standard but valid scripts can still be mined. Miniscript is a modern way to compose scripts that tools can analyse and wallets can sign safely.

MINI CHALLENGEOpcode count

A standard P2PKH locking script is OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG. How many opcodes is that, excluding the pushed hash?

opcodes
CHECK YOURSELF
1/3 Why does Script have no loops?
2/3 In P2PKH, what does OP_EQUALVERIFY check?
3/3 What is the difference between standardness and consensus?
pick an answer for each question

SOURCES

  1. Learn Me A Bitcoin: Script
Progress is saved in this browser only.