Bitcoin Script Execution
Visualize how 4 different script types execute on the stack. But first, let's understand some key concepts:
How Scripts are Combined
In Bitcoin transactions, the unlocking script (from the input) and the locking script (from the previous output) are combined and executed sequentially. The unlocking script is executed first, followed by the locking script. If the combined script executes successfully and leaves 'true' on the stack, the spend is valid.
Understanding Bytecodes
The bytecodes (hexadecimal representations of opcodes) are what actually appear in the Bitcoin script at the software level. They represent the script instructions, but only the data gets pushed to the stack, not the opcodes themselves.
For example, when you see 0x76 in the bytecode, it's the hexadecimal representation of the OP_DUP opcode. The Bitcoin software reads this value to know it should duplicate the top item on the stack.
Now, select a script type from the dropdown below to see how it executes on the stack:
1. Initial Stack (Unlocking Script)
Visual Stack
Bytecode
Operation
Signature (71 bytes) and Compressed Public Key (33 bytes) provided by spender
2. After OP_DUP
Visual Stack
Bytecode
Operation
3. After OP_HASH160
Visual Stack
Bytecode
Operation
4. After pushing <PubKeyHash>
Visual Stack
Bytecode
Operation
PubKeyHash (20 bytes) from locking script
5. After OP_EQUALVERIFY (if successful)
Visual Stack
Bytecode
Operation
6. After OP_CHECKSIG (if valid)
Visual Stack
Bytecode
Operation
Verifies signature against transaction data
Notes:
- The signature is provided in the unlocking script.
- PUSHDATA opcodes (0x47 for 71-byte signature, 0x41 for 65-byte public key) are used to push data onto the stack.
- Bytecodes represent the actual opcodes executed by the Bitcoin script interpreter.
- The signature signs transaction data, including inputs and outputs, determined by the SIGHASH flag (typically SIGHASH_ALL).
- If OP_CHECKSIG fails, the entire script execution fails, and the transaction is considered invalid.