From 9514982d32c360ea45844f1611bbf8b475602873 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 1 Feb 2020 15:43:13 +0100 Subject: [PATCH] Properly support multiple pubkey types in decodePubkey Closes #27 --- packages/bcp/src/decode.spec.ts | 24 ++++++++++++++++++++++-- packages/bcp/src/decode.ts | 20 ++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/bcp/src/decode.spec.ts b/packages/bcp/src/decode.spec.ts index 64fe28f4..dbc94391 100644 --- a/packages/bcp/src/decode.spec.ts +++ b/packages/bcp/src/decode.spec.ts @@ -17,7 +17,7 @@ import { chainId, nonce, signedTxJson, txId } from "./testdata.spec"; import data from "./testdata/cosmoshub.json"; import { TokenInfos } from "./types"; -const { fromBase64 } = Encoding; +const { fromBase64, fromHex } = Encoding; describe("decode", () => { const defaultPubkey = { @@ -62,13 +62,33 @@ describe("decode", () => { ]; describe("decodePubkey", () => { - it("works", () => { + it("works for secp256k1", () => { const pubkey = { type: "tendermint/PubKeySecp256k1", value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP", }; expect(decodePubkey(pubkey)).toEqual(defaultPubkey); }); + + it("works for ed25519", () => { + const pubkey = { + type: "tendermint/PubKeyEd25519", + value: "s69CnMgLTpuRyEfecjws3mWssBrOICUx8C2O1DkKSto=", + }; + expect(decodePubkey(pubkey)).toEqual({ + algo: Algorithm.Ed25519, + data: fromHex("b3af429cc80b4e9b91c847de723c2cde65acb01ace202531f02d8ed4390a4ada"), + }); + }); + + it("throws for unsupported types", () => { + // https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/sr25519/codec.go#L12 + const pubkey = { + type: "tendermint/PubKeySr25519", + value: "N4FJNPE5r/Twz55kO1QEIxyaGF5/HTXH6WgLQJWsy1o=", + }; + expect(() => decodePubkey(pubkey)).toThrowError(/unsupported pubkey type/i); + }); }); describe("decodeSignature", () => { diff --git a/packages/bcp/src/decode.ts b/packages/bcp/src/decode.ts index 4e2f57f3..d0e1941e 100644 --- a/packages/bcp/src/decode.ts +++ b/packages/bcp/src/decode.ts @@ -24,10 +24,22 @@ import { coinToAmount, isAminoStdTx, TokenInfos } from "./types"; const { fromBase64 } = Encoding; export function decodePubkey(pubkey: amino.PubKey): PubkeyBundle { - return { - algo: Algorithm.Secp256k1, - data: fromBase64(pubkey.value) as PubkeyBytes, - }; + switch (pubkey.type) { + // https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/secp256k1/secp256k1.go#L23 + case "tendermint/PubKeySecp256k1": + return { + algo: Algorithm.Secp256k1, + data: fromBase64(pubkey.value) as PubkeyBytes, + }; + // https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/ed25519/ed25519.go#L22 + case "tendermint/PubKeyEd25519": + return { + algo: Algorithm.Ed25519, + data: fromBase64(pubkey.value) as PubkeyBytes, + }; + default: + throw new Error("Unsupported pubkey type"); + } } export function decodeSignature(signature: string): SignatureBytes {