From dc052d8229f805c9bb1eee36bf6e28242b8f331b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 23 Mar 2021 23:52:28 +0100 Subject: [PATCH] Support LegacyAminoPubKey in decodePubkey --- packages/proto-signing/src/pubkey.ts | 32 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/proto-signing/src/pubkey.ts b/packages/proto-signing/src/pubkey.ts index ebf9f0c5..b04c1673 100644 --- a/packages/proto-signing/src/pubkey.ts +++ b/packages/proto-signing/src/pubkey.ts @@ -3,6 +3,7 @@ import { encodeSecp256k1Pubkey, isMultisigThresholdPubkey, isSecp256k1Pubkey, + MultisigThresholdPubkey, Pubkey, SinglePubkey, } from "@cosmjs/amino"; @@ -36,16 +37,37 @@ export function encodePubkey(pubkey: Pubkey): Any { } } -export function decodePubkey(pubkey?: Any | null): SinglePubkey | null { - if (!pubkey || !pubkey.value) { - return null; - } - +function decodeSinglePubkey(pubkey: Any): SinglePubkey { switch (pubkey.typeUrl) { case "/cosmos.crypto.secp256k1.PubKey": { const { key } = PubKey.decode(pubkey.value); return encodeSecp256k1Pubkey(key); } + default: + throw new Error(`Pubkey type_url ${pubkey.typeUrl} not recognized as single public key type`); + } +} + +export function decodePubkey(pubkey?: Any | null): Pubkey | null { + if (!pubkey || !pubkey.value) { + return null; + } + + switch (pubkey.typeUrl) { + case "/cosmos.crypto.secp256k1.PubKey": { + return decodeSinglePubkey(pubkey); + } + case "/cosmos.crypto.multisig.LegacyAminoPubKey": { + const { threshold, publicKeys } = LegacyAminoPubKey.decode(pubkey.value); + const out: MultisigThresholdPubkey = { + type: "tendermint/PubKeyMultisigThreshold", + value: { + threshold: threshold.toString(), + pubkeys: publicKeys.map(decodeSinglePubkey), + }, + }; + return out; + } default: throw new Error(`Pubkey type_url ${pubkey.typeUrl} not recognized`); }