Locking & unlocking

Locking and Unlocking scripts are used to define the conditions under which a transaction can be spent.

Locking Script

A locking script, also known as a ScriptPubKey, is placed on every output you create in a transaction. It specifies the conditions that must be met for the output to be spent.

SVG Image

Example: This script requires that the unlocking script provides a value that, when added to 2, equals 4.

text
1 
2  OP_2 OP_ADD OP_4 OP_EQUAL 
3

Unlocking Script

An unlocking script, also known as a ScriptSig or Witness, is provided for every input you want to spend in a transaction. It contains the data needed to satisfy the conditions set by the locking script.

SVG Image

This script provides the value 2, which satisfies the condition set by the locking script we saw before

text
1 
2  OP_2 
3

Execution

When a transaction is processed, each node in the network checks both the locking and unlocking scripts for each input.

The unlocking script is executed first, followed by the locking script.

If the unlocking script successfully satisfies the conditions set by the locking script, the transaction is considered valid and is added to the blockchain. Otherwise, it is rejected.

SVG Image

Use the Bitcoin Stack simulator below to execute the combined script (unlocking script + locking script) provided below

text
1 
2  locking_script = 2 OP_ADD 4 OP_EQUAL
3  unlocking script = 2
4

If the final stack contains true (1), the transaction is valid.

Bitcoin Stack Simulator
Available Operations:
Stack (Last-In-First-Out):

Stack is empty