<p>There are two types of transaction identifiers, TXID and WTXID.</p>
<p><strong>TXID</strong> represents the result of a double SHA256 of the serialized transaction form without witness data. It is used to uniquely identify the transaction. TXID is also used to create a Merkle tree and indirectly determine the Merkle root.</p>
<p><strong>WTXID</strong> represents the result of a double SHA256 of the serialized transaction form including witness data. It is not used to uniquely identify the transection (although it actually represents a unique identifier). The WTXID is used to create a commitment in a coinbase transaction.</p>
<p>If the transaction does not have witness data, then the TXID is equivalent to the WTXID.</p>
<p><strong>Hash of the transaction</strong> is the result of a double SHA256 of the entire serialized form of the transaction, whatever it may be. If the transaction is segWit (if it has witness data), then this hash will be equal to the WTXID and will only be used for the commitment in the coinbase transaction. If the transaction is not segWit (if it does not have witness data), then this hash will be equal to the TXID (but also with WTXID) and will be used as a unique identifier, but also for the construction of the merkle root in the block header.</p>

CC BY-SA 4.0
2023-12-12T11:14:32.543Z
So I should still build merkleroot with txid? Another question: The block template contains “default_witness_commitment”. Should I put it in coinbase? if yes, which site? Once I have created coinbase data, how do I calculate its txid, double sha256?
Jack Green
CC BY-SA 4.0
2023-12-12T11:27:00.300Z
@JackGreen Yes, you are building Merkle root with TXIDs. Yes, witness commitment is a part of Coinbase transaction, so you must have it inside one of Coinbase transaction outputs. TXID of Coinbase transaction is calculated in the same way as for any other transaction (double sha256)
LeaBit
CC BY-SA 4.0
2023-12-12T10:39:52.907Z
2023-12-12T10:39:52.907Z
LeaBit
<p>The <code>txid</code> or transaction id is a unique identifier of a transaction.
The transaction id is computed by hashing the transaction data twice.</p>
<p>There is one big caveat. For some reason Satoshi decided to revert the byte-order here. I have no clue why he did it. But here we are today and everyone has to revert the bytes.</p>
<p>If it is a segwit transaction the <code>txhash</code> differs from the <code>txid</code>. In this case the <code>txhash</code> includes the witness data and the <code>txid</code> does not. (See <a href="https://bitcoin.stackexchange.com/questions/77699/whats-the-difference-between-txid-and-hash-getrawtransaction-bitcoind">this question</a>)</p>

CC BY-SA 4.0
2023-12-12T08:16:59.230Z
But I sha256 the transaction data twice, the result equals to transaction hash not txid
Jack Green
CC BY-SA 4.0
2023-12-12T10:49:57.103Z
@JackGreen It happens in the case when the transaction has witness data (segWit transaction). Since hash includes all data (and therefore witness data), while TXID does not include witness data, they will differ.
LeaBit
CC BY-SA 4.0
2023-12-12T07:54:53.273Z
2023-12-12T08:29:54.317Z
2023-12-12T08:29:54.317Z
Erik
<p>I downloaded the block template where each transaction contains 'data', 'txid', 'hash'. I read in some articles that the Merkelroot is calculated using 'txid'. Is true? Why txid not hash, and what's the difference between them? Before today I always used hash to compute merkleroot and getting the error message &quot;bad-txnmerkleroot&quot;. If should use txid, how do I figure out the txid for coinbase that I built?</p>

<hash><terminology><coinbase-transaction><merkle-tree><transaction-id>
What is TXID？Is it used to calculate merkleroot
Jack Green
# What is TXID？Is it used to calculate merkleroot

(Asked by: username_0 on 2023-12-12)

I downloaded the block template where each transaction contains 'data', 'txid', 'hash'. I read in some articles that the Merkelroot is calculated using 'txid'. Is true? Why txid not hash, and what's the difference between them? Before today I always used hash to compute merkleroot and getting the error message "bad-txnmerkleroot". If should use txid, how do I figure out the txid for coinbase that I built?

**Tags:** hash, terminology, coinbase-transaction, merkle-tree, transaction-id


---

## 2 answers

### Answer by username_1 on 2023-12-12. Score: 1

There are two types of transaction identifiers, TXID and WTXID.

**TXID** represents the result of a double SHA256 of the serialized transaction form without witness data. It is used to uniquely identify the transaction. TXID is also used to create a Merkle tree and indirectly determine the Merkle root.

**WTXID** represents the result of a double SHA256 of the serialized transaction form including witness data. It is not used to uniquely identify the transection (although it actually represents a unique identifier). The WTXID is used to create a commitment in a coinbase transaction.

If the transaction does not have witness data, then the TXID is equivalent to the WTXID.

**Hash of the transaction** is the result of a double SHA256 of the entire serialized form of the transaction, whatever it may be. If the transaction is segWit (if it has witness data), then this hash will be equal to the WTXID and will only be used for the commitment in the coinbase transaction. If the transaction is not segWit (if it does not have witness data), then this hash will be equal to the TXID (but also with WTXID) and will be used as a unique identifier, but also for the construction of the merkle root in the block header.

> **Comments:**
> **username_0**: So I should still build merkleroot with txid? Another question: The block template contains “default_witness_commitment”. Should I put it in coinbase? if yes, which site? Once I have created coinbase data, how do I calculate its txid, double sha256?  
> **username_1**: @username_0 Yes, you are building Merkle root with TXIDs. Yes, witness commitment is a part of Coinbase transaction, so you must have it inside one of Coinbase transaction outputs. TXID of Coinbase transaction is calculated in the same way as for any other transaction (double sha256)  

---

### Answer by username_2 on 2023-12-12. Score: 0

The `txid` or transaction id is a unique identifier of a transaction.
The transaction id is computed by hashing the transaction data twice.

There is one big caveat. For some reason Satoshi decided to revert the byte-order here. I have no clue why he did it. But here we are today and everyone has to revert the bytes.

If it is a segwit transaction the `txhash` differs from the `txid`. In this case the `txhash` includes the witness data and the `txid` does not. (See [this question](https://bitcoin.stackexchange.com/questions/77699/whats-the-difference-between-txid-and-hash-getrawtransaction-bitcoind))

> **Comments:**
> **username_0**: But I sha256 the transaction data twice, the result equals to transaction hash not txid  
> **username_1**: @username_0 It happens in the case when the transaction has witness data (segWit transaction). Since hash includes all data (and therefore witness data), while TXID does not include witness data, they will differ.  

---

