Add and use CosmWasmClient.getNonce

This commit is contained in:
Simon Warta 2020-02-11 10:35:29 +01:00
parent ae586cc012
commit 0e0ff9455f
5 changed files with 51 additions and 17 deletions

View File

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

View File

@ -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<GetNonceResult> {
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<number> {
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],

View File

@ -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,

View File

@ -3,6 +3,10 @@ import { Coin, StdSignature } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
}
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<string>;
/**
* 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<GetNonceResult>;
/** Uploads code and returns a code ID */
upload(wasmCode: Uint8Array, memo?: string): Promise<number>;
instantiate(

View File

@ -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,