Add CosmWasmClient.getIdentifier

This commit is contained in:
Simon Warta 2020-02-12 09:46:58 +01:00
parent 3c59a68b3a
commit 331702638b
3 changed files with 26 additions and 1 deletions

View File

@ -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();

View File

@ -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<string> {
// 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.
*

View File

@ -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<StdSignature>;
}
@ -25,6 +25,10 @@ export declare class CosmWasmClient {
private get signCallback();
private constructor();
chainId(): Promise<string>;
/**
* Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID)
*/
getIdentifier(tx: CosmosSdkTx): Promise<string>;
/**
* Returns account number and sequence.
*