diff --git a/packages/bcp/src/cosmwasmcodec.spec.ts b/packages/bcp/src/cosmwasmcodec.spec.ts index 405a2db8..d2ceeba0 100644 --- a/packages/bcp/src/cosmwasmcodec.spec.ts +++ b/packages/bcp/src/cosmwasmcodec.spec.ts @@ -2,7 +2,7 @@ import { PostableBytes, PrehashType } from "@iov/bcp"; import { Encoding } from "@iov/encoding"; import { cosmWasmCodec } from "./cosmwasmcodec"; -import { chainId, nonce, sendTxJson, signedTxBin, signedTxJson, txId } from "./testdata.spec"; +import { chainId, nonce, sendTxJson, signedTxBin, signedTxJson } from "./testdata.spec"; const { toUtf8 } = Encoding; @@ -35,12 +35,6 @@ describe("cosmWasmCodec", () => { expect(decoded).toEqual(signedTxJson); }); - xit("generates transaction id", () => { - const id = cosmWasmCodec.identifier(signedTxJson); - expect(id).toMatch(/^[0-9A-F]{64}$/); - expect(id).toEqual(txId); - }); - it("round trip works", () => { const encoded = cosmWasmCodec.bytesToPost(signedTxJson); const decoded = cosmWasmCodec.parseBytes(encoded, chainId, nonce); diff --git a/packages/bcp/src/cosmwasmconnection.spec.ts b/packages/bcp/src/cosmwasmconnection.spec.ts index 8ef2a08e..a87b431e 100644 --- a/packages/bcp/src/cosmwasmconnection.spec.ts +++ b/packages/bcp/src/cosmwasmconnection.spec.ts @@ -17,6 +17,7 @@ import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol"; import { CosmosBech32Prefix } from "./address"; import { CosmWasmCodec, cosmWasmCodec } from "./cosmwasmcodec"; import { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection"; +import { signedTxJson, txId } from "./testdata.spec"; import { nonceToSequence } from "./types"; const { fromBase64, toHex } = Encoding; @@ -133,6 +134,17 @@ describe("CosmWasmConnection", () => { }); }); + describe("encodeTx", () => { + it("properly calculates tx hash", async () => { + pendingWithoutCosmos(); + const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens); + const postable = cosmWasmCodec.bytesToPost(signedTxJson); + const id = await connection.identifier(postable); + expect(id).toMatch(/^[0-9A-F]{64}$/); + expect(id).toEqual(txId); + }); + }); + describe("getAccount", () => { it("gets an empty account by address", async () => { pendingWithoutCosmos(); diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 7abf2904..d3c09490 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { RestClient, TxsResponse } from "@cosmwasm/sdk"; +import { RestClient, TxsResponse, unmarshalTx } from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -27,7 +27,8 @@ import { TransactionState, UnsignedTransaction, } from "@iov/bcp"; -import { Uint53 } from "@iov/encoding"; +import { Sha256 } from "@iov/crypto"; +import { Encoding, Uint53 } from "@iov/encoding"; import { DefaultValueProducer, ValueAndUpdates } from "@iov/stream"; import equal from "fast-deep-equal"; import { ReadonlyDate } from "readonly-date"; @@ -38,6 +39,8 @@ import { Caip5 } from "./caip5"; import { decodeAmount, parseTxsResponse } from "./decode"; import { accountToNonce, TokenInfo } from "./types"; +const { toHex } = Encoding; + interface ChainData { readonly chainId: ChainId; } @@ -138,6 +141,13 @@ export class CosmWasmConnection implements BlockchainConnection { return this.supportedTokens; } + public async identifier(signed: PostableBytes): Promise { + const tx = unmarshalTx(signed); + const bytes = await this.restClient.encodeTx(tx); + const hash = new Sha256(bytes).digest(); + return toHex(hash).toUpperCase() as TransactionId; + } + public async getAccount(query: AccountQuery): Promise { const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.prefix) : query.address; const { result } = await this.restClient.authAccounts(address); diff --git a/packages/bcp/types/cosmwasmconnection.d.ts b/packages/bcp/types/cosmwasmconnection.d.ts index c3c2f616..982ac756 100644 --- a/packages/bcp/types/cosmwasmconnection.d.ts +++ b/packages/bcp/types/cosmwasmconnection.d.ts @@ -45,6 +45,7 @@ export declare class CosmWasmConnection implements BlockchainConnection { height(): Promise; getToken(searchTicker: TokenTicker): Promise; getAllTokens(): Promise; + identifier(signed: PostableBytes): Promise; getAccount(query: AccountQuery): Promise; watchAccount(_account: AccountQuery): Stream; getNonce(query: AddressQuery | PubkeyQuery): Promise;