@waku/discovery 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/bundle/index.js +25137 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/dns/constants.d.ts +9 -0
- package/dist/dns/constants.js +13 -0
- package/dist/dns/constants.js.map +1 -0
- package/dist/dns/dns.d.ts +32 -0
- package/dist/dns/dns.js +160 -0
- package/dist/dns/dns.js.map +1 -0
- package/dist/dns/dns_discovery.d.ts +24 -0
- package/dist/dns/dns_discovery.js +95 -0
- package/dist/dns/dns_discovery.js.map +1 -0
- package/dist/dns/dns_over_https.d.ts +25 -0
- package/dist/dns/dns_over_https.js +72 -0
- package/dist/dns/dns_over_https.js.map +1 -0
- package/dist/dns/enrtree.d.ts +33 -0
- package/dist/dns/enrtree.js +76 -0
- package/dist/dns/enrtree.js.map +1 -0
- package/dist/dns/fetch_nodes.d.ts +13 -0
- package/dist/dns/fetch_nodes.js +133 -0
- package/dist/dns/fetch_nodes.js.map +1 -0
- package/dist/dns/index.d.ts +3 -0
- package/dist/dns/index.js +4 -0
- package/dist/dns/index.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/local-peer-cache/index.d.ts +24 -0
- package/dist/local-peer-cache/index.js +106 -0
- package/dist/local-peer-cache/index.js.map +1 -0
- package/dist/peer-exchange/index.d.ts +2 -0
- package/dist/peer-exchange/index.js +3 -0
- package/dist/peer-exchange/index.js.map +1 -0
- package/dist/peer-exchange/rpc.d.ts +22 -0
- package/dist/peer-exchange/rpc.js +41 -0
- package/dist/peer-exchange/rpc.js.map +1 -0
- package/dist/peer-exchange/waku_peer_exchange.d.ts +21 -0
- package/dist/peer-exchange/waku_peer_exchange.js +80 -0
- package/dist/peer-exchange/waku_peer_exchange.js.map +1 -0
- package/dist/peer-exchange/waku_peer_exchange_discovery.d.ts +53 -0
- package/dist/peer-exchange/waku_peer_exchange_discovery.js +136 -0
- package/dist/peer-exchange/waku_peer_exchange_discovery.js.map +1 -0
- package/package.json +109 -0
- package/src/dns/constants.ts +17 -0
- package/src/dns/dns.ts +215 -0
- package/src/dns/dns_discovery.ts +144 -0
- package/src/dns/dns_over_https.ts +83 -0
- package/src/dns/enrtree.ts +123 -0
- package/src/dns/fetch_nodes.ts +181 -0
- package/src/dns/index.ts +3 -0
- package/src/index.ts +21 -0
- package/src/local-peer-cache/index.ts +160 -0
- package/src/peer-exchange/index.ts +11 -0
- package/src/peer-exchange/rpc.ts +44 -0
- package/src/peer-exchange/waku_peer_exchange.ts +111 -0
- package/src/peer-exchange/waku_peer_exchange_discovery.ts +238 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
import type { DnsClient } from "@waku/interfaces";
|
2
|
+
import { Logger } from "@waku/utils";
|
3
|
+
import { bytesToUtf8 } from "@waku/utils/bytes";
|
4
|
+
import { Endpoint, query, wellknown } from "dns-query";
|
5
|
+
|
6
|
+
const log = new Logger("dns-over-https");
|
7
|
+
|
8
|
+
export class DnsOverHttps implements DnsClient {
|
9
|
+
/**
|
10
|
+
* Create new Dns-Over-Http DNS client.
|
11
|
+
*
|
12
|
+
* @param endpoints The endpoints for Dns-Over-Https queries;
|
13
|
+
* Defaults to using dns-query's API..
|
14
|
+
* @param retries Retries if a given endpoint fails.
|
15
|
+
*
|
16
|
+
* @throws {code: string} If DNS query fails.
|
17
|
+
*/
|
18
|
+
public static async create(
|
19
|
+
endpoints?: Endpoint[],
|
20
|
+
retries?: number
|
21
|
+
): Promise<DnsOverHttps> {
|
22
|
+
const _endpoints = endpoints ?? (await wellknown.endpoints("doh"));
|
23
|
+
|
24
|
+
return new DnsOverHttps(_endpoints, retries);
|
25
|
+
}
|
26
|
+
|
27
|
+
private constructor(
|
28
|
+
private endpoints: Endpoint[],
|
29
|
+
private retries: number = 3
|
30
|
+
) {}
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Resolves a TXT record
|
34
|
+
*
|
35
|
+
* @param domain The domain name
|
36
|
+
*
|
37
|
+
* @throws if the query fails
|
38
|
+
*/
|
39
|
+
async resolveTXT(domain: string): Promise<string[]> {
|
40
|
+
let answers;
|
41
|
+
try {
|
42
|
+
const res = await query(
|
43
|
+
{
|
44
|
+
question: { type: "TXT", name: domain }
|
45
|
+
},
|
46
|
+
{
|
47
|
+
endpoints: this.endpoints,
|
48
|
+
retries: this.retries
|
49
|
+
}
|
50
|
+
);
|
51
|
+
answers = res.answers;
|
52
|
+
} catch (error) {
|
53
|
+
log.error("query failed: ", error);
|
54
|
+
throw new Error("DNS query failed");
|
55
|
+
}
|
56
|
+
|
57
|
+
if (!answers) throw new Error(`Could not resolve ${domain}`);
|
58
|
+
|
59
|
+
const data = answers.map((a) => a.data) as
|
60
|
+
| Array<string | Uint8Array>
|
61
|
+
| Array<Array<string | Uint8Array>>;
|
62
|
+
|
63
|
+
const result: string[] = [];
|
64
|
+
|
65
|
+
data.forEach((d) => {
|
66
|
+
if (typeof d === "string") {
|
67
|
+
result.push(d);
|
68
|
+
} else if (Array.isArray(d)) {
|
69
|
+
d.forEach((sd) => {
|
70
|
+
if (typeof sd === "string") {
|
71
|
+
result.push(sd);
|
72
|
+
} else {
|
73
|
+
result.push(bytesToUtf8(sd));
|
74
|
+
}
|
75
|
+
});
|
76
|
+
} else {
|
77
|
+
result.push(bytesToUtf8(d));
|
78
|
+
}
|
79
|
+
});
|
80
|
+
|
81
|
+
return result;
|
82
|
+
}
|
83
|
+
}
|
@@ -0,0 +1,123 @@
|
|
1
|
+
import { ENR } from "@waku/enr";
|
2
|
+
import { keccak256, verifySignature } from "@waku/enr";
|
3
|
+
import { utf8ToBytes } from "@waku/utils/bytes";
|
4
|
+
import base32 from "hi-base32";
|
5
|
+
import { fromString } from "uint8arrays/from-string";
|
6
|
+
|
7
|
+
export type ENRRootValues = {
|
8
|
+
eRoot: string;
|
9
|
+
lRoot: string;
|
10
|
+
seq: number;
|
11
|
+
signature: string;
|
12
|
+
};
|
13
|
+
|
14
|
+
export type ENRTreeValues = {
|
15
|
+
publicKey: string;
|
16
|
+
domain: string;
|
17
|
+
};
|
18
|
+
|
19
|
+
export class ENRTree {
|
20
|
+
public static readonly RECORD_PREFIX = ENR.RECORD_PREFIX;
|
21
|
+
public static readonly TREE_PREFIX = "enrtree:";
|
22
|
+
public static readonly BRANCH_PREFIX = "enrtree-branch:";
|
23
|
+
public static readonly ROOT_PREFIX = "enrtree-root:";
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Extracts the branch subdomain referenced by a DNS tree root string after verifying
|
27
|
+
* the root record signature with its base32 compressed public key.
|
28
|
+
*/
|
29
|
+
static parseAndVerifyRoot(root: string, publicKey: string): string {
|
30
|
+
if (!root.startsWith(this.ROOT_PREFIX))
|
31
|
+
throw new Error(
|
32
|
+
`ENRTree root entry must start with '${this.ROOT_PREFIX}'`
|
33
|
+
);
|
34
|
+
|
35
|
+
const rootValues = ENRTree.parseRootValues(root);
|
36
|
+
const decodedPublicKey = base32.decode.asBytes(publicKey);
|
37
|
+
|
38
|
+
// The signature is a 65-byte secp256k1 over the keccak256 hash
|
39
|
+
// of the record content, excluding the `sig=` part, encoded as URL-safe base64 string
|
40
|
+
// (Trailing recovery bit must be trimmed to pass `ecdsaVerify` method)
|
41
|
+
const signedComponent = root.split(" sig")[0];
|
42
|
+
|
43
|
+
const signedComponentBuffer = utf8ToBytes(signedComponent);
|
44
|
+
const signatureBuffer = fromString(rootValues.signature, "base64url").slice(
|
45
|
+
0,
|
46
|
+
64
|
47
|
+
);
|
48
|
+
|
49
|
+
const isVerified = verifySignature(
|
50
|
+
signatureBuffer,
|
51
|
+
keccak256(signedComponentBuffer),
|
52
|
+
new Uint8Array(decodedPublicKey)
|
53
|
+
);
|
54
|
+
|
55
|
+
if (!isVerified) throw new Error("Unable to verify ENRTree root signature");
|
56
|
+
|
57
|
+
return rootValues.eRoot;
|
58
|
+
}
|
59
|
+
|
60
|
+
static parseRootValues(txt: string): ENRRootValues {
|
61
|
+
const matches = txt.match(
|
62
|
+
/^enrtree-root:v1 e=([^ ]+) l=([^ ]+) seq=(\d+) sig=([^ ]+)$/
|
63
|
+
);
|
64
|
+
|
65
|
+
if (!Array.isArray(matches))
|
66
|
+
throw new Error("Could not parse ENRTree root entry");
|
67
|
+
|
68
|
+
matches.shift(); // The first entry is the full match
|
69
|
+
const [eRoot, lRoot, seq, signature] = matches;
|
70
|
+
|
71
|
+
if (!eRoot)
|
72
|
+
throw new Error("Could not parse 'e' value from ENRTree root entry");
|
73
|
+
if (!lRoot)
|
74
|
+
throw new Error("Could not parse 'l' value from ENRTree root entry");
|
75
|
+
|
76
|
+
if (!seq)
|
77
|
+
throw new Error("Could not parse 'seq' value from ENRTree root entry");
|
78
|
+
if (!signature)
|
79
|
+
throw new Error("Could not parse 'sig' value from ENRTree root entry");
|
80
|
+
|
81
|
+
return { eRoot, lRoot, seq: Number(seq), signature };
|
82
|
+
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Returns the public key and top level domain of an ENR tree entry.
|
86
|
+
* The domain is the starting point for traversing a set of linked DNS TXT records
|
87
|
+
* and the public key is used to verify the root entry record
|
88
|
+
*/
|
89
|
+
static parseTree(tree: string): ENRTreeValues {
|
90
|
+
if (!tree.startsWith(this.TREE_PREFIX))
|
91
|
+
throw new Error(
|
92
|
+
`ENRTree tree entry must start with '${this.TREE_PREFIX}'`
|
93
|
+
);
|
94
|
+
|
95
|
+
const matches = tree.match(/^enrtree:\/\/([^@]+)@(.+)$/);
|
96
|
+
|
97
|
+
if (!Array.isArray(matches))
|
98
|
+
throw new Error("Could not parse ENRTree tree entry");
|
99
|
+
|
100
|
+
matches.shift(); // The first entry is the full match
|
101
|
+
const [publicKey, domain] = matches;
|
102
|
+
|
103
|
+
if (!publicKey)
|
104
|
+
throw new Error("Could not parse public key from ENRTree tree entry");
|
105
|
+
if (!domain)
|
106
|
+
throw new Error("Could not parse domain from ENRTree tree entry");
|
107
|
+
|
108
|
+
return { publicKey, domain };
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* Returns subdomains listed in an ENR branch entry. These in turn lead to
|
113
|
+
* either further branch entries or ENR records.
|
114
|
+
*/
|
115
|
+
static parseBranch(branch: string): string[] {
|
116
|
+
if (!branch.startsWith(this.BRANCH_PREFIX))
|
117
|
+
throw new Error(
|
118
|
+
`ENRTree branch entry must start with '${this.BRANCH_PREFIX}'`
|
119
|
+
);
|
120
|
+
|
121
|
+
return branch.split(this.BRANCH_PREFIX)[1].split(",");
|
122
|
+
}
|
123
|
+
}
|
@@ -0,0 +1,181 @@
|
|
1
|
+
import type { IEnr, NodeCapabilityCount, Waku2 } from "@waku/interfaces";
|
2
|
+
import { Logger } from "@waku/utils";
|
3
|
+
|
4
|
+
const log = new Logger("discovery:fetch_nodes");
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Fetch nodes using passed [[getNode]] until all wanted capabilities are
|
8
|
+
* fulfilled or the number of [[getNode]] call exceeds the sum of
|
9
|
+
* [[wantedNodeCapabilityCount]] plus [[errorTolerance]].
|
10
|
+
*/
|
11
|
+
export async function fetchNodesUntilCapabilitiesFulfilled(
|
12
|
+
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
13
|
+
errorTolerance: number,
|
14
|
+
getNode: () => Promise<IEnr | null>
|
15
|
+
): Promise<IEnr[]> {
|
16
|
+
const wanted = {
|
17
|
+
relay: wantedNodeCapabilityCount.relay ?? 0,
|
18
|
+
store: wantedNodeCapabilityCount.store ?? 0,
|
19
|
+
filter: wantedNodeCapabilityCount.filter ?? 0,
|
20
|
+
lightPush: wantedNodeCapabilityCount.lightPush ?? 0
|
21
|
+
};
|
22
|
+
|
23
|
+
const maxSearches =
|
24
|
+
wanted.relay + wanted.store + wanted.filter + wanted.lightPush;
|
25
|
+
|
26
|
+
const actual = {
|
27
|
+
relay: 0,
|
28
|
+
store: 0,
|
29
|
+
filter: 0,
|
30
|
+
lightPush: 0
|
31
|
+
};
|
32
|
+
|
33
|
+
let totalSearches = 0;
|
34
|
+
const peers: IEnr[] = [];
|
35
|
+
|
36
|
+
while (
|
37
|
+
!isSatisfied(wanted, actual) &&
|
38
|
+
totalSearches < maxSearches + errorTolerance
|
39
|
+
) {
|
40
|
+
const peer = await getNode();
|
41
|
+
if (peer && isNewPeer(peer, peers)) {
|
42
|
+
// ENRs without a waku2 key are ignored.
|
43
|
+
if (peer.waku2) {
|
44
|
+
if (helpsSatisfyCapabilities(peer.waku2, wanted, actual)) {
|
45
|
+
addCapabilities(peer.waku2, actual);
|
46
|
+
peers.push(peer);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
log.info(
|
50
|
+
`got new peer candidate from DNS address=${peer.nodeId}@${peer.ip}`
|
51
|
+
);
|
52
|
+
}
|
53
|
+
|
54
|
+
totalSearches++;
|
55
|
+
}
|
56
|
+
return peers;
|
57
|
+
}
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Fetch nodes using passed [[getNode]] until all wanted capabilities are
|
61
|
+
* fulfilled or the number of [[getNode]] call exceeds the sum of
|
62
|
+
* [[wantedNodeCapabilityCount]] plus [[errorTolerance]].
|
63
|
+
*/
|
64
|
+
export async function* yieldNodesUntilCapabilitiesFulfilled(
|
65
|
+
wantedNodeCapabilityCount: Partial<NodeCapabilityCount>,
|
66
|
+
errorTolerance: number,
|
67
|
+
getNode: () => Promise<IEnr | null>
|
68
|
+
): AsyncGenerator<IEnr> {
|
69
|
+
const wanted = {
|
70
|
+
relay: wantedNodeCapabilityCount.relay ?? 0,
|
71
|
+
store: wantedNodeCapabilityCount.store ?? 0,
|
72
|
+
filter: wantedNodeCapabilityCount.filter ?? 0,
|
73
|
+
lightPush: wantedNodeCapabilityCount.lightPush ?? 0
|
74
|
+
};
|
75
|
+
|
76
|
+
const maxSearches =
|
77
|
+
wanted.relay + wanted.store + wanted.filter + wanted.lightPush;
|
78
|
+
|
79
|
+
const actual = {
|
80
|
+
relay: 0,
|
81
|
+
store: 0,
|
82
|
+
filter: 0,
|
83
|
+
lightPush: 0
|
84
|
+
};
|
85
|
+
|
86
|
+
let totalSearches = 0;
|
87
|
+
const peerNodeIds = new Set();
|
88
|
+
|
89
|
+
while (
|
90
|
+
!isSatisfied(wanted, actual) &&
|
91
|
+
totalSearches < maxSearches + errorTolerance
|
92
|
+
) {
|
93
|
+
const peer = await getNode();
|
94
|
+
if (peer && peer.nodeId && !peerNodeIds.has(peer.nodeId)) {
|
95
|
+
peerNodeIds.add(peer.nodeId);
|
96
|
+
// ENRs without a waku2 key are ignored.
|
97
|
+
if (peer.waku2) {
|
98
|
+
if (helpsSatisfyCapabilities(peer.waku2, wanted, actual)) {
|
99
|
+
addCapabilities(peer.waku2, actual);
|
100
|
+
yield peer;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
log.info(
|
104
|
+
`got new peer candidate from DNS address=${peer.nodeId}@${peer.ip}`
|
105
|
+
);
|
106
|
+
}
|
107
|
+
totalSearches++;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
function isSatisfied(
|
112
|
+
wanted: NodeCapabilityCount,
|
113
|
+
actual: NodeCapabilityCount
|
114
|
+
): boolean {
|
115
|
+
return (
|
116
|
+
actual.relay >= wanted.relay &&
|
117
|
+
actual.store >= wanted.store &&
|
118
|
+
actual.filter >= wanted.filter &&
|
119
|
+
actual.lightPush >= wanted.lightPush
|
120
|
+
);
|
121
|
+
}
|
122
|
+
|
123
|
+
function isNewPeer(peer: IEnr, peers: IEnr[]): boolean {
|
124
|
+
if (!peer.nodeId) return false;
|
125
|
+
|
126
|
+
for (const existingPeer of peers) {
|
127
|
+
if (peer.nodeId === existingPeer.nodeId) {
|
128
|
+
return false;
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
return true;
|
133
|
+
}
|
134
|
+
|
135
|
+
function addCapabilities(node: Waku2, total: NodeCapabilityCount): void {
|
136
|
+
if (node.relay) total.relay += 1;
|
137
|
+
if (node.store) total.store += 1;
|
138
|
+
if (node.filter) total.filter += 1;
|
139
|
+
if (node.lightPush) total.lightPush += 1;
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* Checks if the proposed ENR [[node]] helps satisfy the [[wanted]] capabilities,
|
144
|
+
* considering the [[actual]] capabilities of nodes retrieved so far..
|
145
|
+
*
|
146
|
+
* @throws If the function is called when the wanted capabilities are already fulfilled.
|
147
|
+
*/
|
148
|
+
function helpsSatisfyCapabilities(
|
149
|
+
node: Waku2,
|
150
|
+
wanted: NodeCapabilityCount,
|
151
|
+
actual: NodeCapabilityCount
|
152
|
+
): boolean {
|
153
|
+
if (isSatisfied(wanted, actual)) {
|
154
|
+
throw "Internal Error: Waku2 wanted capabilities are already fulfilled";
|
155
|
+
}
|
156
|
+
|
157
|
+
const missing = missingCapabilities(wanted, actual);
|
158
|
+
|
159
|
+
return (
|
160
|
+
(missing.relay && node.relay) ||
|
161
|
+
(missing.store && node.store) ||
|
162
|
+
(missing.filter && node.filter) ||
|
163
|
+
(missing.lightPush && node.lightPush)
|
164
|
+
);
|
165
|
+
}
|
166
|
+
|
167
|
+
/**
|
168
|
+
* Return a [[Waku2]] Object for which capabilities are set to true if they are
|
169
|
+
* [[wanted]] yet missing from [[actual]].
|
170
|
+
*/
|
171
|
+
function missingCapabilities(
|
172
|
+
wanted: NodeCapabilityCount,
|
173
|
+
actual: NodeCapabilityCount
|
174
|
+
): Waku2 {
|
175
|
+
return {
|
176
|
+
relay: actual.relay < wanted.relay,
|
177
|
+
store: actual.store < wanted.store,
|
178
|
+
filter: actual.filter < wanted.filter,
|
179
|
+
lightPush: actual.lightPush < wanted.lightPush
|
180
|
+
};
|
181
|
+
}
|
package/src/dns/index.ts
ADDED
package/src/index.ts
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
// DNS Discovery
|
2
|
+
export { PeerDiscoveryDns, wakuDnsDiscovery } from "./dns/dns_discovery.js";
|
3
|
+
export { enrTree } from "./dns/constants.js";
|
4
|
+
export { DnsNodeDiscovery } from "./dns/dns.js";
|
5
|
+
|
6
|
+
// Peer Exchange Discovery
|
7
|
+
export {
|
8
|
+
wakuPeerExchange,
|
9
|
+
PeerExchangeCodec,
|
10
|
+
WakuPeerExchange
|
11
|
+
} from "./peer-exchange/waku_peer_exchange.js";
|
12
|
+
export {
|
13
|
+
wakuPeerExchangeDiscovery,
|
14
|
+
PeerExchangeDiscovery
|
15
|
+
} from "./peer-exchange/waku_peer_exchange_discovery.js";
|
16
|
+
|
17
|
+
// Local Peer Cache Discovery
|
18
|
+
export {
|
19
|
+
LocalPeerCacheDiscovery,
|
20
|
+
wakuLocalPeerCacheDiscovery
|
21
|
+
} from "./local-peer-cache/index.js";
|
@@ -0,0 +1,160 @@
|
|
1
|
+
import { TypedEventEmitter } from "@libp2p/interface";
|
2
|
+
import {
|
3
|
+
CustomEvent,
|
4
|
+
IdentifyResult,
|
5
|
+
PeerDiscovery,
|
6
|
+
PeerDiscoveryEvents,
|
7
|
+
PeerInfo,
|
8
|
+
Startable
|
9
|
+
} from "@libp2p/interface";
|
10
|
+
import { createFromJSON } from "@libp2p/peer-id-factory";
|
11
|
+
import { multiaddr } from "@multiformats/multiaddr";
|
12
|
+
import {
|
13
|
+
type Libp2pComponents,
|
14
|
+
type LocalStoragePeerInfo,
|
15
|
+
Tags
|
16
|
+
} from "@waku/interfaces";
|
17
|
+
import { getWsMultiaddrFromMultiaddrs, Logger } from "@waku/utils";
|
18
|
+
|
19
|
+
const log = new Logger("peer-exchange-discovery");
|
20
|
+
|
21
|
+
type LocalPeerCacheDiscoveryOptions = {
|
22
|
+
tagName?: string;
|
23
|
+
tagValue?: number;
|
24
|
+
tagTTL?: number;
|
25
|
+
};
|
26
|
+
|
27
|
+
export const DEFAULT_LOCAL_TAG_NAME = Tags.LOCAL;
|
28
|
+
const DEFAULT_LOCAL_TAG_VALUE = 50;
|
29
|
+
const DEFAULT_LOCAL_TAG_TTL = 100_000_000;
|
30
|
+
|
31
|
+
export class LocalPeerCacheDiscovery
|
32
|
+
extends TypedEventEmitter<PeerDiscoveryEvents>
|
33
|
+
implements PeerDiscovery, Startable
|
34
|
+
{
|
35
|
+
private isStarted: boolean;
|
36
|
+
private peers: LocalStoragePeerInfo[] = [];
|
37
|
+
|
38
|
+
constructor(
|
39
|
+
private readonly components: Libp2pComponents,
|
40
|
+
private readonly options?: LocalPeerCacheDiscoveryOptions
|
41
|
+
) {
|
42
|
+
super();
|
43
|
+
this.isStarted = false;
|
44
|
+
this.peers = this.getPeersFromLocalStorage();
|
45
|
+
}
|
46
|
+
|
47
|
+
get [Symbol.toStringTag](): string {
|
48
|
+
return "@waku/local-peer-cache-discovery";
|
49
|
+
}
|
50
|
+
|
51
|
+
async start(): Promise<void> {
|
52
|
+
if (this.isStarted) return;
|
53
|
+
|
54
|
+
log.info("Starting Local Storage Discovery");
|
55
|
+
this.components.events.addEventListener(
|
56
|
+
"peer:identify",
|
57
|
+
this.handleNewPeers
|
58
|
+
);
|
59
|
+
|
60
|
+
for (const { id: idStr, address } of this.peers) {
|
61
|
+
const peerId = await createFromJSON({ id: idStr });
|
62
|
+
if (await this.components.peerStore.has(peerId)) continue;
|
63
|
+
|
64
|
+
await this.components.peerStore.save(peerId, {
|
65
|
+
multiaddrs: [multiaddr(address)],
|
66
|
+
tags: {
|
67
|
+
[this.options?.tagName ?? DEFAULT_LOCAL_TAG_NAME]: {
|
68
|
+
value: this.options?.tagValue ?? DEFAULT_LOCAL_TAG_VALUE,
|
69
|
+
ttl: this.options?.tagTTL ?? DEFAULT_LOCAL_TAG_TTL
|
70
|
+
}
|
71
|
+
}
|
72
|
+
});
|
73
|
+
|
74
|
+
this.dispatchEvent(
|
75
|
+
new CustomEvent<PeerInfo>("peer", {
|
76
|
+
detail: {
|
77
|
+
id: peerId,
|
78
|
+
multiaddrs: [multiaddr(address)]
|
79
|
+
}
|
80
|
+
})
|
81
|
+
);
|
82
|
+
}
|
83
|
+
|
84
|
+
log.info(`Discovered ${this.peers.length} peers`);
|
85
|
+
|
86
|
+
this.isStarted = true;
|
87
|
+
}
|
88
|
+
|
89
|
+
stop(): void | Promise<void> {
|
90
|
+
if (!this.isStarted) return;
|
91
|
+
log.info("Stopping Local Storage Discovery");
|
92
|
+
this.components.events.removeEventListener(
|
93
|
+
"peer:identify",
|
94
|
+
this.handleNewPeers
|
95
|
+
);
|
96
|
+
this.isStarted = false;
|
97
|
+
|
98
|
+
this.savePeersToLocalStorage();
|
99
|
+
}
|
100
|
+
|
101
|
+
handleNewPeers = (event: CustomEvent<IdentifyResult>): void => {
|
102
|
+
const { peerId, listenAddrs } = event.detail;
|
103
|
+
|
104
|
+
const websocketMultiaddr = getWsMultiaddrFromMultiaddrs(listenAddrs);
|
105
|
+
|
106
|
+
const localStoragePeers = this.getPeersFromLocalStorage();
|
107
|
+
|
108
|
+
const existingPeerIndex = localStoragePeers.findIndex(
|
109
|
+
(_peer) => _peer.id === peerId.toString()
|
110
|
+
);
|
111
|
+
|
112
|
+
if (existingPeerIndex >= 0) {
|
113
|
+
localStoragePeers[existingPeerIndex].address =
|
114
|
+
websocketMultiaddr.toString();
|
115
|
+
} else {
|
116
|
+
localStoragePeers.push({
|
117
|
+
id: peerId.toString(),
|
118
|
+
address: websocketMultiaddr.toString()
|
119
|
+
});
|
120
|
+
}
|
121
|
+
|
122
|
+
this.peers = localStoragePeers;
|
123
|
+
this.savePeersToLocalStorage();
|
124
|
+
};
|
125
|
+
|
126
|
+
private getPeersFromLocalStorage(): LocalStoragePeerInfo[] {
|
127
|
+
try {
|
128
|
+
const storedPeersData = localStorage.getItem("waku:peers");
|
129
|
+
if (!storedPeersData) return [];
|
130
|
+
const peers = JSON.parse(storedPeersData);
|
131
|
+
return peers.filter(isValidStoredPeer);
|
132
|
+
} catch (error) {
|
133
|
+
log.error("Error parsing peers from local storage:", error);
|
134
|
+
return [];
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
private savePeersToLocalStorage(): void {
|
139
|
+
localStorage.setItem("waku:peers", JSON.stringify(this.peers));
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
function isValidStoredPeer(peer: any): peer is LocalStoragePeerInfo {
|
144
|
+
return (
|
145
|
+
peer &&
|
146
|
+
typeof peer === "object" &&
|
147
|
+
typeof peer.id === "string" &&
|
148
|
+
typeof peer.address === "string"
|
149
|
+
);
|
150
|
+
}
|
151
|
+
|
152
|
+
export function wakuLocalPeerCacheDiscovery(): (
|
153
|
+
components: Libp2pComponents,
|
154
|
+
options?: LocalPeerCacheDiscoveryOptions
|
155
|
+
) => LocalPeerCacheDiscovery {
|
156
|
+
return (
|
157
|
+
components: Libp2pComponents,
|
158
|
+
options?: LocalPeerCacheDiscoveryOptions
|
159
|
+
) => new LocalPeerCacheDiscovery(components, options);
|
160
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export {
|
2
|
+
wakuPeerExchange,
|
3
|
+
PeerExchangeCodec,
|
4
|
+
WakuPeerExchange
|
5
|
+
} from "./waku_peer_exchange.js";
|
6
|
+
export {
|
7
|
+
wakuPeerExchangeDiscovery,
|
8
|
+
PeerExchangeDiscovery,
|
9
|
+
Options,
|
10
|
+
DEFAULT_PEER_EXCHANGE_TAG_NAME
|
11
|
+
} from "./waku_peer_exchange_discovery.js";
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { proto_peer_exchange as proto } from "@waku/proto";
|
2
|
+
import type { Uint8ArrayList } from "uint8arraylist";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* PeerExchangeRPC represents a message conforming to the Waku Peer Exchange protocol
|
6
|
+
*/
|
7
|
+
export class PeerExchangeRPC {
|
8
|
+
public constructor(public proto: proto.PeerExchangeRPC) {}
|
9
|
+
|
10
|
+
static createRequest(params: proto.PeerExchangeQuery): PeerExchangeRPC {
|
11
|
+
const { numPeers } = params;
|
12
|
+
return new PeerExchangeRPC({
|
13
|
+
query: {
|
14
|
+
numPeers: numPeers
|
15
|
+
},
|
16
|
+
response: undefined
|
17
|
+
});
|
18
|
+
}
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Encode the current PeerExchangeRPC request to bytes
|
22
|
+
* @returns Uint8Array
|
23
|
+
*/
|
24
|
+
encode(): Uint8Array {
|
25
|
+
return proto.PeerExchangeRPC.encode(this.proto);
|
26
|
+
}
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Decode the current PeerExchangeRPC request to bytes
|
30
|
+
* @returns Uint8Array
|
31
|
+
*/
|
32
|
+
static decode(bytes: Uint8ArrayList): PeerExchangeRPC {
|
33
|
+
const res = proto.PeerExchangeRPC.decode(bytes);
|
34
|
+
return new PeerExchangeRPC(res);
|
35
|
+
}
|
36
|
+
|
37
|
+
get query(): proto.PeerExchangeQuery | undefined {
|
38
|
+
return this.proto.query;
|
39
|
+
}
|
40
|
+
|
41
|
+
get response(): proto.PeerExchangeResponse | undefined {
|
42
|
+
return this.proto.response;
|
43
|
+
}
|
44
|
+
}
|