Support LegacyAminoPubKey in decodePubkey

This commit is contained in:
Simon Warta 2021-03-23 23:52:28 +01:00
parent b8570f83b3
commit dc052d8229

View File

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