Merge pull request #44 from confio/move-address-decode-to-sdk

Move address decode to sdk
This commit is contained in:
Ethan Frey 2020-02-05 11:28:34 +01:00 committed by GitHub
commit 5ac758c6ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 228 additions and 117 deletions

View File

@ -1,33 +1,11 @@
import { Address, Algorithm, PubkeyBytes } from "@iov/bcp";
import { Algorithm, PubkeyBytes } from "@iov/bcp";
import { Encoding } from "@iov/encoding";
import { decodeCosmosAddress, decodeCosmosPubkey, isValidAddress, pubkeyToAddress } from "./address";
import { decodeCosmosPubkey, isValidAddress, pubkeyToAddress } from "./address";
const { fromBase64, fromHex } = Encoding;
describe("address", () => {
// Bech32 encoding/decoding data generated using https://github.com/bitcoinjs/bech32
describe("decodeCosmosAddress", () => {
it("throws for invalid prefix", () => {
expect(() =>
decodeCosmosAddress("cosmot10q82zkzzmaku5lazhsvxv7hsg4ntpuhd8j5266" as Address),
).toThrowError(/invalid bech32 prefix/i);
});
it("throws for invalid length", () => {
expect(() =>
decodeCosmosAddress("cosmos1alcmj76e030g83fedrnx8lvsythhg70zlct4cwx3" as Address),
).toThrowError(/invalid data length/i);
});
it("decodes valid addresses", () => {
expect(decodeCosmosAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6" as Address)).toEqual({
prefix: "cosmos",
data: fromHex("0d82b1e7c96dbfa42462fe612932e6bff111d51b"),
});
});
});
describe("decodeCosmosPubkey", () => {
it("works", () => {
expect(

View File

@ -1,88 +1,44 @@
import { CosmosBech32Prefix, decodeBech32Pubkey, encodeAddress, isValidAddress } from "@cosmwasm/sdk";
import { Address, Algorithm, PubkeyBundle, PubkeyBytes } from "@iov/bcp";
import { Ripemd160, Secp256k1, Sha256 } from "@iov/crypto";
import { Bech32, Encoding } from "@iov/encoding";
import equal from "fast-deep-equal";
import { Secp256k1 } from "@iov/crypto";
import { Encoding } from "@iov/encoding";
export type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
export type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
export type CosmosBech32Prefix = CosmosAddressBech32Prefix | CosmosPubkeyBech32Prefix;
export { CosmosBech32Prefix, isValidAddress };
// As discussed in https://github.com/binance-chain/javascript-sdk/issues/163
// Prefixes listed here: https://github.com/tendermint/tendermint/blob/d419fffe18531317c28c29a292ad7d253f6cafdf/docs/spec/blockchain/encoding.md#public-key-cryptography
const pubkeyAminoPrefixSecp256k1 = Encoding.fromHex("eb5ae98721");
const pubkeyAminoPrefixEd25519 = Encoding.fromHex("1624de64");
const pubkeyAminoPrefixLength = pubkeyAminoPrefixSecp256k1.length;
function isCosmosAddressBech32Prefix(prefix: string): prefix is CosmosAddressBech32Prefix {
return ["cosmos", "cosmosvalcons", "cosmosvaloper"].includes(prefix);
}
function isCosmosPubkeyBech32Prefix(prefix: string): prefix is CosmosPubkeyBech32Prefix {
return ["cosmospub", "cosmosvalconspub", "cosmosvaloperpub"].includes(prefix);
}
export function decodeCosmosAddress(
address: Address,
): { readonly prefix: CosmosAddressBech32Prefix; readonly data: Uint8Array } {
const { prefix, data } = Bech32.decode(address);
if (!isCosmosAddressBech32Prefix(prefix)) {
throw new Error(`Invalid bech32 prefix. Must be one of cosmos, cosmosvalcons, or cosmosvaloper.`);
}
if (data.length !== 20) {
throw new Error("Invalid data length. Expected 20 bytes.");
}
return { prefix: prefix, data: data };
}
const { fromBase64, toBase64 } = Encoding;
export function decodeCosmosPubkey(
encodedPubkey: string,
): { readonly algo: Algorithm; readonly data: PubkeyBytes } {
const { prefix, data } = Bech32.decode(encodedPubkey);
if (!isCosmosPubkeyBech32Prefix(prefix)) {
throw new Error(`Invalid bech32 prefix. Must be one of cosmos, cosmosvalcons, or cosmosvaloper.`);
}
const aminoPrefix = data.slice(0, pubkeyAminoPrefixLength);
const rest = data.slice(pubkeyAminoPrefixLength);
if (equal(aminoPrefix, pubkeyAminoPrefixSecp256k1)) {
if (rest.length !== 33) {
throw new Error("Invalid rest data length. Expected 33 bytes (compressed secp256k1 pubkey).");
}
return { algo: Algorithm.Secp256k1, data: rest as PubkeyBytes };
} else if (equal(aminoPrefix, pubkeyAminoPrefixEd25519)) {
if (rest.length !== 32) {
throw new Error("Invalid rest data length. Expected 32 bytes (ed25519 pubkey).");
}
return { algo: Algorithm.Ed25519, data: rest as PubkeyBytes };
} else {
throw new Error("Unsupported Pubkey type. Amino prefix: " + Encoding.toHex(aminoPrefix));
}
}
export function isValidAddress(address: string): boolean {
try {
decodeCosmosAddress(address as Address);
return true;
} catch {
return false;
const sdkPubKey = decodeBech32Pubkey(encodedPubkey);
switch (sdkPubKey.type) {
case "tendermint/PubKeySecp256k1":
return { algo: Algorithm.Secp256k1, data: fromBase64(sdkPubKey.value) as PubkeyBytes };
case "tendermint/PubKeyEd25519":
return { algo: Algorithm.Ed25519, data: fromBase64(sdkPubKey.value) as PubkeyBytes };
default:
throw new Error("Unsupported Pubkey type: " + sdkPubKey.type);
}
}
// See https://github.com/tendermint/tendermint/blob/f2ada0a604b4c0763bda2f64fac53d506d3beca7/docs/spec/blockchain/encoding.md#public-key-cryptography
export function pubkeyToAddress(pubkey: PubkeyBundle, prefix: CosmosBech32Prefix): Address {
const pubkeyData =
pubkey.algo === Algorithm.Secp256k1 ? Secp256k1.compressPubkey(pubkey.data) : pubkey.data;
switch (pubkey.algo) {
case Algorithm.Secp256k1: {
const hash1 = new Sha256(pubkeyData).digest();
const hash2 = new Ripemd160(hash1).digest();
return Bech32.encode(prefix, hash2) as Address;
let pubkeyType: "tendermint/PubKeySecp256k1" | "tendermint/PubKeyEd25519";
let pubkeyData: Uint8Array = pubkey.data;
if (pubkey.algo === Algorithm.Secp256k1) {
pubkeyType = "tendermint/PubKeySecp256k1";
if (pubkeyData.length > 33) {
pubkeyData = Secp256k1.compressPubkey(pubkey.data);
}
case Algorithm.Ed25519: {
const hash = new Sha256(pubkeyData).digest();
return Bech32.encode(prefix, hash.slice(0, 20)) as Address;
}
default:
throw new Error("Unrecognized public key algorithm");
} else if (pubkey.algo === Algorithm.Ed25519) {
pubkeyType = "tendermint/PubKeyEd25519";
} else {
throw new Error(`Unsupported algorithm: ${pubkey.algo}`);
}
const sdkKey = {
type: pubkeyType,
value: toBase64(pubkeyData),
};
return encodeAddress(sdkKey, prefix) as Address;
}

View File

@ -62,7 +62,7 @@ describe("decode", () => {
describe("decodePubkey", () => {
it("works for secp256k1", () => {
const pubkey = {
const pubkey: types.PubKey = {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
};
@ -70,7 +70,7 @@ describe("decode", () => {
});
it("works for ed25519", () => {
const pubkey = {
const pubkey: types.PubKey = {
type: "tendermint/PubKeyEd25519",
value: "s69CnMgLTpuRyEfecjws3mWssBrOICUx8C2O1DkKSto=",
};
@ -82,7 +82,7 @@ describe("decode", () => {
it("throws for unsupported types", () => {
// https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/sr25519/codec.go#L12
const pubkey = {
const pubkey: types.PubKey = {
type: "tendermint/PubKeySr25519",
value: "N4FJNPE5r/Twz55kO1QEIxyaGF5/HTXH6WgLQJWsy1o=",
};
@ -100,7 +100,7 @@ describe("decode", () => {
describe("decodeFullSignature", () => {
it("works", () => {
const fullSignature = {
const fullSignature: types.StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",

View File

@ -1,18 +1,10 @@
import { CosmosBech32Prefix, isValidAddress } from "@cosmwasm/sdk";
import { Address, Algorithm, PubkeyBundle, PubkeyBytes } from "@iov/bcp";
export declare type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
export declare type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
export declare type CosmosBech32Prefix = CosmosAddressBech32Prefix | CosmosPubkeyBech32Prefix;
export declare function decodeCosmosAddress(
address: Address,
): {
readonly prefix: CosmosAddressBech32Prefix;
readonly data: Uint8Array;
};
export { CosmosBech32Prefix, isValidAddress };
export declare function decodeCosmosPubkey(
encodedPubkey: string,
): {
readonly algo: Algorithm;
readonly data: PubkeyBytes;
};
export declare function isValidAddress(address: string): boolean;
export declare function pubkeyToAddress(pubkey: PubkeyBundle, prefix: CosmosBech32Prefix): Address;

View File

@ -0,0 +1,55 @@
import { Encoding } from "@iov/encoding";
import { decodeBech32Pubkey, encodeAddress, isValidAddress } from "./address";
const { toBase64, fromHex } = Encoding;
describe("address", () => {
describe("decodeBech32Pubkey", () => {
it("works", () => {
expect(
decodeBech32Pubkey("cosmospub1addwnpepqd8sgxq7aw348ydctp3n5ajufgxp395hksxjzc6565yfp56scupfqhlgyg5"),
).toEqual({
type: "tendermint/PubKeySecp256k1",
value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ",
});
});
});
describe("isValidAddress", () => {
it("accepts valid addresses", () => {
expect(isValidAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6")).toEqual(true);
expect(isValidAddress("cosmosvalcons10q82zkzzmaku5lazhsvxv7hsg4ntpuhdwadmss")).toEqual(true);
expect(isValidAddress("cosmosvaloper17mggn4znyeyg25wd7498qxl7r2jhgue8u4qjcq")).toEqual(true);
});
it("rejects invalid addresses", () => {
// Bad size
expect(isValidAddress("cosmos10q82zkzzmaku5lazhsvxv7hsg4ntpuhh8289f")).toEqual(false);
// Bad checksum
expect(isValidAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs7")).toEqual(false);
// Bad prefix
expect(isValidAddress("cosmot10q82zkzzmaku5lazhsvxv7hsg4ntpuhd8j5266")).toEqual(false);
});
});
describe("encodeAddress", () => {
it("works for Secp256k1 compressed", () => {
const prefix = "cosmos";
const pubkey = {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
};
expect(encodeAddress(pubkey, prefix)).toEqual("cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r");
});
it("works for Ed25519", () => {
const prefix = "cosmos";
const pubkey = {
type: "tendermint/PubKeyEd25519",
value: toBase64(fromHex("12ee6f581fe55673a1e9e1382a0829e32075a0aa4763c968bc526e1852e78c95")),
};
expect(encodeAddress(pubkey, prefix)).toEqual("cosmos1pfq05em6sfkls66ut4m2257p7qwlk448h8mysz");
});
});
});

109
packages/sdk/src/address.ts Normal file
View File

@ -0,0 +1,109 @@
import { Ripemd160, Sha256 } from "@iov/crypto";
import { Bech32, Encoding } from "@iov/encoding";
import equal from "fast-deep-equal";
import { Bech32PubKey, PubKey } from "./types";
const { fromBase64, toBase64 } = Encoding;
// TODO: make this much more configurable
export type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
export type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
export type CosmosBech32Prefix = CosmosAddressBech32Prefix | CosmosPubkeyBech32Prefix;
function isCosmosAddressBech32Prefix(prefix: string): prefix is CosmosAddressBech32Prefix {
return ["cosmos", "cosmosvalcons", "cosmosvaloper"].includes(prefix);
}
function isCosmosPubkeyBech32Prefix(prefix: string): prefix is CosmosPubkeyBech32Prefix {
return ["cosmospub", "cosmosvalconspub", "cosmosvaloperpub"].includes(prefix);
}
// As discussed in https://github.com/binance-chain/javascript-sdk/issues/163
// Prefixes listed here: https://github.com/tendermint/tendermint/blob/d419fffe18531317c28c29a292ad7d253f6cafdf/docs/spec/blockchain/encoding.md#public-key-cryptography
// Last bytes is varint-encoded length prefix
const pubkeyAminoPrefixSecp256k1 = Encoding.fromHex("eb5ae98721");
const pubkeyAminoPrefixEd25519 = Encoding.fromHex("1624de6420");
const pubkeyAminoPrefixSr25519 = Encoding.fromHex("0dfb1005");
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.`);
}
const aminoPrefix = data.slice(0, pubkeyAminoPrefixLength);
const rest = data.slice(pubkeyAminoPrefixLength);
if (equal(aminoPrefix, pubkeyAminoPrefixSecp256k1)) {
if (rest.length !== 33) {
throw new Error("Invalid rest data length. Expected 33 bytes (compressed secp256k1 pubkey).");
}
return {
type: "tendermint/PubKeySecp256k1",
value: toBase64(rest),
};
} else if (equal(aminoPrefix, pubkeyAminoPrefixEd25519)) {
if (rest.length !== 32) {
throw new Error("Invalid rest data length. Expected 32 bytes (Ed25519 pubkey).");
}
return {
type: "tendermint/PubKeyEd25519",
value: toBase64(rest),
};
} else if (equal(aminoPrefix, pubkeyAminoPrefixSr25519)) {
if (rest.length !== 32) {
throw new Error("Invalid rest data length. Expected 32 bytes (Sr25519 pubkey).");
}
return {
type: "tendermint/PubKeySr25519",
value: toBase64(rest),
};
} else {
throw new Error("Unsupported Pubkey type. Amino prefix: " + Encoding.toHex(aminoPrefix));
}
}
export function isValidAddress(address: string): boolean {
try {
const { prefix, data } = Bech32.decode(address);
if (!isCosmosAddressBech32Prefix(prefix)) {
return false;
}
return data.length === 20;
} catch {
return false;
}
}
// See https://github.com/tendermint/tendermint/blob/f2ada0a604b4c0763bda2f64fac53d506d3beca7/docs/spec/blockchain/encoding.md#public-key-cryptography
// This assumes we already have a cosmos-compressed pubkey
export function encodeAddress(pubkey: PubKey, prefix: string): string {
const pubkeyBytes = fromBase64(pubkey.value);
switch (pubkey.type) {
case "tendermint/PubKeySecp256k1": {
if (pubkeyBytes.length !== 33) {
throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyBytes.length}`);
}
const hash1 = new Sha256(pubkeyBytes).digest();
const hash2 = new Ripemd160(hash1).digest();
return Bech32.encode(prefix, hash2);
}
case "tendermint/PubKeyEd25519": {
if (pubkeyBytes.length !== 32) {
throw new Error(`Invalid Ed25519 pubkey length: ${pubkeyBytes.length}`);
}
const hash = new Sha256(pubkeyBytes).digest();
return Bech32.encode(prefix, hash.slice(0, 20));
}
case "tendermint/PubKeySr25519": {
if (pubkeyBytes.length !== 32) {
throw new Error(`Invalid Sr25519 pubkey length: ${pubkeyBytes.length}`);
}
const hash = new Sha256(pubkeyBytes).digest();
return Bech32.encode(prefix, hash.slice(0, 20));
}
default:
throw new Error("Unrecognized public key algorithm");
}
}

View File

@ -1,5 +1,6 @@
import * as types from "./types";
export { CosmosBech32Prefix, decodeBech32Pubkey, encodeAddress, isValidAddress } from "./address";
export { unmarshalTx } from "./decoding";
export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient";

View File

@ -105,7 +105,7 @@ describe("RestClient", () => {
describe("encodeTx", () => {
it("works for cosmoshub example", async () => {
pendingWithoutCosmos();
const tx: StdTx = cosmoshub.tx.value;
const tx = cosmoshub.tx.value;
const client = new RestClient(httpUrl);
expect(await client.encodeTx(tx)).toEqual(fromBase64(cosmoshub.tx_data));
});

View File

@ -101,18 +101,29 @@ export interface StdSignature {
}
export interface PubKey {
// type is one of the strings defined in pubkeyTypes
// I don't use a string literal union here as that makes trouble with json test data:
// https://github.com/confio/cosm-js/pull/44#pullrequestreview-353280504
readonly type: string;
// Value field is base64-encoded in all cases
// Note: if type is Secp256k1, this must contain a COMPRESSED pubkey - to encode from bcp/keycontrol land, you must compress it first
readonly value: string;
}
// AccountPubKey is bech32-encoded amino-binary encoded PubKey interface. oof.
export type AccountPubKey = string;
export const pubkeyTypes: string[] = [
"tendermint/PubKeySecp256k1",
"tendermint/PubKeyEd25519",
"tendermint/PubKeySr25519",
];
// bech32-encoded amino-binary encoded PubKey interface. oof.
export type Bech32PubKey = string;
export interface BaseAccount {
/** Bech32 account address */
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
readonly public_key: AccountPubKey;
readonly public_key: Bech32PubKey;
readonly account_number: number;
readonly sequence: number;
}

7
packages/sdk/types/address.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
import { Bech32PubKey, PubKey } from "./types";
export declare type CosmosAddressBech32Prefix = "cosmos" | "cosmosvalcons" | "cosmosvaloper";
export declare type CosmosPubkeyBech32Prefix = "cosmospub" | "cosmosvalconspub" | "cosmosvaloperpub";
export declare type CosmosBech32Prefix = CosmosAddressBech32Prefix | CosmosPubkeyBech32Prefix;
export declare function decodeBech32Pubkey(bech: Bech32PubKey): PubKey;
export declare function isValidAddress(address: string): boolean;
export declare function encodeAddress(pubkey: PubKey, prefix: string): string;

View File

@ -1,4 +1,5 @@
import * as types from "./types";
export { CosmosBech32Prefix, decodeBech32Pubkey, encodeAddress, isValidAddress } from "./address";
export { unmarshalTx } from "./decoding";
export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient";

View File

@ -74,12 +74,13 @@ export interface PubKey {
readonly type: string;
readonly value: string;
}
export declare type AccountPubKey = string;
export declare const pubkeyTypes: string[];
export declare type Bech32PubKey = string;
export interface BaseAccount {
/** Bech32 account address */
readonly address: string;
readonly coins: ReadonlyArray<Coin>;
readonly public_key: AccountPubKey;
readonly public_key: Bech32PubKey;
readonly account_number: number;
readonly sequence: number;
}