Bitcoin Script

Definition

Bitcoin Script is the stack-based, Turing-incomplete programming language used to define the conditions under which Bitcoin transactions can be spent. It is a simple yet powerful scripting system embedded in every Bitcoin transaction output (locking script or scriptPubKey) and input (unlocking script or scriptSig), enabling programmable spending conditions without exposing the Bitcoin network to the risks of a Turing-complete smart contract platform. Bitcoin Script enables multi-signature wallets, time-locked transactions, hash-locked contracts, payment channels, and atomic swaps — all without requiring a general-purpose blockchain.

 Origin & History

DateEvent
2008Satoshi Nakamoto designs Bitcoin Script as part of the Bitcoin whitepaper design
2009Bitcoin Script deployed in Bitcoin genesis block; Pay-to-Public-Key (P2PK) is first script type
2012BIP 16: Pay-to-Script-Hash (P2SH) introduced, enabling complex redeem scripts
2015CHECKLOCKTIMEVERIFY (CLTV) added enabling absolute time locks on transactions
2016CHECKSEQUENCEVERIFY (CSV) added enabling relative time locks for payment channels
2017SegWit introduces Pay-to-Witness-Public-Key-Hash (P2WPKH) and P2WSH
2021Taproot upgrade (BIP 340/341/342) introduces Schnorr signatures and MAST via Tapscript
2023Bitcoin Ordinals leverage Script to inscribe data in witness data
2023BIP 347 (OP_CAT) revival debate intensifies for potential Script extensions

 “Bitcoin Script is deliberately limited — and that’s a feature, not a bug. Simplicity is security.” — Greg Maxwell, Bitcoin Core Developer

 How It Works

 BITCOIN TRANSACTION SCRIPT EXECUTION ═══════════════════════════════════════════════════════ LOCKING SCRIPT (scriptPubKey) — on the UTXO output: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG (Standard P2PKH — Pay to Public Key Hash)

UNLOCKING SCRIPT (scriptSig) — provided by spender: <signature> <publicKey>

EXECUTION (stack-based): Stack starts empty

  1. Push <signature>          → Stack: [sig]
  2. Push <publicKey>          → Stack: [sig, pubKey]
  3. OP_DUP: duplicate top     → Stack: [sig, pubKey, pubKey]
  4. OP_HASH160: hash top      → Stack: [sig, pubKey, pubKeyHash]
  5. Push <pubKeyHash>         → Stack: [sig, pubKey, pubKeyHash, expected]
  6. OP_EQUALVERIFY: compare   → Stack: [sig, pubKey] (if equal)
  7. OP_CHECKSIG: verify sig   → Stack: [TRUE]

Result: TRUE = transaction is valid and UTXO is spent

SCRIPT TYPES: P2PK   → Pay to Public Key (legacy) P2PKH  → Pay to Public Key Hash (most common) P2SH   → Pay to Script Hash (multi-sig, complex) P2WPKH → Native SegWit (bech32) P2TR   → Pay to Taproot (most efficient/private) ═══════════════════════════════════════════════════════ 

Script TypeAddress FormatUse CasePrivacy
P2PK(no standard address)Original Bitcoin, Satoshi coinsLow
P2PKH1… (Base58)Standard single-sig paymentsLow
P2SH3… (Base58)Multi-sig, complex scriptsMedium
P2WPKHbc1q… (bech32)Native SegWit, lower feesMedium
P2WSHbc1q… (bech32)Native SegWit scriptsMedium
P2TRbc1p… (bech32m)Taproot, Tapscript, OrdinalsHigh

 In Simple Terms

  1. Lock and key: Every Bitcoin output has a lock (locking script) — to spend it, you must provide the correct key (unlocking script) that satisfies the conditions.
  2. Stack machine: Script executes by pushing values onto a stack and running operations — it’s simple, predictable, and deliberately not Turing-complete to prevent infinite loops.
  3. Multi-sig example: A 2-of-3 multisig script requires 2 of 3 designated private key signatures to unlock funds — used for team treasuries and secure wallets.
  4. Taproot privacy: Taproot makes complex multi-sig and smart contract scripts look identical to simple single-sig transactions on the blockchain — improving privacy.
  5. Deliberate limits: Unlike Ethereum’s Solidity, Bitcoin Script cannot access external data, loop, or call other contracts — making it predictable and secure.

 Real-World Examples

ScenarioScript TypeBenefit
Exchange hot walletP2SH 2-of-3 multisigRequires 2 of 3 keys to spend — protects against single key theft
Lightning Network channelHTLC (Hash Time-Locked Contract)Enables instant off-chain BTC payments
Atomic swap BTC↔LTCHTLC on both chainsTrustless cross-chain exchange without intermediary
Time-locked inheritanceCHECKLOCKTIMEVERIFYFunds only spendable after specific date
Ordinals inscriptionP2TR witness dataNFT/data inscription in Taproot witness

 Advantages

AdvantageDetail
SecurityTuring-incomplete design prevents complex attack vectors possible in smart contracts
PredictabilityEvery script executes deterministically with bounded computation
ExpressivenessEnables multi-sig, time locks, HTLCs sufficient for Lightning Network
EfficiencyScripts are compact, minimizing transaction data and fees
Privacy (Taproot)Complex spending conditions indistinguishable from simple payments

 Disadvantages & Risks

RiskDetail
Limited functionalityCannot implement complex DeFi, oracles, or arbitrary smart contracts
Script complexityAdvanced multi-sig scripts are complex to implement correctly
Legacy format issuesOlder script types (P2PK, P2PKH) have lower privacy than modern alternatives
Disabled opcodesSeveral powerful opcodes (OP_CAT, OP_CHECKSIGFROMSTACK) were disabled for security
Script bug risksErrors in custom scripts can permanently lock funds

Risk Management Tips:

  • Always test custom Script implementations on testnet before mainnet
  • Use P2TR (Taproot) addresses for best privacy and future compatibility
  • For multi-sig wallets, use established implementations (Bitcoin Core, Sparrow Wallet) rather than custom scripts
  • Store redeem scripts (P2SH) securely — you need them to spend funds

 FAQ

Q: Why is Bitcoin Script not Turing-complete?

A: Turing completeness allows infinite loops — dangerous in a blockchain where all nodes must execute every script. Bitcoin Script’s deliberate incompleteness ensures scripts terminate predictably, preventing denial-of-service attacks and making formal verification of scripts possible.

Q: What is an HTLC in Bitcoin Script?

A: A Hash Time-Locked Contract (HTLC) is a script that allows a payment to be claimed by revealing a secret (preimage) that hashes to a predetermined value, or refunded to the sender after a timeout. HTLCs are the fundamental building block of the Lightning Network.

Q: How does Taproot improve Bitcoin Script?

A: Taproot (BIP 340/341/342) introduces Schnorr signatures enabling key aggregation, MAST (Merklized Abstract Syntax Trees) via Tapscript enabling complex scripts while revealing only executed branches, and making all Taproot outputs look identical externally — significantly improving privacy and efficiency.

UPay Tip: When setting up a Bitcoin wallet, use Taproot (bc1p…) addresses for the best combination of privacy, efficiency, and future compatibility. Most modern wallets like Sparrow and Phoenix support Taproot natively.

Disclaimer: This content is for educational purposes only and does not constitute financial or technical advice.

UPay — Making Crypto Encyclopedic

News & Events