Bitcoin SegWit v0 Signing Explained: BIP143 Signature Hashes
SegWit version 0 changes how the signature message is calculated for P2WPKH and P2WSH spends. BIP143 defines a digest algorithm that commits to the amount of the output being spent, improving the information available to offline signers.
Why the spent-output amount matters
For SegWit v0 signatures, the signer commits to the value of the referenced UTXO. If a device receives a false amount, the signature will not validate against the real prevout. This is important for hardware wallets that rely on externally supplied transaction data.
Main BIP143 components
The message includes transaction version, hashPrevouts, hashSequence, the current outpoint, scriptCode, the current input amount, the current input sequence, hashOutputs, locktime, and a four-byte hash type. The hashPrevouts, hashSequence, and hashOutputs components depend on the chosen hash-type flags.
For P2WPKH, scriptCode is constructed from the standard P2PKH-style template. For P2WSH, it depends on the witness script and any applicable OP_CODESEPARATOR semantics. These details are consensus-critical.
Digest and ECDSA verification
The BIP143 preimage is hashed twice with SHA-256. The resulting digest is verified against an ECDSA signature and the authorized public key under the relevant Script rules. The signature's hash-type byte is part of its serialized form, but it is not part of the DER-encoded ECDSA signature itself.
Common implementation mistakes
Do not confuse a transaction's txid with its signature digest. Do not use the full witness serialization as the BIP143 message. Validate prevout scripts, amounts, input indexes, and hash flags before signing. P2TR does not use BIP143; Taproot has a separate tagged-hash design.
Primary references
BIP143 — Transaction Signature Verification for Version 0 Witness Program: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki
BIP341 — Taproot: https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
BIP342 — Validation of Taproot Scripts: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki
