Improve interface of CosmWasmConnection.identifier

This commit is contained in:
Simon Warta 2020-02-12 09:40:36 +01:00
parent e0e5f1d568
commit 3c59a68b3a
6 changed files with 22 additions and 23 deletions

View File

@ -18,7 +18,7 @@ import { assert } from "@iov/utils";
import { CosmWasmCodec } from "./cosmwasmcodec";
import { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection";
import { signedTxJson, txId } from "./testdata.spec";
import * as testdata from "./testdata.spec";
const { fromBase64, toHex } = Encoding;
@ -197,12 +197,10 @@ describe("CosmWasmConnection", () => {
describe("identifier", () => {
it("calculates tx hash from PostableBytes", async () => {
pendingWithoutCosmos();
const codec = new CosmWasmCodec(defaultPrefix, atomConfig.bankTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, atomConfig);
const postable = codec.bytesToPost(signedTxJson);
const id = await connection.identifier(postable);
const id = await connection.identifier(testdata.signedTxJson);
expect(id).toMatch(/^[0-9A-F]{64}$/);
expect(id).toEqual(txId);
expect(id).toEqual(testdata.txId);
});
});

View File

@ -1,12 +1,5 @@
/* eslint-disable @typescript-eslint/camelcase */
import {
CosmosAddressBech32Prefix,
CosmWasmClient,
RestClient,
TxsResponse,
types,
unmarshalTx,
} from "@cosmwasm/sdk";
import { CosmosAddressBech32Prefix, CosmWasmClient, RestClient, TxsResponse, types } from "@cosmwasm/sdk";
import {
Account,
AccountQuery,
@ -28,6 +21,7 @@ import {
PostableBytes,
PostTxResponse,
PubkeyQuery,
SignedTransaction,
Token,
TokenTicker,
TransactionId,
@ -46,6 +40,7 @@ import { Stream } from "xstream";
import { decodeCosmosPubkey, pubkeyToAddress } from "./address";
import { Caip5 } from "./caip5";
import { decodeAmount, parseTxsResponse } from "./decode";
import { buildSignedTx } from "./encode";
import { accountToNonce, BankToken, Erc20Token } from "./types";
const { fromAscii, toHex } = Encoding;
@ -162,8 +157,12 @@ export class CosmWasmConnection implements BlockchainConnection {
return this.supportedTokens;
}
public async identifier(signed: PostableBytes): Promise<TransactionId> {
const tx = unmarshalTx(signed);
/**
* This is a replacement for the unimplemented CosmWasmCodec.identifier. Here we have more
* context and network available, which we might use to implement the API in an async way.
*/
public async identifier(signed: SignedTransaction): Promise<TransactionId> {
const tx = buildSignedTx(signed, this.bankTokens, this.erc20Tokens);
// tslint:disable-next-line: deprecation
const bytes = await this.restClient.encodeTx(tx);
const hash = new Sha256(bytes).digest();

View File

@ -14,6 +14,7 @@ import {
PostableBytes,
PostTxResponse,
PubkeyQuery,
SignedTransaction,
Token,
TokenTicker,
TransactionId,
@ -58,7 +59,11 @@ export declare class CosmWasmConnection implements BlockchainConnection {
height(): Promise<number>;
getToken(searchTicker: TokenTicker): Promise<Token | undefined>;
getAllTokens(): Promise<readonly Token[]>;
identifier(signed: PostableBytes): Promise<TransactionId>;
/**
* This is a replacement for the unimplemented CosmWasmCodec.identifier. Here we have more
* context and network available, which we might use to implement the API in an async way.
*/
identifier(signed: SignedTransaction): Promise<TransactionId>;
getAccount(query: AccountQuery): Promise<Account | undefined>;
watchAccount(_account: AccountQuery): Stream<Account | undefined>;
getNonce(query: AddressQuery | PubkeyQuery): Promise<Nonce>;

View File

@ -221,9 +221,8 @@ describe("RestClient", () => {
describe("encodeTx", () => {
it("works for cosmoshub example", async () => {
pendingWithoutCosmos();
const tx = cosmoshub.tx.value;
const client = new RestClient(httpUrl);
expect(await client.encodeTx(tx)).toEqual(fromBase64(cosmoshub.tx_data));
expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data));
});
});

View File

@ -9,7 +9,6 @@ import {
isStdTx,
Model,
parseWasmData,
StdTx,
WasmData,
} from "./types";
@ -221,8 +220,7 @@ export class RestClient {
}
/** returns the amino-encoding of the transaction performed by the server */
public async encodeTx(stdTx: StdTx): Promise<Uint8Array> {
const tx = { type: "cosmos-sdk/StdTx", value: stdTx };
public async encodeTx(tx: CosmosSdkTx): Promise<Uint8Array> {
const responseData = await this.post("/txs/encode", tx);
if (!(responseData as any).tx) {
throw new Error("Unexpected response data format");

View File

@ -1,4 +1,4 @@
import { CodeInfo, ContractInfo, CosmosSdkAccount, CosmosSdkTx, Model, StdTx } from "./types";
import { CodeInfo, ContractInfo, CosmosSdkAccount, CosmosSdkTx, Model } from "./types";
interface NodeInfo {
readonly network: string;
}
@ -92,7 +92,7 @@ export declare class RestClient {
blocksLatest(): Promise<BlocksResponse>;
blocks(height: number): Promise<BlocksResponse>;
/** returns the amino-encoding of the transaction performed by the server */
encodeTx(stdTx: StdTx): Promise<Uint8Array>;
encodeTx(tx: CosmosSdkTx): Promise<Uint8Array>;
authAccounts(address: string, height?: string): Promise<AuthAccountsResponse>;
txs(query: string): Promise<SearchTxsResponse>;
txsById(id: string): Promise<TxsResponse>;