@waku/core 0.0.20 → 0.0.21

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.
Files changed (59) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/bundle/index.js +256 -302
  3. package/bundle/lib/base_protocol.js +11 -6
  4. package/bundle/lib/message/version_0.js +1 -1
  5. package/bundle/lib/predefined_bootstrap_nodes.js +6 -6
  6. package/bundle/{version_0-9c941081.js → version_0-86411fdf.js} +3 -3
  7. package/dist/index.d.ts +2 -4
  8. package/dist/index.js +2 -4
  9. package/dist/index.js.map +1 -1
  10. package/dist/lib/base_protocol.d.ts +9 -5
  11. package/dist/lib/base_protocol.js +11 -6
  12. package/dist/lib/base_protocol.js.map +1 -1
  13. package/dist/lib/connection_manager.d.ts +6 -4
  14. package/dist/lib/connection_manager.js +120 -42
  15. package/dist/lib/connection_manager.js.map +1 -1
  16. package/dist/lib/filter/filter_rpc.js.map +1 -0
  17. package/dist/lib/filter/index.d.ts +2 -0
  18. package/dist/lib/filter/{v2/index.js → index.js} +14 -19
  19. package/dist/lib/filter/index.js.map +1 -0
  20. package/dist/lib/keep_alive_manager.d.ts +2 -2
  21. package/dist/lib/keep_alive_manager.js +1 -1
  22. package/dist/lib/keep_alive_manager.js.map +1 -1
  23. package/dist/lib/light_push/index.d.ts +1 -2
  24. package/dist/lib/light_push/index.js +1 -3
  25. package/dist/lib/light_push/index.js.map +1 -1
  26. package/dist/lib/predefined_bootstrap_nodes.js +6 -6
  27. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
  28. package/dist/lib/store/index.d.ts +1 -2
  29. package/dist/lib/store/index.js +1 -3
  30. package/dist/lib/store/index.js.map +1 -1
  31. package/dist/lib/wait_for_remote_peer.js +3 -3
  32. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  33. package/dist/lib/waku.d.ts +3 -4
  34. package/dist/lib/waku.js.map +1 -1
  35. package/package.json +9 -17
  36. package/src/index.ts +2 -5
  37. package/src/lib/base_protocol.ts +23 -9
  38. package/src/lib/connection_manager.ts +165 -65
  39. package/src/lib/filter/{v2/index.ts → index.ts} +19 -26
  40. package/src/lib/keep_alive_manager.ts +3 -3
  41. package/src/lib/light_push/index.ts +3 -3
  42. package/src/lib/predefined_bootstrap_nodes.ts +6 -6
  43. package/src/lib/store/index.ts +3 -3
  44. package/src/lib/wait_for_remote_peer.ts +7 -9
  45. package/src/lib/waku.ts +3 -4
  46. package/dist/lib/filter/v1/filter_rpc.d.ts +0 -23
  47. package/dist/lib/filter/v1/filter_rpc.js +0 -45
  48. package/dist/lib/filter/v1/filter_rpc.js.map +0 -1
  49. package/dist/lib/filter/v1/index.d.ts +0 -6
  50. package/dist/lib/filter/v1/index.js +0 -153
  51. package/dist/lib/filter/v1/index.js.map +0 -1
  52. package/dist/lib/filter/v2/filter_rpc.js.map +0 -1
  53. package/dist/lib/filter/v2/index.d.ts +0 -3
  54. package/dist/lib/filter/v2/index.js.map +0 -1
  55. package/src/lib/filter/v1/filter_rpc.ts +0 -53
  56. package/src/lib/filter/v1/index.ts +0 -248
  57. /package/dist/lib/filter/{v2/filter_rpc.d.ts → filter_rpc.d.ts} +0 -0
  58. /package/dist/lib/filter/{v2/filter_rpc.js → filter_rpc.js} +0 -0
  59. /package/src/lib/filter/{v2/filter_rpc.ts → filter_rpc.ts} +0 -0
@@ -1,9 +1,15 @@
1
- import type { Connection } from "@libp2p/interface-connection";
2
- import type { Libp2p } from "@libp2p/interface-libp2p";
3
1
  import type { PeerId } from "@libp2p/interface-peer-id";
4
2
  import type { PeerInfo } from "@libp2p/interface-peer-info";
