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 { 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<ENR> {
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"),

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 { 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,

View File

@ -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<ENRKey, ENRValue> implements IEnr {
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> {
if (!Array.isArray(decoded)) {
throw new Error("Decoded ENR must be an array");

View File

@ -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";