THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Bitcoin Script and Opcodes: A Developer Introduction

permalink THAMER
#bitcoin-opcodes#bitcoin-script#scriptpubkey#scriptsig#tapscript#witness

Bitcoin Script is a stack-based language used to express and evaluate spending conditions for transaction outputs. It is intentionally constrained: it is not a general-purpose application runtime, and its rules differ across script versions and output types.

Locking and unlocking conditions

A transaction output includes a scriptPubKey. Spending that output requires data and, where applicable, script execution satisfying the relevant rules. In older transaction types this involves scriptSig; SegWit spends also use witness data. Taproot key-path spending validates a Schnorr signature without exposing a script, while script-path spending reveals a committed script and control information.

Common opcodes

  • OP_DUP duplicates the top stack item.
  • OP_HASH160 applies SHA-256 followed by RIPEMD-160.
  • OP_EQUALVERIFY compares two items and fails if they differ.
  • OP_CHECKSIG checks a signature under the applicable script rules.
  • OP_CHECKLOCKTIMEVERIFY helps enforce an absolute locktime condition.
  • OP_CHECKSEQUENCEVERIFY helps enforce a relative locktime condition.

Opcode behavior depends on the script context and version. A developer should not assume an opcode has identical semantics in legacy Script and Tapscript.

Example: the P2PKH pattern

A traditional pay-to-public-key-hash locking script has the form:

OP_DUP OP_HASH160 <20-byte-public-key-hash> OP_EQUALVERIFY OP_CHECKSIG

This is a teaching example, not a recommended template for every modern wallet. SegWit and Taproot have different output structures and validation paths.

Why Taproot changes the picture

BIP342 defines Tapscript, which modifies signature-checking semantics and resource rules for Taproot script-path spends. BIP340 defines Schnorr signatures, while BIP341 defines Taproot's overall spending structure. A wallet that can send to a P2TR address does not necessarily implement arbitrary Taproot script-path construction or spending.

Developer safety checklist

Use well-tested libraries and network-specific test vectors. Distinguish consensus validity from relay policy. Do not construct production scripts from an unreviewed text example, and test edge cases such as timelocks, signature encodings, and script-version differences.

Primary sources

  • BIP340: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
  • BIP341: https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
  • BIP342: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects