<blockquote>
<p>I don't get what I do wrong.</p>
</blockquote>
<p>That coinbase transaction has a segwit output.</p>
<p>I think you need to exclude the witness data. You need a transaction-id that is compatible with the transaction-id that would be produced by non-segwit nodes. Those non-segwit nodes receive a copy of the transaction that has the segwit data edited out (to avoid freaking them out).</p>
<hr />
<p>Probably relevant?</p>
<ul>
<li><a href="https://bitcoin.stackexchange.com/q/62044/13866">How to compute SegWit txid?</a></li>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#Transaction_ID" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#Transaction_ID</a></li>
</ul>
<blockquote>
<p>Definition of txid remains unchanged: the double SHA256 of the traditional serialization format:</p>
<pre><code>[nVersion][txins][txouts][nLockTime]
</code></pre>
<p>A new wtxid is defined: the double SHA256 of the new serialization with witness data:</p>
<pre><code>[nVersion][marker][flag][txins][txouts][witness][nLockTime]
</code></pre>
</blockquote>
<p>I think you have calculated the <code>wtxid</code> not the <code>txid</code>.</p>

CC BY-SA 4.0
2022-07-21T16:24:35.873Z
Yes you are right. When i call `bitcoin-cli decoderawtransaction`... It shows ID and HASH of the transaction which are different: <br/>```txid": "aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856",
  <br/>"hash": "b33db7a32051e9cf9f73bcbcfb2f6f454c8baa9317738f2c8a703593dd01bf44",```
Vluedo404
CC BY-SA 4.0
2022-07-21T09:23:47.157Z
2022-07-21T09:55:44.037Z
2022-07-21T09:55:44.037Z
RedGrittyBrick
<p>According to all internet sources i have found, the hash/ID of a transaction is the result of the double_sha256 algorithm that takes all raw data of the transaction. I tried to calculate it in Python but get incorrect results in recent blocks. Here ist my test script for block #745778: <a href="https://www.blockchain.com/btc/block/745778" rel="nofollow noreferrer">https://www.blockchain.com/btc/block/745778</a></p>
<pre><code>import hashlib

def d_SHA256(hex_string):
    return hashlib.sha256(hashlib.sha256(bytes.fromhex(hex_string)).digest()).digest()

# https://www.blockchain.com/btc/tx/aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856
coinbase_hash = 'aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856'

# https://blockchain.info/rawtx/aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856?format=hex
coinbase_data       = '010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff640332610b2cfabe6d6ddcb8d8f2a2ddf5191d8191cfa7aa4fd9d85c529f2ce8ed4363c1a8942f14810b10000000f09f909f092f4632506f6f6c2f6500000000000000000000000000000000000000000000000000000000000000000000000500540000000000000004b1bbbb26000000001976a914c825a1ecf2a6830c4401620c3a16f1995057c2ab88ac0000000000000000266a24aa21a9edd02fc86dcb2b66db1a5add17b3660e4046f50cde03199425f0944c7becb6546a0000000000000000266a2448617468e62698d5bdd57572ff76305ed48933e8b787a67df4319ade7d798df03c706edf00000000000000002c6a4c2952534b424c4f434b3a3c622bf845a23d0ee4927d14fe0455d8e30af6e68e7e3620aa1af6270044738b01200000000000000000000000000000000000000000000000000000000000000000730b053f'
tmp_coinbase_hash   = d_SHA256(coinbase_data)       # big-endian in bytes
my_coinbase_hash    = tmp_coinbase_hash[::-1].hex() # litte-endian in hex

print('coinb_hash:', coinbase_hash)
print('my_cb_hash:', my_coinbase_hash)

</code></pre>
<pre><code>coinb_hash: aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856
my_cb_hash: b33db7a32051e9cf9f73bcbcfb2f6f454c8baa9317738f2c8a703593dd01bf44
</code></pre>
<p>I don't get what I do wrong. Did the protocol change? Because it works for older blocks but not for recent mined blocks and I can't find any information on this issue.</p>

<bitcoin-core><transactions><miner-configuration><bitcoin-cli><transaction-id>
How to calculate a bitcoin transaction hash of a coinbase transaction in 2022?
Vluedo404
# How to calculate a bitcoin transaction hash of a coinbase transaction in 2022?

(Asked by: username_0 on 2022-07-21)

According to all internet sources i have found, the hash/ID of a transaction is the result of the double_sha256 algorithm that takes all raw data of the transaction. I tried to calculate it in Python but get incorrect results in recent blocks. Here ist my test script for block #745778: https://www.blockchain.com/btc/block/745778
import hashlib

def d_SHA256(hex_string):
    return hashlib.sha256(hashlib.sha256(bytes.fromhex(hex_string)).digest()).digest()

# https://www.blockchain.com/btc/tx/aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856
coinbase_hash = 'aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856'

# https://blockchain.info/rawtx/aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856?format=hex
coinbase_data       = '010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff640332610b2cfabe6d6ddcb8d8f2a2ddf5191d8191cfa7aa4fd9d85c529f2ce8ed4363c1a8942f14810b10000000f09f909f092f4632506f6f6c2f6500000000000000000000000000000000000000000000000000000000000000000000000500540000000000000004b1bbbb26000000001976a914c825a1ecf2a6830c4401620c3a16f1995057c2ab88ac0000000000000000266a24aa21a9edd02fc86dcb2b66db1a5add17b3660e4046f50cde03199425f0944c7becb6546a0000000000000000266a2448617468e62698d5bdd57572ff76305ed48933e8b787a67df4319ade7d798df03c706edf00000000000000002c6a4c2952534b424c4f434b3a3c622bf845a23d0ee4927d14fe0455d8e30af6e68e7e3620aa1af6270044738b01200000000000000000000000000000000000000000000000000000000000000000730b053f'
tmp_coinbase_hash   = d_SHA256(coinbase_data)       # big-endian in bytes
my_coinbase_hash    = tmp_coinbase_hash[::-1].hex() # litte-endian in hex

print('coinb_hash:', coinbase_hash)
print('my_cb_hash:', my_coinbase_hash)


coinb_hash: aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856
my_cb_hash: b33db7a32051e9cf9f73bcbcfb2f6f454c8baa9317738f2c8a703593dd01bf44

I don't get what I do wrong. Did the protocol change? Because it works for older blocks but not for recent mined blocks and I can't find any information on this issue.

**Tags:** bitcoin-core, transactions, miner-configuration, bitcoin-cli, transaction-id


---

## 1 answers

### Answer by username_1 on 2022-07-21. Score: 0 | Accepted answer

I don't get what I do wrong.

That coinbase transaction has a segwit output.
I think you need to exclude the witness data. You need a transaction-id that is compatible with the transaction-id that would be produced by non-segwit nodes. Those non-segwit nodes receive a copy of the transaction that has the segwit data edited out (to avoid freaking them out).

Probably relevant?

How to compute SegWit txid?
https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#Transaction_ID


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]


I think you have calculated the wtxid not the txid.

> **Comments:**
> **username_0**: Yes you are right. When i call `bitcoin-cli decoderawtransaction`... It shows ID and HASH of the transaction which are different: <br/>```txid": "aa7f5b5f200feac9364ec40b4a5c0cb5c291f846422dec373a71cf30c55ce856",   <br/>"hash": "b33db7a32051e9cf9f73bcbcfb2f6f454c8baa9317738f2c8a703593dd01bf44",```  

---

