merge with master

This commit is contained in:
Sasha 2024-10-24 01:48:11 +02:00
commit 1eaedb3923
No known key found for this signature in database
29 changed files with 1399 additions and 1413 deletions

View File

@ -32,6 +32,7 @@
"Dscore",
"ecies",
"editorconfig",
"Encrypters",
"enr",
"enrs",
"enrtree",

2504
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@
"test": "npx playwright test"
},
"devDependencies": {
"@playwright/test": "^1.40.1",
"@playwright/test": "^1.48.1",
"@waku/create-app": "^0.1.1-504bcd4",
"dotenv-flow": "^4.1.0",
"serve": "^14.2.3"

View File

@ -68,9 +68,9 @@
"node": ">=20"
},
"dependencies": {
"@libp2p/ping": "^1.1.2",
"@waku/enr": "^0.0.27",
"@waku/interfaces": "0.0.28",
"@libp2p/ping": "2.0.1",
"@waku/proto": "0.0.8",
"@waku/utils": "0.0.21",
"debug": "^4.3.4",
@ -81,6 +81,7 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.0.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
@ -103,7 +104,7 @@
},
"peerDependencies": {
"@multiformats/multiaddr": "^12.0.0",
"libp2p": "^1.8.1"
"libp2p": "2.1.8"
},
"peerDependenciesMeta": {
"@multiformats/multiaddr": {

View File

@ -1,5 +1,5 @@
import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import {
ConnectionManagerOptions,
DiscoveryTrigger,

View File

@ -62,8 +62,7 @@
"uint8arrays": "^5.0.1"
},
"devDependencies": {
"@libp2p/peer-id": "^4.2.1",
"@libp2p/peer-id-factory": "^4.2.1",
"@libp2p/peer-id": "5.0.1",
"@multiformats/multiaddr": "^12.3.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
@ -81,7 +80,7 @@
"sinon": "^18.0.0"
},
"peerDependencies": {
"@libp2p/interface": "^1.6.3"
"@libp2p/interface": "2.0.1"
},
"peerDependenciesMeta": {
"@libp2p/interface": {

View File

@ -1,5 +1,4 @@
import {
CustomEvent,
PeerDiscovery,
PeerDiscoveryEvents,
TypedEventEmitter

View File

@ -1,4 +1,5 @@
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { generateKeyPair } from "@libp2p/crypto/keys";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import { ENR } from "@waku/enr";
import { EnrCreator } from "@waku/enr";
@ -8,7 +9,7 @@ import { expect } from "chai";
import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js";
async function createEnr(waku2: Waku2): Promise<ENR> {
const peerId = await createSecp256k1PeerId();
const peerId = await generateKeyPair("secp256k1").then(peerIdFromPrivateKey);
const enr = await EnrCreator.fromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [

View File

@ -1,10 +1,10 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { IdentifyResult } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import tests from "@libp2p/interface-compliance-tests/peer-discovery";
import { prefixLogger } from "@libp2p/logger";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { createFromJSON } from "@libp2p/peer-id-factory";
import { PersistentPeerStore } from "@libp2p/peer-store";
import { peerIdFromPrivateKey, peerIdFromString } from "@libp2p/peer-id";
import { persistentPeerStore } from "@libp2p/peer-store";
import { multiaddr } from "@multiformats/multiaddr";
import { Libp2pComponents } from "@waku/interfaces";
import { LocalStoragePeerInfo } from "@waku/interfaces";
@ -53,9 +53,9 @@ describe("Local Storage Discovery", function () {
beforeEach(async function () {
localStorage.clear();
components = {
peerStore: new PersistentPeerStore({
peerStore: persistentPeerStore({
events: new TypedEventEmitter(),
peerId: await createSecp256k1PeerId(),
peerId: await generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
datastore: new MemoryDatastore(),
logger: prefixLogger("local_discovery.spec.ts")
}),
@ -103,9 +103,7 @@ describe("Local Storage Discovery", function () {
it("should update peers in local storage on 'peer:identify' event", async () => {
const newPeerIdentifyEvent = {
detail: {
peerId: await createFromJSON({
id: mockPeers[1].id
}),
peerId: peerIdFromString(mockPeers[1].id.toString()),
listenAddrs: [multiaddr(mockPeers[1].address)]
}
} as CustomEvent<IdentifyResult>;

View File

@ -1,13 +1,12 @@
import { TypedEventEmitter } from "@libp2p/interface";
import {
CustomEvent,
IdentifyResult,
PeerDiscovery,
PeerDiscoveryEvents,
PeerInfo,
Startable
} from "@libp2p/interface";
import { createFromJSON } from "@libp2p/peer-id-factory";
import { peerIdFromString } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import {
type Libp2pComponents,
@ -58,7 +57,7 @@ export class LocalPeerCacheDiscovery
);
for (const { id: idStr, address } of this.peers) {
const peerId = await createFromJSON({ id: idStr });
const peerId = peerIdFromString(idStr);
if (await this.components.peerStore.has(peerId)) continue;
await this.components.peerStore.save(peerId, {

View File

@ -1,4 +1,4 @@
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import { peerDiscoverySymbol as symbol } from "@libp2p/interface";
import type {
IdentifyResult,

View File

@ -52,8 +52,8 @@
},
"dependencies": {
"@ethersproject/rlp": "^5.7.0",
"@libp2p/crypto": "^4.1.6",
"@libp2p/peer-id": "^4.2.1",
"@libp2p/crypto": "^5.0.1",
"@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.0.0",
"@noble/secp256k1": "^1.7.1",
"@waku/utils": "0.0.21",
@ -61,7 +61,6 @@
"js-sha3": "^0.9.2"
},
"devDependencies": {
"@libp2p/peer-id-factory": "^4.2.1",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.3",

View File

@ -4,13 +4,12 @@ import { utf8ToBytes } from "@waku/utils/bytes";
import { compressPublicKey } from "./crypto.js";
import { ENR } from "./enr.js";
import { getPublicKeyFromPeerId } from "./peer_id.js";
export class EnrCreator {
public static fromPublicKey(
publicKey: Uint8Array,
kvs: Record<ENRKey, ENRValue> = {}
): Promise<ENR> {
): ENR {
// EIP-778 specifies that the key must be in compressed format, 33 bytes
if (publicKey.length !== 33) {
publicKey = compressPublicKey(publicKey);
@ -28,7 +27,7 @@ export class EnrCreator {
): Promise<ENR> {
switch (peerId.type) {
case "secp256k1":
return EnrCreator.fromPublicKey(getPublicKeyFromPeerId(peerId), kvs);
return EnrCreator.fromPublicKey(peerId.publicKey.raw, kvs);
default:
throw new Error();
}

View File

@ -37,7 +37,7 @@ async function fromValues(values: Uint8Array[]): Promise<ENR> {
}
const _seq = decodeSeq(seq);
const enr = await ENR.create(obj, _seq, signature);
const enr = ENR.create(obj, _seq, signature);
checkSignature(seq, kvs, enr, signature);
return enr;
}

View File

@ -1,5 +1,6 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { PeerId } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import * as secp from "@noble/secp256k1";
import type { Waku2 } from "@waku/interfaces";
@ -16,14 +17,13 @@ import {
TransportProtocol,
TransportProtocolPerIpVersion
} from "./enr.js";
import { getPrivateKeyFromPeerId } from "./peer_id.js";
describe("ENR", function () {
describe("Txt codec", () => {
it("should encodeTxt and decodeTxt", async () => {
const peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
const enr = await EnrCreator.fromPeerId(peerId);
const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [
multiaddr("/dns4/node-01.do-ams3.waku.test.status.im/tcp/443/wss"),
@ -42,7 +42,7 @@ describe("ENR", function () {
lightPush: false
};
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKey.raw);
const enr2 = await EnrDecoder.fromString(txt);
if (!enr.signature) throw "enr.signature is undefined";
@ -115,13 +115,13 @@ describe("ENR", function () {
it("should throw error - no id", async () => {
try {
const peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
const enr = await EnrCreator.fromPeerId(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 EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKey.raw);
await EnrDecoder.fromString(txt);
assert.fail("Expect error here");
@ -147,7 +147,7 @@ describe("ENR", function () {
describe("Verify", () => {
it("should throw error - no id", async () => {
try {
const enr = await ENR.create({}, BigInt(0), new Uint8Array());
const enr = ENR.create({}, BigInt(0), new Uint8Array());
enr.verify(new Uint8Array(), new Uint8Array());
assert.fail("Expect error here");
} catch (err: unknown) {
@ -158,7 +158,7 @@ describe("ENR", function () {
it("should throw error - invalid id", async () => {
try {
const enr = await ENR.create(
const enr = ENR.create(
{ id: utf8ToBytes("v3") },
BigInt(0),
new Uint8Array()
@ -173,7 +173,7 @@ describe("ENR", function () {
it("should throw error - no public key", async () => {
try {
const enr = await ENR.create(
const enr = ENR.create(
{ id: utf8ToBytes("v4") },
BigInt(0),
new Uint8Array()
@ -204,7 +204,7 @@ describe("ENR", function () {
privateKey = hexToBytes(
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
);
record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
record = EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
record.setLocationMultiaddr(multiaddr("/ip4/127.0.0.1/udp/30303"));
record.seq = seq;
await EnrEncoder.toString(record, privateKey);
@ -249,7 +249,7 @@ describe("ENR", function () {
privateKey = hexToBytes(
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
);
record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
record = EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
});
it("should get / set UDP multiaddr", () => {
@ -320,7 +320,8 @@ describe("ENR", function () {
let enr: ENR;
before(async function () {
peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
peerId = peerIdFromPrivateKey(privateKey);
enr = await EnrCreator.fromPeerId(peerId);
enr.ip = ip4;
enr.ip6 = ip6;
@ -416,15 +417,16 @@ describe("ENR", function () {
});
describe("waku2 key round trip", () => {
let peerId;
let peerId: PeerId;
let enr: ENR;
let waku2Protocols: Waku2;
let privateKey: Uint8Array;
let privateKeyUint8Arr: Uint8Array;
beforeEach(async function () {
peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
privateKeyUint8Arr = privateKey.raw;
peerId = peerIdFromPrivateKey(privateKey);
enr = await EnrCreator.fromPeerId(peerId);
privateKey = await getPrivateKeyFromPeerId(peerId);
waku2Protocols = {
relay: false,
store: false,
@ -436,7 +438,7 @@ describe("ENR", function () {
it("should set field with all protocols disabled", async () => {
enr.waku2 = waku2Protocols;
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKeyUint8Arr);
const decoded = (await EnrDecoder.fromString(txt)).waku2!;
expect(decoded.relay).to.equal(false);
@ -452,7 +454,7 @@ describe("ENR", function () {
waku2Protocols.lightPush = true;
enr.waku2 = waku2Protocols;
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKeyUint8Arr);
const decoded = (await EnrDecoder.fromString(txt)).waku2!;
expect(decoded.relay).to.equal(true);
@ -465,7 +467,7 @@ describe("ENR", function () {
waku2Protocols.relay = true;
enr.waku2 = waku2Protocols;
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKeyUint8Arr);
const decoded = (await EnrDecoder.fromString(txt)).waku2!;
expect(decoded.relay).to.equal(true);
@ -478,7 +480,7 @@ describe("ENR", function () {
waku2Protocols.store = true;
enr.waku2 = waku2Protocols;
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKeyUint8Arr);
const decoded = (await EnrDecoder.fromString(txt)).waku2!;
expect(decoded.relay).to.equal(false);
@ -491,7 +493,7 @@ describe("ENR", function () {
waku2Protocols.filter = true;
enr.waku2 = waku2Protocols;
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKeyUint8Arr);
const decoded = (await EnrDecoder.fromString(txt)).waku2!;
expect(decoded.relay).to.equal(false);
@ -504,7 +506,7 @@ describe("ENR", function () {
waku2Protocols.lightPush = true;
enr.waku2 = waku2Protocols;
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKeyUint8Arr);
const decoded = (await EnrDecoder.fromString(txt)).waku2!;
expect(decoded.relay).to.equal(false);

View File

@ -34,16 +34,16 @@ export class ENR extends RawEnr implements IEnr {
public static readonly RECORD_PREFIX = "enr:";
public peerId?: PeerId;
public static async create(
public static create(
kvs: Record<ENRKey, ENRValue> = {},
seq: SequenceNumber = BigInt(1),
signature?: Uint8Array
): Promise<ENR> {
): ENR {
const enr = new ENR(kvs, seq, signature);
try {
const publicKey = enr.publicKey;
if (publicKey) {
enr.peerId = await createPeerIdFromPublicKey(publicKey);
enr.peerId = createPeerIdFromPublicKey(publicKey);
}
} catch (e) {
log.error("Could not calculate peer id for ENR", e);

View File

@ -1,38 +1,13 @@
import { unmarshalPrivateKey, unmarshalPublicKey } from "@libp2p/crypto/keys";
import { supportedKeys } from "@libp2p/crypto/keys";
import type { PeerId } from "@libp2p/interface";
import { peerIdFromKeys } from "@libp2p/peer-id";
import { publicKeyFromRaw } from "@libp2p/crypto/keys";
import { type PeerId } from "@libp2p/interface";
import { peerIdFromPublicKey } from "@libp2p/peer-id";
export function createPeerIdFromPublicKey(
publicKey: Uint8Array
): Promise<PeerId> {
const _publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey(publicKey);
return peerIdFromKeys(_publicKey.bytes, undefined);
}
export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array {
if (peerId.type !== "secp256k1") {
throw new Error("Unsupported peer id type");
}
if (!peerId.publicKey) {
throw new Error("Public key not present on peer id");
}
return unmarshalPublicKey(peerId.publicKey).marshal();
}
// Only used in tests
export async function getPrivateKeyFromPeerId(
peerId: PeerId
): Promise<Uint8Array> {
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();
export const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented";
export function createPeerIdFromPublicKey(publicKey: Uint8Array): PeerId {
const pubKey = publicKeyFromRaw(publicKey);
if (pubKey.type !== "secp256k1") {
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
}
return peerIdFromPublicKey(pubKey);
}

View File

@ -47,11 +47,11 @@
"node": ">=20"
},
"devDependencies": {
"@chainsafe/libp2p-gossipsub": "^13.1.0",
"@chainsafe/libp2p-gossipsub": "^14.1.0",
"@multiformats/multiaddr": "^12.0.0",
"cspell": "^8.6.1",
"npm-run-all": "^4.1.5",
"libp2p": "^1.8.1"
"libp2p": "2.1.8"
},
"files": [
"dist",

View File

@ -49,7 +49,7 @@
"node": ">=18"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^13.1.0",
"@chainsafe/libp2p-gossipsub": "^14.1.0",
"@noble/hashes": "^1.3.2",
"@waku/core": "0.0.33",
"@waku/sdk": "0.0.29",
@ -69,7 +69,7 @@
"rollup": "^4.12.0"
},
"peerDependencies": {
"@chainsafe/libp2p-gossipsub": "^12.0.0"
"@chainsafe/libp2p-gossipsub": "^14.1.0"
},
"peerDependenciesMeta": {
"@chainsafe/libp2p-gossipsub": {

View File

@ -1,6 +1,7 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import { TopicValidatorResult } from "@libp2p/interface";
import type { UnsignedMessage } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { createEncoder } from "@waku/core";
import { determinePubsubTopic } from "@waku/utils";
import { expect } from "chai";
@ -15,7 +16,8 @@ describe("Message Validator", () => {
it("Accepts a valid Waku Message", async () => {
await fc.assert(
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
const peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
const encoder = createEncoder({
contentTopic: TestContentTopic,
@ -39,7 +41,8 @@ describe("Message Validator", () => {
it("Rejects garbage", async () => {
await fc.assert(
fc.asyncProperty(fc.uint8Array(), async (data) => {
const peerId = await createSecp256k1PeerId();
const peerId =
await generateKeyPair("secp256k1").then(peerIdFromPrivateKey);
const message: UnsignedMessage = {
type: "unsigned",

View File

@ -60,12 +60,12 @@
"node": ">=20"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^15.1.0",
"@libp2p/bootstrap": "^10",
"@libp2p/identify": "^2.1.2",
"@libp2p/mplex": "^10.1.2",
"@libp2p/ping": "^1.1.2",
"@libp2p/websockets": "^8.1.4",
"@chainsafe/libp2p-noise": "^16.0.0",
"@libp2p/bootstrap": "^11.0.1",
"@libp2p/identify": "^3.0.1",
"@libp2p/mplex": "^11.0.1",
"@libp2p/ping": "2.0.1",
"@libp2p/websockets": "^9.0.1",
"@noble/hashes": "^1.3.3",
"@waku/core": "0.0.33",
"@waku/discovery": "0.0.6",
@ -73,7 +73,7 @@
"@waku/proto": "^0.0.8",
"@waku/utils": "0.0.21",
"@waku/message-hash": "0.1.17",
"libp2p": "^1.8.1"
"libp2p": "2.1.8"
},
"devDependencies": {
"@types/chai": "^4.3.11",

View File

@ -59,12 +59,9 @@ export async function defaultLibp2p(
: wss;
return createLibp2p({
connectionManager: {
minConnections: 1
},
transports: [webSockets({ filter: filter })],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
connectionEncrypters: [noise()],
...options,
services: {
identify: identify({

View File

@ -50,8 +50,8 @@
"node": ">=20"
},
"dependencies": {
"@libp2p/interface-compliance-tests": "^5.4.9",
"@libp2p/peer-id": "^4.2.1",
"@libp2p/interface-compliance-tests": "^6.0.1",
"@libp2p/peer-id": "^5.0.1",
"@waku/core": "*",
"@waku/enr": "*",
"@waku/interfaces": "*",
@ -68,7 +68,8 @@
"tail": "^2.2.6"
},
"devDependencies": {
"@libp2p/bootstrap": "^10.1.2",
"@libp2p/bootstrap": "^11.0.1",
"@libp2p/crypto": "^5.0.1",
"@types/chai": "^4.3.11",
"@types/dockerode": "^3.3.19",
"@types/mocha": "^10.0.6",
@ -76,16 +77,16 @@
"@types/tail": "^2.2.3",
"@waku/discovery": "*",
"@waku/message-encryption": "*",
"@waku/sdk": "*",
"@waku/relay": "*",
"@waku/sdk": "*",
"allure-commandline": "^2.27.0",
"allure-mocha": "^2.9.2",
"chai": "^4.3.10",
"cspell": "^8.6.1",
"datastore-core": "^9.2.7",
"datastore-core": "^10.0.2",
"debug": "^4.3.4",
"interface-datastore": "^8.2.10",
"libp2p": "^1.8.1",
"libp2p": "2.1.8",
"mocha": "^10.3.0",
"mocha-multi-reporters": "^1.5.1",
"npm-run-all": "^4.1.5"

View File

@ -1,6 +1,6 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { PeerInfo } from "@libp2p/interface";
import { CustomEvent } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { LightNode, Tags } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { expect } from "chai";
@ -49,10 +49,12 @@ describe("Dials", function () {
it("should be called at least once on all `peer:discovery` events", async function () {
const totalPeerIds = 5;
for (let i = 1; i <= totalPeerIds; i++) {
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", {
detail: {
id: await createSecp256k1PeerId(),
id: peerId,
multiaddrs: []
}
})
@ -110,7 +112,8 @@ describe("Dials", function () {
describe("For bootstrap peers", function () {
it("should be called for bootstrap peers", async function () {
const bootstrapPeer = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const bootstrapPeer = peerIdFromPrivateKey(privateKey);
// emit a peer:discovery event
waku.libp2p.dispatchEvent(
@ -130,11 +133,13 @@ describe("Dials", function () {
});
it("should not be called more than DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED times for bootstrap peers", async function () {
const privateKey = await generateKeyPair("secp256k1");
const bootstrapPeer = peerIdFromPrivateKey(privateKey);
// emit first peer:discovery event
waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", {
detail: {
id: await createSecp256k1PeerId(),
id: bootstrapPeer,
multiaddrs: []
}
})
@ -151,7 +156,7 @@ describe("Dials", function () {
waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", {
detail: {
id: await createSecp256k1PeerId(),
id: bootstrapPeer,
multiaddrs: []
}
})
@ -168,7 +173,8 @@ describe("Dials", function () {
describe("For peer-exchange peers", function () {
it("should be called for peers with PEER_EXCHANGE tags", async function () {
const pxPeer = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const pxPeer = peerIdFromPrivateKey(privateKey);
// emit a peer:discovery event
waku.libp2p.dispatchEvent(
@ -194,10 +200,12 @@ describe("Dials", function () {
// emit multiple peer:discovery events
const totalPxPeers = 5;
for (let i = 0; i < totalPxPeers; i++) {
const privateKey = await generateKeyPair("secp256k1");
const pxPeer = peerIdFromPrivateKey(privateKey);
waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", {
detail: {
id: await createSecp256k1PeerId(),
id: pxPeer,
multiaddrs: []
}
})

View File

@ -1,6 +1,7 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { PeerId, PeerInfo } from "@libp2p/interface";
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { TypedEventEmitter } from "@libp2p/interface";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import {
EConnectionStateEvents,
EPeersByDiscoveryEvents,
@ -28,7 +29,8 @@ describe("Events", function () {
describe("peer:discovery", () => {
it("should emit `peer:discovery:bootstrap` event when a peer is discovered", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
@ -61,7 +63,8 @@ describe("Events", function () {
});
it("should emit `peer:discovery:peer-exchange` event when a peer is discovered", async function () {
const peerIdPx = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdPx = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdPx, {
tags: {
@ -96,7 +99,8 @@ describe("Events", function () {
describe("peer:connect", () => {
it("should emit `peer:connected:bootstrap` event when a peer is connected", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
@ -123,7 +127,8 @@ describe("Events", function () {
expect(await peerConnectedBootstrap).to.eq(true);
});
it("should emit `peer:connected:peer-exchange` event when a peer is connected", async function () {
const peerIdPx = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdPx = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdPx, {
tags: {
@ -179,8 +184,10 @@ describe("Events", function () {
});
it(`should emit events and trasition isConnected state when has peers or no peers`, async function () {
const peerIdPx = await createSecp256k1PeerId();
const peerIdPx2 = await createSecp256k1PeerId();
const privateKey1 = await generateKeyPair("secp256k1");
const privateKey2 = await generateKeyPair("secp256k1");
const peerIdPx = peerIdFromPrivateKey(privateKey1);
const peerIdPx2 = peerIdFromPrivateKey(privateKey2);
await waku.libp2p.peerStore.save(peerIdPx, {
tags: {
@ -249,7 +256,8 @@ describe("Events", function () {
// have to recreate js-waku for it to pick up new globalThis
waku = await createLightNode();
const peerIdPx = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdPx = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdPx, {
tags: {

View File

@ -1,6 +1,6 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { PeerId } from "@libp2p/interface";
import { CustomEvent } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import {
EPeersByDiscoveryEvents,
LightNode,
@ -26,7 +26,8 @@ describe("Public methods", function () {
await tearDownNodes([], waku);
});
it("addEventListener with correct event", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {
@ -50,7 +51,8 @@ describe("Public methods", function () {
});
it("addEventListener with wrong event", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {
@ -83,7 +85,8 @@ describe("Public methods", function () {
});
it("removeEventListener with correct event", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {
@ -126,7 +129,8 @@ describe("Public methods", function () {
});
it("removeEventListener with wrong event", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {
@ -164,7 +168,8 @@ describe("Public methods", function () {
});
it("getPeersByDiscovery", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
const peers_before = await waku.connectionManager.getPeersByDiscovery();
expect(peers_before.DISCOVERED[Tags.BOOTSTRAP]).to.deep.eq([]);
@ -202,7 +207,8 @@ describe("Public methods", function () {
});
it("listenerCount", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
waku.connectionManager.addEventListener(
EPeersByDiscoveryEvents.PEER_CONNECT_BOOTSTRAP,
({ detail: receivedPeerId }) => {
@ -233,7 +239,8 @@ describe("Public methods", function () {
});
it("dispatchEvent via connectionManager", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {
@ -259,7 +266,8 @@ describe("Public methods", function () {
});
it("safeDispatchEvent", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {
@ -286,7 +294,8 @@ describe("Public methods", function () {
});
it("stop", async function () {
const peerIdBootstrap = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerIdBootstrap = peerIdFromPrivateKey(privateKey);
await waku.libp2p.peerStore.save(peerIdBootstrap, {
tags: {
[Tags.BOOTSTRAP]: {

View File

@ -1,8 +1,9 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import { TypedEventEmitter } from "@libp2p/interface";
import tests from "@libp2p/interface-compliance-tests/peer-discovery";
import { prefixLogger } from "@libp2p/logger";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { PersistentPeerStore } from "@libp2p/peer-store";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { persistentPeerStore } from "@libp2p/peer-store";
import {
DnsNodeDiscovery,
enrTree,
@ -22,11 +23,13 @@ describe("DNS Discovery: Compliance Test", function () {
this.timeout(10000);
tests({
async setup() {
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
// create libp2p mock peerStore
const components = {
peerStore: new PersistentPeerStore({
peerStore: persistentPeerStore({
events: new TypedEventEmitter(),
peerId: await createSecp256k1PeerId(),
peerId,
datastore: new MemoryDatastore(),
logger: prefixLogger("dns-peer-discovery.spec.ts")
})

View File

@ -1,6 +1,5 @@
import type { PeerId } from "@libp2p/interface";
import type { PeerInfo } from "@libp2p/interface";
import { CustomEvent } from "@libp2p/interface";
import { multiaddr } from "@multiformats/multiaddr";
import type { Multiaddr } from "@multiformats/multiaddr";
import type { IWaku } from "@waku/interfaces";

View File

@ -1,5 +1,6 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import { type PeerId } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import { PeerExchangeDiscovery } from "@waku/discovery";
import { IEnr, LightNode } from "@waku/interfaces";
@ -92,7 +93,8 @@ describe("Peer Exchange Continuous Discovery", () => {
});
async function discoverPeerOnce(): Promise<void> {
peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
peerId = peerIdFromPrivateKey(privateKey);
const enr: IEnr = {
peerId,
@ -109,7 +111,8 @@ describe("Peer Exchange Continuous Discovery", () => {
queryStub.resolves({ error: null, peerInfos: [peerInfo] });
randomPeerId = await createSecp256k1PeerId();
const privateKeyRandom = await generateKeyPair("secp256k1");
randomPeerId = peerIdFromPrivateKey(privateKeyRandom);
const result = await (peerExchangeDiscovery as any).query(randomPeerId);
expect(result.error).to.be.null;