Witness
Also called: witness data, scriptWitness, witness stack, segregated witness data, wtxid.
The witness is the part of a SegWit transaction that holds signatures and scripts, kept out of the txid so it can't be changed to alter the transaction ID.
The witness is the data that proves a transaction is allowed to spend its inputs: mainly signatures, public keys and scripts. SegWit (BIP 141) moved this data out of the main transaction body into a separate structure. Each input gets its own witness, a list (stack) of byte strings.
How witness data works
- Not in the txid. The txid is still the double SHA-256 of the transaction without witness data, so changing a signature can't change it. This fixed third-party transaction malleability. A second ID, the wtxid, covers the witness too.
- Serialization. BIP 144 adds a marker byte 0x00 and flag byte 0x01 after the version field, and puts the witnesses after the outputs.
- Commitment. Each block commits to all wtxids through a Merkle tree whose root goes in an OP_RETURN output of the coinbase transaction, starting with the bytes 0x6a24aa21a9ed.
- Discount. Each witness byte counts as 1 weight unit instead of 4 (block weight), so witness-heavy spends cost fewer vbytes.
Witness versions
An output's witness version (0 to 16) says how its witness is checked. Version 0 covers P2WPKH (witness: signature and public key) and P2WSH (witness: inputs plus the script). Version 1 is Taproot (BIP 341): a key path witness is a single 64- or 65-byte Schnorr signature, while a script path witness ends with the script and a control block. The remaining versions are reserved for future soft forks.
Signatures in version 0 witnesses use the BIP 143 sighash algorithm.