mirror of https://github.com/waku-org/js-waku.git
chore!: directly convert from ENR to `PeerInfo`, remove unneeded utility
This commit is contained in:
parent
84f114bfa4
commit
6dbcde041a
|
@ -28228,6 +28228,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
|
"@libp2p/interface-peer-info": "^1.0.8",
|
||||||
"@libp2p/peer-id-factory": "^2.0.1",
|
"@libp2p/peer-id-factory": "^2.0.1",
|
||||||
"@rollup/plugin-commonjs": "^24.0.1",
|
"@rollup/plugin-commonjs": "^24.0.1",
|
||||||
"@rollup/plugin-json": "^6.0.0",
|
"@rollup/plugin-json": "^6.0.0",
|
||||||
|
@ -28274,6 +28275,7 @@
|
||||||
"@libp2p/interface-connection-manager": "^1.3.7",
|
"@libp2p/interface-connection-manager": "^1.3.7",
|
||||||
"@libp2p/interface-libp2p": "^1.1.1",
|
"@libp2p/interface-libp2p": "^1.1.1",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
|
"@libp2p/interface-peer-info": "^1.0.8",
|
||||||
"@libp2p/interface-peer-store": "^1.2.8",
|
"@libp2p/interface-peer-store": "^1.2.8",
|
||||||
"@libp2p/interface-registrar": "^2.0.8",
|
"@libp2p/interface-registrar": "^2.0.8",
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
"@multiformats/multiaddr": "^11.4.0",
|
||||||
|
@ -28520,16 +28522,13 @@
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"license": "MIT OR Apache-2.0",
|
"license": "MIT OR Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@libp2p/peer-id": "^2.0.2",
|
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
"uint8arrays": "^4.0.3"
|
"uint8arrays": "^4.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@libp2p/interface-connection": "^3.0.8",
|
"@libp2p/interface-connection": "^3.0.8",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
"@libp2p/interface-peer-info": "^1.0.8",
|
|
||||||
"@libp2p/interface-peer-store": "^1.2.8",
|
"@libp2p/interface-peer-store": "^1.2.8",
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
|
||||||
"@rollup/plugin-commonjs": "^24.0.1",
|
"@rollup/plugin-commonjs": "^24.0.1",
|
||||||
"@rollup/plugin-json": "^6.0.0",
|
"@rollup/plugin-json": "^6.0.0",
|
||||||
"@rollup/plugin-node-resolve": "^15.0.1",
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
||||||
|
@ -32902,6 +32901,7 @@
|
||||||
"@ethersproject/rlp": "^5.7.0",
|
"@ethersproject/rlp": "^5.7.0",
|
||||||
"@libp2p/crypto": "^1.0.12",
|
"@libp2p/crypto": "^1.0.12",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
|
"@libp2p/interface-peer-info": "^1.0.8",
|
||||||
"@libp2p/peer-id": "^2.0.2",
|
"@libp2p/peer-id": "^2.0.2",
|
||||||
"@libp2p/peer-id-factory": "^2.0.1",
|
"@libp2p/peer-id-factory": "^2.0.1",
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
"@multiformats/multiaddr": "^11.4.0",
|
||||||
|
@ -32949,6 +32949,7 @@
|
||||||
"@libp2p/interface-connection-manager": "^1.3.7",
|
"@libp2p/interface-connection-manager": "^1.3.7",
|
||||||
"@libp2p/interface-libp2p": "^1.1.1",
|
"@libp2p/interface-libp2p": "^1.1.1",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
|
"@libp2p/interface-peer-info": "^1.0.8",
|
||||||
"@libp2p/interface-peer-store": "^1.2.8",
|
"@libp2p/interface-peer-store": "^1.2.8",
|
||||||
"@libp2p/interface-registrar": "^2.0.8",
|
"@libp2p/interface-registrar": "^2.0.8",
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
"@multiformats/multiaddr": "^11.4.0",
|
||||||
|
@ -33151,10 +33152,7 @@
|
||||||
"requires": {
|
"requires": {
|
||||||
"@libp2p/interface-connection": "^3.0.8",
|
"@libp2p/interface-connection": "^3.0.8",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
"@libp2p/interface-peer-info": "^1.0.8",
|
|
||||||
"@libp2p/interface-peer-store": "^1.2.8",
|
"@libp2p/interface-peer-store": "^1.2.8",
|
||||||
"@libp2p/peer-id": "^2.0.2",
|
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
|
||||||
"@rollup/plugin-commonjs": "^24.0.1",
|
"@rollup/plugin-commonjs": "^24.0.1",
|
||||||
"@rollup/plugin-json": "^6.0.0",
|
"@rollup/plugin-json": "^6.0.0",
|
||||||
"@rollup/plugin-node-resolve": "^15.0.1",
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
||||||
|
|
|
@ -7,7 +7,6 @@ import type { PeerInfo } from "@libp2p/interface-peer-info";
|
||||||
import type { PeerStore } from "@libp2p/interface-peer-store";
|
import type { PeerStore } from "@libp2p/interface-peer-store";
|
||||||
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
|
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
|
||||||
import type { IEnr } from "@waku/interfaces";
|
import type { IEnr } from "@waku/interfaces";
|
||||||
import { multiaddrsToPeerInfo } from "@waku/utils";
|
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
import { DnsNodeDiscovery, NodeCapabilityCount } from "./dns.js";
|
import { DnsNodeDiscovery, NodeCapabilityCount } from "./dns.js";
|
||||||
|
@ -94,14 +93,16 @@ export class PeerDiscoveryDns
|
||||||
this._started = true;
|
this._started = true;
|
||||||
for await (const peer of this.nextPeer()) {
|
for await (const peer of this.nextPeer()) {
|
||||||
if (!this._started) return;
|
if (!this._started) return;
|
||||||
const peerInfos = multiaddrsToPeerInfo(peer.getFullMultiaddrs());
|
|
||||||
peerInfos.forEach(async (peerInfo) => {
|
const peerInfo = peer.peerInfo;
|
||||||
|
if (!peerInfo) continue;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(await this._components.peerStore.getTags(peerInfo.id)).find(
|
(await this._components.peerStore.getTags(peerInfo.id)).find(
|
||||||
({ name }) => name === DEFAULT_BOOTSTRAP_TAG_NAME
|
({ name }) => name === DEFAULT_BOOTSTRAP_TAG_NAME
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return;
|
continue;
|
||||||
|
|
||||||
await this._components.peerStore.tagPeer(
|
await this._components.peerStore.tagPeer(
|
||||||
peerInfo.id,
|
peerInfo.id,
|
||||||
|
@ -114,7 +115,6 @@ export class PeerDiscoveryDns
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
|
new CustomEvent<PeerInfo>("peer", { detail: peerInfo })
|
||||||
);
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
|
"@libp2p/interface-peer-info": "^1.0.8",
|
||||||
"@libp2p/peer-id-factory": "^2.0.1",
|
"@libp2p/peer-id-factory": "^2.0.1",
|
||||||
"@rollup/plugin-commonjs": "^24.0.1",
|
"@rollup/plugin-commonjs": "^24.0.1",
|
||||||
"@rollup/plugin-json": "^6.0.0",
|
"@rollup/plugin-json": "^6.0.0",
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||||
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 * as secp from "@noble/secp256k1";
|
import * as secp from "@noble/secp256k1";
|
||||||
|
@ -10,7 +11,11 @@ import { ERR_INVALID_ID } from "./constants.js";
|
||||||
import { EnrCreator } from "./creator.js";
|
import { EnrCreator } from "./creator.js";
|
||||||
import { EnrDecoder } from "./decoder.js";
|
import { EnrDecoder } from "./decoder.js";
|
||||||
import { EnrEncoder } from "./encoder.js";
|
import { EnrEncoder } from "./encoder.js";
|
||||||
import { ENR } from "./enr.js";
|
import {
|
||||||
|
ENR,
|
||||||
|
TransportProtocol,
|
||||||
|
TransportProtocolPerIpVersion,
|
||||||
|
} from "./enr.js";
|
||||||
import { getPrivateKeyFromPeerId } from "./peer_id.js";
|
import { getPrivateKeyFromPeerId } from "./peer_id.js";
|
||||||
|
|
||||||
describe("ENR", function () {
|
describe("ENR", function () {
|
||||||
|
@ -42,7 +47,7 @@ describe("ENR", function () {
|
||||||
if (!enr2.signature) throw "enr.signature is undefined";
|
if (!enr2.signature) throw "enr.signature is undefined";
|
||||||
|
|
||||||
expect(bytesToHex(enr2.signature)).to.be.equal(bytesToHex(enr.signature));
|
expect(bytesToHex(enr2.signature)).to.be.equal(bytesToHex(enr.signature));
|
||||||
const ma = enr2.getLocationMultiaddr("udp")!;
|
const ma = enr2.getLocationMultiaddr(TransportProtocol.UDP)!;
|
||||||
expect(ma.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
|
expect(ma.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
|
||||||
expect(enr2.multiaddrs).to.not.be.undefined;
|
expect(enr2.multiaddrs).to.not.be.undefined;
|
||||||
expect(enr2.multiaddrs!.length).to.be.equal(3);
|
expect(enr2.multiaddrs!.length).to.be.equal(3);
|
||||||
|
@ -256,16 +261,16 @@ describe("ENR", function () {
|
||||||
record.set("ip", tuples0[0][1]);
|
record.set("ip", tuples0[0][1]);
|
||||||
record.set("udp", tuples0[1][1]);
|
record.set("udp", tuples0[1][1]);
|
||||||
// and get the multiaddr
|
// and get the multiaddr
|
||||||
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(
|
expect(
|
||||||
multi0.toString()
|
record.getLocationMultiaddr(TransportProtocol.UDP)!.toString()
|
||||||
);
|
).to.equal(multi0.toString());
|
||||||
// set the multiaddr
|
// set the multiaddr
|
||||||
const multi1 = multiaddr("/ip4/0.0.0.0/udp/30300");
|
const multi1 = multiaddr("/ip4/0.0.0.0/udp/30300");
|
||||||
record.setLocationMultiaddr(multi1);
|
record.setLocationMultiaddr(multi1);
|
||||||
// and get the multiaddr
|
// and get the multiaddr
|
||||||
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(
|
expect(
|
||||||
multi1.toString()
|
record.getLocationMultiaddr(TransportProtocol.UDP)!.toString()
|
||||||
);
|
).to.equal(multi1.toString());
|
||||||
// and get the underlying records
|
// and get the underlying records
|
||||||
const tuples1 = multi1.tuples();
|
const tuples1 = multi1.tuples();
|
||||||
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
|
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
|
||||||
|
@ -284,16 +289,16 @@ describe("ENR", function () {
|
||||||
record.set("ip", tuples0[0][1]);
|
record.set("ip", tuples0[0][1]);
|
||||||
record.set("tcp", tuples0[1][1]);
|
record.set("tcp", tuples0[1][1]);
|
||||||
// and get the multiaddr
|
// and get the multiaddr
|
||||||
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(
|
expect(
|
||||||
multi0.toString()
|
record.getLocationMultiaddr(TransportProtocol.TCP)!.toString()
|
||||||
);
|
).to.equal(multi0.toString());
|
||||||
// set the multiaddr
|
// set the multiaddr
|
||||||
const multi1 = multiaddr("/ip4/0.0.0.0/tcp/30300");
|
const multi1 = multiaddr("/ip4/0.0.0.0/tcp/30300");
|
||||||
record.setLocationMultiaddr(multi1);
|
record.setLocationMultiaddr(multi1);
|
||||||
// and get the multiaddr
|
// and get the multiaddr
|
||||||
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(
|
expect(
|
||||||
multi1.toString()
|
record.getLocationMultiaddr(TransportProtocol.TCP)!.toString()
|
||||||
);
|
).to.equal(multi1.toString());
|
||||||
// and get the underlying records
|
// and get the underlying records
|
||||||
const tuples1 = multi1.tuples();
|
const tuples1 = multi1.tuples();
|
||||||
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
|
expect(record.get("ip")).to.deep.equal(tuples1[0][1]);
|
||||||
|
@ -306,7 +311,7 @@ describe("ENR", function () {
|
||||||
const ip6 = "::1";
|
const ip6 = "::1";
|
||||||
const tcp = 8080;
|
const tcp = 8080;
|
||||||
const udp = 8080;
|
const udp = 8080;
|
||||||
let peerId;
|
let peerId: PeerId;
|
||||||
let enr: ENR;
|
let enr: ENR;
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
|
@ -321,43 +326,43 @@ describe("ENR", function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should properly create location multiaddrs - udp4", () => {
|
it("should properly create location multiaddrs - udp4", () => {
|
||||||
expect(enr.getLocationMultiaddr("udp4")).to.deep.equal(
|
expect(
|
||||||
multiaddr(`/ip4/${ip4}/udp/${udp}`)
|
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.UDP4)
|
||||||
);
|
).to.deep.equal(multiaddr(`/ip4/${ip4}/udp/${udp}`));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should properly create location multiaddrs - tcp4", () => {
|
it("should properly create location multiaddrs - tcp4", () => {
|
||||||
expect(enr.getLocationMultiaddr("tcp4")).to.deep.equal(
|
expect(
|
||||||
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
|
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.TCP4)
|
||||||
);
|
).to.deep.equal(multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should properly create location multiaddrs - udp6", () => {
|
it("should properly create location multiaddrs - udp6", () => {
|
||||||
expect(enr.getLocationMultiaddr("udp6")).to.deep.equal(
|
expect(
|
||||||
multiaddr(`/ip6/${ip6}/udp/${udp}`)
|
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.UDP6)
|
||||||
);
|
).to.deep.equal(multiaddr(`/ip6/${ip6}/udp/${udp}`));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should properly create location multiaddrs - tcp6", () => {
|
it("should properly create location multiaddrs - tcp6", () => {
|
||||||
expect(enr.getLocationMultiaddr("tcp6")).to.deep.equal(
|
expect(
|
||||||
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
|
enr.getLocationMultiaddr(TransportProtocolPerIpVersion.TCP6)
|
||||||
);
|
).to.deep.equal(multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should properly create location multiaddrs - udp", () => {
|
it("should properly create location multiaddrs - udp", () => {
|
||||||
// default to ip4
|
// default to ip4
|
||||||
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
|
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
|
||||||
multiaddr(`/ip4/${ip4}/udp/${udp}`)
|
multiaddr(`/ip4/${ip4}/udp/${udp}`)
|
||||||
);
|
);
|
||||||
// if ip6 is set, use it
|
// if ip6 is set, use it
|
||||||
enr.ip = undefined;
|
enr.ip = undefined;
|
||||||
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
|
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
|
||||||
multiaddr(`/ip6/${ip6}/udp/${udp}`)
|
multiaddr(`/ip6/${ip6}/udp/${udp}`)
|
||||||
);
|
);
|
||||||
// if ip6 does not exist, use ip4
|
// if ip6 does not exist, use ip4
|
||||||
enr.ip6 = undefined;
|
enr.ip6 = undefined;
|
||||||
enr.ip = ip4;
|
enr.ip = ip4;
|
||||||
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(
|
expect(enr.getLocationMultiaddr(TransportProtocol.UDP)).to.deep.equal(
|
||||||
multiaddr(`/ip4/${ip4}/udp/${udp}`)
|
multiaddr(`/ip4/${ip4}/udp/${udp}`)
|
||||||
);
|
);
|
||||||
enr.ip6 = ip6;
|
enr.ip6 = ip6;
|
||||||
|
@ -365,22 +370,41 @@ describe("ENR", function () {
|
||||||
|
|
||||||
it("should properly create location multiaddrs - tcp", () => {
|
it("should properly create location multiaddrs - tcp", () => {
|
||||||
// default to ip4
|
// default to ip4
|
||||||
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
|
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
|
||||||
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
|
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
|
||||||
);
|
);
|
||||||
// if ip6 is set, use it
|
// if ip6 is set, use it
|
||||||
enr.ip = undefined;
|
enr.ip = undefined;
|
||||||
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
|
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
|
||||||
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
|
multiaddr(`/ip6/${ip6}/tcp/${tcp}`)
|
||||||
);
|
);
|
||||||
// if ip6 does not exist, use ip4
|
// if ip6 does not exist, use ip4
|
||||||
enr.ip6 = undefined;
|
enr.ip6 = undefined;
|
||||||
enr.ip = ip4;
|
enr.ip = ip4;
|
||||||
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(
|
expect(enr.getLocationMultiaddr(TransportProtocol.TCP)).to.deep.equal(
|
||||||
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
|
multiaddr(`/ip4/${ip4}/tcp/${tcp}`)
|
||||||
);
|
);
|
||||||
enr.ip6 = ip6;
|
enr.ip6 = ip6;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should properly create peer info with all multiaddrs", () => {
|
||||||
|
const peerInfo = enr.peerInfo!;
|
||||||
|
console.log(peerInfo);
|
||||||
|
expect(peerInfo.id.toString()).to.equal(peerId.toString());
|
||||||
|
expect(peerInfo.multiaddrs.length).to.equal(4);
|
||||||
|
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
|
||||||
|
multiaddr(`/ip4/${ip4}/tcp/${tcp}`).toString()
|
||||||
|
);
|
||||||
|
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
|
||||||
|
multiaddr(`/ip6/${ip6}/tcp/${tcp}`).toString()
|
||||||
|
);
|
||||||
|
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
|
||||||
|
multiaddr(`/ip4/${ip4}/udp/${udp}`).toString()
|
||||||
|
);
|
||||||
|
expect(peerInfo.multiaddrs.map((ma) => ma.toString())).to.contain(
|
||||||
|
multiaddr(`/ip6/${ip6}/udp/${udp}`).toString()
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("waku2 key round trip", async () => {
|
describe("waku2 key round trip", async () => {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||||
|
import type { PeerInfo } from "@libp2p/interface-peer-info";
|
||||||
import type { Multiaddr } from "@multiformats/multiaddr";
|
import type { Multiaddr } from "@multiformats/multiaddr";
|
||||||
import {
|
import {
|
||||||
convertToBytes,
|
convertToBytes,
|
||||||
|
@ -25,6 +26,17 @@ import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
|
||||||
|
|
||||||
const log = debug("waku:enr");
|
const log = debug("waku:enr");
|
||||||
|
|
||||||
|
export enum TransportProtocol {
|
||||||
|
TCP = "tcp",
|
||||||
|
UDP = "udp",
|
||||||
|
}
|
||||||
|
export enum TransportProtocolPerIpVersion {
|
||||||
|
TCP4 = "tcp4",
|
||||||
|
UDP4 = "udp4",
|
||||||
|
TCP6 = "tcp6",
|
||||||
|
UDP6 = "udp6",
|
||||||
|
}
|
||||||
|
|
||||||
export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
||||||
public static readonly RECORD_PREFIX = "enr:";
|
public static readonly RECORD_PREFIX = "enr:";
|
||||||
public seq: SequenceNumber;
|
public seq: SequenceNumber;
|
||||||
|
@ -232,7 +244,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
||||||
}
|
}
|
||||||
|
|
||||||
getLocationMultiaddr: (
|
getLocationMultiaddr: (
|
||||||
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
|
protocol: TransportProtocol | TransportProtocolPerIpVersion
|
||||||
) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
|
) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
|
||||||
|
|
||||||
setLocationMultiaddr(multiaddr: Multiaddr): void {
|
setLocationMultiaddr(multiaddr: Multiaddr): void {
|
||||||
|
@ -259,6 +271,32 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAllLocationMultiaddrs(): Multiaddr[] {
|
||||||
|
const multiaddrs = [];
|
||||||
|
|
||||||
|
for (const protocol of Object.values(TransportProtocolPerIpVersion)) {
|
||||||
|
const ma = this.getLocationMultiaddr(
|
||||||
|
protocol as TransportProtocolPerIpVersion
|
||||||
|
);
|
||||||
|
if (ma) multiaddrs.push(ma);
|
||||||
|
}
|
||||||
|
|
||||||
|
const _multiaddrs = this.multiaddrs ?? [];
|
||||||
|
multiaddrs.concat(_multiaddrs);
|
||||||
|
|
||||||
|
return multiaddrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
get peerInfo(): PeerInfo | undefined {
|
||||||
|
const id = this.peerId;
|
||||||
|
if (!id) return;
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
multiaddrs: this.getAllLocationMultiaddrs(),
|
||||||
|
protocols: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the full multiaddr from the ENR fields matching the provided
|
* Returns the full multiaddr from the ENR fields matching the provided
|
||||||
* `protocol` parameter.
|
* `protocol` parameter.
|
||||||
|
@ -268,7 +306,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
||||||
* @param protocol
|
* @param protocol
|
||||||
*/
|
*/
|
||||||
getFullMultiaddr(
|
getFullMultiaddr(
|
||||||
protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
|
protocol: TransportProtocol | TransportProtocolPerIpVersion
|
||||||
): Multiaddr | undefined {
|
): Multiaddr | undefined {
|
||||||
if (this.peerId) {
|
if (this.peerId) {
|
||||||
const locationMultiaddr = this.getLocationMultiaddr(protocol);
|
const locationMultiaddr = this.getLocationMultiaddr(protocol);
|
||||||
|
|
|
@ -54,6 +54,7 @@
|
||||||
"@libp2p/interface-connection-manager": "^1.3.7",
|
"@libp2p/interface-connection-manager": "^1.3.7",
|
||||||
"@libp2p/interface-libp2p": "^1.1.1",
|
"@libp2p/interface-libp2p": "^1.1.1",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
|
"@libp2p/interface-peer-info": "^1.0.8",
|
||||||
"@libp2p/interface-peer-store": "^1.2.8",
|
"@libp2p/interface-peer-store": "^1.2.8",
|
||||||
"@libp2p/interface-registrar": "^2.0.8",
|
"@libp2p/interface-registrar": "^2.0.8",
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
"@multiformats/multiaddr": "^11.4.0",
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||||
|
import type { PeerInfo } from "@libp2p/interface-peer-info";
|
||||||
import type { Multiaddr } from "@multiformats/multiaddr";
|
import type { Multiaddr } from "@multiformats/multiaddr";
|
||||||
|
|
||||||
export type ENRKey = string;
|
export type ENRKey = string;
|
||||||
|
@ -32,6 +33,10 @@ export interface IEnr extends Map<ENRKey, ENRValue> {
|
||||||
udp6?: number;
|
udp6?: number;
|
||||||
multiaddrs?: Multiaddr[];
|
multiaddrs?: Multiaddr[];
|
||||||
waku2?: Waku2;
|
waku2?: Waku2;
|
||||||
|
peerInfo: PeerInfo | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated: use { @link IEnr.peerInfo } instead.
|
||||||
|
*/
|
||||||
getFullMultiaddrs(): Multiaddr[];
|
getFullMultiaddrs(): Multiaddr[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,20 +165,25 @@ export class PeerExchangeDiscovery
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { peerId } = ENR;
|
const peerInfo = ENR.peerInfo;
|
||||||
const multiaddrs = ENR.getFullMultiaddrs();
|
|
||||||
|
|
||||||
if (!peerId || !multiaddrs || multiaddrs.length === 0) continue;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(await this.components.peerStore.getTags(peerId)).find(
|
!peerInfo ||
|
||||||
|
!peerInfo.id ||
|
||||||
|
!peerInfo.multiaddrs ||
|
||||||
|
!peerInfo.multiaddrs.length
|
||||||
|
)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (
|
||||||
|
(await this.components.peerStore.getTags(peerInfo.id)).find(
|
||||||
({ name }) => name === DEFAULT_PEER_EXCHANGE_TAG_NAME
|
({ name }) => name === DEFAULT_PEER_EXCHANGE_TAG_NAME
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
await this.components.peerStore.tagPeer(
|
await this.components.peerStore.tagPeer(
|
||||||
peerId,
|
peerInfo.id,
|
||||||
DEFAULT_PEER_EXCHANGE_TAG_NAME,
|
DEFAULT_PEER_EXCHANGE_TAG_NAME,
|
||||||
{
|
{
|
||||||
value:
|
value:
|
||||||
|
@ -191,11 +196,7 @@ export class PeerExchangeDiscovery
|
||||||
|
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent<PeerInfo>("peer", {
|
new CustomEvent<PeerInfo>("peer", {
|
||||||
detail: {
|
detail: peerInfo,
|
||||||
id: peerId,
|
|
||||||
multiaddrs,
|
|
||||||
protocols: [],
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,16 +50,13 @@
|
||||||
"node": ">=16"
|
"node": ">=16"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@libp2p/peer-id": "^2.0.2",
|
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
"uint8arrays": "^4.0.3"
|
"uint8arrays": "^4.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@libp2p/interface-connection": "^3.0.8",
|
"@libp2p/interface-connection": "^3.0.8",
|
||||||
"@libp2p/interface-peer-id": "^2.0.1",
|
"@libp2p/interface-peer-id": "^2.0.1",
|
||||||
"@libp2p/interface-peer-info": "^1.0.8",
|
|
||||||
"@libp2p/interface-peer-store": "^1.2.8",
|
"@libp2p/interface-peer-store": "^1.2.8",
|
||||||
"@multiformats/multiaddr": "^11.4.0",
|
|
||||||
"@rollup/plugin-commonjs": "^24.0.1",
|
"@rollup/plugin-commonjs": "^24.0.1",
|
||||||
"@rollup/plugin-json": "^6.0.0",
|
"@rollup/plugin-json": "^6.0.0",
|
||||||
"@rollup/plugin-node-resolve": "^15.0.1",
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
import type { Connection } from "@libp2p/interface-connection";
|
import type { Connection } from "@libp2p/interface-connection";
|
||||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||||
import type { PeerInfo } from "@libp2p/interface-peer-info";
|
|
||||||
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
|
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
|
||||||
import { peerIdFromString } from "@libp2p/peer-id";
|
|
||||||
import type { Multiaddr } from "@multiformats/multiaddr";
|
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
const log = debug("waku:libp2p-utils");
|
const log = debug("waku:libp2p-utils");
|
||||||
|
@ -78,20 +75,6 @@ export async function selectPeerForProtocol(
|
||||||
return { peer, protocol };
|
return { peer, protocol };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function multiaddrsToPeerInfo(mas: Multiaddr[]): PeerInfo[] {
|
|
||||||
return mas
|
|
||||||
.map((ma) => {
|
|
||||||
const peerIdStr = ma.getPeerId();
|
|
||||||
const protocols: string[] = [];
|
|
||||||
return {
|
|
||||||
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
|
|
||||||
multiaddrs: [ma.decapsulateCode(421)],
|
|
||||||
protocols,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.filter((peerInfo): peerInfo is PeerInfo => peerInfo.id !== null);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function selectConnection(
|
export function selectConnection(
|
||||||
connections: Connection[]
|
connections: Connection[]
|
||||||
): Connection | undefined {
|
): Connection | undefined {
|
||||||
|
|
Loading…
Reference in New Issue