Bitcoin's scripting system is a fundamental component that enables the execution of transactions on the blockchain. This guide explores Bitcoin Script, its opcodes, functionalities, and practical applications.
What is Bitcoin Script?
Bitcoin uses a scripting system for processing transactions. Inspired by Forth, Script is:
- Stack-based: Operates using a last-in-first-out (LIFO) data structure.
- Processed left to right: Instructions are executed sequentially.
- Non-Turing complete: Intentionally lacks loops to prevent infinite execution.
How Script Works in Transactions
A script is essentially a list of instructions attached to each transaction, describing how the recipient can access the transferred Bitcoins. For a standard transfer to address D, the script requires:
- A public key whose hash matches address D.
- A signature proving ownership of the corresponding private key.
๐ Learn more about Bitcoin transactions
Key Features of Bitcoin Script
- Flexibility: Scripts can require multiple private keys or custom spending conditions.
- Validation: A transaction is valid if the combined script executes without failure and leaves a true value on the stack.
- Security: The original sender defines the unlocking script, while the spender provides the necessary inputs.
Stack Operations and Data Types
- Byte vectors: Stored as little-endian integers with variable length.
- Boolean values: False is represented as zero, True as any non-zero value.
- Size limits: Stack items cannot exceed 520 bytes; integers are typically limited to 4 bytes.
Bitcoin Script Opcodes
Opcodes are the building blocks of Bitcoin Script. Below is a categorized list:
Constants
| Opcode | Hex | Description |
|---|---|---|
OP_0 | 0x00 | Pushes an empty byte array. |
OP_1NEGATE | 0x4f | Pushes -1 onto the stack. |
OP_1 | 0x51 | Pushes 1 onto the stack. |
Flow Control
| Opcode | Hex | Description |
|---|---|---|
OP_IF | 0x63 | Executes if top stack value is true. |
OP_VERIFY | 0x69 | Fails if top stack value is false. |
OP_RETURN | 0x6a | Marks transaction as invalid. |
Stack Manipulation
| Opcode | Hex | Description |
|---|---|---|
OP_DUP | 0x76 | Duplicates the top stack item. |
OP_SWAP | 0x7c | Swaps the top two stack items. |
OP_2DROP | 0x6d | Removes the top two stack items. |
Cryptographic Opcodes
| Opcode | Hex | Description |
|---|---|---|
OP_HASH160 | 0xa9 | Performs SHA-256 + RIPEMD-160 hash. |
OP_CHECKSIG | 0xac | Verifies an ECDSA signature. |
OP_CHECKMULTISIG | 0xae | Verifies multiple signatures. |
๐ Explore Bitcoin security features
Practical Script Examples
Pay-to-Public-Key-Hash (P2PKH)
ScriptPubKey: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
ScriptSig: <signature> <pubKey>
Multisig Transactions
ScriptPubKey: OP_2 <pubKey1> <pubKey2> <pubKey3> OP_3 OP_CHECKMULTISIG
ScriptSig: OP_0 <sig1> <sig2>
Time-Locked Transactions
Using OP_CHECKLOCKTIMEVERIFY to freeze funds until a future date:
ScriptPubKey: OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Frequently Asked Questions
Is Bitcoin Script Turing-complete?
No, Bitcoin Script is intentionally not Turing-complete to prevent infinite loops and ensure predictable execution times.
Can disabled opcodes be reactivated?
Disabled opcodes (like OP_CAT) cannot be used in current transactions. Reactivation would require a softfork with broad consensus.
What is the maximum script size?
Standard scripts are limited to 10,000 bytes, though most implementations enforce stricter limits for security.
How does OP_RETURN work?
OP_RETURN marks transactions as invalid, allowing users to embed data (up to 83 bytes) without bloating the UTXO set.
What are the most commonly used opcodes?
The most frequently used opcodes include:
OP_DUPOP_HASH160OP_CHECKSIGOP_EQUALVERIFY
Conclusion
Bitcoin Script provides a flexible yet secure framework for defining transaction conditions. While intentionally limited for safety, its opcodes enable everything from simple transfers to complex multisig and time-locked contracts. As Bitcoin evolves, new opcodes may be introduced via softforks, expanding its capabilities further.
For developers, mastering Script is essential for building advanced Bitcoin applications. For users, understanding its basics helps demystify how transactions are verified on the blockchain.