chore: extract test code

createFrom* functions are only used in test.
By extracting them in different class it should help
bundlers.
This commit is contained in:
fryorcraken.eth 2023-03-03 13:34:04 +11:00
parent 031f4c0c21
commit 8fd1455122
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 48 additions and 39 deletions

View File

@ -1,6 +1,7 @@
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { multiaddr } from "@multiformats/multiaddr"; import { multiaddr } from "@multiformats/multiaddr";
import { ENR } from "@waku/enr"; import { ENR } from "@waku/enr";
import { EnrCreator } from "@waku/enr";
import type { Waku2 } from "@waku/interfaces"; import type { Waku2 } from "@waku/interfaces";
import { expect } from "chai"; import { expect } from "chai";
@ -8,7 +9,7 @@ import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js";
async function createEnr(waku2: Waku2): Promise<ENR> { async function createEnr(waku2: Waku2): Promise<ENR> {
const peerId = await createSecp256k1PeerId(); 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.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [ enr.multiaddrs = [
multiaddr("/dns4/node1.do-ams.wakuv2.test.statusim.net/tcp/443/wss"), multiaddr("/dns4/node1.do-ams.wakuv2.test.statusim.net/tcp/443/wss"),

View File

@ -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<ENRKey, ENRValue> = {}
): Promise<ENR> {
// 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<ENRKey, ENRValue> = {}
): Promise<ENR> {
switch (peerId.type) {
case "secp256k1":
return EnrCreator.fromPublicKey(getPublicKeyFromPeerId(peerId), kvs);
default:
throw new Error();
}
}
}

View File

@ -7,6 +7,7 @@ import { assert, expect } from "chai";
import { equals } from "uint8arrays/equals"; import { equals } from "uint8arrays/equals";
import { ERR_INVALID_ID } from "./constants.js"; import { ERR_INVALID_ID } from "./constants.js";
import { EnrCreator } from "./creator.js";
import { ENR } from "./enr.js"; import { ENR } from "./enr.js";
import { getPrivateKeyFromPeerId } from "./peer_id.js"; import { getPrivateKeyFromPeerId } from "./peer_id.js";
@ -14,7 +15,7 @@ describe("ENR", function () {
describe("Txt codec", () => { describe("Txt codec", () => {
it("should encodeTxt and decodeTxt", async () => { it("should encodeTxt and decodeTxt", async () => {
const peerId = await createSecp256k1PeerId(); const peerId = await createSecp256k1PeerId();
const enr = await ENR.createFromPeerId(peerId); const enr = await EnrCreator.fromPeerId(peerId);
const privateKey = await getPrivateKeyFromPeerId(peerId); const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [ enr.multiaddrs = [
@ -106,7 +107,7 @@ describe("ENR", function () {
it("should throw error - no id", async () => { it("should throw error - no id", async () => {
try { try {
const peerId = await createSecp256k1PeerId(); const peerId = await createSecp256k1PeerId();
const enr = await ENR.createFromPeerId(peerId); const enr = await EnrCreator.fromPeerId(peerId);
const privateKey = await getPrivateKeyFromPeerId(peerId); const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
@ -194,7 +195,7 @@ describe("ENR", function () {
privateKey = hexToBytes( privateKey = hexToBytes(
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" "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.setLocationMultiaddr(multiaddr("/ip4/127.0.0.1/udp/30303"));
record.seq = seq; record.seq = seq;
await record.encodeTxt(privateKey); await record.encodeTxt(privateKey);
@ -239,7 +240,7 @@ describe("ENR", function () {
privateKey = hexToBytes( privateKey = hexToBytes(
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
); );
record = await ENR.createFromPublicKey(secp.getPublicKey(privateKey)); record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
}); });
it("should get / set UDP multiaddr", () => { it("should get / set UDP multiaddr", () => {
@ -308,7 +309,7 @@ describe("ENR", function () {
before(async function () { before(async function () {
peerId = await createSecp256k1PeerId(); peerId = await createSecp256k1PeerId();
enr = await ENR.createFromPeerId(peerId); enr = await EnrCreator.fromPeerId(peerId);
enr.ip = ip4; enr.ip = ip4;
enr.ip6 = ip6; enr.ip6 = ip6;
enr.tcp = tcp; enr.tcp = tcp;
@ -388,7 +389,7 @@ describe("ENR", function () {
beforeEach(async function () { beforeEach(async function () {
peerId = await createSecp256k1PeerId(); peerId = await createSecp256k1PeerId();
enr = await ENR.createFromPeerId(peerId); enr = await EnrCreator.fromPeerId(peerId);
privateKey = await getPrivateKeyFromPeerId(peerId); privateKey = await getPrivateKeyFromPeerId(peerId);
waku2Protocols = { waku2Protocols = {
relay: false, relay: false,

View File

@ -23,13 +23,10 @@ import {
ERR_NO_SIGNATURE, ERR_NO_SIGNATURE,
MAX_RECORD_SIZE, MAX_RECORD_SIZE,
} from "./constants.js"; } from "./constants.js";
import { compressPublicKey, keccak256, verifySignature } from "./crypto.js"; import { keccak256, verifySignature } from "./crypto.js";
import { multiaddrFromFields } from "./multiaddr_from_fields.js"; import { multiaddrFromFields } from "./multiaddr_from_fields.js";
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js"; import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
import { import { createPeerIdFromPublicKey } from "./peer_id.js";
createPeerIdFromPublicKey,
getPublicKeyFromPeerId,
} from "./peer_id.js";
import * as v4 from "./v4.js"; import * as v4 from "./v4.js";
import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js"; import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
@ -69,33 +66,6 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
return enr; return enr;
} }
static createFromPublicKey(
publicKey: Uint8Array,
kvs: Record<ENRKey, ENRValue> = {}
): Promise<ENR> {
// 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<ENRKey, ENRValue> = {}
): Promise<ENR> {
switch (peerId.type) {
case "secp256k1":
return ENR.createFromPublicKey(getPublicKeyFromPeerId(peerId), kvs);
default:
throw new Error();
}
}
static async decodeFromValues(decoded: Uint8Array[]): Promise<ENR> { static async decodeFromValues(decoded: Uint8Array[]): Promise<ENR> {
if (!Array.isArray(decoded)) { if (!Array.isArray(decoded)) {
throw new Error("Decoded ENR must be an array"); throw new Error("Decoded ENR must be an array");

View File

@ -1,4 +1,5 @@
export * from "./constants.js"; export * from "./constants.js";
export * from "./creator.js";
export * from "./enr.js"; export * from "./enr.js";
export * from "./peer_id.js"; export * from "./peer_id.js";
export * from "./waku2_codec.js"; export * from "./waku2_codec.js";