From 5ad11bdef6c42a93fd212939574155af93ced2bf Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 28 Jul 2020 12:39:33 +0200 Subject: [PATCH] Use string | PubKey | null as public key type --- CHANGELOG.md | 3 +++ packages/cosmwasm/src/cosmwasmclient.ts | 4 ++-- packages/sdk38/src/cosmosclient.ts | 12 +++++++++--- packages/sdk38/src/lcdapi/auth.ts | 14 ++++++++++++-- packages/sdk38/types/lcdapi/auth.d.ts | 14 ++++++++++++-- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26d09893..f5305260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ - @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/sdk38: `BaseAccount` now uses `string | PubKey | null` as the type for + `public_key`. The new helper `normalizePubkey` allows 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 b404422b..c16a8965 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -6,9 +6,9 @@ import { BroadcastMode, Coin, CosmosSdkTx, - decodeBech32Pubkey, IndexedTx, LcdClient, + normalizePubkey, PostTxResult, PubKey, setupAuthExtension, @@ -234,7 +234,7 @@ export class CosmWasmClient { return { address: value.address, balance: value.coins, - pubkey: value.public_key ? decodeBech32Pubkey(value.public_key) : undefined, + pubkey: normalizePubkey(value.public_key) || undefined, accountNumber: uint64ToNumber(value.account_number), sequence: uint64ToNumber(value.sequence), }; diff --git a/packages/sdk38/src/cosmosclient.ts b/packages/sdk38/src/cosmosclient.ts index cf90cd19..555afd43 100644 --- a/packages/sdk38/src/cosmosclient.ts +++ b/packages/sdk38/src/cosmosclient.ts @@ -3,9 +3,15 @@ import { fromBase64, fromHex, toHex } from "@cosmjs/encoding"; import { Uint53 } from "@cosmjs/math"; import { Coin } from "./coins"; -import { AuthExtension, BroadcastMode, LcdClient, setupAuthExtension, uint64ToNumber } from "./lcdapi"; +import { + AuthExtension, + BroadcastMode, + LcdClient, + normalizePubkey, + setupAuthExtension, + uint64ToNumber, +} from "./lcdapi"; import { Log, parseLogs } from "./logs"; -import { decodeBech32Pubkey } from "./pubkey"; import { CosmosSdkTx, PubKey, StdTx } from "./types"; export interface GetSequenceResult { @@ -234,7 +240,7 @@ export class CosmosClient { return { address: value.address, balance: value.coins, - pubkey: value.public_key ? decodeBech32Pubkey(value.public_key) : undefined, + pubkey: normalizePubkey(value.public_key) || undefined, accountNumber: uint64ToNumber(value.account_number), sequence: uint64ToNumber(value.sequence), }; diff --git a/packages/sdk38/src/lcdapi/auth.ts b/packages/sdk38/src/lcdapi/auth.ts index 21da0e80..6b316466 100644 --- a/packages/sdk38/src/lcdapi/auth.ts +++ b/packages/sdk38/src/lcdapi/auth.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { Coin } from "../coins"; +import { PubKey } from "../types"; import { LcdClient } from "./lcdclient"; /** @@ -14,8 +15,17 @@ export interface BaseAccount { /** Bech32 account address */ readonly address: string; readonly coins: readonly Coin[]; - /** Bech32 encoded pubkey */ - readonly public_key: string; + /** + * The public key of the account. This is not available on-chain as long as the account + * did not send a transaction. + * + * This was a type/value object in Cosmos SDK 0.37, changed to bech32 in Cosmos SDK 0.38 ([1]) + * and changed back to type/value object 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 public_key: string | PubKey | null; /** * The account number assigned by the blockchain. * diff --git a/packages/sdk38/types/lcdapi/auth.d.ts b/packages/sdk38/types/lcdapi/auth.d.ts index dc5261b5..e20b7102 100644 --- a/packages/sdk38/types/lcdapi/auth.d.ts +++ b/packages/sdk38/types/lcdapi/auth.d.ts @@ -1,4 +1,5 @@ import { Coin } from "../coins"; +import { PubKey } from "../types"; import { LcdClient } from "./lcdclient"; /** * A Cosmos SDK base account. @@ -12,8 +13,17 @@ export interface BaseAccount { /** Bech32 account address */ readonly address: string; readonly coins: readonly Coin[]; - /** Bech32 encoded pubkey */ - readonly public_key: string; + /** + * The public key of the account. This is not available on-chain as long as the account + * did not send a transaction. + * + * This was a type/value object in Cosmos SDK 0.37, changed to bech32 in Cosmos SDK 0.38 ([1]) + * and changed back to type/value object 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 public_key: string | PubKey | null; /** * The account number assigned by the blockchain. *