From dc93bfc1e7a88e61470cc708145ac94729620da9 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 5 Feb 2020 10:30:21 +0100 Subject: [PATCH] Simplify sdk pubkey types --- packages/bcp/src/decode.spec.ts | 4 ++-- packages/sdk/src/address.spec.ts | 5 ++--- packages/sdk/src/types.ts | 27 ++++++++++++--------------- packages/sdk/types/types.d.ts | 14 +++----------- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/packages/bcp/src/decode.spec.ts b/packages/bcp/src/decode.spec.ts index ac9ddd40..fe8feb51 100644 --- a/packages/bcp/src/decode.spec.ts +++ b/packages/bcp/src/decode.spec.ts @@ -157,7 +157,7 @@ describe("decode", () => { describe("parseTx", () => { it("works", () => { - expect(parseTx(data.tx.value as types.StdTx, chainId, nonce, defaultTokens)).toEqual(signedTxJson); + expect(parseTx(data.tx.value, chainId, nonce, defaultTokens)).toEqual(signedTxJson); }); }); @@ -168,7 +168,7 @@ describe("decode", () => { height: "2823", txhash: txId, raw_log: '[{"msg_index":0,"success":true,"log":""}]', - tx: data.tx as types.AminoTx, + tx: data.tx, }; const expected = { ...signedTxJson, diff --git a/packages/sdk/src/address.spec.ts b/packages/sdk/src/address.spec.ts index c3160ce2..7c73ebaa 100644 --- a/packages/sdk/src/address.spec.ts +++ b/packages/sdk/src/address.spec.ts @@ -1,7 +1,6 @@ import { Encoding } from "@iov/encoding"; import { decodeBech32Pubkey, encodeAddress, isValidAddress } from "./address"; -import { PubKeyEd25519, PubKeySecp256k1 } from "./types"; const { toBase64, fromHex } = Encoding; @@ -37,7 +36,7 @@ describe("address", () => { describe("encodeAddress", () => { it("works for Secp256k1 compressed", () => { const prefix = "cosmos"; - const pubkey: PubKeySecp256k1 = { + const pubkey = { type: "tendermint/PubKeySecp256k1", value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP", }; @@ -46,7 +45,7 @@ describe("address", () => { it("works for Ed25519", () => { const prefix = "cosmos"; - const pubkey: PubKeyEd25519 = { + const pubkey = { type: "tendermint/PubKeyEd25519", value: toBase64(fromHex("12ee6f581fe55673a1e9e1382a0829e32075a0aa4763c968bc526e1852e78c95")), }; diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index b7db4963..5c3f1d60 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -100,24 +100,21 @@ export interface StdSignature { readonly signature: string; } -// value field is base64-encoded in all cases -export type PubKey = PubKeyEd25519 | PubKeySecp256k1 | PubKeySr25519; - -export interface PubKeySecp256k1 { - readonly type: "tendermint/PubKeySecp256k1"; - // Note: this contains a Secp256k1 COMPRESSED pubkey - to encode from bcp/keycontrol land, you must compress it first +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; } -export interface PubKeyEd25519 { - readonly type: "tendermint/PubKeyEd25519"; - readonly value: string; -} - -export interface PubKeySr25519 { - readonly type: "tendermint/PubKeySr25519"; - readonly value: string; -} +export const pubkeyTypes: string[] = [ + "tendermint/PubKeySecp256k1", + "tendermint/PubKeyEd25519", + "tendermint/PubKeySr25519", +]; // bech32-encoded amino-binary encoded PubKey interface. oof. export type Bech32PubKey = string; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 4723c474..81880b03 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -70,19 +70,11 @@ export interface StdSignature { readonly pub_key: PubKey; readonly signature: string; } -export declare type PubKey = PubKeyEd25519 | PubKeySecp256k1 | PubKeySr25519; -export interface PubKeySecp256k1 { - readonly type: "tendermint/PubKeySecp256k1"; - readonly value: string; -} -export interface PubKeyEd25519 { - readonly type: "tendermint/PubKeyEd25519"; - readonly value: string; -} -export interface PubKeySr25519 { - readonly type: "tendermint/PubKeySr25519"; +export interface PubKey { + readonly type: string; readonly value: string; } +export declare const pubkeyTypes: string[]; export declare type Bech32PubKey = string; export interface BaseAccount { /** Bech32 account address */