— —
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 8
BY THE END YOU WILL BE ABLE TO ANSWER
  • Why does Script have no loops?
  • In P2PKH, what does OP_EQUALVERIFY check?

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

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.

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

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.

CHECK YOURSELF
1/2 Why does Script have no loops?
2/2 In P2PKH, what does OP_EQUALVERIFY check?
pick an answer for each question

SOURCES

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