WIP: test identifier via rest client amino encoding

This commit is contained in:
Ethan Frey 2020-02-03 20:25:08 +01:00
parent b99340bf76
commit 449a83120a
4 changed files with 26 additions and 9 deletions

View File

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

View File

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

View File

@ -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<TransactionId> {
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<Account | undefined> {
const address = isPubkeyQuery(query) ? pubkeyToAddress(query.pubkey, this.prefix) : query.address;
const { result } = await this.restClient.authAccounts(address);

View File

@ -45,6 +45,7 @@ export declare class CosmWasmConnection implements BlockchainConnection {
height(): Promise<number>;
getToken(searchTicker: TokenTicker): Promise<Token | undefined>;
getAllTokens(): Promise<readonly Token[]>;
identifier(signed: PostableBytes): Promise<TransactionId>;
getAccount(query: AccountQuery): Promise<Account | undefined>;
watchAccount(_account: AccountQuery): Stream<Account | undefined>;
getNonce(query: AddressQuery | PubkeyQuery): Promise<Nonce>;