Overview

10 Segregated witness

Segregated Witness (segwit) is presented as a comprehensive, backward- and forward‑compatible upgrade that tackles several long‑standing issues in Bitcoin: transaction malleability, inefficient signature verification, wasted bandwidth for lightweight clients, and the difficulty of extending Script. The core idea is to separate signature data (the “witness”) from the base transaction so that transaction IDs no longer commit to signatures. This simple relocation unlocks a soft‑fork pathway to fix malleability, streamline verification, improve network efficiency, and create a clean versioning mechanism for future Script upgrades—while keeping legacy nodes operational.

In practice, segwit introduces outputs that carry a version byte and a “witness program,” enabling modern forms of payment such as p2wpkh and p2wsh and new Bech32 addresses. Signatures move from the legacy signature script into a witness attached to each input, and miners commit to all witnesses via a witness commitment placed in the coinbase transaction, using a wtxid-based merkle root. Upgraded nodes validate witnesses according to the indicated version and program, while old nodes still accept these transactions because they appear as anyone‑can‑spend (yet are economically enforced invalid without the correct witness). For broad wallet compatibility, segwit can be nested in p2sh, allowing legacy senders to pay segwit recipients seamlessly.

Segwit also modernizes performance and scaling. A new signature hashing method (BIP143) reuses an intermediate hash so verification grows roughly linearly with input count, and signatures now commit to the spent amounts, protecting offline and hardware wallets from fee‑mismatch attacks. On the block level, “weight” discounts witness bytes, effectively raising throughput as segwit usage increases and lifting the practical ceiling on signature operations by counting segwit checks more cheaply. Together with reduced bandwidth for SPV clients (who can omit witnesses) and a witness versioning scheme that enables robust future Script enhancements, segwit strengthens Bitcoin’s integrity, efficiency, and upgradability without breaking existing software.

1  You spend one of the outputs that the previous time locked transaction spends and create a new time locked transaction that you give to your daughter.
inheritance transaction.png
  . Inheritance fails because your daughter’s time locked transaction is forever invalid due to transaction malleability.
inheritance fails.png
  Sign the first input. Prepare by copying the pubkey script to the signature script.
sign old digest 1.png
  Sign the second input.
sign old digest 2.png
  Total time for hashing during signature verification. Time roughly quadruples when number of inputs double.
sighash n2.png
  Without the signature scripts, a lightweight wallet will not be able to verify that a transaction is included in the block.
cannot verify tx included in block.png
  Using  OP_CHECKLOCKTIMEVERIFY to lock an output until May 1.
cltv allowance.png
  The txid is calculated from the whole transaction, including signature scripts.
normal transaction.png
  A segwit transaction contains no signature data. The signatures are attached instead. The txid does not commit to the signatures.
segwit transaction simple.png
  Amy decodes the segwit address to get the witness version and the witness program.
bech32 decode.png
  Amy sends 0.1 BTC to your segwit address. Pubkey script doesn’t contain any script operators, just data.
segwit output.png
  You create and broadcast a payment to the popcorn machine owner.
segwit spend wpkh.png
  A full node verifies the witness of your transaction. The pattern 00 followed by exactly 20 bytes gets special treatment.
segwit spend wpkh verify.png
  An old node will just see two data items in the pubkey script and an empty signature script.
segwit spend wpkh verify old node.png
  Your segwit transaction gets included in a block. The block commits to the witnesses by putting the witness commitment into an output of the coinbase transaction.
block segwit.png
  The coinbase transaction’s witness contains the witness reserved value and an OP_RETURN output contains the witness commitment.
segwit coinbase tx.png
  An old node verifies the block with your transaction. It will not verify the signatures or the witness commitment.
block segwit old node.png
  John and Faiza spends an output from their multisig wallet.
p2sh overview.png
  The witness script is hashed into a witness script hash
witness script and wsh.png
  The popcorn guy sends the money to the charity’s p2wsh address.
tx popcorn guy to charity.png
  The charity pays 0.07 BTC to the shelter’s address. The witness is the signatures followed by a data item that contains the actual witness script.
tx charity to shelter.png
  Preparing to verify the p2wsh input
segwit spend wsh verify 1.png
  Verifying the witness of a p2wsh payment.
segwit spend wsh verify 2.png
  Hashing is done in two steps. The intermediate hash is reused for each input.
new sighash algo.png
  An off-line wallet cannot know the fee of a transaction.
fee unknown.png
  Nina sends 0.1 BTC to your segwit wallet using a p2wpkh inside a p2sh address.
p2wpkh in p2sh.png
  You spend the money you got from Nina by setting the version byte and witness program in the redeem script in your signature script of your input.
p2wpkh in p2sh spend.png
p2pkh. Address format 1<some base58 characters>`
recap payment types p2pkh.png
p2sh. Address format 3<some base58 characters>`
recap payment types p2sh.png
p2wpkh. Address format bc1q<38 base32 characters>`
recap payment types p2wpkh.png
p2wsh. Address format bc1q<58 base32 characters>`
recap payment types p2wsh.png
p2wpkh nested in p2sh. Address format 3<some base58 characters>`
recap payment types p2wpkh in p2sh.png
p2wsh nested in p2sh. Address format 3<some base58 characters>`
recap payment types p2wsh in p2sh.png
  . Witness bytes and non-witness bytes are counted differently. Witness bytes contribute less to the block weight and not at all to the traditional block size, the base block size.
