hyperledger / iroha-javascript

JavaScript library for Iroha, a Distributed Ledger Technology (blockchain) platform.
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
94 stars 64 forks source link

Get transaction hash from `Client.submit()` #117

Closed 0x009922 closed 2 years ago

0x009922 commented 2 years ago

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.

s8sato commented 2 years ago

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

0x009922 commented 2 years ago

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)