Move pubkey prefixes to pubkey.ts
This commit is contained in:
parent
00c033b669
commit
4520953e53
@ -7,7 +7,6 @@ const { fromBase64 } = Encoding;
|
||||
|
||||
// TODO: make this much more configurable
|
||||
export type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
|
||||
export type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
|
||||
|
||||
function isCosmosAddressBech32Prefix(prefix: string): prefix is CosmosAddressBech32Prefix {
|
||||
return ["cosmos", "cosmosvalcons", "cosmosvaloper"].includes(prefix);
|
||||
|
||||
@ -7,4 +7,9 @@ export { unmarshalTx } from "./decoding";
|
||||
export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding";
|
||||
export { RestClient, TxsResponse } from "./restclient";
|
||||
export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen";
|
||||
export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey";
|
||||
export {
|
||||
CosmosPubkeyBech32Prefix,
|
||||
decodeBech32Pubkey,
|
||||
encodeBech32Pubkey,
|
||||
encodeSecp256k1Pubkey,
|
||||
} from "./pubkey";
|
||||
|
||||
@ -2,7 +2,6 @@ import { Secp256k1 } from "@iov/crypto";
|
||||
import { Bech32, Encoding } from "@iov/encoding";
|
||||
import equal from "fast-deep-equal";
|
||||
|
||||
import { CosmosPubkeyBech32Prefix } from "./address";
|
||||
import { Bech32PubKey, PubKey, pubkeyType } from "./types";
|
||||
|
||||
export function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey {
|
||||
@ -12,8 +11,11 @@ export function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey {
|
||||
};
|
||||
}
|
||||
|
||||
export type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
|
||||
const validPubkeyPrefixes = ["cosmospub", "cosmosvalconspub", "cosmosvaloperpub"];
|
||||
|
||||
function isCosmosPubkeyBech32Prefix(prefix: string): prefix is CosmosPubkeyBech32Prefix {
|
||||
return ["cosmospub", "cosmosvalconspub", "cosmosvaloperpub"].includes(prefix);
|
||||
return validPubkeyPrefixes.includes(prefix);
|
||||
}
|
||||
|
||||
// As discussed in https://github.com/binance-chain/javascript-sdk/issues/163
|
||||
@ -27,7 +29,7 @@ const pubkeyAminoPrefixLength = pubkeyAminoPrefixSecp256k1.length;
|
||||
export function decodeBech32Pubkey(bech: Bech32PubKey): PubKey {
|
||||
const { prefix, data } = Bech32.decode(bech);
|
||||
if (!isCosmosPubkeyBech32Prefix(prefix)) {
|
||||
throw new Error(`Invalid bech32 prefix. Must be one of cosmos, cosmosvalcons, or cosmosvaloper.`);
|
||||
throw new Error(`Invalid bech32 prefix. Must be one of ${validPubkeyPrefixes.join(", ")}.`);
|
||||
}
|
||||
|
||||
const aminoPrefix = data.slice(0, pubkeyAminoPrefixLength);
|
||||
|
||||
1
packages/sdk/types/address.d.ts
vendored
1
packages/sdk/types/address.d.ts
vendored
@ -1,5 +1,4 @@
|
||||
import { PubKey } from "./types";
|
||||
export declare type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
|
||||
export declare type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
|
||||
export declare function isValidAddress(address: string): boolean;
|
||||
export declare function encodeAddress(pubkey: PubKey, prefix: string): string;
|
||||
|
||||
7
packages/sdk/types/index.d.ts
vendored
7
packages/sdk/types/index.d.ts
vendored
@ -6,4 +6,9 @@ export { unmarshalTx } from "./decoding";
|
||||
export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding";
|
||||
export { RestClient, TxsResponse } from "./restclient";
|
||||
export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen";
|
||||
export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey";
|
||||
export {
|
||||
CosmosPubkeyBech32Prefix,
|
||||
decodeBech32Pubkey,
|
||||
encodeBech32Pubkey,
|
||||
encodeSecp256k1Pubkey,
|
||||
} from "./pubkey";
|
||||
|
||||
2
packages/sdk/types/pubkey.d.ts
vendored
2
packages/sdk/types/pubkey.d.ts
vendored
@ -1,5 +1,5 @@
|
||||
import { CosmosPubkeyBech32Prefix } from "./address";
|
||||
import { Bech32PubKey, PubKey } from "./types";
|
||||
export declare function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey;
|
||||
export declare type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
|
||||
export declare function decodeBech32Pubkey(bech: Bech32PubKey): PubKey;
|
||||
export declare function encodeBech32Pubkey(pubkey: PubKey, prefix: CosmosPubkeyBech32Prefix): Bech32PubKey;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user