web3.path

PHASE 19 Bitcoin engineering · 18–26 hours

Bitcoin Protocol, Wallet & Custody Engineering

Bitcoin development is not “Ethereum without contracts.” It is UTXO accounting, restrictive Script, peer policy, fee operations, wallet standards, and explicit custody boundaries.

Goal — operate a local Bitcoin Core node, construct and inspect real SegWit/Taproot PSBTs on regtest, and build a safe wallet-service boundary.

1. Local node and safe network choice

Use regtest for deterministic integration tests, signet for public integration, and testnet only when a dependency specifically requires it. Never use mainnet keys in a course project.

# Start Bitcoin Core in regtest mode (adjust paths for your install).
bitcoind -regtest -daemon -fallbackfee=0.0002
alias btc='bitcoin-cli -regtest'
btc createwallet "lab"
ADDR=$(btc -rpcwallet=lab getnewaddress "funding" bech32)
btc generatetoaddress 101 "$ADDR"
btc getblockchaininfo
btc listunspent

Learn the RPC surface: getblockchaininfo, getmempoolinfo, listunspent, decoderawtransaction, testmempoolaccept, sendrawtransaction, and estimatesmartfee. Use Esplora/electrs as an indexing boundary when a product should not expose a Core wallet directly.

2. UTXO transactions, not account transactions

ComponentWhat to understand
InputOutpoint (txid:vout), sequence, unlocking witness/script data, and the previous output being spent
OutputValue in sats and locking script; recipient plus deliberate change output
FeeInputs − outputs; selected from feerate × virtual bytes, never a “gas price” field
PolicyDust, standardness, mempool acceptance, ancestor/descendant limits, and confirmation targets
FinalitynLockTime, sequence, and timelocks; these are part of the spending policy

Implement coin selection with a privacy and fee policy: avoid needless address reuse, identify change, respect dust, and surface the exact fee in sats and sat/vB. Test RBF and CPFP as operational recovery tools, not as trivia.

3. Script, SegWit, and Taproot

4. Wallet standards and custody boundaries

Standard / primitiveUse
BIP-32 / BIP-39 / BIP-44/84/86Seed handling, HD derivation, and purpose-specific paths; never log a mnemonic or xprv.
DescriptorsExplicit wallet policy and derivation definition; avoid opaque “address-only” wallet state.
PSBT (BIP-174/370)Separate construction, signing, and finalization; supports hardware-wallet and multisig flows.
Miniscript / multisigAnalyseable spending policies, threshold custody, recovery and timelock branches.
Hardware signerKey isolation; display and confirm destination, amount, fee, and policy on a trusted device.
Rule — a web server should request a signature from a custody boundary, not store seed phrases. Treat descriptors, xpubs, derivation paths, and PSBT metadata as sensitive operational data even when they are not private keys.

5. Practical lab — PSBT-backed treasury flow

Deliverable
  1. Run Core on regtest and create a descriptor wallet. Fund at least two UTXOs, then inspect their outpoints and scripts with listunspent and gettxout.
  2. Construct a PSBT that pays a recipient and produces a change output. Decode it before signing; show inputs, outputs, fee in sats/sat-vB, locktime, and change classification in a CLI or small service.
  3. Sign in a separate wallet process or hardware-wallet emulator, finalize, run testmempoolaccept, broadcast, mine a block, and verify the resulting UTXO set.
  4. Create an RBF replacement and a CPFP child in regtest. Write tests/assertions for the intended fee and confirmation behavior.
  5. Implement a 2-of-3 descriptor policy with a timelocked recovery branch. Document the signer locations, recovery drill, and the policy’s failure modes.

6. Recommended implementation stack

For browser/server JavaScript, evaluate bitcoinjs-lib carefully with pinned versions and independent transaction decoding. For Rust, use rust-bitcoin and BDK; for mobile/native wallets, prefer a well-maintained descriptor/PSBT-capable library. Do not invent signing or serialization code for a production wallet without deep review and test vectors.

Track note — concepts here; full PSBT/air-gap lab in Phase 25 (labs/phase-25-bitcoin/).
← Phase 18Phase 20: Lightning & Production Capstone →