Progress pill
Bitcoin Network Inner Workings

Advanced Node Communication and Segregated Witness

Programming Bitcoin

Advanced Node Communication and Segregated Witness

  • Block-Based Transaction Retrieval and Privacy
  • Trustless Network Interaction
  • SegWit Goals and Design
  • SegWit’s Impact
This session unifies advanced P2P networking with Segregated Witness, showing how modern Bitcoin software interacts directly with nodes while using SegWit-aware transaction structures. Developers learn to retrieve blocks, scan for relevant transactions, and construct transactions using only raw network communication—no block explorers required.

Block-Based Transaction Retrieval and Privacy

Wallets must detect incoming payments by scanning blocks for outputs matching their scriptPubKey. Retrieving whole blocks protects privacy better than requesting individual transactions, which leaks strong signals about user activity. Even block requests may leak information on low-volume chains, making compact block filters (BIP158) essential for privacy‑preserving light clients. Filters may produce false positives but never false negatives, allowing clients to download only potentially relevant blocks without revealing specific addresses.

Trustless Network Interaction

The get_block workflow demonstrates proper network usage: send getdata, receive a block, then independently verify its Merkle root and proof of work. A block is accepted only if the received header hash matches the requested hash and its computed Merkle root matches the header. This embodies “don’t trust, verify,” ensuring even malicious peers cannot trick nodes into accepting altered data.

Retrieving Transactions from Blocks

A block’s transactions can be scanned for matching scriptPubKeys using simple iteration. Production wallets perform this continuously as new blocks arrive, proving ownership of outputs strictly through cryptographic validation rather than relying on third‑party APIs.

SegWit Goals and Design

Segregated Witness (SegWit) fixed transaction malleability by removing signature data from the txid calculation. This enabled reliable pre‑signed transaction chains and made the Lightning Network practical. SegWit also increased block capacity using “block weight”: old nodes still see ≤1 MB blocks, while upgraded nodes validate up to 4 MB including witness data. Versioned witness programs (v0–v1 so far) create a structured upgrade path for future script types.

P2WPKH (Native SegWit)

P2WPKH replaces the legacy P2PKH structure with a 22‑byte script: OP_0 + push20 + hash160(pubkey). Spending moves the signature and pubkey into a separate witness field.
  • Pre‑SegWit nodes: see “anyone‑can‑spend,” treat it as valid.
  • Post‑SegWit nodes: recognize OP_0 + 20‑byte hash and validate using witness data.
This separation fixes malleability and reduces fees. The witness uses a varint count followed by the signature and pubkey.

P2SH-P2WPKH (Backward-Compatible SegWit)

To allow old wallets to send to SegWit addresses, P2WPKH scripts can be wrapped in P2SH.
  • scriptPubKey: standard P2SH hash160(redeemScript)
  • scriptSig: the redeemScript (the P2WPKH program)
  • witness: signature + pubkey
Validation layers:
  1. Pre‑BIP16 nodes accept the redeemScript as valid.
  2. Post‑BIP16 nodes evaluate it, leaving OP_0 + hash on the stack.
  3. SegWit nodes perform full witness validation.

P2WSH for Complex Scripts

P2WSH generalizes SegWit for multisig and advanced scripts by committing to SHA256(script) instead of hash160. A typical 2‑of‑3 multisig witness stack:
  • empty element (CHECKMULTISIG bug)
  • sig1
  • sig2
  • witness script (the multisig script)
SegWit nodes verify SHA256(witnessScript) matches the scriptPubKey hash and then execute it. Using SHA256 and a 32‑byte hash allows distinguishing P2WSH from P2WPKH and strengthens security.

P2SH-P2WSH (Maximum Compatibility)

Complex SegWit scripts can also be P2SH‑wrapped:
  • scriptSig: redeemScript (OP_0 + 32‑byte hash)
  • witness: signatures + witnessScript
Validation passes through three generations of rules exactly as with P2SH‑P2WPKH. This wrapper was essential for early Lightning deployments needing multisig without malleability. While native P2WSH is preferred today, the wrapped form ensures compatibility across older wallet systems.

SegWit’s Impact

SegWit provided:
  • stable transaction IDs
  • lower fees via discounted witness data
  • higher block throughput via block weight
  • compatibility for old nodes
  • a clean upgrade path for Taproot and future extensions
Together with trustless network interaction, these tools form the backbone of modern Bitcoin development.
Quiz
Quiz1/5
What is the primary cryptographic property that enables compact block filters (BIP158) to provide privacy for light clients while maintaining perfect accuracy?