diff --git a/.eslintrc.js b/.eslintrc.js index 8576e84b..ae6c05b0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,6 +25,7 @@ module.exports = { "no-console": ["warn", { allow: ["error", "info", "warn"] }], "no-param-reassign": "warn", "no-shadow": "warn", + "no-unused-vars": "off", // disabled in favour of @typescript-eslint/no-unused-vars, see https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md "prefer-const": "warn", "radix": ["warn", "always"], "spaced-comment": ["warn", "always", { line: { markers: ["/ { // 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(); + const response = await this.restClient.encodeTx(tx); + const hash = new Sha256(fromBase64(response.tx)).digest(); return toHex(hash).toUpperCase(); } diff --git a/packages/cosmwasm/src/index.ts b/packages/cosmwasm/src/index.ts index 53b22ee4..801e29f9 100644 --- a/packages/cosmwasm/src/index.ts +++ b/packages/cosmwasm/src/index.ts @@ -1,7 +1,7 @@ import * as logs from "./logs"; export { logs }; -export { RestClient, TxsResponse } from "./restclient"; +export { RestClient } from "./restclient"; export { Account, Block, diff --git a/packages/cosmwasm/src/restclient.spec.ts b/packages/cosmwasm/src/restclient.spec.ts index 39a2a765..955565d3 100644 --- a/packages/cosmwasm/src/restclient.spec.ts +++ b/packages/cosmwasm/src/restclient.spec.ts @@ -15,6 +15,7 @@ import { StdFee, StdSignature, StdTx, + TxsResponse, } from "@cosmjs/sdk38"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; @@ -28,9 +29,8 @@ import { MsgInstantiateContract, MsgStoreCode, } from "./msgs"; -import { RestClient, TxsResponse } from "./restclient"; +import { RestClient } from "./restclient"; import { SigningCosmWasmClient } from "./signingcosmwasmclient"; -import cosmoshub from "./testdata/cosmoshub.json"; import { alice, bech32AddressMatcher, @@ -746,14 +746,6 @@ describe("RestClient", () => { }); }); - describe("encodeTx", () => { - it("works for cosmoshub example", async () => { - pendingWithoutWasmd(); - const client = new RestClient(wasmd.endpoint); - expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data)); - }); - }); - describe("postTx", () => { it("can send tokens", async () => { pendingWithoutWasmd(); diff --git a/packages/cosmwasm/src/restclient.ts b/packages/cosmwasm/src/restclient.ts index 8da81232..06931bfd 100644 --- a/packages/cosmwasm/src/restclient.ts +++ b/packages/cosmwasm/src/restclient.ts @@ -1,5 +1,5 @@ import { fromBase64, fromUtf8, toHex, toUtf8 } from "@cosmjs/encoding"; -import { BroadcastMode, CosmosSdkTx, RestClient as BaseRestClient } from "@cosmjs/sdk38"; +import { BroadcastMode, RestClient as BaseRestClient } from "@cosmjs/sdk38"; import { JsonObject, Model, parseWasmData, WasmData } from "./types"; @@ -17,23 +17,6 @@ interface WasmError { readonly error: string; } -export interface TxsResponse { - readonly height: string; - readonly txhash: string; - /** 🤷‍♂️ */ - readonly codespace?: string; - /** Falsy when transaction execution succeeded. Contains error code on error. */ - readonly code?: number; - readonly raw_log: string; - readonly logs?: object; - readonly tx: CosmosSdkTx; - /** The gas limit as set by the user */ - readonly gas_wanted?: string; - /** The gas used by the execution */ - readonly gas_used?: string; - readonly timestamp: string; -} - export interface CodeInfo { readonly id: number; /** Bech32 account address */ diff --git a/packages/cosmwasm/types/index.d.ts b/packages/cosmwasm/types/index.d.ts index 047f950e..85de7876 100644 --- a/packages/cosmwasm/types/index.d.ts +++ b/packages/cosmwasm/types/index.d.ts @@ -1,6 +1,6 @@ import * as logs from "./logs"; export { logs }; -export { RestClient, TxsResponse } from "./restclient"; +export { RestClient } from "./restclient"; export { Account, Block, diff --git a/packages/cosmwasm/types/restclient.d.ts b/packages/cosmwasm/types/restclient.d.ts index 7862f463..bfee9ec9 100644 --- a/packages/cosmwasm/types/restclient.d.ts +++ b/packages/cosmwasm/types/restclient.d.ts @@ -1,21 +1,5 @@ -import { BroadcastMode, CosmosSdkTx, RestClient as BaseRestClient } from "@cosmjs/sdk38"; +import { BroadcastMode, RestClient as BaseRestClient } from "@cosmjs/sdk38"; import { JsonObject, Model } from "./types"; -export interface TxsResponse { - readonly height: string; - readonly txhash: string; - /** 🤷‍♂️ */ - readonly codespace?: string; - /** Falsy when transaction execution succeeded. Contains error code on error. */ - readonly code?: number; - readonly raw_log: string; - readonly logs?: object; - readonly tx: CosmosSdkTx; - /** The gas limit as set by the user */ - readonly gas_wanted?: string; - /** The gas used by the execution */ - readonly gas_used?: string; - readonly timestamp: string; -} export interface CodeInfo { readonly id: number; /** Bech32 account address */ diff --git a/packages/sdk38/src/cosmosclient.ts b/packages/sdk38/src/cosmosclient.ts index 5b43dc97..fc6fd5b5 100644 --- a/packages/sdk38/src/cosmosclient.ts +++ b/packages/sdk38/src/cosmosclient.ts @@ -181,8 +181,8 @@ export class CosmosClient { */ 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(); + const response = await this.restClient.encodeTx(tx); + const hash = new Sha256(fromBase64(response.tx)).digest(); return toHex(hash).toUpperCase(); } diff --git a/packages/sdk38/src/index.ts b/packages/sdk38/src/index.ts index 50ca53e3..332b6398 100644 --- a/packages/sdk38/src/index.ts +++ b/packages/sdk38/src/index.ts @@ -24,6 +24,7 @@ export { AuthAccountsResponse, BlockResponse, BroadcastMode, + EncodeTxResponse, PostTxsResponse, NodeInfoResponse, RestClient, diff --git a/packages/sdk38/src/restclient.spec.ts b/packages/sdk38/src/restclient.spec.ts index bdaea11a..f2544e1e 100644 --- a/packages/sdk38/src/restclient.spec.ts +++ b/packages/sdk38/src/restclient.spec.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { fromBase64 } from "@cosmjs/encoding"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; @@ -523,7 +522,12 @@ describe("RestClient", () => { it("works for cosmoshub example", async () => { pendingWithoutWasmd(); const client = new RestClient(wasmd.endpoint); - expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data)); + const response = await client.encodeTx(cosmoshub.tx); + expect(response).toEqual( + jasmine.objectContaining({ + tx: cosmoshub.tx_data, + }), + ); }); }); diff --git a/packages/sdk38/src/restclient.ts b/packages/sdk38/src/restclient.ts index 7404c807..0cf8e62a 100644 --- a/packages/sdk38/src/restclient.ts +++ b/packages/sdk38/src/restclient.ts @@ -1,4 +1,3 @@ -import { fromBase64 } from "@cosmjs/encoding"; import { isNonNullObject } from "@cosmjs/utils"; import axios, { AxiosError, AxiosInstance } from "axios"; @@ -147,8 +146,9 @@ export interface PostTxsResponse { readonly gas_used?: string; } -interface EncodeTxResponse { - // base64-encoded amino-binary encoded representation +/** A reponse from the /txs/encode endpoint */ +export interface EncodeTxResponse { + /** base64-encoded amino-binary encoded representation */ readonly tx: string; } @@ -289,12 +289,12 @@ export class RestClient { } /** returns the amino-encoding of the transaction performed by the server */ - public async encodeTx(tx: CosmosSdkTx): Promise { + public async encodeTx(tx: CosmosSdkTx): Promise { const responseData = await this.post("/txs/encode", tx); if (!responseData.tx) { throw new Error("Unexpected response data format"); } - return fromBase64((responseData as EncodeTxResponse).tx); + return responseData as EncodeTxResponse; } /** diff --git a/packages/sdk38/types/index.d.ts b/packages/sdk38/types/index.d.ts index 7a9de872..26f37186 100644 --- a/packages/sdk38/types/index.d.ts +++ b/packages/sdk38/types/index.d.ts @@ -22,6 +22,7 @@ export { AuthAccountsResponse, BlockResponse, BroadcastMode, + EncodeTxResponse, PostTxsResponse, NodeInfoResponse, RestClient, diff --git a/packages/sdk38/types/restclient.d.ts b/packages/sdk38/types/restclient.d.ts index d27863db..830457cb 100644 --- a/packages/sdk38/types/restclient.d.ts +++ b/packages/sdk38/types/restclient.d.ts @@ -125,6 +125,11 @@ export interface PostTxsResponse { /** The gas used by the execution */ readonly gas_used?: string; } +/** A reponse from the /txs/encode endpoint */ +export interface EncodeTxResponse { + /** base64-encoded amino-binary encoded representation */ + readonly tx: string; +} /** * The mode used to send transaction * @@ -162,7 +167,7 @@ export declare class RestClient { txById(id: string): Promise; txsQuery(query: string): Promise; /** returns the amino-encoding of the transaction performed by the server */ - encodeTx(tx: CosmosSdkTx): Promise; + encodeTx(tx: CosmosSdkTx): Promise; /** * Broadcasts a signed transaction to into the transaction pool. * Depending on the RestClient's broadcast mode, this might or might