Merge pull request #231 from CosmWasm/cleanup-api-types

Cleanup some REST API types
This commit is contained in:
Will Clark 2020-06-23 12:29:25 +02:00 committed by GitHub
commit 7eb6dc42d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 30 additions and 59 deletions

View File

@ -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: ["/ <reference"] } }],

View File

@ -199,8 +199,8 @@ export class CosmWasmClient {
*/
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();
const response = await this.restClient.encodeTx(tx);
const hash = new Sha256(fromBase64(response.tx)).digest();
return toHex(hash).toUpperCase();
}

View File

@ -1,7 +1,7 @@
import * as logs from "./logs";
export { logs };
export { RestClient, TxsResponse } from "./restclient";
export { RestClient } from "./restclient";
export {
Account,
Block,

View File

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

View File

@ -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 */

View File

@ -1,6 +1,6 @@
import * as logs from "./logs";
export { logs };
export { RestClient, TxsResponse } from "./restclient";
export { RestClient } from "./restclient";
export {
Account,
Block,

View File

@ -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 */

View File

@ -181,8 +181,8 @@ export class CosmosClient {
*/
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();
const response = await this.restClient.encodeTx(tx);
const hash = new Sha256(fromBase64(response.tx)).digest();
return toHex(hash).toUpperCase();
}

View File

@ -24,6 +24,7 @@ export {
AuthAccountsResponse,
BlockResponse,
BroadcastMode,
EncodeTxResponse,
PostTxsResponse,
NodeInfoResponse,
RestClient,

View File

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

View File

@ -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<Uint8Array> {
public async encodeTx(tx: CosmosSdkTx): Promise<EncodeTxResponse> {
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;
}
/**

View File

@ -22,6 +22,7 @@ export {
AuthAccountsResponse,
BlockResponse,
BroadcastMode,
EncodeTxResponse,
PostTxsResponse,
NodeInfoResponse,
RestClient,

View File

@ -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<TxsResponse>;
txsQuery(query: string): Promise<SearchTxsResponse>;
/** returns the amino-encoding of the transaction performed by the server */
encodeTx(tx: CosmosSdkTx): Promise<Uint8Array>;
encodeTx(tx: CosmosSdkTx): Promise<EncodeTxResponse>;
/**
* Broadcasts a signed transaction to into the transaction pool.
* Depending on the RestClient's broadcast mode, this might or might