<h2>Calculating TxID</h2>
<ol>
<li><p>convert data into <a href="https://bitcoin.stackexchange.com/a/113632/13866">Bitcoin's pre-segwit network serialisation</a>. Taking care not to make any of the most common mistakes:</p>
<ul>
<li>Encode counts/lengths using the <a href="https://bitcoin.stackexchange.com/q/112712/13866">Bitcoin variable-length Compact-Integer format (sometimes called VarInt)</a> and</li>
<li>Take into account Bitcoin's inconsistent and non-standard use of Endianness (byte order).</li>
<li>Never convert to, or use, hexadecimal string representations (until you have a final TxId result that you want to present that way). Because <em>f(&quot;B5&quot;) ≠ f(0xB5)</em></li>
</ul>
</li>
<li><p>Calculate SHA256(SHA256(serialised-data)).</p>
</li>
</ol>
<p><a href="https://go.dev/play/p/VBjUcstKsSP" rel="nofollow noreferrer">Example program</a></p>
<pre><code>TxID  88877248B629DA0DF5A8E31FD0D1FBECAFE48A3D1FC65EAFF141DDD9BDB3361A
</code></pre>
<hr />
<h2>A note about Segregated Witness (Segwit)</h2>
<p>When Segwit was introduced, it was designed to be <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#user-content-Backward_compatibility" rel="nofollow noreferrer">backwards compatible</a> with pre-Segwit nodes. So, when sending data to nodes that don't signal Segwit support, Segwit nodes send them data in the pre-Segwit network serialisation, not the Segwit serialisation used for sending data to Segwit nodes. To ensure that both Segwit and pre-Segwit nodes use the same TxID, <a href="https://en.bitcoin.it/wiki/BIP_0141#Transaction_ID" rel="nofollow noreferrer">the pre-Segwit serialisation was used to calculate the TxID even for Segwit transactions</a>. This is what is always used in transaction input references. This makes sure that both Segwit and pre-Segwit nodes maintain the same view of Bitcoin network state, the same view of the UTXO set.</p>
<p>There is a separate WTxID based on a hash of the Segwit serialisation. As BIP 141 says:</p>
<blockquote>
<h3>Transaction ID</h3>
<p>A new data structure, witness, is defined. Each transaction will have 2 IDs.</p>
<p>Definition of txid remains unchanged: the double SHA256 of the traditional serialization format:</p>
</blockquote>

CC BY-SA 4.0
2024-04-10T15:38:54.813Z
Thanks for this!
yuivc
CC BY-SA 4.0
2024-04-10T15:23:21.283Z
2024-04-11T20:56:31.510Z
2024-04-11T20:56:31.510Z
RedGrittyBrick
<p>Consider this is the transaction.json file:</p>
<pre><code>{
  &quot;version&quot;: 1,
  &quot;locktime&quot;: 0,
  &quot;vin&quot;: [
    {
      &quot;txid&quot;: &quot;5d1ad7d75b89e7051f17254f7e7342d70fcb502539ddb2ac1b97d5710d5fec37&quot;,
      &quot;vout&quot;: 1,
      &quot;prevout&quot;: {
        &quot;scriptpubkey&quot;: &quot;76a9140a7a059306f69dec3d4658c618d4dd33a4b288bb88ac&quot;,
        &quot;scriptpubkey_asm&quot;: &quot;OP_DUP OP_HASH160 OP_PUSHBYTES_20 0a7a059306f69dec3d4658c618d4dd33a4b288bb OP_EQUALVERIFY OP_CHECKSIG&quot;,
        &quot;scriptpubkey_type&quot;: &quot;p2pkh&quot;,
        &quot;scriptpubkey_address&quot;: &quot;1xPw97SFdG39TyTon7ZFeon7SgEa77HPa&quot;,
        &quot;value&quot;: 167972
      },
      &quot;scriptsig&quot;: &quot;473044022073cb5faf80c08efe1dc3b2bfbabe93f42ab16aeabe3c091ddfc795a30033ee74022017ac1f308e6ec9eb8360c181f11ebfb0f23d1554baeae47a02628f4ef778a2b7012103f498484d89d98539e4260c378584f3fc41f63e627c86b7001d0845d374d80991&quot;,
      &quot;scriptsig_asm&quot;: &quot;OP_PUSHBYTES_71 3044022073cb5faf80c08efe1dc3b2bfbabe93f42ab16aeabe3c091ddfc795a30033ee74022017ac1f308e6ec9eb8360c181f11ebfb0f23d1554baeae47a02628f4ef778a2b701 OP_PUSHBYTES_33 03f498484d89d98539e4260c378584f3fc41f63e627c86b7001d0845d374d80991&quot;,
      &quot;is_coinbase&quot;: false,
      &quot;sequence&quot;: 4294967295
    }
  ],
  &quot;vout&quot;: [
    {
      &quot;scriptpubkey&quot;: &quot;76a91437ca544d6159d8602780b854f196637f94a734fa88ac&quot;,
      &quot;scriptpubkey_asm&quot;: &quot;OP_DUP OP_HASH160 OP_PUSHBYTES_20 37ca544d6159d8602780b854f196637f94a734fa OP_EQUALVERIFY OP_CHECKSIG&quot;,
      &quot;scriptpubkey_type&quot;: &quot;p2pkh&quot;,
      &quot;scriptpubkey_address&quot;: &quot;165zYdxqvNELWdGfrSM6yrqkqBEXKeTZ5A&quot;,
      &quot;value&quot;: 164132
    }
  ]
}
</code></pre>
<p>How can we calculate the txid from this json file?</p>

