Add encode logic to rest client

This commit is contained in:
Ethan Frey 2020-02-03 20:22:02 +01:00
parent 3da5865614
commit b99340bf76
3 changed files with 28 additions and 8 deletions

View File

@ -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<Uint8Array> {
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<AuthAccountsResponse> {
const path =
height === undefined ? `/auth/accounts/${address}` : `/auth/accounts/${address}?tx.height=${height}`;

View File

@ -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<NodeInfoResponse>;
blocksLatest(): Promise<BlocksResponse>;
blocks(height: number): Promise<BlocksResponse>;
encodeTx(tx: StdTx): Promise<Uint8Array>;
authAccounts(address: string, height?: string): Promise<AuthAccountsResponse>;
txs(query: string): Promise<SearchTxsResponse>;
txsById(id: string): Promise<TxsResponse>;

View File

@ -8,6 +8,10 @@ export interface StdTx {
readonly signatures: ReadonlyArray<StdSignature>;
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;