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

02a9825d...6e5c (33 bytes)
30450221...01 (71 bytes)

Bytecode

0x21
0x47

Operation

PUSHDATA opcodes

Signature (71 bytes) and Compressed Public Key (33 bytes) provided by spender

2. After OP_DUP

Visual Stack

02a9825d...6e5c
02a9825d...6e5c
30450221...01

Bytecode

0x76

Operation

OP_DUP

3. After OP_HASH160

Visual Stack

728a2d...37b9
02a9825d...6e5c
30450221...01

Bytecode

0xa9

Operation

OP_HASH160
Consumed: Top Public Key

4. After pushing <PubKeyHash>

Visual Stack

728a2d...37b9
728a2d...37b9
02a9825d...6e5c
30450221...01

Bytecode

0x14

Operation

PUSHDATA 20 bytes

PubKeyHash (20 bytes) from locking script

5. After OP_EQUALVERIFY (if successful)

Visual Stack

02a9825d...6e5c
30450221...01

Bytecode

0x88

Operation

OP_EQUALVERIFY
Consumed: Both PubKeyHash values

6. After OP_CHECKSIG (if valid)

Visual Stack

01

Bytecode

0xac

Operation

OP_CHECKSIG

Verifies signature against transaction data

Consumed: Compressed Public Key, Signature
Signature Used (verifies against tx data)(SIGHASH flags determine what parts of the transaction are signed)

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.