Can we calculate transaction hash before submitting? How?

I’m currently working on an app that submits transactions to a cardano-node, and I’m looking for the best or most efficient way to confirm if the transaction recently submitted actually went through.

I see that projects like cntools verify submitted transactions only by checking the before and after balances. And then later just taking the hash of the latest trx.

But I’ve had a few cases already where this kind of checking fails. And this easily fails… like if 2 similar transactions produce the same result on the address that’s being checked.

So if it is possible, I’m thinking it would be a lot better to just calculate the actual transaction hash prior to submitting it and then later check for that transaction hash to confirm if it has been finalized.

If this is possible, can someone point me in the right direction? I’m planning to implement this in a nodejs app but any sample logic or spec should help…

1 Like

You can view signed transaction via CLI - using cardano-cli transaction view ... (or query specific utxo instead of address).

Such checks are not essential for basic wallet operating wrapper scripts, but for example exchanges often verify specific UTXOs.

1 Like

Thanks! I’ll dig into this.

So… the result of this looks like the following:

auxiliary data: null
auxiliary data hash: null
certificates: []
era: Mary
fee: 180285
inputs:
- 36c8bab969639292eb7eb874fb8c7cd73985728c6d89400e0c2773490ad991e6#1
mint:
  lovelace: 0
  policies: {}
outputs:
- address:
    Bech32: addr1qx8etwdj3vpky8nenxldxzc4gm7llpddad3szf99puffyvdaazlltw7cf2r36fdr47a3pwsu9yjhk7zjud5zwwmt0crqyjynkm
    credential:
      key hash: 951d46072977ce7f3b0dea694ca75b42bb4e65cb8803f5f6b39d53fa
    network: Mainnet
    stake reference: StakeRefBase (KeyHashObj (KeyHash "f612f34ffac18ab70a3604d4e5b912c9b82e62a8337512e177627588"))
  amount:
    lovelace: 8000000
    policies: {}
- address:
    Bech32: addr1q9yxcf5ffaq5nxrsfflfh0tq0fgzgqdmyghyflq936dmyldrqd7gxj690rjeljhg8d9cckd7zwq0pgv7j2e86km69wusfgdadn
    credential:
      key hash: 3235b5de9d5d5ed88f5e40f19ee77429d0754b4d6045d1edece1c004
    network: Mainnet
    stake reference: StakeRefBase (KeyHashObj (KeyHash "ee15aaabc6508905cabb26fd1b3d1e2c748b7e3f9d68d7c02d864085"))
  amount:
    lovelace: 40143633
    policies:
      407ca40d92693cb737f1cfedb98023ae753038d8c6ca9e02483d79c6:
        '524e52': 46875050
      6814db7e0dd9fc618dfa0583e9019ba68b3e993f8c7482c8a6fb59f2:
        '535347': 427000
update: null
validity interval:
  invalid before: null
  invalid hereafter: null
withdrawals: []

How can we get the trx hash from this? The outputs only contain the receiving addresses and key hashes. What else needs to be done to arrive at the trx hash / utxo hash for the transaction?

Just use txid instead of view (or just cardano-cli transaction --help for complete list of subcommands against transaction).

3 Likes

This is it! :+1:

For those who come here in the future looking for the same thing:

cardano-cli transaction txid --tx-file tx.signed

Sample output (trx hash):

5fe8709128e0db69b86d3abe3215a3bdaf4c804c1a413f1725ff2c5cc546263c

This is not so clear in the official docs, especially if you’re new to this. :disappointed:

3 Likes

For most command line tools (across the industry), you can check the usage instructions using --help or -h

1 Like

This, I already know. There is also the same description for the txid subcommand in the docs.

Looking back, it does seem very stupid of me not to have even tried it first. :sweat_smile: But it just wasn’t that apparent to me then.