chore: remove waku dep from @waku/interfaces by defining IEnr

@waku packages are expected to (dev)depend on @waku/interfaces to
implement them. Hence, to avoid possible cyclic (dev)dependency, define
IEnr interface and use it as a type across @waku/packages.
This commit is contained in:
fryorcraken.eth 2022-12-13 12:31:27 +11:00
parent 42f07df969
commit f48278adce
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
18 changed files with 84 additions and 45 deletions

View File

@ -3,11 +3,11 @@
"private": true,
"type": "module",
"workspaces": [
"packages/proto",
"packages/byte-utils",
"packages/enr",
"packages/interfaces",
"packages/libp2p-utils",
"packages/proto",
"packages/interfaces",
"packages/enr",
"packages/peer-exchange",
"packages/core",
"packages/dns-discovery",

View File

@ -70,6 +70,7 @@
"@libp2p/peer-id": "^1.1.10"
},
"devDependencies": {
"@waku/interfaces": "*",
"@multiformats/multiaddr": "^11.0.6",
"@libp2p/peer-id-factory": "^1.0.15",
"chai": "^4.3.4",

View File

@ -1,4 +1,5 @@
import { ENR } from "@waku/enr";
import type { IEnr } from "@waku/interfaces";
import debug from "debug";
import { DnsOverHttps } from "./dns_over_https.js";
@ -49,7 +50,7 @@ export class DnsNodeDiscovery {
async getPeers(
enrTreeUrls: string[],
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
): Promise<ENR[]> {
): Promise<IEnr[]> {
const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
const context: SearchContext = {
@ -86,7 +87,7 @@ export class DnsNodeDiscovery {
async *getNextPeer(
enrTreeUrls: string[],
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
): AsyncGenerator<ENR> {
): AsyncGenerator<IEnr> {
const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
const context: SearchContext = {

View File

@ -1,6 +1,7 @@
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
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 { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js";

View File

@ -1,4 +1,4 @@
import { ENR, Waku2 } from "@waku/enr";
import type { IEnr, Waku2 } from "@waku/interfaces";
import debug from "debug";
import { NodeCapabilityCount } from "./dns.js";
@ -13,8 +13,8 @@ const log = debug("waku:discovery:fetch_nodes");
export async function fetchNodesUntilCapabilitiesFulfilled(
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
errorTolerance: number,
getNode: () => Promise<ENR | null>
): Promise<ENR[]> {
getNode: () => Promise<IEnr | null>
): Promise<IEnr[]> {
const wanted = {
relay: wantedNodeCapabilityCount.relay ?? 0,
store: wantedNodeCapabilityCount.store ?? 0,
@ -33,7 +33,7 @@ export async function fetchNodesUntilCapabilitiesFulfilled(
};
let totalSearches = 0;
const peers: ENR[] = [];
const peers: IEnr[] = [];
while (
!isSatisfied(wanted, actual) &&
@ -64,8 +64,8 @@ export async function fetchNodesUntilCapabilitiesFulfilled(
export async function* yieldNodesUntilCapabilitiesFulfilled(
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
errorTolerance: number,
getNode: () => Promise<ENR | null>
): AsyncGenerator<ENR> {
getNode: () => Promise<IEnr | null>
): AsyncGenerator<IEnr> {
const wanted = {
relay: wantedNodeCapabilityCount.relay ?? 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;
for (const existingPeer of peers) {

View File

@ -5,7 +5,7 @@ import type {
import { symbol } from "@libp2p/interface-peer-discovery";
import type { PeerInfo } from "@libp2p/interface-peer-info";
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 debug from "debug";
@ -22,7 +22,7 @@ export class PeerDiscoveryDns
extends EventEmitter<PeerDiscoveryEvents>
implements PeerDiscovery
{
private readonly nextPeer: () => AsyncGenerator<ENR>;
private readonly nextPeer: () => AsyncGenerator<IEnr>;
private _started: boolean;
/**

View File

@ -63,6 +63,7 @@
"js-sha3": "^0.8.0"
},
"devDependencies": {
"@waku/interfaces": "*",
"uint8arrays": "^4.0.2",
"@libp2p/peer-id-factory": "^1.0.15",
"@rollup/plugin-commonjs": "^22.0.0",

View File

@ -1,6 +1,7 @@
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { multiaddr } from "@multiformats/multiaddr";
import { bytesToHex, hexToBytes, utf8ToBytes } from "@waku/byte-utils";
import type { Waku2 } from "@waku/interfaces";
import { assert, expect } from "chai";
import { equals } from "uint8arrays/equals";
@ -8,7 +9,6 @@ import { ERR_INVALID_ID } from "./constants.js";
import { getPublicKey } from "./crypto.js";
import { ENR } from "./enr.js";
import { createKeypairFromPeerId, IKeypair } from "./keypair/index.js";
import { Waku2 } from "./waku2_codec.js";
describe("ENR", function () {
describe("Txt codec", () => {

View File

@ -11,6 +11,14 @@ import {
hexToBytes,
utf8ToBytes,
} from "@waku/byte-utils";
import type {
ENRKey,
ENRValue,
IEnr,
NodeId,
SequenceNumber,
Waku2,
} from "@waku/interfaces";
import debug from "debug";
import { fromString } from "uint8arrays/from-string";
import { toString } from "uint8arrays/to-string";
@ -30,22 +38,21 @@ import {
} from "./keypair/index.js";
import { multiaddrFromFields } from "./multiaddr_from_fields.js";
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types.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");
export class ENR extends Map<ENRKey, ENRValue> {
export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
public static readonly RECORD_PREFIX = "enr:";
public seq: SequenceNumber;
public signature: Uint8Array | null;
public signature?: Uint8Array;
public peerId?: PeerId;
private constructor(
kvs: Record<ENRKey, ENRValue> = {},
seq: SequenceNumber = BigInt(1),
signature: Uint8Array | null = null
signature?: Uint8Array
) {
super(Object.entries(kvs));
this.seq = seq;
@ -55,7 +62,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
static async create(
kvs: Record<ENRKey, ENRValue> = {},
seq: SequenceNumber = BigInt(1),
signature: Uint8Array | null = null
signature?: Uint8Array
): Promise<ENR> {
const enr = new ENR(kvs, seq, signature);
try {
@ -150,7 +157,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
}
set(k: ENRKey, v: ENRValue): this {
this.signature = null;
this.signature = undefined;
this.seq++;
return super.set(k, v);
}

View File

@ -1,6 +1,5 @@
export * from "./constants.js";
export * from "./enr.js";
export * from "./types.js";
export * from "./keypair/index.js";
export * from "./waku2_codec.js";
export * from "./crypto.js";

View File

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

View File

@ -1,8 +1,8 @@
import * as secp from "@noble/secp256k1";
import { bytesToHex } from "@waku/byte-utils";
import type { NodeId } from "@waku/interfaces";
import { keccak256 } from "./crypto.js";
import { NodeId } from "./types.js";
export async function sign(
privKey: Uint8Array,

View File

@ -1,6 +1,7 @@
import type { Waku2 } from "@waku/interfaces";
import { expect } from "chai";
import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec.js";
import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
const waku2FieldEncodings = {
relay: 1,

View File

@ -1,9 +1,4 @@
export interface Waku2 {
relay: boolean;
store: boolean;
filter: boolean;
lightPush: boolean;
}
import type { Waku2 } from "@waku/interfaces";
export function encodeWaku2(protocols: Waku2): number {
let byte = 0;

View File

@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- `IPeerExchange` interface.
- `IEnr` interface.
## [0.0.6] - 2022-12-15
### Changed

View 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[];
}

View File

@ -1,3 +1,4 @@
export * from "./enr.js";
export * from "./filter.js";
export * from "./light_push.js";
export * from "./message.js";

View File

@ -1,8 +1,8 @@
import type { ConnectionManager } from "@libp2p/interface-connection-manager";
import type { PeerStore } from "@libp2p/interface-peer-store";
import type { Registrar } from "@libp2p/interface-registrar";
import { ENR } from "@waku/enr";
import { IEnr } from "./enr.js";
import { PointToPointProtocol } from "./protocols.js";
export interface IPeerExchange extends PointToPointProtocol {
@ -21,7 +21,7 @@ export interface PeerExchangeResponse {
}
export interface PeerInfo {
ENR?: ENR;
ENR?: IEnr;
}
export interface PeerExchangeComponents {