chore: upgrade to libp2p v2 (#2143)

* chore: upgrade libp2p to v2 and related deps

* chore: fix ENR

* chore(core): remove CustomEvent polyfill import

* chore: `peer-id-factory` has been removed

* chore(discovery): fix local-cache & remove CustomEvent imports

* chore(sdk): update config

* chore(tests): update tests without peer-id-factory

* fix: spec tests

* chore: fix test

* chore: upgrade dataset-core

* chore: upgrade libp2p and stale references

* chore: upgrade playwright

* chore: rm console log

* fix: lock
This commit is contained in:
Danish Arora 2024-10-21 16:43:24 +05:30 committed by GitHub
parent c591e6d294
commit 7c0ce7b2ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
31 changed files with 1468 additions and 1472 deletions

View File

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

2568
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import { Peer } from "@libp2p/interface"; import { Peer } from "@libp2p/interface";
import type { Tag } from "@libp2p/interface"; import type { Tag } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { Tags } from "@waku/interfaces"; import { Tags } from "@waku/interfaces";
import { expect } from "chai"; import { expect } from "chai";
@ -8,9 +9,11 @@ import { filterPeersByDiscovery } from "./filterPeers.js";
describe("filterPeersByDiscovery function", function () { describe("filterPeersByDiscovery function", function () {
it("should return all peers when numPeers is 0", async function () { it("should return all peers when numPeers is 0", async function () {
const peer1 = await createSecp256k1PeerId(); const [peer1, peer2, peer3] = await Promise.all([
const peer2 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer3 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);
const mockPeers = [ const mockPeers = [
{ {
@ -32,10 +35,12 @@ describe("filterPeersByDiscovery function", function () {
}); });
it("should return all non-bootstrap peers and no bootstrap peer when numPeers is 0 and maxBootstrapPeers is 0", async function () { it("should return all non-bootstrap peers and no bootstrap peer when numPeers is 0 and maxBootstrapPeers is 0", async function () {
const peer1 = await createSecp256k1PeerId(); const [peer1, peer2, peer3, peer4] = await Promise.all([
const peer2 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer3 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer4 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);
const mockPeers = [ const mockPeers = [
{ {
@ -66,11 +71,13 @@ describe("filterPeersByDiscovery function", function () {
}); });
it("should return one bootstrap peer, and all non-boostrap peers, when numPeers is 0 & maxBootstrap is 1", async function () { it("should return one bootstrap peer, and all non-boostrap peers, when numPeers is 0 & maxBootstrap is 1", async function () {
const peer1 = await createSecp256k1PeerId(); const [peer1, peer2, peer3, peer4, peer5] = await Promise.all([
const peer2 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer3 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer4 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer5 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);
const mockPeers = [ const mockPeers = [
{ {
@ -105,11 +112,13 @@ describe("filterPeersByDiscovery function", function () {
}); });
it("should return only bootstrap peers up to maxBootstrapPeers", async function () { it("should return only bootstrap peers up to maxBootstrapPeers", async function () {
const peer1 = await createSecp256k1PeerId(); const [peer1, peer2, peer3, peer4, peer5] = await Promise.all([
const peer2 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer3 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer4 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
const peer5 = await createSecp256k1PeerId(); generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);
const mockPeers = [ const mockPeers = [
{ {

View File

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

View File

@ -1,5 +1,4 @@
import { import {
CustomEvent,
PeerDiscovery, PeerDiscovery,
PeerDiscoveryEvents, PeerDiscoveryEvents,
TypedEventEmitter 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 { multiaddr } from "@multiformats/multiaddr";
import { ENR } from "@waku/enr"; import { ENR } from "@waku/enr";
import { EnrCreator } from "@waku/enr"; import { EnrCreator } from "@waku/enr";
@ -8,7 +9,7 @@ import { expect } from "chai";
import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js"; 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 generateKeyPair("secp256k1").then(peerIdFromPrivateKey);
const enr = await EnrCreator.fromPeerId(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 = [

View File

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

View File

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

View File

@ -52,8 +52,8 @@
}, },
"dependencies": { "dependencies": {
"@ethersproject/rlp": "^5.7.0", "@ethersproject/rlp": "^5.7.0",
"@libp2p/crypto": "^4.1.6", "@libp2p/crypto": "^5.0.1",
"@libp2p/peer-id": "^4.2.1", "@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.0.0", "@multiformats/multiaddr": "^12.0.0",
"@noble/secp256k1": "^1.7.1", "@noble/secp256k1": "^1.7.1",
"@waku/utils": "0.0.21", "@waku/utils": "0.0.21",
@ -61,7 +61,6 @@
"js-sha3": "^0.9.2" "js-sha3": "^0.9.2"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/peer-id-factory": "^4.2.1",
"@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0", "@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.3", "@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 { compressPublicKey } from "./crypto.js";
import { ENR } from "./enr.js"; import { ENR } from "./enr.js";
import { getPublicKeyFromPeerId } from "./peer_id.js";
export class EnrCreator { export class EnrCreator {
public static fromPublicKey( public static fromPublicKey(
publicKey: Uint8Array, publicKey: Uint8Array,
kvs: Record<ENRKey, ENRValue> = {} kvs: Record<ENRKey, ENRValue> = {}
): Promise<ENR> { ): ENR {
// EIP-778 specifies that the key must be in compressed format, 33 bytes // EIP-778 specifies that the key must be in compressed format, 33 bytes
if (publicKey.length !== 33) { if (publicKey.length !== 33) {
publicKey = compressPublicKey(publicKey); publicKey = compressPublicKey(publicKey);
@ -28,7 +27,7 @@ export class EnrCreator {
): Promise<ENR> { ): Promise<ENR> {
switch (peerId.type) { switch (peerId.type) {
case "secp256k1": case "secp256k1":
return EnrCreator.fromPublicKey(getPublicKeyFromPeerId(peerId), kvs); return EnrCreator.fromPublicKey(peerId.publicKey.raw, kvs);
default: default:
throw new Error(); throw new Error();
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -50,8 +50,8 @@
"node": ">=20" "node": ">=20"
}, },
"dependencies": { "dependencies": {
"@libp2p/interface-compliance-tests": "^5.4.9", "@libp2p/interface-compliance-tests": "^6.0.1",
"@libp2p/peer-id": "^4.2.1", "@libp2p/peer-id": "^5.0.1",
"@waku/core": "*", "@waku/core": "*",
"@waku/enr": "*", "@waku/enr": "*",
"@waku/interfaces": "*", "@waku/interfaces": "*",
@ -68,7 +68,8 @@
"tail": "^2.2.6" "tail": "^2.2.6"
}, },
"devDependencies": { "devDependencies": {
"@libp2p/bootstrap": "^10.1.2", "@libp2p/bootstrap": "^11.0.1",
"@libp2p/crypto": "^5.0.1",
"@types/chai": "^4.3.11", "@types/chai": "^4.3.11",
"@types/dockerode": "^3.3.19", "@types/dockerode": "^3.3.19",
"@types/mocha": "^10.0.6", "@types/mocha": "^10.0.6",
@ -76,16 +77,16 @@
"@types/tail": "^2.2.3", "@types/tail": "^2.2.3",
"@waku/discovery": "*", "@waku/discovery": "*",
"@waku/message-encryption": "*", "@waku/message-encryption": "*",
"@waku/sdk": "*",
"@waku/relay": "*", "@waku/relay": "*",
"@waku/sdk": "*",
"allure-commandline": "^2.27.0", "allure-commandline": "^2.27.0",
"allure-mocha": "^2.9.2", "allure-mocha": "^2.9.2",
"chai": "^4.3.10", "chai": "^4.3.10",
"cspell": "^8.6.1", "cspell": "^8.6.1",
"datastore-core": "^9.2.7", "datastore-core": "^10.0.2",
"debug": "^4.3.4", "debug": "^4.3.4",
"interface-datastore": "^8.2.10", "interface-datastore": "^8.2.10",
"libp2p": "^1.8.1", "libp2p": "2.1.8",
"mocha": "^10.3.0", "mocha": "^10.3.0",
"mocha-multi-reporters": "^1.5.1", "mocha-multi-reporters": "^1.5.1",
"npm-run-all": "^4.1.5" "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 type { PeerInfo } from "@libp2p/interface";
import { CustomEvent } from "@libp2p/interface"; import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { LightNode, Tags } from "@waku/interfaces"; import { LightNode, Tags } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk"; import { createLightNode } from "@waku/sdk";
import { expect } from "chai"; 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 () { it("should be called at least once on all `peer:discovery` events", async function () {
const totalPeerIds = 5; const totalPeerIds = 5;
for (let i = 1; i <= totalPeerIds; i++) { for (let i = 1; i <= totalPeerIds; i++) {
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
waku.libp2p.dispatchEvent( waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", { new CustomEvent<PeerInfo>("peer:discovery", {
detail: { detail: {
id: await createSecp256k1PeerId(), id: peerId,
multiaddrs: [] multiaddrs: []
} }
}) })
@ -110,7 +112,8 @@ describe("Dials", function () {
describe("For bootstrap peers", function () { describe("For bootstrap peers", function () {
it("should be called for bootstrap peers", async 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 // emit a peer:discovery event
waku.libp2p.dispatchEvent( 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 () { 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 // emit first peer:discovery event
waku.libp2p.dispatchEvent( waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", { new CustomEvent<PeerInfo>("peer:discovery", {
detail: { detail: {
id: await createSecp256k1PeerId(), id: bootstrapPeer,
multiaddrs: [] multiaddrs: []
} }
}) })
@ -151,7 +156,7 @@ describe("Dials", function () {
waku.libp2p.dispatchEvent( waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", { new CustomEvent<PeerInfo>("peer:discovery", {
detail: { detail: {
id: await createSecp256k1PeerId(), id: bootstrapPeer,
multiaddrs: [] multiaddrs: []
} }
}) })
@ -168,7 +173,8 @@ describe("Dials", function () {
describe("For peer-exchange peers", function () { describe("For peer-exchange peers", function () {
it("should be called for peers with PEER_EXCHANGE tags", async 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 // emit a peer:discovery event
waku.libp2p.dispatchEvent( waku.libp2p.dispatchEvent(
@ -194,10 +200,12 @@ describe("Dials", function () {
// emit multiple peer:discovery events // emit multiple peer:discovery events
const totalPxPeers = 5; const totalPxPeers = 5;
for (let i = 0; i < totalPxPeers; i++) { for (let i = 0; i < totalPxPeers; i++) {
const privateKey = await generateKeyPair("secp256k1");
const pxPeer = peerIdFromPrivateKey(privateKey);
waku.libp2p.dispatchEvent( waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", { new CustomEvent<PeerInfo>("peer:discovery", {
detail: { detail: {
id: await createSecp256k1PeerId(), id: pxPeer,
multiaddrs: [] multiaddrs: []
} }
}) })

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { Connection, Peer, PeerStore } from "@libp2p/interface"; import type { Connection, Peer, PeerStore } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { import {
createLightNode, createLightNode,
Libp2pComponents, Libp2pComponents,
@ -54,14 +55,14 @@ describe("getPeers", function () {
differentCodecPeerId, differentCodecPeerId,
anotherDifferentCodecPeerId anotherDifferentCodecPeerId
] = await Promise.all([ ] = await Promise.all([
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId(), generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
createSecp256k1PeerId() generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]); ]);
lowPingBootstrapPeer = { lowPingBootstrapPeer = {

View File

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