Witness
2 min read
The witness of a transaction is a section of SegWit transactions not included when a transaction is hashed and signed. The witness contains the Script Witnesses for all SegWit inputs of a transaction. The Script Witness is analogous to a ScriptSig in legacy transactions: it contains signatures and scripts required to spend a previous SegWit output.
Because the spending script is included in the witness, the ScriptSig of SegWit transactions is left blank. Since the witness is not included in the hash of a transaction, it does not affect the txid. This architecture was implemented in order to eliminate transaction malleability.
Witness data is considered part of a transaction, and is stored alongside each transaction by all Bitcoin nodes who implemented the SegWit upgrade. However, witness data receives a discount when the weight of a transaction is calculated. While a regular byte of a transaction is equivalent to 4 weight units, a byte of witness data weighs only 1 weight unit. This discount makes spending SegWit outputs cheaper than legacy outputs. It also effectively increases the maximum size of blocks from 1MB to 4MB.
In order to ensure that a transaction’s witness cannot be altered once it is included in the blockchain, a separate witness txid (wtxid) is calculated. This wtxid is used to create a distinct Merkle tree of all SegWit transactions in a block, similar to a block’s regular Merkle tree. This Merkle tree is added as the scriptPubKey of an empty coinbase output using OP_RETURN.