Since BIP144, transaction data consists of two parts (normal data and witness data). There are also two distinct transaction serialization formats:
- The legacy serialization format, which stores version number, transaction inputs, transaction outputs, and locktime.
- The extended serialization format, which also has a flag/marker to indicate the extended format is in use, and has witness data for every transaction input.
The extended format must be used if witness data is present. The legacy format must be used if no witness data is present.
The txid is the double-SHA256 hash of the serialized transaction in legacy format, meaning that any witness data has to be stripped out (as the legacy format does not support witness data). If the transaction had no witness data in the first place (as pre-segwit transaction do), the txid is just the hash of the serialized transaction.
The hash (or wtxid) of a transaction is the double-SHA256 hash of the serialization including potential witness data. If the transaction contains no witness data, this is identical to the txid.