From 2b52c8e75dca35d85f53a27a0667a9071049f96e Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 25 Jul 2020 16:12:21 +0200 Subject: [PATCH] Add normalizePubkey --- packages/sdk38/src/index.ts | 1 + packages/sdk38/src/lcdapi/index.ts | 2 +- packages/sdk38/src/lcdapi/utils.spec.ts | 29 ++++++++++++++++++++++++- packages/sdk38/src/lcdapi/utils.ts | 15 +++++++++++++ packages/sdk38/types/index.d.ts | 1 + packages/sdk38/types/lcdapi/index.d.ts | 2 +- packages/sdk38/types/lcdapi/utils.d.ts | 8 +++++++ 7 files changed, 55 insertions(+), 3 deletions(-) diff --git a/packages/sdk38/src/index.ts b/packages/sdk38/src/index.ts index 9d38062d..05adf9ce 100644 --- a/packages/sdk38/src/index.ts +++ b/packages/sdk38/src/index.ts @@ -61,6 +61,7 @@ export { MintParametersResponse, NodeInfoResponse, normalizeLcdApiArray, + normalizePubkey, PostTxsResponse, SearchTxsResponse, setupAuthExtension, diff --git a/packages/sdk38/src/lcdapi/index.ts b/packages/sdk38/src/lcdapi/index.ts index e305cf46..db3bdb73 100644 --- a/packages/sdk38/src/lcdapi/index.ts +++ b/packages/sdk38/src/lcdapi/index.ts @@ -81,4 +81,4 @@ export { LcdApiArray, LcdClient, normalizeLcdApiArray } from "./lcdclient"; // // Utils for interacting with the client/API // -export { uint64ToNumber, uint64ToString } from "./utils"; +export { normalizePubkey, uint64ToNumber, uint64ToString } from "./utils"; diff --git a/packages/sdk38/src/lcdapi/utils.spec.ts b/packages/sdk38/src/lcdapi/utils.spec.ts index a4628096..ace1c60e 100644 --- a/packages/sdk38/src/lcdapi/utils.spec.ts +++ b/packages/sdk38/src/lcdapi/utils.spec.ts @@ -1,4 +1,5 @@ -import { uint64ToNumber, uint64ToString } from "./utils"; +import { PubKey } from "../types"; +import { normalizePubkey, uint64ToNumber, uint64ToString } from "./utils"; describe("utils", () => { describe("uint64ToNumber", () => { @@ -64,4 +65,30 @@ describe("utils", () => { expect(() => uint64ToString("18446744073709551616")).toThrow(); }); }); + + describe("normalizePubkey", () => { + it("interprets empty bech32 string as unset", () => { + expect(normalizePubkey("")).toBeNull(); + }); + + it("decodes bech32 pubkey", () => { + const input = "cosmospub1addwnpepqd8sgxq7aw348ydctp3n5ajufgxp395hksxjzc6565yfp56scupfqhlgyg5"; + expect(normalizePubkey(input)).toEqual({ + type: "tendermint/PubKeySecp256k1", + value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ", + }); + }); + + it("interprets null as unset", () => { + expect(normalizePubkey(null)).toBeNull(); + }); + + it("passes PubKey unchanged", () => { + const original: PubKey = { + type: "tendermint/PubKeySecp256k1", + value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ", + }; + expect(original).toEqual(original); + }); + }); }); diff --git a/packages/sdk38/src/lcdapi/utils.ts b/packages/sdk38/src/lcdapi/utils.ts index 4268fc13..5f797746 100644 --- a/packages/sdk38/src/lcdapi/utils.ts +++ b/packages/sdk38/src/lcdapi/utils.ts @@ -1,5 +1,8 @@ import { Uint64 } from "@cosmjs/math"; +import { decodeBech32Pubkey } from "../pubkey"; +import { PubKey } from "../types"; + /** * Converts an integer expressed as number or string to a number. * Throws if input is not a valid uint64 or if the value exceeds MAX_SAFE_INTEGER. @@ -21,3 +24,15 @@ export function uint64ToString(input: number | string): string { const value = typeof input === "number" ? Uint64.fromNumber(input) : Uint64.fromString(input); return value.toString(); } + +/** + * Normalizes a pubkey as in `BaseAccount.public_key` to allow supporting + * Comsos SDK 0.37–0.39. + * + * Returns null when unset. + */ +export function normalizePubkey(input: string | PubKey | null): PubKey | null { + if (!input) return null; + if (typeof input === "string") return decodeBech32Pubkey(input); + return input; +} diff --git a/packages/sdk38/types/index.d.ts b/packages/sdk38/types/index.d.ts index b6312441..b136e0d0 100644 --- a/packages/sdk38/types/index.d.ts +++ b/packages/sdk38/types/index.d.ts @@ -59,6 +59,7 @@ export { MintParametersResponse, NodeInfoResponse, normalizeLcdApiArray, + normalizePubkey, PostTxsResponse, SearchTxsResponse, setupAuthExtension, diff --git a/packages/sdk38/types/lcdapi/index.d.ts b/packages/sdk38/types/lcdapi/index.d.ts index 3d14ccf2..07b83300 100644 --- a/packages/sdk38/types/lcdapi/index.d.ts +++ b/packages/sdk38/types/lcdapi/index.d.ts @@ -68,4 +68,4 @@ export { TxsResponse, } from "./base"; export { LcdApiArray, LcdClient, normalizeLcdApiArray } from "./lcdclient"; -export { uint64ToNumber, uint64ToString } from "./utils"; +export { normalizePubkey, uint64ToNumber, uint64ToString } from "./utils"; diff --git a/packages/sdk38/types/lcdapi/utils.d.ts b/packages/sdk38/types/lcdapi/utils.d.ts index a7be38a5..d3268564 100644 --- a/packages/sdk38/types/lcdapi/utils.d.ts +++ b/packages/sdk38/types/lcdapi/utils.d.ts @@ -1,3 +1,4 @@ +import { PubKey } from "../types"; /** * Converts an integer expressed as number or string to a number. * Throws if input is not a valid uint64 or if the value exceeds MAX_SAFE_INTEGER. @@ -12,3 +13,10 @@ export declare function uint64ToNumber(input: number | string): number; * This is needed for supporting Comsos SDK 0.37/0.38/0.39 with one client. */ export declare function uint64ToString(input: number | string): string; +/** + * Normalizes a pubkey as in `BaseAccount.public_key` to allow supporting + * Comsos SDK 0.37–0.39. + * + * Returns null when unset. + */ +export declare function normalizePubkey(input: string | PubKey | null): PubKey | null;