Bitcoin script
Bitcoin Script
Bitcoin Script is a stack-based, forth-like scripting language used in the Bitcoin network to define the conditions that must be met to spend Bitcoins. It's not a general-purpose programming language like Python or JavaScript; its design deliberately limits its capabilities to ensure the security and determinism of the blockchain. Understanding Bitcoin Script is crucial for grasping the intricacies of cryptocurrency transactions and smart contracts within the Bitcoin ecosystem.
Overview
Bitcoin Script is used within Bitcoin transactions in the locking script (scriptPubKey) and the unlocking script (scriptSig). The locking script defines the conditions for spending the Bitcoins, and the unlocking script provides the data to satisfy those conditions. When a transaction is submitted, the Bitcoin network executes both scripts. If the unlocking script, when run against the locking script's conditions, results in a "true" outcome, the transaction is considered valid.
It’s important to note that Bitcoin Script is *not* Turing complete. This limitation was intentional to prevent infinite loops and complex computations that could potentially compromise the network’s stability. While it can perform basic operations, it lacks features like loops and conditional branching found in fully Turing-complete languages. This impacts the complexity of decentralized applications that can be built directly on Bitcoin, prompting the development of solutions like Lightning Network and sidechains.
Core Concepts
- Stack-Based*: Bitcoin Script operates using a stack. All operations involve pushing data onto the stack, manipulating the top elements, and popping results. Think of it like a pile of plates – you can only directly access the top plate.
- Forth-Like*: The language's syntax and structure resemble Forth, a lesser-known programming language. This means expressions are written in Reverse Polish Notation (RPN).
- Determinism*: Script execution must be deterministic. Given the same inputs and scripts, the outcome must always be the same. This is vital for consensus on the blockchain.
- ScriptPubKey (Locking Script)*: This part of a transaction output defines the conditions required to spend the Bitcoins. It's essentially a 'lock' on the funds.
- ScriptSig (Unlocking Script)*: This part of a transaction input provides the data needed to fulfill the conditions specified in the ScriptPubKey. It's the 'key' to unlock the funds.
Data Types
Bitcoin Script supports several data types:
| Data Type !! Description | ||
|---|---|---|
| Bytes || Raw byte strings. | Integer || Signed 256-bit integers. | Boolean || True or False. |
Common Opcodes
Opcodes (operation codes) are the instructions that Bitcoin Script understands. Here are some frequently used opcodes:
| Opcode !! Description | ||||||||
|---|---|---|---|---|---|---|---|---|
| `OP_DUP` || Duplicates the top item on the stack. | `OP_HASH160` || Computes the RIPEMD160 hash of the top item on the stack. Crucial for P2PKH addresses. | `OP_EQUALVERIFY` || Compares the top two items on the stack for equality. If they are not equal, the script fails. | `OP_CHECKSIG` || Verifies a digital signature against a public key and a message. Essential for digital signatures. | `OP_ADD` || Adds the top two items on the stack. | `OP_SUB` || Subtracts the top two items on the stack. | `OP_DROP` || Removes the top item from the stack. | `OP_TRUE` || Pushes the boolean TRUE onto the stack. | `OP_FALSE` || Pushes the boolean FALSE onto the stack. |
There are many other opcodes, but these illustrate the basic building blocks of Bitcoin Script. Understanding these is key to analyzing transaction analysis and identifying potential vulnerabilities.
Pay-to-Public-Key-Hash (P2PKH)
The most common type of Bitcoin transaction uses the P2PKH scheme. Here’s a simplified breakdown:
1. Locking Script (ScriptPubKey) : `OP_DUP OP_HASH160
The unlocking script provides the signature and public key. The locking script verifies that the signature is valid for the provided public key and that the hash of the public key matches the hash stored in the locking script. This process ensures that only the owner of the private key corresponding to the public key can spend the Bitcoins. This is related to understanding Elliptic Curve Cryptography.
Pay-to-Script-Hash (P2SH)
P2SH allows for more complex spending conditions. Instead of directly embedding the locking script in the transaction output, the output contains a hash of the script.
1. Locking Script (ScriptPubKey) : `OP_HASH160