A padlock written as a tiny program
Every output in the last lesson had a locking script. To spend it, you hand over data that makes the script finish with "true". Nodes all over the network run the same check.
The language is called Script. It has no loops, so every script ends quickly.
By the end of this lesson
Note
I can follow a script on a stack and say whether a spend passes.
Key words
| Word | What it means | Example |
|---|---|---|
| Stack | a pile where the last item in is the first out | |
| Opcode | one instruction in a script | OP_DUP |
| Locking script | the rules written on an output | |
| Witness | the data a spender gives to meet the rules | |
| Timelock | a rule that a coin waits until a height | |
| Locktime | the field saying the earliest block for a transaction |
The cells students fill in are left blank in this preview.
Script runs on a stack
A script is a list of items read from left to right. A number or some data is pushed on top of the stack. An opcode takes items off the top, works on them and may push a result.
OP_ADD takes two numbers and pushes their sum. OP_EQUAL pushes 1 if the two top items match and 0 if not. The spend passes if the stack ends with a true item on top.
Watch three scripts run
docker exec jupyter python3 steps/stack.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: Alice spends her coin
| Item | What it does | Stack after |
|---|---|---|
| sig:alice, alice | The spender pushes a signature and a key | sig:alice alice |
| OP_DUP | Copies the key | sig:alice alice alice |
| OP_HASH160 | Hashes the copy | sig:alice alice 49099657.. |
| 49099657.. | The lock pushes the hash it expects | two equal hashes on top |
| OP_EQUALVERIFY | Stops unless they match | sig:alice alice |
| OP_CHECKSIG | Checks the signature against the key | 1, so the spend passes |
The cells students fill in are left blank in this preview.
Try it: run a script by hand
# A Python list makes a good stack: append pushes, pop takes the top.
stack = []
stack.append(7) # push 7
stack.append(stack[-1]) # OP_DUP copies the top
right = stack.pop()
left = stack.pop()
stack.append(left + right) # OP_ADD
print("after OP_ADD:", stack)
stack.append(14) # push 14
right = stack.pop()
left = stack.pop()
stack.append(1 if left == right else 0) # OP_EQUAL
print("top of the stack:", stack[-1])
# Change 14 to 15 and run again. Would this script let a coin be spent?
Segwit moves the key into the witness
The old form, pay to public key hash, puts that whole script on the output. Its segwit form, P2WPKH, writes only a version number and the 20-byte key hash.
The node knows what version 0 with 20 bytes means and runs the same check. The signature and key travel in the witness, which weighs less. That is why output 0 showed witness_v0_keyhash.
Two kinds of timelock
OP_CHECKLOCKTIMEVERIFY, or CLTV, is absolute: the coin waits until the chain reaches a height. The spending transaction must set its locktime to at least that height. A transaction with locktime 110 is valid only in block 111 or later.
OP_CHECKSEQUENCEVERIFY, or CSV, is relative: the coin waits a number of blocks after it was confirmed.
Try to spend the timelocked output too early
docker exec bitcoind sh steps/cltv.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: two refusals at height 102
| Spend | The node said | Why |
|---|---|---|
| Locktime 110 | non-final | Block 103 is too early for locktime 110 |
| Locktime 100 | Locktime requirement not satisfied | The script asks for at least 110 |
The cells students fill in are left blank in this preview.
Mine eight empty blocks
docker exec bitcoind sh steps/mine.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try the same spend at height 110
docker exec bitcoind sh steps/cltv.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a timelocked coin moves by itself when the height arrives. Nothing moves on its own. The height only makes a spend possible, and somebody still has to sign and send it.
See both rules as a table
docker exec jupyter python3 steps/timelock.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.