Move pubkey prefixes to pubkey.ts

This commit is contained in:
Simon Warta 2020-02-09 13:47:47 +01:00
parent 00c033b669
commit 4520953e53
6 changed files with 18 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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