Support LegacyAminoPubKey in decodePubkey
This commit is contained in:
parent
b8570f83b3
commit
dc052d8229
@ -3,6 +3,7 @@ import {
|
|||||||
encodeSecp256k1Pubkey,
|
encodeSecp256k1Pubkey,
|
||||||
isMultisigThresholdPubkey,
|
isMultisigThresholdPubkey,
|
||||||
isSecp256k1Pubkey,
|
isSecp256k1Pubkey,
|
||||||
|
MultisigThresholdPubkey,
|
||||||
Pubkey,
|
Pubkey,
|
||||||
SinglePubkey,
|
SinglePubkey,
|
||||||
} from "@cosmjs/amino";
|
} from "@cosmjs/amino";
|
||||||
@ -36,16 +37,37 @@ export function encodePubkey(pubkey: Pubkey): Any {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decodePubkey(pubkey?: Any | null): SinglePubkey | null {
|
function decodeSinglePubkey(pubkey: Any): SinglePubkey {
|
||||||
if (!pubkey || !pubkey.value) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (pubkey.typeUrl) {
|
switch (pubkey.typeUrl) {
|
||||||
case "/cosmos.crypto.secp256k1.PubKey": {
|
case "/cosmos.crypto.secp256k1.PubKey": {
|
||||||
const { key } = PubKey.decode(pubkey.value);
|
const { key } = PubKey.decode(pubkey.value);
|
||||||
return encodeSecp256k1Pubkey(key);
|
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:
|
default:
|
||||||
throw new Error(`Pubkey type_url ${pubkey.typeUrl} not recognized`);
|
throw new Error(`Pubkey type_url ${pubkey.typeUrl} not recognized`);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user