From 4dca3619de8564764b4f6a0d7a7b21763c704fb7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 25 Jul 2020 15:58:17 +0200 Subject: [PATCH] Change account_number/sequence type to number | string --- CHANGELOG.md | 3 +++ packages/cosmwasm/src/cosmwasmclient.ts | 5 +++-- packages/sdk38/src/cosmosclient.ts | 6 +++--- packages/sdk38/src/encoding.ts | 9 +++++---- packages/sdk38/src/lcdapi/auth.ts | 22 ++++++++++++++++++++-- packages/sdk38/types/encoding.d.ts | 4 ++-- packages/sdk38/types/lcdapi/auth.d.ts | 22 ++++++++++++++++++++-- 7 files changed, 56 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b9a1daa..26d09893 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ `Secp256k1Pen` class in favour of `Secp256k1Wallet` which takes an `OfflineSigner` instead of a `SigningCallback`. - @cosmjs/sdk38: Rename `CosmosSdkAccount` to `BaseAccount` and export the type. +- @cosmjs/sdk38: `BaseAccount` now uses `number | string` as the type for + `account_number` and `sequence`. The new helpers `uint64ToNumber` and + `uint64ToString` allow you to normalize the mixed input. - @cosmjs/math: Add missing integer check to `Uint64.fromNumber`. Before `Uint64.fromNumber(1.1)` produced some result. - @cosmjs/sdk38: Add `SigningCosmosClient.signAndPost` as a mid-level diff --git a/packages/cosmwasm/src/cosmwasmclient.ts b/packages/cosmwasm/src/cosmwasmclient.ts index f2543c97..b404422b 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -13,6 +13,7 @@ import { PubKey, setupAuthExtension, StdTx, + uint64ToNumber, } from "@cosmjs/sdk38"; import { setupWasmExtension, WasmExtension } from "./lcdapi/wasm"; @@ -234,8 +235,8 @@ export class CosmWasmClient { address: value.address, balance: value.coins, pubkey: value.public_key ? decodeBech32Pubkey(value.public_key) : undefined, - accountNumber: value.account_number, - sequence: value.sequence, + accountNumber: uint64ToNumber(value.account_number), + sequence: uint64ToNumber(value.sequence), }; } } diff --git a/packages/sdk38/src/cosmosclient.ts b/packages/sdk38/src/cosmosclient.ts index 34ee4fe0..cf90cd19 100644 --- a/packages/sdk38/src/cosmosclient.ts +++ b/packages/sdk38/src/cosmosclient.ts @@ -3,7 +3,7 @@ import { fromBase64, fromHex, toHex } from "@cosmjs/encoding"; import { Uint53 } from "@cosmjs/math"; import { Coin } from "./coins"; -import { AuthExtension, BroadcastMode, LcdClient, setupAuthExtension } from "./lcdapi"; +import { AuthExtension, BroadcastMode, LcdClient, setupAuthExtension, uint64ToNumber } from "./lcdapi"; import { Log, parseLogs } from "./logs"; import { decodeBech32Pubkey } from "./pubkey"; import { CosmosSdkTx, PubKey, StdTx } from "./types"; @@ -235,8 +235,8 @@ export class CosmosClient { address: value.address, balance: value.coins, pubkey: value.public_key ? decodeBech32Pubkey(value.public_key) : undefined, - accountNumber: value.account_number, - sequence: value.sequence, + accountNumber: uint64ToNumber(value.account_number), + sequence: uint64ToNumber(value.sequence), }; } } diff --git a/packages/sdk38/src/encoding.ts b/packages/sdk38/src/encoding.ts index 70ef1f23..17f8de77 100644 --- a/packages/sdk38/src/encoding.ts +++ b/packages/sdk38/src/encoding.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { toUtf8 } from "@cosmjs/encoding"; +import { uint64ToString } from "./lcdapi"; import { Msg } from "./msgs"; import { StdFee } from "./types"; @@ -41,16 +42,16 @@ export function makeSignBytes( fee: StdFee, chainId: string, memo: string, - accountNumber: number, - sequence: number, + accountNumber: number | string, + sequence: number | string, ): Uint8Array { const signDoc: StdSignDoc = { - account_number: accountNumber.toString(), + account_number: uint64ToString(accountNumber), chain_id: chainId, fee: fee, memo: memo, msgs: msgs, - sequence: sequence.toString(), + sequence: uint64ToString(sequence), }; const sortedSignDoc = sortJson(signDoc); return toUtf8(JSON.stringify(sortedSignDoc)); diff --git a/packages/sdk38/src/lcdapi/auth.ts b/packages/sdk38/src/lcdapi/auth.ts index c882c557..21da0e80 100644 --- a/packages/sdk38/src/lcdapi/auth.ts +++ b/packages/sdk38/src/lcdapi/auth.ts @@ -16,8 +16,26 @@ export interface BaseAccount { readonly coins: readonly Coin[]; /** Bech32 encoded pubkey */ readonly public_key: string; - readonly account_number: number; - readonly sequence: number; + /** + * The account number assigned by the blockchain. + * + * This was string encoded in Cosmos SDK 0.37, changed to number in Cosmos SDK 0.38 ([1]) + * and changed back to string in Cosmos SDK 0.39 ([2]). + * + * [1]: https://github.com/cosmos/cosmos-sdk/pull/5280 + * [2]: https://github.com/cosmos/cosmos-sdk/pull/6749 + */ + readonly account_number: number | string; + /** + * The sequence number for replay protection. + * + * This was string encoded in Cosmos SDK 0.37, changed to number in Cosmos SDK 0.38 ([1]) + * and changed back to string in Cosmos SDK 0.39 ([2]). + * + * [1]: https://github.com/cosmos/cosmos-sdk/pull/5280 + * [2]: https://github.com/cosmos/cosmos-sdk/pull/6749 + */ + readonly sequence: number | string; } export interface AuthAccountsResponse { diff --git a/packages/sdk38/types/encoding.d.ts b/packages/sdk38/types/encoding.d.ts index 6eece8ca..35885e4d 100644 --- a/packages/sdk38/types/encoding.d.ts +++ b/packages/sdk38/types/encoding.d.ts @@ -5,6 +5,6 @@ export declare function makeSignBytes( fee: StdFee, chainId: string, memo: string, - accountNumber: number, - sequence: number, + accountNumber: number | string, + sequence: number | string, ): Uint8Array; diff --git a/packages/sdk38/types/lcdapi/auth.d.ts b/packages/sdk38/types/lcdapi/auth.d.ts index 2166627e..dc5261b5 100644 --- a/packages/sdk38/types/lcdapi/auth.d.ts +++ b/packages/sdk38/types/lcdapi/auth.d.ts @@ -14,8 +14,26 @@ export interface BaseAccount { readonly coins: readonly Coin[]; /** Bech32 encoded pubkey */ readonly public_key: string; - readonly account_number: number; - readonly sequence: number; + /** + * The account number assigned by the blockchain. + * + * This was string encoded in Cosmos SDK 0.37, changed to number in Cosmos SDK 0.38 ([1]) + * and changed back to string in Cosmos SDK 0.39 ([2]). + * + * [1]: https://github.com/cosmos/cosmos-sdk/pull/5280 + * [2]: https://github.com/cosmos/cosmos-sdk/pull/6749 + */ + readonly account_number: number | string; + /** + * The sequence number for replay protection. + * + * This was string encoded in Cosmos SDK 0.37, changed to number in Cosmos SDK 0.38 ([1]) + * and changed back to string in Cosmos SDK 0.39 ([2]). + * + * [1]: https://github.com/cosmos/cosmos-sdk/pull/5280 + * [2]: https://github.com/cosmos/cosmos-sdk/pull/6749 + */ + readonly sequence: number | string; } export interface AuthAccountsResponse { readonly height: string;