From 0e0ff9455f904deeae5d3805004225f373359f71 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 11 Feb 2020 10:35:29 +0100 Subject: [PATCH] Add and use CosmWasmClient.getNonce --- packages/sdk/src/cosmwasmclient.spec.ts | 15 ++++++++++ packages/sdk/src/cosmwasmclient.ts | 39 +++++++++++++++---------- packages/sdk/src/index.ts | 2 +- packages/sdk/types/cosmwasmclient.d.ts | 10 +++++++ packages/sdk/types/index.d.ts | 2 +- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 30a3686b..9c6e0b5c 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -27,6 +27,10 @@ const faucet = { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", }; +const unusedAccount = { + address: "cosmos1cjsxept9rkggzxztslae9ndgpdyt2408lk850u", +}; + describe("CosmWasmClient", () => { describe("makeReadOnly", () => { it("can be constructed", () => { @@ -43,6 +47,17 @@ describe("CosmWasmClient", () => { }); }); + describe("getNonce", () => { + it("works", async () => { + pendingWithoutCosmos(); + const client = CosmWasmClient.makeReadOnly(httpUrl); + expect(await client.getNonce(unusedAccount.address)).toEqual({ + accountNumber: 5, + sequence: 0, + }); + }); + }); + describe("upload", () => { it("works", async () => { pendingWithoutCosmos(); diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index eaa72bd8..2e32ddea 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -21,6 +21,11 @@ interface SigningData { readonly signCallback: SigningCallback; } +export interface GetNonceResult { + readonly accountNumber: number; + readonly sequence: number; +} + export class CosmWasmClient { public static makeReadOnly(url: string): CosmWasmClient { return new CosmWasmClient(url); @@ -60,6 +65,19 @@ export class CosmWasmClient { return response.node_info.network; } + /** + * Returns account number and sequence. + * + * @param address returns data for this address. When unset, the client's sender adddress is used. + */ + public async getNonce(address?: string): Promise { + const account = (await this.restClient.authAccounts(address || this.senderAddress)).result.value; + return { + accountNumber: account.account_number, + sequence: account.sequence, + }; + } + /** Uploads code and returns a code ID */ public async upload(wasmCode: Uint8Array, memo = ""): Promise { const storeCodeMsg: MsgStoreCode = { @@ -82,12 +100,9 @@ export class CosmWasmClient { gas: "89000000", }; - // eslint-disable-next-line @typescript-eslint/camelcase - const { account_number, sequence } = ( - await this.restClient.authAccounts(this.senderAddress) - ).result.value; + const { accountNumber, sequence } = await this.getNonce(); const chainId = await this.chainId(); - const signBytes = makeSignBytes([storeCodeMsg], fee, chainId, memo, account_number, sequence); + const signBytes = makeSignBytes([storeCodeMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); const signedTx = { msg: [storeCodeMsg], @@ -134,12 +149,9 @@ export class CosmWasmClient { gas: "89000000", }; - // eslint-disable-next-line @typescript-eslint/camelcase - const { account_number, sequence } = ( - await this.restClient.authAccounts(this.senderAddress) - ).result.value; + const { accountNumber, sequence } = await this.getNonce(); const chainId = await this.chainId(); - const signBytes = makeSignBytes([instantiateMsg], fee, chainId, memo, account_number, sequence); + const signBytes = makeSignBytes([instantiateMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); const signedTx = { @@ -183,12 +195,9 @@ export class CosmWasmClient { gas: "89000000", }; - // eslint-disable-next-line @typescript-eslint/camelcase - const { account_number, sequence } = ( - await this.restClient.authAccounts(this.senderAddress) - ).result.value; + const { accountNumber, sequence } = await this.getNonce(); const chainId = await this.chainId(); - const signBytes = makeSignBytes([executeMsg], fee, chainId, memo, account_number, sequence); + const signBytes = makeSignBytes([executeMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); const signedTx = { msg: [executeMsg], diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 1785b071..abfadd1a 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -6,7 +6,7 @@ export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./addr export { unmarshalTx } from "./decoding"; export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; -export { CosmWasmClient } from "./cosmwasmclient"; +export { CosmWasmClient, GetNonceResult } from "./cosmwasmclient"; export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen"; export { CosmosPubkeyBech32Prefix, diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index 5a07c95f..dac30039 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -3,6 +3,10 @@ import { Coin, StdSignature } from "./types"; export interface SigningCallback { (signBytes: Uint8Array): Promise; } +export interface GetNonceResult { + readonly accountNumber: number; + readonly sequence: number; +} export declare class CosmWasmClient { static makeReadOnly(url: string): CosmWasmClient; static makeWritable(url: string, senderAddress: string, signCallback: SigningCallback): CosmWasmClient; @@ -12,6 +16,12 @@ export declare class CosmWasmClient { private get signCallback(); private constructor(); chainId(): Promise; + /** + * Returns account number and sequence. + * + * @param address returns data for this address. When unset, the client's sender adddress is used. + */ + getNonce(address?: string): Promise; /** Uploads code and returns a code ID */ upload(wasmCode: Uint8Array, memo?: string): Promise; instantiate( diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index e0d05707..67cdb1c0 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -5,7 +5,7 @@ export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./addr export { unmarshalTx } from "./decoding"; export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; -export { CosmWasmClient } from "./cosmwasmclient"; +export { CosmWasmClient, GetNonceResult } from "./cosmwasmclient"; export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen"; export { CosmosPubkeyBech32Prefix,