0

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?

6
  • 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? Commented Apr 10 at 10:45
  • @VojtěchStrnad just wanted to know how txid is calculated.
    – yuivc
    Commented Apr 10 at 14:13
  • @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
    Commented Apr 10 at 14:35
  • I converted my comments into an answer. Commented Apr 10 at 15:24
  • @RedGrittyBrick, how about segwit transactions
    – yuivc
    Commented Apr 11 at 6:05

1 Answer 1

0

Calculating TxID

  1. 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)
  2. 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:

1
  • Thanks for this!
    – yuivc
    Commented Apr 10 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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