iroha-javascript icon indicating copy to clipboard operation
iroha-javascript copied to clipboard

Get transaction hash from `Client.submit()`

Open 0x009922 opened this issue 3 years ago • 1 comments

Description

Transaction hash might be needed to listen for related events after its submitting.

Currently submit methods returns Promise<void>, but it can return a shortcut to get the hash of the submitted transaction.

How it might look like

const txResult = await client.submit(...)
const hash: Uint8Array = txResult.transactionHash()

transactionHash() is a function to avoid unnecessary hash computations when it is not really needed.

0x009922 avatar Aug 17 '22 10:08 0x009922

After thinking, I'd suggest to separate transaction build from submission, because "submit then calculate hash" would not give clients time to subscribe events specifying the hash

s8sato avatar Aug 17 '22 11:08 s8sato

I guess this issue is closed after merging of #124.

Now you can compute transaction hash with ease in the following pipeline:

import { Client, computeTransactionHash, makeSignedTransaction, makeTransactionPayload } from '@iroha2/client'
import { Executable, VecInstruction } from '@iroha2/data-model'

// 0. We have a `Client`, or just a `Signer` + `Torii`
declare const client: Client

// 1. Building a TX payload with a helper or without it
const payload = makeTransactionPayload({
  executable: Executable(
    'Instructions',
    VecInstruction([
      /* ... */
    ]),
  ),
  accountId: client.signer.accountId,
  // options like `nonce` etc
})

// 2. Computing the transaction hash for our needs
const hash = computeTransactionHash(payload)
//    ^^^^

// 3. Wrapping the payload into `VersionedTransaction` with a signature
const tx = makeSignedTransaction(payload, client.signer)

// 4. Submitting it
await client.torii.submit(tx)

0x009922 avatar Sep 20 '22 03:09 0x009922