diff --git a/packages/enr/src/enr.spec.ts b/packages/enr/src/enr.spec.ts index c45a1eb2b2..6a61d9a4d2 100644 --- a/packages/enr/src/enr.spec.ts +++ b/packages/enr/src/enr.spec.ts @@ -8,14 +8,14 @@ import { equals } from "uint8arrays/equals"; import { ERR_INVALID_ID } from "./constants.js"; import { getPublicKey } from "./crypto.js"; import { ENR } from "./enr.js"; -import { createKeypairFromPeerId, IKeypair } from "./keypair/index.js"; +import { getPrivateKeyFromPeerId } from "./keypair/index.js"; describe("ENR", function () { describe("Txt codec", () => { it("should encodeTxt and decodeTxt", async () => { const peerId = await createSecp256k1PeerId(); const enr = await ENR.createFromPeerId(peerId); - const keypair = await createKeypairFromPeerId(peerId); + const privateKey = await getPrivateKeyFromPeerId(peerId); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.multiaddrs = [ multiaddr("/dns4/node1.do-ams.wakuv2.test.statusim.net/tcp/443/wss"), @@ -32,7 +32,7 @@ describe("ENR", function () { lightPush: false, }; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const enr2 = await ENR.decodeTxt(txt); if (!enr.signature) throw "enr.signature is undefined"; @@ -107,11 +107,11 @@ describe("ENR", function () { try { const peerId = await createSecp256k1PeerId(); const enr = await ENR.createFromPeerId(peerId); - const keypair = await createKeypairFromPeerId(peerId); + const privateKey = await getPrivateKeyFromPeerId(peerId); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.set("id", new Uint8Array([0])); - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); await ENR.decodeTxt(txt); assert.fail("Expect error here"); @@ -384,12 +384,12 @@ describe("ENR", function () { let peerId; let enr: ENR; let waku2Protocols: Waku2; - let keypair: IKeypair; + let privateKey: Uint8Array; beforeEach(async function () { peerId = await createSecp256k1PeerId(); enr = await ENR.createFromPeerId(peerId); - keypair = await createKeypairFromPeerId(peerId); + privateKey = await getPrivateKeyFromPeerId(peerId); waku2Protocols = { relay: false, store: false, @@ -401,7 +401,7 @@ describe("ENR", function () { it("should set field with all protocols disabled", async () => { enr.waku2 = waku2Protocols; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const decoded = (await ENR.decodeTxt(txt)).waku2!; expect(decoded.relay).to.equal(false); @@ -417,7 +417,7 @@ describe("ENR", function () { waku2Protocols.lightPush = true; enr.waku2 = waku2Protocols; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const decoded = (await ENR.decodeTxt(txt)).waku2!; expect(decoded.relay).to.equal(true); @@ -430,7 +430,7 @@ describe("ENR", function () { waku2Protocols.relay = true; enr.waku2 = waku2Protocols; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const decoded = (await ENR.decodeTxt(txt)).waku2!; expect(decoded.relay).to.equal(true); @@ -443,7 +443,7 @@ describe("ENR", function () { waku2Protocols.store = true; enr.waku2 = waku2Protocols; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const decoded = (await ENR.decodeTxt(txt)).waku2!; expect(decoded.relay).to.equal(false); @@ -456,7 +456,7 @@ describe("ENR", function () { waku2Protocols.filter = true; enr.waku2 = waku2Protocols; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const decoded = (await ENR.decodeTxt(txt)).waku2!; expect(decoded.relay).to.equal(false); @@ -469,7 +469,7 @@ describe("ENR", function () { waku2Protocols.lightPush = true; enr.waku2 = waku2Protocols; - const txt = await enr.encodeTxt(keypair.privateKey); + const txt = await enr.encodeTxt(privateKey); const decoded = (await ENR.decodeTxt(txt)).waku2!; expect(decoded.relay).to.equal(false); diff --git a/packages/enr/src/enr.ts b/packages/enr/src/enr.ts index 8b690e66da..682f7db603 100644 --- a/packages/enr/src/enr.ts +++ b/packages/enr/src/enr.ts @@ -26,8 +26,8 @@ import { import { compressPublicKey, keccak256, verifySignature } from "./crypto.js"; import { createKeypair, - createKeypairFromPeerId, createPeerIdFromPublicKey, + getPublicKeyFromPeerId, IKeypair, KeypairType, } from "./keypair/index.js"; @@ -91,10 +91,9 @@ export class ENR extends Map implements IEnr { peerId: PeerId, kvs: Record = {} ): Promise { - const keypair = await createKeypairFromPeerId(peerId); - switch (keypair.type) { - case KeypairType.secp256k1: - return ENR.createV4(keypair.publicKey, kvs); + switch (peerId.type) { + case "secp256k1": + return ENR.createV4(getPublicKeyFromPeerId(peerId), kvs); default: throw new Error(); } diff --git a/packages/enr/src/keypair/index.ts b/packages/enr/src/keypair/index.ts index 7f76c0465f..b656d65788 100644 --- a/packages/enr/src/keypair/index.ts +++ b/packages/enr/src/keypair/index.ts @@ -30,34 +30,25 @@ export function createKeypair( } } -export async function createKeypairFromPeerId( - peerId: PeerId -): Promise { - let keypairType; - switch (peerId.type) { - case "RSA": - keypairType = KeypairType.rsa; - break; - case "Ed25519": - keypairType = KeypairType.ed25519; - break; - case "secp256k1": - keypairType = KeypairType.secp256k1; - break; - default: - throw new Error("Unsupported peer id type"); +export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array { + if (peerId.type !== "secp256k1") { + throw new Error("Unsupported peer id type"); } - const publicKey = peerId.publicKey - ? unmarshalPublicKey(peerId.publicKey) - : undefined; - const privateKey = peerId.privateKey - ? await unmarshalPrivateKey(peerId.privateKey) - : undefined; - - return createKeypair( - keypairType, - privateKey?.marshal(), - publicKey?.marshal() - ); + return unmarshalPublicKey(peerId.publicKey).marshal(); +} + +// Only used in tests +export async function getPrivateKeyFromPeerId( + peerId: PeerId +): Promise { + if (peerId.type !== "secp256k1") { + throw new Error("Unsupported peer id type"); + } + if (!peerId.privateKey) { + throw new Error("Private key not present on peer id"); + } + + const privateKey = await unmarshalPrivateKey(peerId.privateKey); + return privateKey.marshal(); }