Add normalizePubkey

This commit is contained in:
Simon Warta 2020-07-25 16:12:21 +02:00
parent 4dca3619de
commit 2b52c8e75d
7 changed files with 55 additions and 3 deletions

View File

@ -61,6 +61,7 @@ export {
MintParametersResponse,
NodeInfoResponse,
normalizeLcdApiArray,
normalizePubkey,
PostTxsResponse,
SearchTxsResponse,
setupAuthExtension,

View File

@ -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";

View File

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

View File

@ -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.370.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;
}

View File

@ -59,6 +59,7 @@ export {
MintParametersResponse,
NodeInfoResponse,
normalizeLcdApiArray,
normalizePubkey,
PostTxsResponse,
SearchTxsResponse,
setupAuthExtension,

View File

@ -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";

View File

@ -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.370.39.
*
* Returns null when unset.
*/
export declare function normalizePubkey(input: string | PubKey | null): PubKey | null;