Understanding Bitcoin Script: A Stack-Based Scripting Language and Its Execution Model

Bitcoin Script is a stack-based scripting language used to verify transaction spending conditions. This article starts with the concept of a stack, illustrates its execution process with examples, and explores key points such as P2PKH, restricted design, and BSV applications, helping readers understand the core mechanism of this on-chain verification language.

Ethan Lin

Ethan Lin

technical_editor

Published Jun 1, 20263 min read

In a Nutshell

Bitcoin Script is a stack-based scripting language — data and opcodes enter the stack sequentially, are popped off, and produce results. Understanding the "stack" execution model helps you grasp P2PKH, signature verification, hash checks, and more complex BSV scripts.

What is a Stack

A stack is a Last In, First Out (LIFO) data structure. Think of it as a stack of plates: new plates go on top, and you also take plates from the top. The last one placed is the first one taken out — that's LIFO.

During Script execution, data is pushed onto the stack, and opcodes pop data from the stack and push results back.

A Simple Example

Consider the following script:

Code
12 3 OP_ADD 5 OP_EQUAL

The execution process is roughly:

  1. Push 2
  2. Push 3
  3. OP_ADD: Pop 2 and 3, add them to get 5, push the result back
  4. Push 5
  5. OP_EQUAL: Compare the two 5s, result is true

The final result is true, script passes. Real Bitcoin/BSV Script has more details, but the basic intuition is: use the stack to hold intermediate data and process it with opcodes.

Stack-Based Execution in P2PKH

P2PKH scripts can also be understood using the stack. The Unlocking script first provides: signature, public key; then the Locking script executes: duplicate the public key, compute its hash, compare it with the locked pubkey hash, verify the signature. Each step pushes, pops, compares, or verifies data on the stack. This is why the order of data is crucial — if the signature and public key are swapped, the script may fail.

Script is Not JavaScript

The word "Script" can mislead newcomers. Bitcoin Script is not JavaScript, nor Python. It doesn't have the complex runtime environment of typical high-level languages; instead, it is a restricted, verification-oriented stack language. Its purpose is not to write arbitrary programs but to determine whether a given output can be spent by a given input.

Why Use a Restricted Script

Bitcoin Script is designed to be relatively restricted to make verification more predictable. Blockchain nodes have to validate a huge number of transactions; if scripts could loop indefinitely, access external networks, or read/write arbitrary state, verification costs would spiral out of control. Therefore, Script focuses on: hashing, signature verification, data comparison, stack operations, conditionals, and controlled verification logic. BSV emphasizes restoring and using Bitcoin Script capabilities, but it remains an on-chain verification language, not a general-purpose backend language.

Script in BSV Applications

BSV applications can express richer spending conditions through Script. For example:

  • Simple payment: requires a signature.
  • Multisig control: requires multiple signatures.
  • Hashlock: requires revealing the preimage of a given hash.
  • Token state: requires complying with protocol transfer rules.
  • Credential update: requires signature from issuer or holder.

However, complex scripts come with higher development and debugging costs. Beginners should first understand P2PKH and OP_RETURN, then gradually move to custom scripts.

Importance of Stack Order

Stack-based languages are very sensitive to order. For example, an opcode may expect the signature at the top of the stack and the public key below it. If you reverse the order, the script will fail. This is why extreme caution is needed when writing scripts manually. SDKs and templates can reduce error probability, but understanding the stack model aids debugging.

Common Misconceptions for Beginners

  • Bitcoin Script is not JavaScript.
  • The core use of Script is to verify spending conditions, not to write general application backends.
  • Stack order affects script outcomes.
  • P2PKH is also script execution, not a special account logic.
  • Complex scripts need to consider standardness, fees, and miner policies.
  • Application data protocols should not cram all logic into the script; much logic can be placed in Overlay or application-layer validation.

References

Recommended articles