<p>That's a SegWit transaction, not all of it is part of the transaction hash preimage (see <a href="https://bitcoin.stackexchange.com/questions/113697/what-are-the-parts-of-a-bitcoin-transaction-in-segwit-format">here</a> for details). You have to exclude the SegWit parts. This Bash one-liner returns the correct TXID:</p>
<pre><code>echo '0100000001189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c200000000000ffffffff0240420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf500000000' | tr -d '\n' | xxd -r -p | sha256sum | grep -oE '^[0-9a-f]+' | xxd -r -p | sha256sum | grep -oE '^[0-9a-f]+' | tac -rs '..'; echo
</code></pre>
<p>Output</p>
<p><code>9d2d91b764ba2559b098cdd30525e2160bb15c6e27d6869e7b121a9037d8bd86</code></p>
<h3>Explanation</h3>
<p>Your TX can be disassembled as follows, and you have to hash everything <strong>except the bold parts</strong>:</p>
<p>TX version: <code>01000000</code><br />
<strong>Witness marker and flag: <code>0001</code></strong><br />
Input count: <code>01</code><br />
Prevout reference: <code>189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c2000000000</code><br />
Unlocking script length: <code>00</code><br />
<em>Unlocking script: EMPTY</em> (note: non-SegWit inputs will have script length &gt;0 and have the unlocking script here instead of in the witness, so for non-SegWit this will not be EMPTY)<br />
Sequence: <code>ffffffff</code><br />
Output count: <code>02</code><br />
Output 01: <code>40420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac</code><br />
Output 02: <code>6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf5</code><br />
<strong>Witness count: <code>02</code></strong><br />
<strong>Witness 01: <code>473044022001609cd43eb8e9b8f8438eded9f6b10bad32efd7620724ccd2ed5277c0c6a3ae02200f0c1c3f4c409ada536d2363a2d8bdad418df67fed9b36bfa4482bd9985bf39601</code></strong><br />
<strong>Witness 02: <code>2102ee3c98964dd1bfe13bee16c0b95fcf8281f12c5885d1fcb7b59fc2cb01ca7632</code></strong><br />
TX locktime: <code>00000000</code></p>
<p>Concatenating only the non-SegWit parts gives us the TX hash preimage: <code>0100000001189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c200000000000ffffffff0240420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf500000000</code></p>

CC BY-SA 4.0
2023-11-07T11:45:08.920Z
Thank you bca-0353f40e for you answer, would you be able to edit my python code so that all is needed from the user is the TXID? Thanks
RabbitHole6565
CC BY-SA 4.0
2023-11-07T11:48:31.787Z
That's too much work for me. You can figure it out, just parse the transaction according to format specification and skip over SegWit parts.
bca-0353f40e
CC BY-SA 4.0
2023-11-07T12:26:28.563Z
Do you know of any API's which will return a TX hash preimage, from a TXID?
RabbitHole6565
CC BY-SA 4.0
2023-11-07T13:42:02.107Z
Don't know, but maybe you can look for a library. I found PyPI which implements "tx_hash : (hex or bin tx) -> hash" https://pypi.org/project/pycryptotools/0.2.1/
bca-0353f40e
CC BY-SA 4.0
2023-11-07T13:42:57.710Z
Source seems to have been deleted, here's a mirror: https://bitcointalk.org/index.php?topic=5445428.0

Link to tx_hash implementation: https://github.com/ZenulAbidin/pycryptotools/blob/main/pycryptotools/transaction.py#L278
bca-0353f40e
CC BY-SA 4.0
2023-11-07T11:37:06.593Z
2023-11-07T13:48:10.800Z
2023-11-07T13:48:10.800Z
bca-0353f40e
<p>there, I've already looked at other similar posts on here but with little luck. All im trying to do is calculate the TXID of a transaction after downloading the transaction in hexadecimal from a blockchain explorer VIA an API.</p>
<p>The problem is that the TXID calculated Isn't the TXID we expect.</p>
<p>Here's the code, I'm using bitcoin-utils</p>
<pre><code>
from bitcoinutils.transactions import Transaction
import requests


# Fetch the raw transaction data from the blockchain.info API
tx_hash = '9d2d91b764ba2559b098cdd30525e2160bb15c6e27d6869e7b121a9037d8bd86'
api_response = requests.get(f'https://blockchain.info/rawtx/{tx_hash}?format=hex')

