Bitcoin Script Language Basics
Lesson by Uvin Vindula
Bitcoin is often described as "digital gold" — a simple store of value. But beneath the surface, every Bitcoin transaction is powered by a small but powerful programming language called Bitcoin Script. This language determines the conditions under which Bitcoin can be spent, and understanding it unlocks a deeper appreciation of what Bitcoin can actually do.
What Is Bitcoin Script?
Bitcoin Script is a stack-based, Forth-like programming language that is intentionally simple and limited. Every Bitcoin transaction contains two scripts:
- ScriptPubKey (Locking Script): Attached to the transaction output — it defines the conditions that must be met to spend the Bitcoin. Think of it as a lock on a safe.
- ScriptSig (Unlocking Script): Provided by the spender in a new transaction — it provides the data (like a digital signature) that satisfies the locking conditions. Think of it as the key.
When a node validates a transaction, it combines the unlocking script with the locking script and executes them together. If the combined script evaluates to TRUE, the transaction is valid and the Bitcoin can be spent.
Stack-Based Execution
Unlike languages like JavaScript or Python, Bitcoin Script does not use variables or loops. Instead, it uses a stack — a last-in, first-out (LIFO) data structure. Operations (called opcodes) push data onto the stack, manipulate it, or pop data off. For example:
- OP_DUP: Duplicates the top item on the stack.
- OP_HASH160: Pops the top item, hashes it with SHA-256 and then RIPEMD-160, and pushes the result.
- OP_EQUALVERIFY: Checks that the top two stack items are equal; fails the script if they are not.
- OP_CHECKSIG: Verifies a digital signature against a public key.
The Standard P2PKH Transaction
The most common Bitcoin script pattern is Pay-to-Public-Key-Hash (P2PKH). Here is how it works step by step:
- The locking script says: "Provide a public key that hashes to this address, and a valid signature for that key."
- The unlocking script provides: the signature and the public key.
- The node executes: duplicate the public key, hash it, compare it to the expected hash, then verify the signature.
- If everything checks out, the script returns TRUE and the spend is valid.
Why Script Is Intentionally Limited
Satoshi Nakamoto deliberately made Bitcoin Script non-Turing-complete. It has no loops, no recursion, and limited opcodes. This was a critical design decision:
- Security: A limited language has a smaller attack surface. No infinite loops means no denial-of-service attacks via script execution.
- Predictability: Every script execution is guaranteed to terminate in a bounded number of steps.
- Auditability: Simple scripts are easier to verify and reason about — critical when billions of dollars are at stake.
For Sri Lankan developers interested in Bitcoin development, understanding Script is the foundation. It reveals that Bitcoin is not just money — it is a programmable settlement layer with carefully designed constraints.
Key Takeaways
- •Bitcoin Script is a stack-based programming language that powers every transaction
- •Every transaction has a locking script (conditions) and unlocking script (proof)
- •Common opcodes include OP_DUP, OP_HASH160, OP_EQUALVERIFY, and OP_CHECKSIG
- •Script is intentionally non-Turing-complete for security and predictability
- •P2PKH (Pay-to-Public-Key-Hash) is the most common script pattern
Quick Quiz
Question 1 of 3
0 correct so far
What type of programming language is Bitcoin Script?