From b99340bf76c57291885a80a32c851bac280f8427 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 3 Feb 2020 20:22:02 +0100 Subject: [PATCH] Add encode logic to rest client --- packages/sdk/src/restclient.ts | 19 +++++++++++++++++-- packages/sdk/types/restclient.d.ts | 9 +++++++-- packages/sdk/types/types.d.ts | 8 ++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index f715393e..e3794ee1 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -1,7 +1,7 @@ import { Encoding } from "@iov/encoding"; import axios, { AxiosInstance } from "axios"; -import { AminoTx, BaseAccount, isAminoStdTx } from "./types"; +import { AminoTx, BaseAccount, isAminoStdTx, StdTx } from "./types"; const { fromUtf8 } = Encoding; @@ -66,13 +66,19 @@ interface PostTxsResponse { readonly raw_log?: string; } +interface EncodeTxResponse { + // base64-encoded amino-binary encoded representation + readonly tx: string; +} + type RestClientResponse = | NodeInfoResponse | BlocksResponse | AuthAccountsResponse | TxsResponse | SearchTxsResponse - | PostTxsResponse; + | PostTxsResponse + | EncodeTxResponse; type BroadcastMode = "block" | "sync" | "async"; @@ -133,6 +139,15 @@ export class RestClient { return responseData as BlocksResponse; } + // encodeTx returns the amino-encoding of the transaction + public async encodeTx(tx: StdTx): Promise { + const responseData = await this.post("/txs/encode", tx); + if (!(responseData as any).tx) { + throw new Error("Unexpected response data format"); + } + return Encoding.fromBase64((responseData as EncodeTxResponse).tx); + } + public async authAccounts(address: string, height?: string): Promise { const path = height === undefined ? `/auth/accounts/${address}` : `/auth/accounts/${address}?tx.height=${height}`; diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index d952d480..02c0fa53 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -1,4 +1,4 @@ -import { AminoTx, BaseAccount } from "./types"; +import { AminoTx, BaseAccount, StdTx } from "./types"; interface NodeInfo { readonly network: string; } @@ -50,13 +50,17 @@ interface PostTxsResponse { readonly code?: number; readonly raw_log?: string; } +interface EncodeTxResponse { + readonly tx: string; +} declare type RestClientResponse = | NodeInfoResponse | BlocksResponse | AuthAccountsResponse | TxsResponse | SearchTxsResponse - | PostTxsResponse; + | PostTxsResponse + | EncodeTxResponse; declare type BroadcastMode = "block" | "sync" | "async"; export declare class RestClient { private readonly client; @@ -67,6 +71,7 @@ export declare class RestClient { nodeInfo(): Promise; blocksLatest(): Promise; blocks(height: number): Promise; + encodeTx(tx: StdTx): Promise; authAccounts(address: string, height?: string): Promise; txs(query: string): Promise; txsById(id: string): Promise; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index a5e5672d..4f1bb326 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -8,6 +8,10 @@ export interface StdTx { readonly signatures: ReadonlyArray; readonly memo: string | undefined; } +export declare type AminoTx = Tx & { + readonly value: StdTx; +}; +export declare function isAminoStdTx(txValue: unknown): txValue is StdTx; export interface Msg { readonly type: string; readonly value: MsgSend | unknown; @@ -44,7 +48,3 @@ export interface BaseAccount { readonly account_number: string; readonly sequence: string; } -export declare type AminoTx = Tx & { - readonly value: StdTx; -}; -export declare function isAminoStdTx(txValue: unknown): txValue is StdTx;