if api_response.status_code == 200:
    raw_tx_hex = str(api_response.text)
else:
    print(f&quot;Error fetching transaction data: {api_response.status_code}&quot;)


mytrans = Transaction.from_raw(raw_tx_hex)
print(mytrans.get_txid())

</code></pre>

<python><transaction-id>
How to compute a TXID of any bitcoin transaction in Python
RabbitHole6565
# How to compute a TXID of any bitcoin transaction in Python

(Asked by: username_0 on 2023-11-07)

there, I've already looked at other similar posts on here but with little luck. All im trying to do is calculate the TXID of a transaction after downloading the transaction in hexadecimal from a blockchain explorer VIA an API.
The problem is that the TXID calculated Isn't the TXID we expect.
Here's the code, I'm using bitcoin-utils

from bitcoinutils.transactions import Transaction
import requests


# Fetch the raw transaction data from the blockchain.info API
tx_hash = '9d2d91b764ba2559b098cdd30525e2160bb15c6e27d6869e7b121a9037d8bd86'
api_response = requests.get(f'https://blockchain.info/rawtx/{tx_hash}?format=hex')

if api_response.status_code == 200:
    raw_tx_hex = str(api_response.text)
else:
    print(f"Error fetching transaction data: {api_response.status_code}")


mytrans = Transaction.from_raw(raw_tx_hex)
print(mytrans.get_txid())

**Tags:** python, transaction-id


---

## 1 answers

### Answer by username_1 on 2023-11-07. Score: 3

That's a SegWit transaction, not all of it is part of the transaction hash preimage (see here for details). You have to exclude the SegWit parts. This Bash one-liner returns the correct TXID:
echo '0100000001189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c200000000000ffffffff0240420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf500000000' | tr -d '\n' | xxd -r -p | sha256sum | grep -oE '^[0-9a-f]+' | xxd -r -p | sha256sum | grep -oE '^[0-9a-f]+' | tac -rs '..'; echo

Output
9d2d91b764ba2559b098cdd30525e2160bb15c6e27d6869e7b121a9037d8bd86
Explanation
Your TX can be disassembled as follows, and you have to hash everything except the bold parts:
TX version: 01000000
Witness marker and flag: 0001
Input count: 01
Prevout reference: 189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c2000000000
Unlocking script length: 00
Unlocking script: EMPTY (note: non-SegWit inputs will have script length >0 and have the unlocking script here instead of in the witness, so for non-SegWit this will not be EMPTY)
Sequence: ffffffff
Output count: 02
Output 01: 40420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac
Output 02: 6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf5
Witness count: 02
Witness 01: 473044022001609cd43eb8e9b8f8438eded9f6b10bad32efd7620724ccd2ed5277c0c6a3ae02200f0c1c3f4c409ada536d2363a2d8bdad418df67fed9b36bfa4482bd9985bf39601
Witness 02: 2102ee3c98964dd1bfe13bee16c0b95fcf8281f12c5885d1fcb7b59fc2cb01ca7632
TX locktime: 00000000
Concatenating only the non-SegWit parts gives us the TX hash preimage: 0100000001189a3f6fbfb614264c8176cd2d3836882afc3c0940d698d6b481f3e5cb733c200000000000ffffffff0240420f00000000001976a914341b568f59229818c460b1795ad48cd78895c54d88ac6eeefa4a00000000160014d701ce5e753bd9454d343c8a3b86d84a3c34dbf500000000

> **Comments:**
> **username_0**: Thank you username_1 for you answer, would you be able to edit my python code so that all is needed from the user is the TXID? Thanks  
> **username_1**: That's too much work for me. You can figure it out, just parse the transaction according to format specification and skip over SegWit parts.  
> **username_0**: Do you know of any API's which will return a TX hash preimage, from a TXID?  
> **username_1**: Don't know, but maybe you can look for a library. I found PyPI which implements "tx_hash : (hex or bin tx) -> hash" https://pypi.org/project/pycryptotools/0.2.1/  
> **username_1**: Source seems to have been deleted, here's a mirror: https://bitcointalk.org/index.php?topic=5445428.0  Link to tx_hash implementation: https://github.com/ZenulAbidin/pycryptotools/blob/main/pycryptotools/transaction.py#L278  

---

