From d27736589dd39a239813cec1b8881b750af40649 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 7 Oct 2023 00:35:19 +0300 Subject: [PATCH] Add decodeOptionalPubkey and use it --- packages/proto-signing/src/index.ts | 2 +- packages/proto-signing/src/pubkey.ts | 26 +++++++++++++++++++++++++- packages/stargate/src/accounts.ts | 4 ++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index c34d0ff7..9c25e61a 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -8,7 +8,7 @@ export { } from "./directsecp256k1hdwallet"; export { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; export { makeCosmoshubPath } from "./paths"; -export { anyToSinglePubkey, decodePubkey, encodePubkey } from "./pubkey"; +export { anyToSinglePubkey, decodeOptionalPubkey, decodePubkey, encodePubkey } from "./pubkey"; export { DecodeObject, EncodeObject, diff --git a/packages/proto-signing/src/pubkey.ts b/packages/proto-signing/src/pubkey.ts index 8fc935f9..8ccaa943 100644 --- a/packages/proto-signing/src/pubkey.ts +++ b/packages/proto-signing/src/pubkey.ts @@ -97,6 +97,30 @@ export function decodePubkey(pubkey: Any): Pubkey { return out; } default: - throw new Error(`Pubkey type_url ${pubkey.typeUrl} not recognized`); + throw new Error(`Pubkey type URL '${pubkey.typeUrl}' not recognized`); + } +} + +/** + * Decodes an optional pubkey from a protobuf `Any` into `Pubkey | null`. + * This supports single pubkeys such as Cosmos ed25519 and secp256k1 keys + * as well as multisig threshold pubkeys. + */ +export function decodeOptionalPubkey(pubkey: Any | null | undefined): Pubkey | null { + if (!pubkey) return null; + if (pubkey.typeUrl) { + if (pubkey.value.length) { + // both set + return decodePubkey(pubkey); + } else { + throw new Error(`Pubkey is an Any with type URL '${pubkey.typeUrl}' but an empty value`); + } + } else { + if (pubkey.value.length) { + throw new Error(`Pubkey is an Any with an empty type URL but a value set`); + } else { + // both unset, assuming this empty instance means null + return null; + } } } diff --git a/packages/stargate/src/accounts.ts b/packages/stargate/src/accounts.ts index a0cac7ca..95a765ab 100644 --- a/packages/stargate/src/accounts.ts +++ b/packages/stargate/src/accounts.ts @@ -1,6 +1,6 @@ import { Pubkey } from "@cosmjs/amino"; import { Uint64 } from "@cosmjs/math"; -import { decodePubkey } from "@cosmjs/proto-signing"; +import { decodeOptionalPubkey } from "@cosmjs/proto-signing"; import { assert } from "@cosmjs/utils"; import { BaseAccount, ModuleAccount } from "cosmjs-types/cosmos/auth/v1beta1/auth"; import { @@ -25,7 +25,7 @@ function uint64FromProto(input: number | bigint): Uint64 { function accountFromBaseAccount(input: BaseAccount): Account { const { address, pubKey, accountNumber, sequence } = input; - const pubkey = pubKey ? decodePubkey(pubKey) : null; + const pubkey = decodeOptionalPubkey(pubKey); return { address: address, pubkey: pubkey,