Bitcoin Scripts
Definition
Bitcoin Script is a non-Turing-complete, stack-based scripting language used in Bitcoin transactions to lock and unlock coins according to predefined spending conditions. It works by combining a locking script, usually placed in the output of a previous transaction, and an unlocking script, provided in the input of a new transaction. If the combined scripts evaluate to true, the transaction is considered valid under those conditions. Common uses include standard pay-to-public-key-hash payments, multi-signature wallets, hash locks, and time-locked transactions.
Main Content
1. Script Architecture and Stack-Based Design
- Bitcoin Script operates on a stack, meaning data is pushed onto the stack and commands manipulate the top elements of that stack. This design is simple and efficient for validation because nodes only need to check whether the script evaluates correctly, not execute complex logic or maintain shared program state.
- The script language is deliberately limited and non-Turing-complete. This means it cannot run arbitrary loops or unbounded computations, which reduces the risk of infinite execution, vulnerabilities, and resource exhaustion. Because of this limitation, Bitcoin Script is suitable for secure transaction validation rather than general software development.
Bitcoin Script typically appears in two parts:
ScriptPubKey (locking script)
- Sets the spending condition in the output.
ScriptSig or witness data (unlocking data)
- Provides the proof needed to satisfy that condition.
For example, a standard payment to a Bitcoin address often uses a script requiring the recipient to provide:
- A public key.
- A digital signature matching that public key.
When the script runs, the unlocking data is checked against the locking conditions. If the conditions are met, the spending transaction is valid.
2. Common Script Types and Spending Conditions
- The most widely used script type is Pay-to-Public-Key-Hash (P2PKH), where coins are locked to the hash of a public key. To spend them, the recipient must reveal the public key and provide a valid signature. This improves privacy and reduces the amount of public data visible on-chain until the funds are spent.
- Other important script types include Pay-to-Script-Hash (P2SH), Pay-to-Witness-Public-Key-Hash (P2WPKH), Pay-to-Witness-Script-Hash (P2WSH), and multisignature scripts. These allow more complex conditions such as requiring multiple approvals, using SegWit-based transaction formats, or hiding complex scripts behind a single hash.
Examples of script functionality:
Single-signature spending
- One private key authorizes the transaction.
Multisignature spending
- Two or more signatures are required from different keys.
Hash lock
- The spender must reveal a secret whose hash matches the one in the script.
Timelock
- Funds cannot be spent until a specific block height or time has passed.
These script types make Bitcoin more than just a simple transfer system; they enable programmable ownership rules.
3. Script Evaluation, Security, and Practical Limitations
- Every full node independently verifies script execution when confirming a transaction. This decentralized validation is essential because it ensures no central authority decides whether a transaction is valid. Instead, validity is determined strictly by consensus rules that all honest nodes enforce.
- Bitcoin Script includes many security safeguards. It is intentionally designed to avoid dangerous features such as unrestricted loops, recursion, or complex memory management. As a result, scripts are easier to audit and less likely to introduce catastrophic bugs.
Important limitations include:
- Scripts are not meant for complex smart contracts like those on more expressive blockchains.
- Many operations are restricted to avoid malleability and security issues.
- The language is conservative, so creating advanced logic often requires careful design or layering with protocols like P2SH and SegWit.
Despite these limitations, Bitcoin Script is powerful enough for a large variety of secure transaction conditions. It balances flexibility with robustness, which is one reason Bitcoin has remained reliable over time.
Working / Process
- A sender creates a transaction output with a locking script that defines the condition for spending the coins.
- When spending those coins later, the spender supplies unlocking data such as a signature, public key, secret value, or other required proof.
- Nodes combine the unlocking and locking scripts, evaluate them on the stack, and accept the transaction only if the final result is valid.
Advantages / Applications
- Bitcoin Script enables secure ownership rules without needing a central intermediary, making Bitcoin transactions trust-minimized and verifiable by anyone running a node.
- It supports many practical applications such as single-signature payments, multisignature wallets, escrow-like arrangements, timelocks, hash locks, and advanced wallet recovery structures.
- Its simple and restricted design improves security, reduces the chance of infinite computation, and helps keep Bitcoin consensus stable across the entire network.
Summary
Bitcoin Script is the rule system that controls how bitcoins can be spent. It uses simple, stack-based logic to verify that transaction conditions are met, such as signatures, multiple approvals, or time restrictions. Although limited in expressiveness, it is highly secure, reliable, and essential for Bitcoin’s flexibility and decentralized operation.
- Key point 1: Bitcoin Script defines spending conditions for transactions.
- Key point 2: It works by combining unlocking data with locking scripts and evaluating them on a stack.
- Key point 3: It powers features like signatures, multisig, timelocks, and hash-based conditions.
- Important terms to remember: ScriptPubKey, ScriptSig, P2PKH, P2SH, SegWit, multisignature, timelock, hash lock