mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Add Ed25519 support to encodePubkey/anyToSinglePubkey
This commit is contained in:
parent
e8d0126613
commit
3067ab09ed
@ -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.
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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",
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user