0

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?

0

2 Answers 2

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.

2
  • 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
    Commented Dec 12, 2023 at 11:14
  • @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
    Commented Dec 12, 2023 at 11:27
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)

2
  • But I sha256 the transaction data twice, the result equals to transaction hash not txid
    – Jack Green
    Commented Dec 12, 2023 at 8:16
  • @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
    Commented Dec 12, 2023 at 10:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.