From 4b91ed8f0c76466d33400ed2bfd1045a70458e3c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 4 Feb 2020 08:53:53 +0100 Subject: [PATCH] Test and fix RestClient.authAccounts --- packages/bcp/src/cosmwasmconnection.ts | 2 +- packages/bcp/src/types.spec.ts | 12 ++++++------ packages/bcp/src/types.ts | 18 ++++++------------ packages/bcp/types/types.d.ts | 8 +++----- packages/sdk/src/restclient.spec.ts | 13 +++++++++++++ packages/sdk/src/types.ts | 4 ++-- packages/sdk/types/types.d.ts | 4 ++-- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 84177c3a..21b65fe4 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -309,7 +309,7 @@ export class CosmWasmConnection implements BlockchainConnection { const accountForHeight = await this.restClient.authAccounts(sender, response.height); // this is technically not the proper nonce. maybe this causes issues for sig validation? // leaving for now unless it causes issues - const sequence = (parseInt(accountForHeight.result.value.sequence, 10) - 1) as Nonce; + const sequence = (accountForHeight.result.value.sequence - 1) as Nonce; return parseTxsResponse(chainId, parseInt(response.height, 10), sequence, response, this.tokenInfo); } } diff --git a/packages/bcp/src/types.spec.ts b/packages/bcp/src/types.spec.ts index 0023e63f..be1c8702 100644 --- a/packages/bcp/src/types.spec.ts +++ b/packages/bcp/src/types.spec.ts @@ -4,8 +4,8 @@ import { accountToNonce, nonceToAccountNumber, nonceToSequence } from "./types"; describe("nonceEncoding", () => { it("works for input in range", () => { const nonce = accountToNonce({ - account_number: "1234", - sequence: "7890", + account_number: 1234, + sequence: 7890, }); expect(nonceToAccountNumber(nonce)).toEqual("1234"); expect(nonceToSequence(nonce)).toEqual("7890"); @@ -14,14 +14,14 @@ describe("nonceEncoding", () => { it("errors on input too large", () => { expect(() => accountToNonce({ - account_number: "1234567890", - sequence: "7890", + account_number: 1234567890, + sequence: 7890, }), ).toThrow(); expect(() => accountToNonce({ - account_number: "178", - sequence: "97320247923", + account_number: 178, + sequence: 97320247923, }), ).toThrow(); }); diff --git a/packages/bcp/src/types.ts b/packages/bcp/src/types.ts index 32133828..dd804395 100644 --- a/packages/bcp/src/types.ts +++ b/packages/bcp/src/types.ts @@ -1,3 +1,4 @@ +import { types } from "@cosmwasm/sdk"; import { Nonce } from "@iov/bcp"; export interface TokenInfo { @@ -24,28 +25,21 @@ const maxSeq = 1 << 20; // NonceInfo is the data we need from account to create a nonce // Use this so no confusion about order of arguments -export interface NonceInfo { - readonly account_number: string; - readonly sequence: string; -} +export type NonceInfo = Pick; // this (lossily) encodes the two pieces of info (uint64) needed to sign into // one (53-bit) number. Cross your fingers. -/* eslint-disable-next-line @typescript-eslint/camelcase */ -export function accountToNonce({ account_number, sequence }: NonceInfo): Nonce { - const acct = parseInt(account_number, 10); - const seq = parseInt(sequence, 10); - +export function accountToNonce({ account_number: account, sequence }: NonceInfo): Nonce { // we allow 23 bits (8 million) for accounts, and 20 bits (1 million) for tx/account // let's fix this soon - if (acct > maxAcct) { + if (account > maxAcct) { throw new Error("Account number is greater than 2^23, must update Nonce handler"); } - if (seq > maxSeq) { + if (sequence > maxSeq) { throw new Error("Sequence is greater than 2^20, must update Nonce handler"); } - const val = acct * maxSeq + seq; + const val = account * maxSeq + sequence; return val as Nonce; } diff --git a/packages/bcp/types/types.d.ts b/packages/bcp/types/types.d.ts index e8ac6ce2..e100d159 100644 --- a/packages/bcp/types/types.d.ts +++ b/packages/bcp/types/types.d.ts @@ -1,3 +1,4 @@ +import { types } from "@cosmwasm/sdk"; import { Nonce } from "@iov/bcp"; export interface TokenInfo { readonly denom: string; @@ -14,10 +15,7 @@ export interface TokenInfo { readonly fractionalDigits: number; } export declare type TokenInfos = ReadonlyArray; -export interface NonceInfo { - readonly account_number: string; - readonly sequence: string; -} -export declare function accountToNonce({ account_number, sequence }: NonceInfo): Nonce; +export declare type NonceInfo = Pick; +export declare function accountToNonce({ account_number: account, sequence }: NonceInfo): Nonce; export declare function nonceToAccountNumber(nonce: Nonce): string; export declare function nonceToSequence(nonce: Nonce): string; diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 55293bad..33f27ffd 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -9,6 +9,8 @@ const { fromBase64 } = Encoding; const httpUrl = "http://localhost:1317"; const defaultNetworkId = "testing"; +const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6"; + function pendingWithoutCosmos(): void { if (!process.env.COSMOS_ENABLED) { return pending("Set COSMOS_ENABLED to enable Cosmos node-based tests"); @@ -30,6 +32,17 @@ describe("RestClient", () => { }); }); + describe("authAccounts", () => { + it("works", async () => { + pendingWithoutCosmos(); + const client = new RestClient(httpUrl); + const { result } = await client.authAccounts(faucetAddress); + const account = result.value; + expect(account.account_number).toEqual(4); + expect(account.sequence).toBeGreaterThanOrEqual(0); + }); + }); + describe("encodeTx", () => { it("works for cosmoshub example", async () => { pendingWithoutCosmos(); diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 1d5e9c86..9ce32263 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -64,6 +64,6 @@ export interface BaseAccount { readonly address: string; readonly coins: ReadonlyArray; readonly public_key: AccountPubKey; - readonly account_number: string; - readonly sequence: string; + readonly account_number: number; + readonly sequence: number; } diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 4f1bb326..a2c0abd1 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -45,6 +45,6 @@ export interface BaseAccount { readonly address: string; readonly coins: ReadonlyArray; readonly public_key: AccountPubKey; - readonly account_number: string; - readonly sequence: string; + readonly account_number: number; + readonly sequence: number; }