diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 27ff42e3..80a5b9a2 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -3,6 +3,7 @@ import { makeSignBytes, marshalTx } from "./encoding"; import { findAttribute } from "./logs"; import { Secp256k1Pen } from "./pen"; import { RestClient } from "./restclient"; +import cosmoshub from "./testdata/cosmoshub.json"; import { getRandomizedHackatom, makeRandomAddress } from "./testutils.spec"; import { Coin, MsgSend, StdFee } from "./types"; @@ -59,6 +60,14 @@ describe("CosmWasmClient", () => { }); }); + describe("getIdentifier", () => { + it("works", async () => { + pendingWithoutCosmos(); + const client = CosmWasmClient.makeReadOnly(httpUrl); + expect(await client.getIdentifier(cosmoshub.tx)).toEqual(cosmoshub.id); + }); + }); + describe("postTx", () => { it("works", async () => { pendingWithoutCosmos(); diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index f1c191e7..d0ff8a94 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -1,3 +1,4 @@ +import { Sha256 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import { makeSignBytes, marshalTx } from "./encoding"; @@ -5,6 +6,7 @@ import { findAttribute, Log, parseLogs } from "./logs"; import { RestClient } from "./restclient"; import { Coin, + CosmosSdkTx, MsgExecuteContract, MsgInstantiateContract, MsgStoreCode, @@ -106,6 +108,16 @@ export class CosmWasmClient { return response.node_info.network; } + /** + * Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID) + */ + public async getIdentifier(tx: CosmosSdkTx): Promise { + // We consult the REST API because we don't have a local amino encoder + const bytes = await this.restClient.encodeTx(tx); + const hash = new Sha256(bytes).digest(); + return Encoding.toHex(hash).toUpperCase(); + } + /** * Returns account number and sequence. * diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index bee7e6a2..4fae10bd 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -1,5 +1,5 @@ import { Log } from "./logs"; -import { Coin, StdSignature } from "./types"; +import { Coin, CosmosSdkTx, StdSignature } from "./types"; export interface SigningCallback { (signBytes: Uint8Array): Promise; } @@ -25,6 +25,10 @@ export declare class CosmWasmClient { private get signCallback(); private constructor(); chainId(): Promise; + /** + * Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID) + */ + getIdentifier(tx: CosmosSdkTx): Promise; /** * Returns account number and sequence. *