Author: Ishaana Misra
Adapted for SEO clarity and readability
In my journey to understand Bitcoin's inner workings, I discovered something surprising: Bitcoin has always supported smart contracts, albeit in a limited form. While these contracts are simple, their power lies in securing Bitcoin transactions. Grasping how Script works is also key to appreciating the significance of the Taproot upgrade.
How Transactions Work in the Bitcoin Network
Bitcoin transactions operate by meeting predefined conditions to spend funds. Let’s break this down step by step.
The Structure of a Bitcoin Transaction
- Inputs and Outputs: Every transaction can have multiple inputs and outputs.
- Output Components: Each output contains a Bitcoin amount (in satoshis) and a locking script (conditions to spend those funds).
Outputs are categorized as:
- UTXO (Unspent Transaction Output): Funds awaiting spending.
- STXO (Spent Transaction Output): Funds already spent and irreusable.
How Inputs Unlock Outputs
A transaction input spends a UTXO by providing:
- The transaction ID that created the UTXO.
- The output index (to identify which UTXO is being spent).
- An unlocking script that satisfies the locking script’s conditions.
Key Insight: Your Bitcoin balance isn’t stored under an address but is the sum of all UTXOs spendable by your private key(s). Wallet software scans the blockchain to tally these UTXOs.
Script: Bitcoin’s Programming Language for Smart Contracts
Bitcoin transactions are built using Script, a unique stack-based language designed for security and determinism.
Why Script Is Different
- Not Turing-complete: Avoids infinite loops/unpredictable execution times.
- Stack-Based Operations: Uses
PUSH
/POP
commands for data handling.
Example: Adding 1 + 1
in Script:
OP_1 OP_1 OP_ADD // Pushes two 1s, adds them, leaves result (2) on the stack.
Common Transaction Script Types
1. Pay-to-Public-Key-Hash (P2PKH)
Locking Script:
OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Unlocking Script:
<Signature> <PublicKey>
- Validation: Combines both scripts to verify the public key hash matches and the signature is valid.
2. Pay-to-Script-Hash (P2SH)
Enables complex conditions (e.g., multisig) with:
Locking Script:
OP_HASH160 <RedeemScriptHash> OP_EQUAL
- Unlocking Script: Provides the redeem script + signatures.
Example (3-of-4 Multisig):
Redeem Script: <3> <4> OP_CHECKMULTISIG Unlocking Script: <Sig1> <Sig2> <Sig3> <RedeemScript>
Advantage: P2SH decouples conditions from the UTXO, allowing flexible spending logic.
FAQs: Bitcoin Script Explained
Q1: Can Bitcoin Script handle loops?
A: No. Script avoids Turing completeness to ensure predictable execution times for network validation.
Q2: Why use P2SH over P2PKH?
A: P2SH supports advanced conditions (like multisig) without bloating the blockchain with redeem script details.
Q3: Is Script as flexible as Ethereum’s Solidity?
A: No, but its simplicity ensures security and reliability for Bitcoin’s core use case: peer-to-peer value transfer.
👉 Explore Bitcoin’s smart contract potential further
Key Takeaways
- Bitcoin’s Script enables basic smart contracts via locking/unlocking scripts.
- P2PKH and P2SH are foundational for transaction security and flexibility.
- Understanding Script is crucial for innovations like Taproot.
References: