mirror of
https://github.com/status-im/js-waku.git
synced 2025-02-23 10:28:15 +00:00
Merge pull request #1065 from waku-org/chore/avoid-interface-dep
This commit is contained in:
commit
49f06989e3
@ -3,11 +3,11 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"packages/proto",
|
|
||||||
"packages/byte-utils",
|
"packages/byte-utils",
|
||||||
"packages/enr",
|
|
||||||
"packages/interfaces",
|
|
||||||
"packages/libp2p-utils",
|
"packages/libp2p-utils",
|
||||||
|
"packages/proto",
|
||||||
|
"packages/interfaces",
|
||||||
|
"packages/enr",
|
||||||
"packages/peer-exchange",
|
"packages/peer-exchange",
|
||||||
"packages/core",
|
"packages/core",
|
||||||
"packages/dns-discovery",
|
"packages/dns-discovery",
|
||||||
|
@ -70,6 +70,7 @@
|
|||||||
"@libp2p/peer-id": "^1.1.10"
|
"@libp2p/peer-id": "^1.1.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@waku/interfaces": "*",
|
||||||
"@multiformats/multiaddr": "^11.0.6",
|
"@multiformats/multiaddr": "^11.0.6",
|
||||||
"@libp2p/peer-id-factory": "^1.0.15",
|
"@libp2p/peer-id-factory": "^1.0.15",
|
||||||
"chai": "^4.3.4",
|
"chai": "^4.3.4",
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { ENR } from "@waku/enr";
|
import { ENR } from "@waku/enr";
|
||||||
|
import type { IEnr } from "@waku/interfaces";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
import { DnsOverHttps } from "./dns_over_https.js";
|
import { DnsOverHttps } from "./dns_over_https.js";
|
||||||
@ -49,7 +50,7 @@ export class DnsNodeDiscovery {
|
|||||||
async getPeers(
|
async getPeers(
|
||||||
enrTreeUrls: string[],
|
enrTreeUrls: string[],
|
||||||
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
|
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
|
||||||
): Promise<ENR[]> {
|
): Promise<IEnr[]> {
|
||||||
const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
|
const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
|
||||||
const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
|
const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
|
||||||
const context: SearchContext = {
|
const context: SearchContext = {
|
||||||
@ -86,7 +87,7 @@ export class DnsNodeDiscovery {
|
|||||||
async *getNextPeer(
|
async *getNextPeer(
|
||||||
enrTreeUrls: string[],
|
enrTreeUrls: string[],
|
||||||
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
|
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
|
||||||
): AsyncGenerator<ENR> {
|
): AsyncGenerator<IEnr> {
|
||||||
const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
|
const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
|
||||||
const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
|
const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
|
||||||
const context: SearchContext = {
|
const context: SearchContext = {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
|
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
|
||||||
import { multiaddr } from "@multiformats/multiaddr";
|
import { multiaddr } from "@multiformats/multiaddr";
|
||||||
import { ENR, Waku2 } from "@waku/enr";
|
import { ENR } from "@waku/enr";
|
||||||
|
import type { Waku2 } from "@waku/interfaces";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
|
|
||||||
import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js";
|
import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js";
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ENR, Waku2 } from "@waku/enr";
|
import type { IEnr, Waku2 } from "@waku/interfaces";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
import { NodeCapabilityCount } from "./dns.js";
|
import { NodeCapabilityCount } from "./dns.js";
|
||||||
@ -13,8 +13,8 @@ const log = debug("waku:discovery:fetch_nodes");
|
|||||||
export async function fetchNodesUntilCapabilitiesFulfilled(
|
export async function fetchNodesUntilCapabilitiesFulfilled(
|
||||||
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
||||||
errorTolerance: number,
|
errorTolerance: number,
|
||||||
getNode: () => Promise<ENR | null>
|
getNode: () => Promise<IEnr | null>
|
||||||
): Promise<ENR[]> {
|
): Promise<IEnr[]> {
|
||||||
const wanted = {
|
const wanted = {
|
||||||
relay: wantedNodeCapabilityCount.relay ?? 0,
|
relay: wantedNodeCapabilityCount.relay ?? 0,
|
||||||
store: wantedNodeCapabilityCount.store ?? 0,
|
store: wantedNodeCapabilityCount.store ?? 0,
|
||||||
@ -33,7 +33,7 @@ export async function fetchNodesUntilCapabilitiesFulfilled(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let totalSearches = 0;
|
let totalSearches = 0;
|
||||||
const peers: ENR[] = [];
|
const peers: IEnr[] = [];
|
||||||
|
|
||||||
while (
|
while (
|
||||||
!isSatisfied(wanted, actual) &&
|
!isSatisfied(wanted, actual) &&
|
||||||
@ -64,8 +64,8 @@ export async function fetchNodesUntilCapabilitiesFulfilled(
|
|||||||
export async function* yieldNodesUntilCapabilitiesFulfilled(
|
export async function* yieldNodesUntilCapabilitiesFulfilled(
|
||||||
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
||||||
errorTolerance: number,
|
errorTolerance: number,
|
||||||
getNode: () => Promise<ENR | null>
|
getNode: () => Promise<IEnr | null>
|
||||||
): AsyncGenerator<ENR> {
|
): AsyncGenerator<IEnr> {
|
||||||
const wanted = {
|
const wanted = {
|
||||||
relay: wantedNodeCapabilityCount.relay ?? 0,
|
relay: wantedNodeCapabilityCount.relay ?? 0,
|
||||||
store: wantedNodeCapabilityCount.store ?? 0,
|
store: wantedNodeCapabilityCount.store ?? 0,
|
||||||
@ -118,7 +118,7 @@ function isSatisfied(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNewPeer(peer: ENR, peers: ENR[]): boolean {
|
function isNewPeer(peer: IEnr, peers: IEnr[]): boolean {
|
||||||
if (!peer.nodeId) return false;
|
if (!peer.nodeId) return false;
|
||||||
|
|
||||||
for (const existingPeer of peers) {
|
for (const existingPeer of peers) {
|
||||||
|
@ -5,7 +5,7 @@ import type {
|
|||||||
import { symbol } from "@libp2p/interface-peer-discovery";
|
import { symbol } from "@libp2p/interface-peer-discovery";
|
||||||
import type { PeerInfo } from "@libp2p/interface-peer-info";
|
import type { PeerInfo } from "@libp2p/interface-peer-info";
|
||||||
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
|
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
|
||||||
import { ENR } from "@waku/enr";
|
import type { IEnr } from "@waku/interfaces";
|
||||||
import { multiaddrsToPeerInfo } from "@waku/libp2p-utils";
|
import { multiaddrsToPeerInfo } from "@waku/libp2p-utils";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ export class PeerDiscoveryDns
|
|||||||
extends EventEmitter<PeerDiscoveryEvents>
|
extends EventEmitter<PeerDiscoveryEvents>
|
||||||
implements PeerDiscovery
|
implements PeerDiscovery
|
||||||
{
|
{
|
||||||
private readonly nextPeer: () => AsyncGenerator<ENR>;
|
private readonly nextPeer: () => AsyncGenerator<IEnr>;
|
||||||
private _started: boolean;
|
private _started: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,6 +63,7 @@
|
|||||||
"js-sha3": "^0.8.0"
|
"js-sha3": "^0.8.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@waku/interfaces": "*",
|
||||||
"uint8arrays": "^4.0.2",
|
"uint8arrays": "^4.0.2",
|
||||||
"@libp2p/peer-id-factory": "^1.0.15",
|
"@libp2p/peer-id-factory": "^1.0.15",
|
||||||
"@rollup/plugin-commonjs": "^22.0.0",
|
"@rollup/plugin-commonjs": "^22.0.0",
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
|
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
|
||||||
import { multiaddr } from "@multiformats/multiaddr";
|
import { multiaddr } from "@multiformats/multiaddr";
|
||||||
import { bytesToHex, hexToBytes, utf8ToBytes } from "@waku/byte-utils";
|
import { bytesToHex, hexToBytes, utf8ToBytes } from "@waku/byte-utils";
|
||||||
|
import type { Waku2 } from "@waku/interfaces";
|
||||||
import { assert, expect } from "chai";
|
import { assert, expect } from "chai";
|
||||||
import { equals } from "uint8arrays/equals";
|
import { equals } from "uint8arrays/equals";
|
||||||
|
|
||||||
@ -8,7 +9,6 @@ import { ERR_INVALID_ID } from "./constants.js";
|
|||||||
import { getPublicKey } from "./crypto.js";
|
import { getPublicKey } from "./crypto.js";
|
||||||
import { ENR } from "./enr.js";
|
import { ENR } from "./enr.js";
|
||||||
import { createKeypairFromPeerId, IKeypair } from "./keypair/index.js";
|
import { createKeypairFromPeerId, IKeypair } from "./keypair/index.js";
|
||||||
import { Waku2 } from "./waku2_codec.js";
|
|
||||||
|
|
||||||
describe("ENR", function () {
|
describe("ENR", function () {
|
||||||
describe("Txt codec", () => {
|
describe("Txt codec", () => {
|
||||||
|
@ -11,6 +11,14 @@ import {
|
|||||||
hexToBytes,
|
hexToBytes,
|
||||||
utf8ToBytes,
|
utf8ToBytes,
|
||||||
} from "@waku/byte-utils";
|
} from "@waku/byte-utils";
|
||||||
|
import type {
|
||||||
|
ENRKey,
|
||||||
|
ENRValue,
|
||||||
|
IEnr,
|
||||||
|
NodeId,
|
||||||
|
SequenceNumber,
|
||||||
|
Waku2,
|
||||||
|
} from "@waku/interfaces";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
import { fromString } from "uint8arrays/from-string";
|
import { fromString } from "uint8arrays/from-string";
|
||||||
import { toString } from "uint8arrays/to-string";
|
import { toString } from "uint8arrays/to-string";
|
||||||
@ -30,22 +38,21 @@ import {
|
|||||||
} from "./keypair/index.js";
|
} from "./keypair/index.js";
|
||||||
import { multiaddrFromFields } from "./multiaddr_from_fields.js";
|
import { multiaddrFromFields } from "./multiaddr_from_fields.js";
|
||||||
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
|
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
|
||||||
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types.js";
|
|
||||||
import * as v4 from "./v4.js";
|
import * as v4 from "./v4.js";
|
||||||
import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec.js";
|
import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
|
||||||
|
|
||||||
const log = debug("waku:enr");
|
const log = debug("waku:enr");
|
||||||
|
|
||||||
export class ENR extends Map<ENRKey, ENRValue> {
|
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;
|
||||||
public signature: Uint8Array | null;
|
public signature?: Uint8Array;
|
||||||
public peerId?: PeerId;
|
public peerId?: PeerId;
|
||||||
|
|
||||||
private constructor(
|
private constructor(
|
||||||
kvs: Record<ENRKey, ENRValue> = {},
|
kvs: Record<ENRKey, ENRValue> = {},
|
||||||
seq: SequenceNumber = BigInt(1),
|
seq: SequenceNumber = BigInt(1),
|
||||||
signature: Uint8Array | null = null
|
signature?: Uint8Array
|
||||||
) {
|
) {
|
||||||
super(Object.entries(kvs));
|
super(Object.entries(kvs));
|
||||||
this.seq = seq;
|
this.seq = seq;
|
||||||
@ -55,7 +62,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
static async create(
|
static async create(
|
||||||
kvs: Record<ENRKey, ENRValue> = {},
|
kvs: Record<ENRKey, ENRValue> = {},
|
||||||
seq: SequenceNumber = BigInt(1),
|
seq: SequenceNumber = BigInt(1),
|
||||||
signature: Uint8Array | null = null
|
signature?: Uint8Array
|
||||||
): Promise<ENR> {
|
): Promise<ENR> {
|
||||||
const enr = new ENR(kvs, seq, signature);
|
const enr = new ENR(kvs, seq, signature);
|
||||||
try {
|
try {
|
||||||
@ -150,7 +157,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
set(k: ENRKey, v: ENRValue): this {
|
set(k: ENRKey, v: ENRValue): this {
|
||||||
this.signature = null;
|
this.signature = undefined;
|
||||||
this.seq++;
|
this.seq++;
|
||||||
return super.set(k, v);
|
return super.set(k, v);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
export * from "./constants.js";
|
export * from "./constants.js";
|
||||||
export * from "./enr.js";
|
export * from "./enr.js";
|
||||||
export * from "./types.js";
|
|
||||||
export * from "./keypair/index.js";
|
export * from "./keypair/index.js";
|
||||||
export * from "./waku2_codec.js";
|
export * from "./waku2_codec.js";
|
||||||
export * from "./crypto.js";
|
export * from "./crypto.js";
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
// Custom and aliased types for ENRs
|
|
||||||
|
|
||||||
/**
|
|
||||||
* We represent NodeId as a hex string, since node equality is used very heavily
|
|
||||||
* and it is convenient to index data by NodeId
|
|
||||||
*/
|
|
||||||
export type NodeId = string;
|
|
||||||
export type SequenceNumber = bigint;
|
|
||||||
|
|
||||||
export type ENRKey = string;
|
|
||||||
export type ENRValue = Uint8Array;
|
|
@ -1,8 +1,8 @@
|
|||||||
import * as secp from "@noble/secp256k1";
|
import * as secp from "@noble/secp256k1";
|
||||||
import { bytesToHex } from "@waku/byte-utils";
|
import { bytesToHex } from "@waku/byte-utils";
|
||||||
|
import type { NodeId } from "@waku/interfaces";
|
||||||
|
|
||||||
import { keccak256 } from "./crypto.js";
|
import { keccak256 } from "./crypto.js";
|
||||||
import { NodeId } from "./types.js";
|
|
||||||
|
|
||||||
export async function sign(
|
export async function sign(
|
||||||
privKey: Uint8Array,
|
privKey: Uint8Array,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
import type { Waku2 } from "@waku/interfaces";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
|
|
||||||
import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec.js";
|
import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
|
||||||
|
|
||||||
const waku2FieldEncodings = {
|
const waku2FieldEncodings = {
|
||||||
relay: 1,
|
relay: 1,
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
export interface Waku2 {
|
import type { Waku2 } from "@waku/interfaces";
|
||||||
relay: boolean;
|
|
||||||
store: boolean;
|
|
||||||
filter: boolean;
|
|
||||||
lightPush: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function encodeWaku2(protocols: Waku2): number {
|
export function encodeWaku2(protocols: Waku2): number {
|
||||||
let byte = 0;
|
let byte = 0;
|
||||||
|
@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `IPeerExchange` interface.
|
||||||
|
- `IEnr` interface.
|
||||||
|
|
||||||
## [0.0.6] - 2022-12-15
|
## [0.0.6] - 2022-12-15
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
38
packages/interfaces/src/enr.ts
Normal file
38
packages/interfaces/src/enr.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||||
|
import type { Multiaddr } from "@multiformats/multiaddr";
|
||||||
|
|
||||||
|
export type ENRKey = string;
|
||||||
|
export type ENRValue = Uint8Array;
|
||||||
|
/**
|
||||||
|
* We represent NodeId as a hex string, since node equality is used very heavily
|
||||||
|
* and it is convenient to index data by NodeId
|
||||||
|
*/
|
||||||
|
export type NodeId = string;
|
||||||
|
export type SequenceNumber = bigint;
|
||||||
|
|
||||||
|
export interface Waku2 {
|
||||||
|
relay: boolean;
|
||||||
|
store: boolean;
|
||||||
|
filter: boolean;
|
||||||
|
lightPush: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IEnr extends Map<ENRKey, ENRValue> {
|
||||||
|
nodeId?: NodeId;
|
||||||
|
peerId?: PeerId;
|
||||||
|
id: string;
|
||||||
|
seq: SequenceNumber;
|
||||||
|
publicKey?: Uint8Array;
|
||||||
|
signature?: Uint8Array;
|
||||||
|
ip?: string;
|
||||||
|
tcp?: number;
|
||||||
|
udp?: number;
|
||||||
|
ip6?: string;
|
||||||
|
tcp6?: number;
|
||||||
|
udp6?: number;
|
||||||
|
multiaddrs?: Multiaddr[];
|
||||||
|
waku2?: Waku2;
|
||||||
|
|
||||||
|
encode(privateKey?: Uint8Array): Promise<Uint8Array>;
|
||||||
|
getFullMultiaddrs(): Multiaddr[];
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
export * from "./enr.js";
|
||||||
export * from "./filter.js";
|
export * from "./filter.js";
|
||||||
export * from "./light_push.js";
|
export * from "./light_push.js";
|
||||||
export * from "./message.js";
|
export * from "./message.js";
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import type { ConnectionManager } from "@libp2p/interface-connection-manager";
|
import type { ConnectionManager } from "@libp2p/interface-connection-manager";
|
||||||
import type { PeerStore } from "@libp2p/interface-peer-store";
|
import type { PeerStore } from "@libp2p/interface-peer-store";
|
||||||
import type { Registrar } from "@libp2p/interface-registrar";
|
import type { Registrar } from "@libp2p/interface-registrar";
|
||||||
import { ENR } from "@waku/enr";
|
|
||||||
|
|
||||||
|
import { IEnr } from "./enr.js";
|
||||||
import { PointToPointProtocol } from "./protocols.js";
|
import { PointToPointProtocol } from "./protocols.js";
|
||||||
|
|
||||||
export interface IPeerExchange extends PointToPointProtocol {
|
export interface IPeerExchange extends PointToPointProtocol {
|
||||||
@ -21,7 +21,7 @@ export interface PeerExchangeResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface PeerInfo {
|
export interface PeerInfo {
|
||||||
ENR?: ENR;
|
ENR?: IEnr;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PeerExchangeComponents {
|
export interface PeerExchangeComponents {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user