Understanding Bitcoin Script: A Comprehensive Guide

ยท

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:

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:

  1. A public key whose hash matches address D.
  2. A signature proving ownership of the corresponding private key.

๐Ÿ‘‰ Learn more about Bitcoin transactions

Key Features of Bitcoin Script

  1. Flexibility: Scripts can require multiple private keys or custom spending conditions.
  2. Validation: A transaction is valid if the combined script executes without failure and leaves a true value on the stack.
  3. Security: The original sender defines the unlocking script, while the spender provides the necessary inputs.

Stack Operations and Data Types

Bitcoin Script Opcodes

Opcodes are the building blocks of Bitcoin Script. Below is a categorized list:

Constants

OpcodeHexDescription
OP_00x00Pushes an empty byte array.
OP_1NEGATE0x4fPushes -1 onto the stack.
OP_10x51Pushes 1 onto the stack.

Flow Control

OpcodeHexDescription
OP_IF0x63Executes if top stack value is true.
OP_VERIFY0x69Fails if top stack value is false.
OP_RETURN0x6aMarks transaction as invalid.

Stack Manipulation

OpcodeHexDescription
OP_DUP0x76Duplicates the top stack item.
OP_SWAP0x7cSwaps the top two stack items.
OP_2DROP0x6dRemoves the top two stack items.

Cryptographic Opcodes

OpcodeHexDescription
OP_HASH1600xa9Performs SHA-256 + RIPEMD-160 hash.
OP_CHECKSIG0xacVerifies an ECDSA signature.
OP_CHECKMULTISIG0xaeVerifies 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:

  1. OP_DUP
  2. OP_HASH160
  3. OP_CHECKSIG
  4. OP_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.