CC BY-SA 4.0
2024-04-10T10:45:00.120Z
Hello Yuvraj, out of curiosity, what do you need this for and how did you obtain the transaction.json file without knowing the TXID in the first place?
Vojtěch Strnad
CC BY-SA 4.0
2024-04-10T14:13:03.643Z
@VojtěchStrnad just wanted to know how txid is calculated.
yuivc
CC BY-SA 4.0
2024-04-10T14:35:34.350Z
@RedGrittyBrick, basically I need to calculate the input counts, output counts and then convert all of then in binary first and then in hex code later. Lastly I need to double sha256 to get the txid right?
yuivc
CC BY-SA 4.0
2024-04-10T15:24:13.437Z
I converted my comments into an answer.
RedGrittyBrick
CC BY-SA 4.0
2024-04-11T06:05:52.483Z
@RedGrittyBrick, how about segwit transactions
yuivc
CC BY-SA 4.0
2024-04-11T08:18:10.530Z
@YuvrajChhetri See new addendum to answer.
RedGrittyBrick
<bitcoin-core><segregated-witness><transaction-id>
how to create txid from transaction.json
yuivc
# how to create txid from transaction.json

(Asked by: username_0 on 2024-04-10)

Consider this is the transaction.json file:
{
  "version": 1,
  "locktime": 0,
  "vin": [
    {
      "txid": "5d1ad7d75b89e7051f17254f7e7342d70fcb502539ddb2ac1b97d5710d5fec37",
      "vout": 1,
      "prevout": {
        "scriptpubkey": "76a9140a7a059306f69dec3d4658c618d4dd33a4b288bb88ac",
        "scriptpubkey_asm": "OP_DUP OP_HASH160 OP_PUSHBYTES_20 0a7a059306f69dec3d4658c618d4dd33a4b288bb OP_EQUALVERIFY OP_CHECKSIG",
        "scriptpubkey_type": "p2pkh",
        "scriptpubkey_address": "1xPw97SFdG39TyTon7ZFeon7SgEa77HPa",
        "value": 167972
      },
      "scriptsig": "473044022073cb5faf80c08efe1dc3b2bfbabe93f42ab16aeabe3c091ddfc795a30033ee74022017ac1f308e6ec9eb8360c181f11ebfb0f23d1554baeae47a02628f4ef778a2b7012103f498484d89d98539e4260c378584f3fc41f63e627c86b7001d0845d374d80991",
      "scriptsig_asm": "OP_PUSHBYTES_71 3044022073cb5faf80c08efe1dc3b2bfbabe93f42ab16aeabe3c091ddfc795a30033ee74022017ac1f308e6ec9eb8360c181f11ebfb0f23d1554baeae47a02628f4ef778a2b701 OP_PUSHBYTES_33 03f498484d89d98539e4260c378584f3fc41f63e627c86b7001d0845d374d80991",
      "is_coinbase": false,
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "scriptpubkey": "76a91437ca544d6159d8602780b854f196637f94a734fa88ac",
      "scriptpubkey_asm": "OP_DUP OP_HASH160 OP_PUSHBYTES_20 37ca544d6159d8602780b854f196637f94a734fa OP_EQUALVERIFY OP_CHECKSIG",
      "scriptpubkey_type": "p2pkh",
      "scriptpubkey_address": "165zYdxqvNELWdGfrSM6yrqkqBEXKeTZ5A",
      "value": 164132
    }
  ]
}

How can we calculate the txid from this json file?

**Tags:** bitcoin-core, segregated-witness, transaction-id

> **Comments:**
> **username_1**: Hello Yuvraj, out of curiosity, what do you need this for and how did you obtain the transaction.json file without knowing the TXID in the first place?  
> **username_0**: @VojtěchStrnad just wanted to know how txid is calculated.  
> **username_0**: @username_2, basically I need to calculate the input counts, output counts and then convert all of then in binary first and then in hex code later. Lastly I need to double sha256 to get the txid right?  
> **username_2**: I converted my comments into an answer.  
> **username_0**: @username_2, how about segwit transactions  
> **username_2**: @YuvrajChhetri See new addendum to answer.  

---

## 1 answers

### Answer by username_2 on 2024-04-10. Score: 0 | Accepted answer

Calculating TxID

convert data into Bitcoin's pre-segwit network serialisation. Taking care not to make any of the most common mistakes:

Encode counts/lengths using the Bitcoin variable-length Compact-Integer format (sometimes called VarInt) and
Take into account Bitcoin's inconsistent and non-standard use of Endianness (byte order).
Never convert to, or use, hexadecimal string representations (until you have a final TxId result that you want to present that way). Because f("B5") ≠ f(0xB5)


Calculate SHA256(SHA256(serialised-data)).


Example program
TxID  88877248B629DA0DF5A8E31FD0D1FBECAFE48A3D1FC65EAFF141DDD9BDB3361A


A note about Segregated Witness (Segwit)
When Segwit was introduced, it was designed to be backwards compatible with pre-Segwit nodes. So, when sending data to nodes that don't signal Segwit support, Segwit nodes send them data in the pre-Segwit network serialisation, not the Segwit serialisation used for sending data to Segwit nodes. To ensure that both Segwit and pre-Segwit nodes use the same TxID, the pre-Segwit serialisation was used to calculate the TxID even for Segwit transactions. This is what is always used in transaction input references. This makes sure that both Segwit and pre-Segwit nodes maintain the same view of Bitcoin network state, the same view of the UTXO set.
There is a separate WTxID based on a hash of the Segwit serialisation. As BIP 141 says:

Transaction ID
A new data structure, witness, is defined. Each transaction will have 2 IDs.
Definition of txid remains unchanged: the double SHA256 of the traditional serialization format:

> **Comments:**
> **username_0**: Thanks for this!  

---

