Add normalizePubkey
This commit is contained in:
parent
4dca3619de
commit
2b52c8e75d
@ -61,6 +61,7 @@ export {
|
||||
MintParametersResponse,
|
||||
NodeInfoResponse,
|
||||
normalizeLcdApiArray,
|
||||
normalizePubkey,
|
||||
PostTxsResponse,
|
||||
SearchTxsResponse,
|
||||
setupAuthExtension,
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
1
packages/sdk38/types/index.d.ts
vendored
1
packages/sdk38/types/index.d.ts
vendored
@ -59,6 +59,7 @@ export {
|
||||
MintParametersResponse,
|
||||
NodeInfoResponse,
|
||||
normalizeLcdApiArray,
|
||||
normalizePubkey,
|
||||
PostTxsResponse,
|
||||
SearchTxsResponse,
|
||||
setupAuthExtension,
|
||||
|
||||
2
packages/sdk38/types/lcdapi/index.d.ts
vendored
2
packages/sdk38/types/lcdapi/index.d.ts
vendored
@ -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";
|
||||
|
||||
8
packages/sdk38/types/lcdapi/utils.d.ts
vendored
8
packages/sdk38/types/lcdapi/utils.d.ts
vendored
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user