<p>Been writing a bitcoin library from scratch, which eventually involves calculating transaction IDs.</p>
<p>Version 1 transactions work fine when their hex data is being SHA256'd twice and then reversed.</p>
<p>Version 2 transactions ids don't seem to work that way, according to data I found on block explorers. The IDs I generate never match.</p>
<p>Why?</p>

CC BY-SA 4.0
2022-11-13T00:01:25.173Z
The algorithm for computing txids is the same for version 1 and version 2. But is it possible that the v2 transactions you're looking at are segwit transactions? Because those do have a different hashing algorithm, see BIP141 and BIP144.
Pieter Wuille
CC BY-SA 4.0
2022-11-13T00:49:06.067Z
Take any V1 transaction in mempool.space, click `Transaction hex`, and then double-sha256 what you get, then reverse it... matches the ID in the URL. The same doesn't seem to apply for any v2 transaction... no idea why.
RooSoft
CC BY-SA 4.0
2022-11-13T01:08:46.690Z
Recent transactions are likely segwit transactions. For those you need to drop the witness before hashing to obtain the txid. It has nothing to do with v1 or v2 however; either can be segwit or not.
Pieter Wuille
CC BY-SA 4.0
2022-11-13T02:01:37.603Z
Invaluable information, Pieter... thank you so much! Will take a good look a that.
RooSoft
CC BY-SA 4.0
2022-11-13T18:45:37.630Z
Sure does, @RedGrittyBrick !
RooSoft
<transactions><transaction-id>
Calculate a transaction version 2 ID
RooSoft
# Calculate a transaction version 2 ID

(Asked by: username_0 on 2022-11-12)

Been writing a bitcoin library from scratch, which eventually involves calculating transaction IDs.

Version 1 transactions work fine when their hex data is being SHA256'd twice and then reversed.

Version 2 transactions ids don't seem to work that way, according to data I found on block explorers. The IDs I generate never match.

Why?

**Tags:** transactions, transaction-id

> **Comments:**
> **username_1**: The algorithm for computing txids is the same for version 1 and version 2. But is it possible that the v2 transactions you're looking at are segwit transactions? Because those do have a different hashing algorithm, see BIP141 and BIP144.  
> **username_0**: Take any V1 transaction in mempool.space, click `Transaction hex`, and then double-sha256 what you get, then reverse it... matches the ID in the URL. The same doesn't seem to apply for any v2 transaction... no idea why.  
> **username_1**: Recent transactions are likely segwit transactions. For those you need to drop the witness before hashing to obtain the txid. It has nothing to do with v1 or v2 however; either can be segwit or not.  
> **username_0**: Invaluable information, Pieter... thank you so much! Will take a good look a that.  
> **username_0**: Sure does, @RedGrittyBrick !  

---

## 0 answers

