From 3067ab09ed1a8919dc9d14d11c5f3876cbd4ab6b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 13 Oct 2022 14:34:32 +0200 Subject: [PATCH] Add Ed25519 support to encodePubkey/anyToSinglePubkey --- CHANGELOG.md | 2 ++ packages/proto-signing/src/index.ts | 2 +- packages/proto-signing/src/pubkey.spec.ts | 14 +++++++++++ packages/proto-signing/src/pubkey.ts | 30 +++++++++++++++++------ 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce58ea2..fbf0aaaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to - @cosmjs/amino: Add `encodeEd25519Pubkey` analogue to the existing `encodeSecp256k1Pubkey`. +- @cosmjs/proto-signing: Add Ed25519 support to `anyToSinglePubkey` and make + export `anyToSinglePubkey`. - @cosmjs/utils: Add `isDefined` which checks for `undefined` in a TypeScript-friendly way. diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index 1993b90a..c34d0ff7 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 { decodePubkey, encodePubkey } from "./pubkey"; +export { anyToSinglePubkey, decodePubkey, encodePubkey } from "./pubkey"; export { DecodeObject, EncodeObject, diff --git a/packages/proto-signing/src/pubkey.spec.ts b/packages/proto-signing/src/pubkey.spec.ts index fe7f9d56..5d5f190e 100644 --- a/packages/proto-signing/src/pubkey.spec.ts +++ b/packages/proto-signing/src/pubkey.spec.ts @@ -8,6 +8,9 @@ describe("pubkey", () => { const defaultPubkeyBase64 = "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP"; const defaultPubkeyBytes = fromBase64(defaultPubkeyBase64); const defaultPubkeyProtoBytes = Uint8Array.from([0x0a, defaultPubkeyBytes.length, ...defaultPubkeyBytes]); + const ed25519PubkeyBase64 = "kEX3edqZB+HdCV92TPS7ePX0DtP62GWIjmrveZ5pnaQ="; + const ed25519PubkeyBytes = fromBase64(ed25519PubkeyBase64); + const ed25519PubkeyProtoBytes = Uint8Array.from([0x0a, ed25519PubkeyBytes.length, ...ed25519PubkeyBytes]); describe("encodePubkey", () => { it("works for secp256k1", () => { @@ -41,6 +44,17 @@ describe("pubkey", () => { }); }); + it("works for ed25519", () => { + const pubkey = { + typeUrl: "/cosmos.crypto.ed25519.PubKey", + value: ed25519PubkeyProtoBytes, + }; + expect(decodePubkey(pubkey)).toEqual({ + type: "tendermint/PubKeyEd25519", + value: ed25519PubkeyBase64, + }); + }); + it("throws for unsupported pubkey types", () => { const pubkey = { typeUrl: "/cosmos.crypto.unknown.PubKey", diff --git a/packages/proto-signing/src/pubkey.ts b/packages/proto-signing/src/pubkey.ts index 9af1af61..38b3b260 100644 --- a/packages/proto-signing/src/pubkey.ts +++ b/packages/proto-signing/src/pubkey.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { + encodeEd25519Pubkey, encodeSecp256k1Pubkey, isMultisigThresholdPubkey, isSecp256k1Pubkey, @@ -9,18 +10,19 @@ import { } from "@cosmjs/amino"; import { fromBase64 } from "@cosmjs/encoding"; import { Uint53 } from "@cosmjs/math"; +import { PubKey as CosmosCryptoEd25519Pubkey } from "cosmjs-types/cosmos/crypto/ed25519/keys"; import { LegacyAminoPubKey } from "cosmjs-types/cosmos/crypto/multisig/keys"; -import { PubKey } from "cosmjs-types/cosmos/crypto/secp256k1/keys"; +import { PubKey as CosmosCryptoSecp256k1Pubkey } from "cosmjs-types/cosmos/crypto/secp256k1/keys"; import { Any } from "cosmjs-types/google/protobuf/any"; export function encodePubkey(pubkey: Pubkey): Any { if (isSecp256k1Pubkey(pubkey)) { - const pubkeyProto = PubKey.fromPartial({ + const pubkeyProto = CosmosCryptoSecp256k1Pubkey.fromPartial({ key: fromBase64(pubkey.value), }); return Any.fromPartial({ typeUrl: "/cosmos.crypto.secp256k1.PubKey", - value: Uint8Array.from(PubKey.encode(pubkeyProto).finish()), + value: Uint8Array.from(CosmosCryptoSecp256k1Pubkey.encode(pubkeyProto).finish()), }); } else if (isMultisigThresholdPubkey(pubkey)) { const pubkeyProto = LegacyAminoPubKey.fromPartial({ @@ -36,12 +38,23 @@ export function encodePubkey(pubkey: Pubkey): Any { } } -function decodeSinglePubkey(pubkey: Any): SinglePubkey { +/** + * Decodes a single pubkey (i.e. not a multisig pubkey) from `Any` into + * `SinglePubkey`. + * + * In most cases you probably want to use `decodePubkey`, but `anyToSinglePubkey` + * might be preferred in CosmJS 0.29.x due to https://github.com/cosmos/cosmjs/issues/1289. + */ +export function anyToSinglePubkey(pubkey: Any): SinglePubkey { switch (pubkey.typeUrl) { case "/cosmos.crypto.secp256k1.PubKey": { - const { key } = PubKey.decode(pubkey.value); + const { key } = CosmosCryptoSecp256k1Pubkey.decode(pubkey.value); return encodeSecp256k1Pubkey(key); } + case "/cosmos.crypto.ed25519.PubKey": { + const { key } = CosmosCryptoEd25519Pubkey.decode(pubkey.value); + return encodeEd25519Pubkey(key); + } default: throw new Error(`Pubkey type_url ${pubkey.typeUrl} not recognized as single public key type`); } @@ -53,8 +66,9 @@ export function decodePubkey(pubkey?: Any | null): Pubkey | null { } switch (pubkey.typeUrl) { - case "/cosmos.crypto.secp256k1.PubKey": { - return decodeSinglePubkey(pubkey); + case "/cosmos.crypto.secp256k1.PubKey": + case "/cosmos.crypto.ed25519.PubKey": { + return anyToSinglePubkey(pubkey); } case "/cosmos.crypto.multisig.LegacyAminoPubKey": { const { threshold, publicKeys } = LegacyAminoPubKey.decode(pubkey.value); @@ -62,7 +76,7 @@ export function decodePubkey(pubkey?: Any | null): Pubkey | null { type: "tendermint/PubKeyMultisigThreshold", value: { threshold: threshold.toString(), - pubkeys: publicKeys.map(decodeSinglePubkey), + pubkeys: publicKeys.map(anyToSinglePubkey), }, }; return out;