5
- import type { ConnectionManagerOptions, IRelay } from "@waku/interfaces";
6
- import { Tags } from "@waku/interfaces";
3
+ import type { Peer } from "@libp2p/interface-peer-store";
4
+ import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
5
+ import {
6
+ ConnectionManagerOptions,
7
+ EPeersByDiscoveryEvents,
8
+ IPeersByDiscoveryEvents,
9
+ IRelay,
10
+ PeersByDiscoveryResult,
11
+ } from "@waku/interfaces";
12
+ import { Libp2p, Tags } from "@waku/interfaces";
7
13
  import debug from "debug";
8
14
 
9
15
  import { KeepAliveManager, KeepAliveOptions } from "./keep_alive_manager.js";
@@ -14,11 +20,11 @@ export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
14
20
  export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3;
15
21
  export const DEFAULT_MAX_PARALLEL_DIALS = 3;
16
22
 
17
- export class ConnectionManager {
23
+ export class ConnectionManager extends EventEmitter<IPeersByDiscoveryEvents> {
18
24
  private static instances = new Map<string, ConnectionManager>();
19
25
  private keepAliveManager: KeepAliveManager;
20
26
  private options: ConnectionManagerOptions;
21
- private libp2pComponents: Libp2p;
27
+ private libp2p: Libp2p;
22
28
  private dialAttemptsForPeer: Map<string, number> = new Map();
23
29
  private dialErrorsForPeer: Map<string, any> = new Map();
24
30
 
@@ -46,13 +52,58 @@ export class ConnectionManager {
46
52
  return instance;
47
53
  }
48
54
 
55
+ public async getPeersByDiscovery(): Promise<PeersByDiscoveryResult> {
56
+ const peersDiscovered = await this.libp2p.peerStore.all();
57
+ const peersConnected = this.libp2p
58
+ .getConnections()
59
+ .map((conn) => conn.remotePeer);
60
+
61
+ const peersDiscoveredByBootstrap: Peer[] = [];
62
+ const peersDiscoveredByPeerExchange: Peer[] = [];
63
+ const peersConnectedByBootstrap: Peer[] = [];
64
+ const peersConnectedByPeerExchange: Peer[] = [];
65
+
66
+ for (const peer of peersDiscovered) {
67
+ const tags = await this.getTagNamesForPeer(peer.id);
68
+
69
+ if (tags.includes(Tags.BOOTSTRAP)) {
70
+ peersDiscoveredByBootstrap.push(peer);
71
+ } else if (tags.includes(Tags.PEER_EXCHANGE)) {
72
+ peersDiscoveredByPeerExchange.push(peer);
73
+ }
74
+ }
75
+
76
+ for (const peerId of peersConnected) {
77
+ const peer = await this.libp2p.peerStore.get(peerId);
78
+ const tags = await this.getTagNamesForPeer(peerId);
79
+
80
+ if (tags.includes(Tags.BOOTSTRAP)) {
81
+ peersConnectedByBootstrap.push(peer);
82
+ } else if (tags.includes(Tags.PEER_EXCHANGE)) {
83
+ peersConnectedByPeerExchange.push(peer);
84
+ }
85
+ }
86
+
87
+ return {
88
+ DISCOVERED: {
89
+ [Tags.BOOTSTRAP]: peersDiscoveredByBootstrap,
90
+ [Tags.PEER_EXCHANGE]: peersDiscoveredByPeerExchange,
91
+ },
92
+ CONNECTED: {
93
+ [Tags.BOOTSTRAP]: peersConnectedByBootstrap,
94
+ [Tags.PEER_EXCHANGE]: peersConnectedByPeerExchange,
95
+ },
96
+ };
97
+ }
98
+
49
99
  private constructor(
50
- libp2pComponents: Libp2p,
100
+ libp2p: Libp2p,
51
101
  keepAliveOptions: KeepAliveOptions,
52
102
  relay?: IRelay,
53
103
  options?: Partial<ConnectionManagerOptions>
54
104
  ) {
55
- this.libp2pComponents = libp2pComponents;
105
+ super();
106
+ this.libp2p = libp2p;
56
107
  this.options = {
57
108
  maxDialAttemptsForPeer: DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER,
58
109
  maxBootstrapPeersAllowed: DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED,
@@ -69,17 +120,17 @@ export class ConnectionManager {
69
120
  // libp2p emits `peer:discovery` events during its initialization
70
121
  // which means that before the ConnectionManager is initialized, some peers may have been discovered
71
122
  // we will dial the peers in peerStore ONCE before we start to listen to the `peer:discovery` events within the ConnectionManager
72
- this.dialPeerStorePeers();
123
+ this.dialPeerStorePeers().catch((error) =>
124
+ log(`Unexpected error while dialing peer store peers`, error)
125
+ );
73
126
  }
74
127
 
75
128
  private async dialPeerStorePeers(): Promise<void> {
76
- const peerInfos = await this.libp2pComponents.peerStore.all();
129
+ const peerInfos = await this.libp2p.peerStore.all();
77
130
  const dialPromises = [];
78
131
  for (const peerInfo of peerInfos) {
79
132
  if (
80
- this.libp2pComponents
81
- .getConnections()
82
- .find((c) => c.remotePeer === peerInfo.id)
133
+ this.libp2p.getConnections().find((c) => c.remotePeer === peerInfo.id)
83
134
  )
84
135
  continue;
85
136
 
@@ -101,15 +152,15 @@ export class ConnectionManager {
101
152
 
102
153
  stop(): void {
103
154
  this.keepAliveManager.stopAll();
104
- this.libp2pComponents.removeEventListener(
155
+ this.libp2p.removeEventListener(
105
156
  "peer:connect",
106
157
  this.onEventHandlers["peer:connect"]
107
158
  );
108
- this.libp2pComponents.removeEventListener(
159
+ this.libp2p.removeEventListener(
109
160
  "peer:disconnect",
110
161
  this.onEventHandlers["peer:disconnect"]
111
162
  );
112
- this.libp2pComponents.removeEventListener(
163
+ this.libp2p.removeEventListener(
113
164
  "peer:discovery",
114
165
  this.onEventHandlers["peer:discovery"]
115
166
  );
@@ -121,12 +172,12 @@ export class ConnectionManager {
121
172
  while (dialAttempt <= this.options.maxDialAttemptsForPeer) {
122
173
  try {
123
174
  log(`Dialing peer ${peerId.toString()}`);
124
- await this.libp2pComponents.dial(peerId);
175
+ await this.libp2p.dial(peerId);
125
176
 
126
177
  const tags = await this.getTagNamesForPeer(peerId);
127
178
  // add tag to connection describing discovery mechanism
128
179
  // don't add duplicate tags
129
- this.libp2pComponents
180
+ this.libp2p
130
181
  .getConnections(peerId)
131
182
  .forEach(
132
183
  (conn) => (conn.tags = Array.from(new Set([...conn.tags, ...tags])))
@@ -157,7 +208,7 @@ export class ConnectionManager {
157
208
  }`
158
209
  );
159
210
  this.dialErrorsForPeer.delete(peerId.toString());
160
- return await this.libp2pComponents.peerStore.delete(peerId);
211
+ return await this.libp2p.peerStore.delete(peerId);
161
212
  } catch (error) {
162
213
  throw `Error deleting undialable peer ${peerId.toString()} from peer store - ${error}`;
163
214
  } finally {
@@ -168,7 +219,8 @@ export class ConnectionManager {
168
219
 
169
220
  async dropConnection(peerId: PeerId): Promise<void> {
170
221
  try {
171
- await this.libp2pComponents.hangUp(peerId);
222
+ this.keepAliveManager.stop(peerId);
223
+ await this.libp2p.hangUp(peerId);
172
224
  log(`Dropped connection with peer ${peerId.toString()}`);
173
225
  } catch (error) {
174
226
  log(
@@ -177,7 +229,7 @@ export class ConnectionManager {
177
229
  }
178
230
  }
179
231
 
180
- private async processDialQueue(): Promise<void> {
232
+ private processDialQueue(): void {
181
233
  if (
182
234
  this.pendingPeerDialQueue.length > 0 &&
183
235
  this.currentActiveDialCount < this.options.maxParallelDials
@@ -191,14 +243,14 @@ export class ConnectionManager {
191
243
  }
192
244
 
193
245
  private startPeerDiscoveryListener(): void {
194
- this.libp2pComponents.peerStore.addEventListener(
195
- "peer",
246
+ this.libp2p.addEventListener(
247
+ "peer:discovery",
196
248
  this.onEventHandlers["peer:discovery"]
197
249
  );
198
250
  }
199
251
 
200
252
  private startPeerConnectionListener(): void {
201
- this.libp2pComponents.addEventListener(
253
+ this.libp2p.addEventListener(
202
254
  "peer:connect",
203
255
  this.onEventHandlers["peer:connect"]
204
256
  );
@@ -217,7 +269,7 @@ export class ConnectionManager {
217
269
  * >this event will **only** be triggered when the last connection is closed.
218
270
  * @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
219
271
  */
220
- this.libp2pComponents.addEventListener(
272
+ this.libp2p.addEventListener(
221
273
  "peer:disconnect",
222
274
  this.onEventHandlers["peer:disconnect"]
223
275
  );
@@ -237,41 +289,86 @@ export class ConnectionManager {
237
289
  }
238
290
 
239
291
  private onEventHandlers = {
240
- "peer:discovery": async (evt: CustomEvent<PeerInfo>): Promise<void> => {
241
- const { id: peerId } = evt.detail;
292
+ "peer:discovery": (evt: CustomEvent<PeerInfo>): void => {
293
+ void (async () => {
294
+ const { id: peerId } = evt.detail;
295
+
296
+ const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
297
+ Tags.BOOTSTRAP
298
+ );
299
+
300
+ if (isBootstrap) {
301
+ this.dispatchEvent(
302
+ new CustomEvent<PeerId>(
303
+ EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP,
304
+ {
305
+ detail: peerId,
306
+ }
307
+ )
308
+ );
309
+ } else {
310
+ this.dispatchEvent(
311
+ new CustomEvent<PeerId>(
312
+ EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE,
313
+ {
314
+ detail: peerId,
315
+ }
316
+ )
317
+ );
318
+ }
242
319
 
243
- this.attemptDial(peerId).catch((err) =>
244
- log(`Error dialing peer ${peerId.toString()} : ${err}`)
245
- );
320
+ try {
321
+ await this.attemptDial(peerId);
322
+ } catch (error) {
323
+ log(`Error dialing peer ${peerId.toString()} : ${error}`);
324
+ }
325
+ })();
246
326
  },
247
- "peer:connect": async (evt: CustomEvent<Connection>): Promise<void> => {
248
- const { remotePeer: peerId } = evt.detail;
249
-
250
- this.keepAliveManager.start(
251
- peerId,
252
- this.libp2pComponents.ping.bind(this)
253
- );
254
-
255
- const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
256
- Tags.BOOTSTRAP
257
- );
258
-
259
- if (isBootstrap) {
260
- const bootstrapConnections = this.libp2pComponents
261
- .getConnections()
262
- .filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
263
-
264
- // If we have too many bootstrap connections, drop one
265
- if (
266
- bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
267
- ) {
268
- await this.dropConnection(peerId);
327
+ "peer:connect": (evt: CustomEvent<PeerId>): void => {
328
+ void (async () => {
329
+ const peerId = evt.detail;
330
+
331
+ this.keepAliveManager.start(peerId, this.libp2p.services.ping);
332
+
333
+ const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
334
+ Tags.BOOTSTRAP
335
+ );
336
+
337
+ if (isBootstrap) {
338
+ const bootstrapConnections = this.libp2p
339
+ .getConnections()
340
+ .filter((conn) => conn.tags.includes(Tags.BOOTSTRAP));
341
+
342
+ // If we have too many bootstrap connections, drop one
343
+ if (
344
+ bootstrapConnections.length > this.options.maxBootstrapPeersAllowed
345
+ ) {
346
+ await this.dropConnection(peerId);
347
+ } else {
348
+ this.dispatchEvent(
349
+ new CustomEvent<PeerId>(
350
+ EPeersByDiscoveryEvents.PEER_CONNECT_BOOTSTRAP,
351
+ {
352
+ detail: peerId,
353
+ }
354
+ )
355
+ );
356
+ }
357
+ } else {
358
+ this.dispatchEvent(
359
+ new CustomEvent<PeerId>(
360
+ EPeersByDiscoveryEvents.PEER_CONNECT_PEER_EXCHANGE,
361
+ {
362
+ detail: peerId,
363
+ }
364
+ )
365
+ );
269
366
  }
270
- }
367
+ })();
271
368
  },
272
369
  "peer:disconnect": () => {
273
- return (evt: CustomEvent<Connection>): void => {
274
- this.keepAliveManager.stop(evt.detail.remotePeer);
370
+ return (evt: CustomEvent<PeerId>): void => {
371
+ this.keepAliveManager.stop(evt.detail);
275
372
  };
276
373
  },
277
374
  };
@@ -282,19 +379,19 @@ export class ConnectionManager {
282
379
  * 2. If the peer is not a bootstrap peer
283
380
  */
284
381
  private async shouldDialPeer(peerId: PeerId): Promise<boolean> {
285
- const isConnected = this.libp2pComponents.getConnections(peerId).length > 0;
382
+ const isConnected = this.libp2p.getConnections(peerId).length > 0;
286
383
 
287
384
  if (isConnected) return false;
288
385
 
289
- const isBootstrap = (await this.getTagNamesForPeer(peerId)).some(
290
- (tagName) => tagName === Tags.BOOTSTRAP
291
- );
386
+ const tagNames = await this.getTagNamesForPeer(peerId);
387
+
388
+ const isBootstrap = tagNames.some((tagName) => tagName === Tags.BOOTSTRAP);
292
389
 
293
390
  if (isBootstrap) {
294
- const currentBootstrapConnections = this.libp2pComponents
391
+ const currentBootstrapConnections = this.libp2p
295
392
  .getConnections()
296
393
  .filter((conn) => {
297
- conn.tags.find((name) => name === Tags.BOOTSTRAP);
394
+ return conn.tags.find((name) => name === Tags.BOOTSTRAP);
298
395
  }).length;
299
396
  if (currentBootstrapConnections < this.options.maxBootstrapPeersAllowed)
300
397
  return true;
@@ -309,9 +406,12 @@ export class ConnectionManager {
309
406
  * Fetches the tag names for a given peer
310
407
  */
311
408
  private async getTagNamesForPeer(peerId: PeerId): Promise<string[]> {
312
- const tags = (await this.libp2pComponents.peerStore.getTags(peerId)).map(
313
- (tag) => tag.name
314
- );
315
- return tags;
409
+ try {
410
+ const peer = await this.libp2p.peerStore.get(peerId);
411
+ return Array.from(peer.tags.keys());
412
+ } catch (error) {
413
+ log(`Failed to get peer ${peerId}, error: ${error}`);
414
+ return [];
415
+ }
316
416
  }
317
417
  }
@@ -1,5 +1,4 @@
1
1
  import { Stream } from "@libp2p/interface-connection";
2
- import type { Libp2p } from "@libp2p/interface-libp2p";
3
2
  import type { PeerId } from "@libp2p/interface-peer-id";
4
3
  import type { Peer } from "@libp2p/interface-peer-store";
5
4
  import type { IncomingStreamData } from "@libp2p/interface-registrar";
@@ -9,9 +8,10 @@ import type {
9
8
  IAsyncIterator,
10
9
  IDecodedMessage,
11
10
  IDecoder,
12
- IFilterV2,
11
+ IFilter,
13
12
  IProtoMessage,
14
13
  IReceiver,
14
+ Libp2p,
15
15
  PeerIdStr,
16
16
  ProtocolCreateOptions,
17
17
  ProtocolOptions,
@@ -25,8 +25,8 @@ import all from "it-all";
25
25
  import * as lp from "it-length-prefixed";
26
26
  import { pipe } from "it-pipe";
27
27
 
28
- import { BaseProtocol } from "../../base_protocol.js";
29
- import { DefaultPubSubTopic } from "../../constants.js";
28
+ import { BaseProtocol } from "../base_protocol.js";
29
+ import { DefaultPubSubTopic } from "../constants.js";
30
30
 
31
31
  import {
32
32
  FilterPushRpc,
@@ -41,7 +41,7 @@ type SubscriptionCallback<T extends IDecodedMessage> = {
41
41
  callback: Callback<T>;
42
42
  };
43
43
 
44
- const FilterV2Codecs = {
44
+ const FilterCodecs = {
45
45
  SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
46
46
  PUSH: "/vac/waku/filter-push/2.0.0-beta1",
47
47
  };
@@ -225,7 +225,7 @@ class Subscription {
225
225
  }
226
226
  }
227
227
 
228
- class FilterV2 extends BaseProtocol implements IReceiver {
228
+ class Filter extends BaseProtocol implements IReceiver {
229
229
  private readonly options: ProtocolCreateOptions;
230
230
  private activeSubscriptions = new Map<string, Subscription>();
231
231
 
@@ -245,18 +245,12 @@ class FilterV2 extends BaseProtocol implements IReceiver {
245
245
  return subscription;
246
246
  }
247
247
 
248
- constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
249
- super(
250
- FilterV2Codecs.SUBSCRIBE,
251
- libp2p.peerStore,
252
- libp2p.getConnections.bind(libp2p)
253
- );
248
+ constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
249
+ super(FilterCodecs.SUBSCRIBE, libp2p.components);
254
250
 
255
- this.libp2p
256
- .handle(FilterV2Codecs.PUSH, this.onRequest.bind(this))
257
- .catch((e) => {
258
- log("Failed to register ", FilterV2Codecs.PUSH, e);
259
- });
251
+ libp2p.handle(FilterCodecs.PUSH, this.onRequest.bind(this)).catch((e) => {
252
+ log("Failed to register ", FilterCodecs.PUSH, e);
253
+ });
260
254
 
261
255
  this.activeSubscriptions = new Map();
262
256
 
@@ -312,7 +306,7 @@ class FilterV2 extends BaseProtocol implements IReceiver {
312
306
  ): Promise<Unsubscribe> {
313
307
  const subscription = await this.createSubscription(undefined, opts?.peerId);
314
308
 
315
- subscription.subscribe(decoders, callback);
309
+ await subscription.subscribe(decoders, callback);
316
310
 
317
311
  const contentTopics = Array.from(
318
312
  groupByContentTopic(
@@ -371,10 +365,10 @@ class FilterV2 extends BaseProtocol implements IReceiver {
371
365
  }
372
366
  }
373
367
 
374
- export function wakuFilterV2(
368
+ export function wakuFilter(
375
369
  init: Partial<ProtocolCreateOptions> = {}
376
- ): (libp2p: Libp2p) => IFilterV2 {
377
- return (libp2p: Libp2p) => new FilterV2(libp2p, init);
370
+ ): (libp2p: Libp2p) => IFilter {
371
+ return (libp2p: Libp2p) => new Filter(libp2p, init);
378
372
  }
379
373
 
380
374
  async function pushMessage<T extends IDecodedMessage>(
@@ -394,20 +388,19 @@ async function pushMessage<T extends IDecodedMessage>(
394
388
  // We don't want to wait for decoding failure, just attempt to decode
395
389
  // all messages and do the call back on the one that works
396
390
  // noinspection ES6MissingAwait
397
- decoders.forEach(async (dec: IDecoder<T>) => {
398
- if (didDecodeMsg) return;
391
+ for (const dec of decoders) {
392
+ if (didDecodeMsg) break;
399
393
  const decoded = await dec.fromProtoObj(
400
394
  pubSubTopic,
401
395
  message as IProtoMessage
402
396
  );
403
- // const decoded = await dec.fromProtoObj(pubSubTopic, message);
404
397
  if (!decoded) {
405
398
  log("Not able to decode message");
406
- return;
399
+ continue;
407
400
  }
408
401
  // This is just to prevent more decoding attempt
409
402
  // TODO: Could be better if we were to abort promises
410
403
  didDecodeMsg = Boolean(decoded);
411
404
  await callback(decoded);
412
- });
405
+ }
413
406
  }
@@ -1,7 +1,7 @@
1
1
  import type { PeerId } from "@libp2p/interface-peer-id";
2
2
  import type { IRelay } from "@waku/interfaces";
3
3
  import debug from "debug";
4
- import type { Libp2p } from "libp2p";
4
+ import type { PingService } from "libp2p/ping";
5
5
 
6
6
  import { createEncoder } from "../index.js";
7
7
 
@@ -26,7 +26,7 @@ export class KeepAliveManager {
26
26
  this.relay = relay;
27
27
  }
28
28
 
29
- public start(peerId: PeerId, libp2pPing: Libp2p["ping"]): void {
29
+ public start(peerId: PeerId, libp2pPing: PingService): void {
30
30
  // Just in case a timer already exist for this peer
31
31
  this.stop(peerId);
32
32
 
@@ -37,7 +37,7 @@ export class KeepAliveManager {
37
37
 
38
38
  if (pingPeriodSecs !== 0) {
39
39
  const interval = setInterval(() => {
40
- libp2pPing(peerId).catch((e) => {
40
+ libp2pPing.ping(peerId).catch((e) => {
41
41
  log(`Ping failed (${peerIdStr})`, e);
42
42
  });
43
43
  }, pingPeriodSecs * 1000);
@@ -1,9 +1,9 @@
1
- import type { Libp2p } from "@libp2p/interface-libp2p";
2
1
  import type { PeerId } from "@libp2p/interface-peer-id";
3
2
  import {
4
3
  IEncoder,
5
4
  ILightPush,
6
5
  IMessage,
6
+ Libp2p,
7
7
  ProtocolCreateOptions,
8
8
  ProtocolOptions,
9
9
  SendError,
@@ -33,8 +33,8 @@ export { PushResponse };
33
33
  class LightPush extends BaseProtocol implements ILightPush {
34
34
  options: ProtocolCreateOptions;
35
35
 
36
- constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
37
- super(LightPushCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
36
+ constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
37
+ super(LightPushCodec, libp2p.components);
38
38
  this.options = options || {};
39
39
  }
40
40
 
@@ -47,21 +47,21 @@ export const fleets = {
47
47
  "wakuv2.prod": {
48
48
  "waku-websocket": {
49
49
  "node-01.ac-cn-hongkong-c.wakuv2.prod":
50
- "/dns4/node-01.ac-cn-hongkong-c.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD",
50
+ "/dns4/node-01.ac-cn-hongkong-c.wakuv2.prod.statusim.net/tcp/8000/wss/p2p/16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD",
51
51
  "node-01.do-ams3.wakuv2.prod":
52
- "/dns4/node-01.do-ams3.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmL5okWopX7NqZWBUKVqW8iUxCEmd5GMHLVPwCgzYzQv3e",
52
+ "/dns4/node-01.do-ams3.wakuv2.prod.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmL5okWopX7NqZWBUKVqW8iUxCEmd5GMHLVPwCgzYzQv3e",
53
53
  "node-01.gc-us-central1-a.wakuv2.prod":
54
- "/dns4/node-01.gc-us-central1-a.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmVkKntsECaYfefR1V2yCR79CegLATuTPE6B9TxgxBiiiA",
54
+ "/dns4/node-01.gc-us-central1-a.wakuv2.prod.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmVkKntsECaYfefR1V2yCR79CegLATuTPE6B9TxgxBiiiA",
55
55
  },
56
56
  },
57
57
  "wakuv2.test": {
58
58
  "waku-websocket": {
59
59
  "node-01.ac-cn-hongkong-c.wakuv2.test":
60
- "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm",
60
+ "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm",
61
61
  "node-01.do-ams3.wakuv2.test":
62
- "/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ",
62
+ "/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ",
63
63
  "node-01.gc-us-central1-a.wakuv2.test":
64
- "/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS",
64
+ "/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS",
65
65
  },
66
66
  },
67
67
  },
@@ -1,5 +1,4 @@
1
1
  import type { Stream } from "@libp2p/interface-connection";
2
- import type { Libp2p } from "@libp2p/interface-libp2p";
3
2
  import type { PeerId } from "@libp2p/interface-peer-id";
4
3
  import { sha256 } from "@noble/hashes/sha256";
5
4
  import {
@@ -7,6 +6,7 @@ import {
7
6
  IDecodedMessage,
8
7
  IDecoder,
9
8
  IStore,
9
+ Libp2p,
10
10
  ProtocolCreateOptions,
11
11
  } from "@waku/interfaces";
12
12
  import { proto_store as proto } from "@waku/proto";
@@ -81,8 +81,8 @@ export interface QueryOptions {
81
81
  class Store extends BaseProtocol implements IStore {
82
82
  options: ProtocolCreateOptions;
83
83
 
84
- constructor(public libp2p: Libp2p, options?: ProtocolCreateOptions) {
85
- super(StoreCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
84
+ constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
85
+ super(StoreCodec, libp2p.components);
86
86
  this.options = options ?? {};
87
87
  }
88
88
 
@@ -1,5 +1,5 @@
1
- import type { PeerProtocolsChangeData } from "@libp2p/interface-peer-store";
2
- import type { IRelay, PointToPointProtocol, Waku } from "@waku/interfaces";
1
+ import type { IdentifyResult } from "@libp2p/interface-libp2p";
2
+ import type { IBaseProtocol, IRelay, Waku } from "@waku/interfaces";
3
3
  import { Protocols } from "@waku/interfaces";
4
4
  import debug from "debug";
5
5
  import { pEvent } from "p-event";
@@ -74,9 +74,7 @@ export async function waitForRemotePeer(
74
74
  /**
75
75
  * Wait for a peer with the given protocol to be connected.
76
76
  */
77
- async function waitForConnectedPeer(
78
- protocol: PointToPointProtocol
79
- ): Promise<void> {
77
+ async function waitForConnectedPeer(protocol: IBaseProtocol): Promise<void> {
80
78
  const codec = protocol.multicodec;
81
79
  const peers = await protocol.peers();
82
80
 
@@ -86,14 +84,14 @@ async function waitForConnectedPeer(
86
84
  }
87
85
 
88
86
  await new Promise<void>((resolve) => {
89
- const cb = (evt: CustomEvent<PeerProtocolsChangeData>): void => {
90
- if (evt.detail.protocols.includes(codec)) {
87
+ const cb = (evt: CustomEvent<IdentifyResult>): void => {
88
+ if (evt.detail?.protocols?.includes(codec)) {
91
89
  log("Resolving for", codec, evt.detail.protocols);
92
- protocol.peerStore.removeEventListener("change:protocols", cb);
90
+ protocol.removeLibp2pEventListener("peer:identify", cb);
93
91
  resolve();
94
92
  }
95
93
  };
96
- protocol.peerStore.addEventListener("change:protocols", cb);
94
+ protocol.addLibp2pEventListener("peer:identify", cb);
97
95
  });
98
96
  }
99
97
 
package/src/lib/waku.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import type { Stream } from "@libp2p/interface-connection";
2
- import type { Libp2p } from "@libp2p/interface-libp2p";
3
2
  import { isPeerId, PeerId } from "@libp2p/interface-peer-id";
4
3
  import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
5
4
  import type {
6
5
  IFilter,
7
- IFilterV2,
8
6
  ILightPush,
9
7
  IRelay,
10
8
  IStore,
9
+ Libp2p,
11
10
  Waku,
12
11
  } from "@waku/interfaces";
13
12
  import { Protocols } from "@waku/interfaces";
@@ -47,7 +46,7 @@ export class WakuNode implements Waku {
47
46
  public libp2p: Libp2p;
48
47
  public relay?: IRelay;
49
48
  public store?: IStore;
50
- public filter?: IFilter | IFilterV2;
49
+ public filter?: IFilter;
51
50
  public lightPush?: ILightPush;
52
51
  public connectionManager: ConnectionManager;
53
52
 
@@ -56,7 +55,7 @@ export class WakuNode implements Waku {
56
55
  libp2p: Libp2p,
57
56
  store?: (libp2p: Libp2p) => IStore,
58
57
  lightPush?: (libp2p: Libp2p) => ILightPush,
59
- filter?: (libp2p: Libp2p) => IFilter | IFilterV2,
58
+ filter?: (libp2p: Libp2p) => IFilter,
60
59
  relay?: (libp2p: Libp2p) => IRelay
61
60
  ) {
62
61
  this.libp2p = libp2p;
@@ -1,23 +0,0 @@
1
- import { ContentFilter } from "@waku/interfaces";
2
- import { proto_filter as proto } from "@waku/proto";
3
- /**
4
- * FilterRPC represents a message conforming to the Waku Filter protocol
5
- */
6
- export declare class FilterRpc {
7
- proto: proto.FilterRpc;
8
- constructor(proto: proto.FilterRpc);
9
- static createRequest(topic: string, contentFilters: ContentFilter[], requestId?: string, subscribe?: boolean): FilterRpc;
10
- /**
11
- *
12
- * @param bytes Uint8Array of bytes from a FilterRPC message
13
- * @returns FilterRpc
14
- */
15
- static decode(bytes: Uint8Array): FilterRpc;
16
- /**
17
- * Encode the current FilterRPC request to bytes
18
- * @returns Uint8Array
19
- */
20
- encode(): Uint8Array;
21
- get push(): proto.MessagePush | undefined;
22
- get requestId(): string;
23
- }