Bitcoin Script Programming

16 min readarticleIncludes quiz · 5 questions

Learn Bitcoin's scripting language and how to create custom transaction conditions.

Script Basics:

  • Stack-based: Operations manipulate a stack
  • Forth-like: Reverse Polish notation
  • Deliberately limited: No loops (Turing incomplete)
  • Predicate: Returns true/false

Common Opcodes:

  • OP_DUP: Duplicate top stack item
  • OP_HASH160: Hash with RIPEMD-160(SHA-256())
  • OP_EQUALVERIFY: Check equality and fail if false
  • OP_CHECKSIG: Verify signature
Writing Bitcoin Scripts
Writing Bitcoin Scripts

Example P2PKH Script:

`scriptPubKey:` `OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG`

`scriptSig:` `<signature> <publicKey>`

Execution flow: 1. Signature and public key pushed to stack 2. OP_DUP duplicates public key 3. OP_HASH160 hashes the public key 4. Compare with stored hash 5. Verify signature

Advanced Script Features:

  • OP_IF/OP_ELSE: Conditional execution
  • OP_CHECKMULTISIG: M-of-N signatures
  • OP_CHECKLOCKTIMEVERIFY: Time locks
  • OP_CHECKSEQUENCEVERIFY: Relative time
  • OP_RETURN: Provably unspendable outputs

These enable complex smart contracts like escrow, inheritance, and atomic swaps.

Test Your Knowledge

This lesson includes a 5-question quiz (passing score: 75%).

Quiz functionality available in the mobile app.