block weight.png
 Signatures are not part of the transaction id, because they are moved out from the base transaction.
summary segwit transaction.png
  The old style signature hashing algorithm will hash the whole transaction for each signature
summary new sighash algo old.png
  With segwit, the whole transaction is only hashed once, and that hash is reused for each signature.
summary new sighash algo new.png
block segwit.png
ex segwit spend wpkh verify old node.png
ex segwit spend wpkh verify.png
segwit coinbase tx.png

 Recap

In this chapter you learned that

  • Segwit moves signature script data out of transactions to solve transaction malleability issues.
  • A new signature hashing algorithm is used by segwit that makes transaction verification faster. Helps nodes staying up to date with less resources.
  • Lightweight wallets get better privacy with preserved data traffic by not downloading witness data.
  • The witness version byte of the pubkey script makes upgrading the script language easier.
  • The block size can be somewhat increased by counting witness bytes with a discount.
  • A new address format is introduced to help wallets distinguish between legacy payments and segwit payments.
  • Segwit can be "embedded" in old style p2sh addresses to allow old wallets to send money to segwit wallets.

FAQ

What problems does Segregated Witness (SegWit) aim to solve?SegWit targets four main issues: transaction malleability (txids changing due to mutable signatures), inefficient signature verification (quadratic hashing work with many inputs), wasted bandwidth (light wallets downloading signatures they can’t verify), and difficulty upgrading Script (limited, awkward OP_NOP repurposing). By moving signatures into a separate “witness” structure and introducing versioned witness programs, SegWit addresses all four.
How does moving signatures out of transactions fix transaction malleability?In SegWit, the transaction ID (txid) is computed without the witness (signature) data. Because signatures are no longer part of the txid, any modification to signatures or their encoding won’t change the txid. This makes unconfirmed transaction chains robust against malleation and prevents dependent transactions from breaking.
What are p2wpkh and p2wsh, and how are they recognized?They are SegWit v0 output types identified by a version byte followed by a witness program. p2wpkh (pay-to-witness-public-key-hash) uses version 0 plus a 20-byte program (a public key hash). p2wsh (pay-to-witness-script-hash) uses version 0 plus a 32-byte program (a script hash). Nodes detect SegWit by the “version + 2–40 byte program” pattern.
How can old nodes “validate” SegWit transactions they don’t understand?Old nodes don’t download witnesses and see SegWit outputs as simple data pushes, which evaluate as true (“anyone-can-spend”) under legacy rules. They therefore relay and accept such transactions and blocks. New SegWit-aware nodes enforce the real spending rules, and economic majority ensures invalid “anyone-can-spend” attempts won’t be accepted chain-wide.
What is the witness commitment, and where is it stored?The witness commitment is a hash that commits the block to all transaction witnesses. It combines the merkle root of all transaction wtxids (tx hashes including witnesses; coinbase wtxid is defined as zero) with a witness reserved value. Miners include this commitment in an OP_RETURN output of the coinbase transaction, and the coinbase’s witness carries the reserved value so nodes can verify it.
How does BIP143 make signature verification faster and safer?BIP143 hashes transactions in two steps: an intermediate hash over shared parts (inputs/outputs) and a per-input extension, making total work grow linearly with input count. It also commits each signature to the spent outpoint, script, and amount. Including the amount lets offline/hardware wallets show accurate fees and prevents tricking them into signing mismatched amounts.
What benefits do Bech32 SegWit addresses provide?Bech32 (bc1...) encodes a human-readable part, a version, and a witness program with a strong error-detecting checksum. Benefits include smaller QR codes, no mixed-case confusion, robust error detection (up to four character errors with certainty), and explicit encoding of the SegWit version and program for safer decoding.
How can non-SegWit wallets pay SegWit addresses?Use “nested SegWit”: p2wpkh-in-p2sh or p2wsh-in-p2sh. The payer sends to a legacy p2sh address (3...), whose redeemScript is a SegWit version byte plus witness program. Old nodes validate the p2sh hash match; new nodes detect the embedded SegWit program and verify the witness accordingly.
What is block weight, and how did SegWit increase capacity without a hard fork?SegWit introduced block weight with a 4,000,000 WU limit: base (non-witness) bytes count as 4 WU each, witness bytes as 1 WU. Old nodes still enforce the 1 MB base size, while new nodes allow additional witness data at a discount. As SegWit usage grows, effective total block size (base + witness) increases, boosting throughput. Signature operation limits were similarly adjusted with a discount for SegWit ops.
Why does SegWit reduce bandwidth for lightweight wallets?Light wallets can fetch transactions without witnesses since the normal merkle root (over txids) excludes witness data. They can still verify inclusion proofs while skipping signatures they can’t validate anyway. This cuts per-transaction data by roughly half in typical cases and can be used to improve privacy (smaller filters) or reduce traffic.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bitcoin ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bitcoin ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bitcoin ebook for free