diff --git a/packages/dns-discovery/src/fetch_nodes.spec.ts b/packages/dns-discovery/src/fetch_nodes.spec.ts index 93eaffbbd0..efb085c7aa 100644 --- a/packages/dns-discovery/src/fetch_nodes.spec.ts +++ b/packages/dns-discovery/src/fetch_nodes.spec.ts @@ -1,6 +1,7 @@ import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; import { multiaddr } from "@multiformats/multiaddr"; import { ENR } from "@waku/enr"; +import { EnrCreator } from "@waku/enr"; import type { Waku2 } from "@waku/interfaces"; import { expect } from "chai"; @@ -8,7 +9,7 @@ import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js"; async function createEnr(waku2: Waku2): Promise { const peerId = await createSecp256k1PeerId(); - const enr = await ENR.createFromPeerId(peerId); + const enr = await EnrCreator.fromPeerId(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"), diff --git a/packages/enr/src/creator.ts b/packages/enr/src/creator.ts new file mode 100644 index 0000000000..b310789184 --- /dev/null +++ b/packages/enr/src/creator.ts @@ -0,0 +1,36 @@ +import { PeerId } from "@libp2p/interface-peer-id"; +import type { ENRKey, ENRValue } from "@waku/interfaces"; +import { utf8ToBytes } from "@waku/utils"; + +import { compressPublicKey } from "./crypto.js"; +import { ENR } from "./enr.js"; +import { getPublicKeyFromPeerId } from "./peer_id.js"; + +export class EnrCreator { + static fromPublicKey( + publicKey: Uint8Array, + kvs: Record = {} + ): Promise { + // EIP-778 specifies that the key must be in compressed format, 33 bytes + if (publicKey.length !== 33) { + publicKey = compressPublicKey(publicKey); + } + return ENR.create({ + ...kvs, + id: utf8ToBytes("v4"), + secp256k1: publicKey, + }); + } + + static async fromPeerId( + peerId: PeerId, + kvs: Record = {} + ): Promise { + switch (peerId.type) { + case "secp256k1": + return EnrCreator.fromPublicKey(getPublicKeyFromPeerId(peerId), kvs); + default: + throw new Error(); + } + } +} diff --git a/packages/enr/src/enr.spec.ts b/packages/enr/src/enr.spec.ts index c23e596e11..65e60dd6ea 100644 --- a/packages/enr/src/enr.spec.ts +++ b/packages/enr/src/enr.spec.ts @@ -7,6 +7,7 @@ import { assert, expect } from "chai"; import { equals } from "uint8arrays/equals"; import { ERR_INVALID_ID } from "./constants.js"; +import { EnrCreator } from "./creator.js"; import { ENR } from "./enr.js"; import { getPrivateKeyFromPeerId } from "./peer_id.js"; @@ -14,7 +15,7 @@ describe("ENR", function () { describe("Txt codec", () => { it("should encodeTxt and decodeTxt", async () => { const peerId = await createSecp256k1PeerId(); - const enr = await ENR.createFromPeerId(peerId); + const enr = await EnrCreator.fromPeerId(peerId); const privateKey = await getPrivateKeyFromPeerId(peerId); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.multiaddrs = [ @@ -106,7 +107,7 @@ describe("ENR", function () { it("should throw error - no id", async () => { try { const peerId = await createSecp256k1PeerId(); - const enr = await ENR.createFromPeerId(peerId); + const enr = await EnrCreator.fromPeerId(peerId); const privateKey = await getPrivateKeyFromPeerId(peerId); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); @@ -194,7 +195,7 @@ describe("ENR", function () { privateKey = hexToBytes( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" ); - record = await ENR.createFromPublicKey(secp.getPublicKey(privateKey)); + record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey)); record.setLocationMultiaddr(multiaddr("/ip4/127.0.0.1/udp/30303")); record.seq = seq; await record.encodeTxt(privateKey); @@ -239,7 +240,7 @@ describe("ENR", function () { privateKey = hexToBytes( "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" ); - record = await ENR.createFromPublicKey(secp.getPublicKey(privateKey)); + record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey)); }); it("should get / set UDP multiaddr", () => { @@ -308,7 +309,7 @@ describe("ENR", function () { before(async function () { peerId = await createSecp256k1PeerId(); - enr = await ENR.createFromPeerId(peerId); + enr = await EnrCreator.fromPeerId(peerId); enr.ip = ip4; enr.ip6 = ip6; enr.tcp = tcp; @@ -388,7 +389,7 @@ describe("ENR", function () { beforeEach(async function () { peerId = await createSecp256k1PeerId(); - enr = await ENR.createFromPeerId(peerId); + enr = await EnrCreator.fromPeerId(peerId); privateKey = await getPrivateKeyFromPeerId(peerId); waku2Protocols = { relay: false, diff --git a/packages/enr/src/enr.ts b/packages/enr/src/enr.ts index 73a12b5cbc..8ee793ab8f 100644 --- a/packages/enr/src/enr.ts +++ b/packages/enr/src/enr.ts @@ -23,13 +23,10 @@ import { ERR_NO_SIGNATURE, MAX_RECORD_SIZE, } from "./constants.js"; -import { compressPublicKey, keccak256, verifySignature } from "./crypto.js"; +import { keccak256, verifySignature } from "./crypto.js"; import { multiaddrFromFields } from "./multiaddr_from_fields.js"; import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js"; -import { - createPeerIdFromPublicKey, - getPublicKeyFromPeerId, -} from "./peer_id.js"; +import { createPeerIdFromPublicKey } from "./peer_id.js"; import * as v4 from "./v4.js"; import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js"; @@ -69,33 +66,6 @@ export class ENR extends Map implements IEnr { return enr; } - static createFromPublicKey( - publicKey: Uint8Array, - kvs: Record = {} - ): Promise { - // EIP-778 specifies that the key must be in compressed format, 33 bytes - if (publicKey.length !== 33) { - publicKey = compressPublicKey(publicKey); - } - return ENR.create({ - ...kvs, - id: utf8ToBytes("v4"), - secp256k1: publicKey, - }); - } - - static async createFromPeerId( - peerId: PeerId, - kvs: Record = {} - ): Promise { - switch (peerId.type) { - case "secp256k1": - return ENR.createFromPublicKey(getPublicKeyFromPeerId(peerId), kvs); - default: - throw new Error(); - } - } - static async decodeFromValues(decoded: Uint8Array[]): Promise { if (!Array.isArray(decoded)) { throw new Error("Decoded ENR must be an array"); diff --git a/packages/enr/src/index.ts b/packages/enr/src/index.ts index 45e2a3bb4e..496c7e82c2 100644 --- a/packages/enr/src/index.ts +++ b/packages/enr/src/index.ts @@ -1,4 +1,5 @@ export * from "./constants.js"; +export * from "./creator.js"; export * from "./enr.js"; export * from "./peer_id.js"; export * from "./waku2_codec.js";