Whats the difference between "txid" and "hash" - getrawtransaction bitcoind Sometimes the txid key is the same as the hash key in the return of getrawtransaction, sometimes different. I'm trying to understand more about these...

What are the differences, and why are they sometimes identical and sometimes different?

Example - identical in this testnet tx:

bitcoin-cli -testnet getrawtransaction c3f11d1708ac20eff9137d4eadd62dd99d71ce22dc51bc51d4e5b83958d9596d 1

Example - different in this livenet tx:

bitcoin-cli getrawtransaction df2fd78e42590f301b5174e7d6665887380ad1f49a4688a0f8adffa0829855cc 1
Summary: The difference is when the tx is a segwit tx, the calculation of hash does not include the witness data, whereas the txid does.

The definition of transaction hash remains the same as txid for non-witness transactions (non Segwit). The reason the mainnet tx has a different hash than the txid is because it is a witness transaction. If you run bitcoin-cli help getrawtransaction it will show you:

Result (if verbose is set to true):
{
  ...
  "txid" : "id",        (string) The transaction id (same as provided)
  "hash" : "id",        (string) The transaction hash (differs from txid for witness transactions)
  ...


Segregated Witness

The following is from BIP 141.

Note that the BIP states, "signature data is no longer part of the transaction hash."

Transaction ID

A new data structure, witness, is defined. Each transaction will have 2 IDs.

Definition of txid remains unchanged: the double SHA256 of the traditional serialization format:

[nVersion][txins][txouts][nLockTime]


A new wtxid is defined: the double SHA256 of the new serialization with witness data:

[nVersion][marker][flag][txins][txouts][witness][nLockTime]


Format of nVersion, txins, txouts, and nLockTime are same as traditional serialization.

The marker MUST be a 1-byte zero value: 0x00.

The flag MUST be a 1-byte non-zero value. Currently, 0x01 MUST be used.

The witness is a serialization of all witness data of the transaction. Each txin is associated with a witness field. A witness field starts with a var_int to indicate the number of stack items for the txin. It is followed by stack items, with each item starts with a var_int to indicate the length. Witness data is NOT script.

A non-witness program (defined hereinafter) txin MUST be associated with an empty witness field, represented by a 0x00. If all txins are not witness program, a transaction's wtxid is equal to its txid.