@waku/core 0.0.14 → 0.0.16
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 +31 -1
- package/bundle/index.js +108 -66
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/filter/index.js +18 -3
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/relay/index.d.ts +4 -2
- package/dist/lib/relay/index.js +84 -38
- package/dist/lib/relay/index.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +1 -1
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/dist/lib/waku.d.ts +1 -1
- package/dist/lib/waku.js +4 -16
- package/dist/lib/waku.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +5 -1
- package/src/lib/filter/index.ts +26 -4
- package/src/lib/relay/index.ts +124 -47
- package/src/lib/wait_for_remote_peer.ts +1 -1
- package/src/lib/waku.ts +7 -19
package/CHANGELOG.md
CHANGED
@@ -5,8 +5,38 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The file is maintained by [Release Please](https://github.com/googleapis/release-please) based on [Conventional Commits](https://www.conventionalcommits.org) specification,
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
-
## [0.0.
|
8
|
+
## [0.0.16](https://github.com/waku-org/js-waku/compare/core-v0.0.15...core-v0.0.16) (2023-04-03)
|
9
|
+
|
10
|
+
|
11
|
+
### ⚠ BREAKING CHANGES
|
12
|
+
|
13
|
+
* add and implement IReceiver ([#1219](https://github.com/waku-org/js-waku/issues/1219))
|
14
|
+
|
15
|
+
### Features
|
16
|
+
|
17
|
+
* Add and implement IReceiver ([#1219](https://github.com/waku-org/js-waku/issues/1219)) ([e11e5b4](https://github.com/waku-org/js-waku/commit/e11e5b4870aede7813b3ee4b60f5e625f6eac5a2))
|
18
|
+
|
9
19
|
|
20
|
+
### Dependencies
|
21
|
+
|
22
|
+
* The following workspace dependencies were updated
|
23
|
+
* dependencies
|
24
|
+
* @waku/interfaces bumped from 0.0.10 to 0.0.11
|
25
|
+
* @waku/utils bumped from 0.0.3 to 0.0.4
|
26
|
+
|
27
|
+
## [0.0.15](https://github.com/waku-org/js-waku/compare/core-v0.0.14...core-v0.0.15) (2023-03-31)
|
28
|
+
|
29
|
+
|
30
|
+
### Bug Fixes
|
31
|
+
|
32
|
+
* Update @waku/core changelog ([#1287](https://github.com/waku-org/js-waku/issues/1287)) ([5def8dd](https://github.com/waku-org/js-waku/commit/5def8ddb4d9a43424bf2124a521d618368ffa9dc))
|
33
|
+
|
34
|
+
## [0.0.14](https://github.com/waku-org/js-waku/compare/core-v0.0.12...core-v0.0.13) (2023-03-29)
|
35
|
+
|
36
|
+
### Features
|
37
|
+
Nothing. Was published by mistake and does not contain valid code. Same as 0.0.13
|
38
|
+
|
39
|
+
## [0.0.13](https://github.com/waku-org/js-waku/compare/core-v0.0.12...core-v0.0.13) (2023-03-24)
|
10
40
|
|
11
41
|
### Bug Fixes
|
12
42
|
|
package/bundle/index.js
CHANGED
@@ -243,7 +243,7 @@ const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
|
243
243
|
const DefaultUserAgent = "js-waku";
|
244
244
|
const log$7 = debug("waku:waku");
|
245
245
|
class WakuNode {
|
246
|
-
constructor(options, libp2p, store, lightPush, filter) {
|
246
|
+
constructor(options, libp2p, store, lightPush, filter, relay) {
|
247
247
|
this.libp2p = libp2p;
|
248
248
|
if (store) {
|
249
249
|
this.store = store(libp2p);
|
@@ -254,8 +254,8 @@ class WakuNode {
|
|
254
254
|
if (lightPush) {
|
255
255
|
this.lightPush = lightPush(libp2p);
|
256
256
|
}
|
257
|
-
if (
|
258
|
-
this.relay = libp2p
|
257
|
+
if (relay) {
|
258
|
+
this.relay = relay(libp2p);
|
259
259
|
}
|
260
260
|
const pingKeepAlive = options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
261
261
|
const relayKeepAlive = this.relay
|
@@ -283,7 +283,7 @@ class WakuNode {
|
|
283
283
|
const codecs = [];
|
284
284
|
if (_protocols.includes(Protocols.Relay)) {
|
285
285
|
if (this.relay) {
|
286
|
-
this.relay.multicodecs.forEach((codec) => codecs.push(codec));
|
286
|
+
this.relay.gossipSub.multicodecs.forEach((codec) => codecs.push(codec));
|
287
287
|
}
|
288
288
|
else {
|
289
289
|
log$7("Relay codec not included in dial codec: protocol not mounted locally");
|
@@ -341,17 +341,6 @@ class WakuNode {
|
|
341
341
|
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
|
342
342
|
}
|
343
343
|
}
|
344
|
-
function isRelay(pubsub) {
|
345
|
-
if (pubsub) {
|
346
|
-
try {
|
347
|
-
return pubsub.multicodecs.includes(RelayCodecs[RelayCodecs.length - 1]);
|
348
|
-
// Exception is expected if `libp2p` was not instantiated with pubsub
|
349
|
-
// eslint-disable-next-line no-empty
|
350
|
-
}
|
351
|
-
catch (e) { }
|
352
|
-
}
|
353
|
-
return false;
|
354
|
-
}
|
355
344
|
|
356
345
|
var waku = /*#__PURE__*/Object.freeze({
|
357
346
|
__proto__: null,
|
@@ -1919,14 +1908,15 @@ class Filter extends BaseProtocol {
|
|
1919
1908
|
.catch((e) => log$6("Failed to register filter protocol", e));
|
1920
1909
|
}
|
1921
1910
|
/**
|
1922
|
-
* @param decoders
|
1911
|
+
* @param decoders Decoder or array of Decoders to use to decode messages, it also specifies the content topics.
|
1923
1912
|
* @param callback A function that will be called on each message returned by the filter.
|
1924
1913
|
* @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to.
|
1925
1914
|
* @returns Unsubscribe function that can be used to end the subscription.
|
1926
1915
|
*/
|
1927
1916
|
async subscribe(decoders, callback, opts) {
|
1917
|
+
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
|
1928
1918
|
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
1929
|
-
const contentTopics = Array.from(groupByContentTopic(
|
1919
|
+
const contentTopics = Array.from(groupByContentTopic(decodersArray).keys());
|
1930
1920
|
const contentFilters = contentTopics.map((contentTopic) => ({
|
1931
1921
|
contentTopic,
|
1932
1922
|
}));
|
@@ -1942,13 +1932,27 @@ class Filter extends BaseProtocol {
|
|
1942
1932
|
log$6("Error subscribing to peer ", peer.id.toString(), "for content topics", contentTopics, ": ", e);
|
1943
1933
|
throw e;
|
1944
1934
|
}
|
1945
|
-
const subscription = {
|
1935
|
+
const subscription = {
|
1936
|
+
callback,
|
1937
|
+
decoders: decodersArray,
|
1938
|
+
pubSubTopic,
|
1939
|
+
};
|
1946
1940
|
this.subscriptions.set(requestId, subscription);
|
1947
1941
|
return async () => {
|
1948
1942
|
await this.unsubscribe(pubSubTopic, contentFilters, requestId, peer);
|
1949
1943
|
this.subscriptions.delete(requestId);
|
1950
1944
|
};
|
1951
1945
|
}
|
1946
|
+
getActiveSubscriptions() {
|
1947
|
+
const map = new Map();
|
1948
|
+
const subscriptions = this.subscriptions;
|
1949
|
+
for (const item of subscriptions.values()) {
|
1950
|
+
const values = map.get(item.pubSubTopic) || [];
|
1951
|
+
const nextValues = item.decoders.map((decoder) => decoder.contentTopic);
|
1952
|
+
map.set(item.pubSubTopic, [...values, ...nextValues]);
|
1953
|
+
}
|
1954
|
+
return map;
|
1955
|
+
}
|
1952
1956
|
onRequest(streamData) {
|
1953
1957
|
log$6("Receiving message push");
|
1954
1958
|
try {
|
@@ -35412,15 +35416,6 @@ class GossipSub extends EventEmitter {
|
|
35412
35416
|
}
|
35413
35417
|
GossipSub.multicodec = GossipsubIDv11;
|
35414
35418
|
|
35415
|
-
function pushOrInitMapSet(map, key, newValue) {
|
35416
|
-
let arr = map.get(key);
|
35417
|
-
if (typeof arr === "undefined") {
|
35418
|
-
map.set(key, new Set());
|
35419
|
-
arr = map.get(key);
|
35420
|
-
}
|
35421
|
-
arr.add(newValue);
|
35422
|
-
}
|
35423
|
-
|
35424
35419
|
const log$3 = debug("waku:relay");
|
35425
35420
|
function messageValidator(peer, message$1) {
|
35426
35421
|
const startTime = performance.now();
|
@@ -35446,20 +35441,18 @@ function messageValidator(peer, message$1) {
|
|
35446
35441
|
const log$2 = debug("waku:relay");
|
35447
35442
|
/**
|
35448
35443
|
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
|
35449
|
-
*
|
35450
|
-
*
|
35451
|
-
* @implements {require('libp2p-interfaces/src/pubsub')}
|
35444
|
+
* Throws if libp2p.pubsub does not support Waku Relay
|
35452
35445
|
*/
|
35453
|
-
class Relay
|
35454
|
-
constructor(
|
35455
|
-
|
35456
|
-
|
35457
|
-
|
35458
|
-
|
35459
|
-
});
|
35460
|
-
super(components, options);
|
35461
|
-
this.multicodecs = RelayCodecs;
|
35446
|
+
class Relay {
|
35447
|
+
constructor(libp2p, options) {
|
35448
|
+
if (!this.isRelayPubSub(libp2p.pubsub)) {
|
35449
|
+
throw Error(`Failed to initialize Relay. libp2p.pubsub does not support ${Relay.multicodec}`);
|
35450
|
+
}
|
35451
|
+
this.gossipSub = libp2p.pubsub;
|
35462
35452
|
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
35453
|
+
if (this.gossipSub.isStarted()) {
|
35454
|
+
this.gossipSubSubscribe(this.pubSubTopic);
|
35455
|
+
}
|
35463
35456
|
this.observers = new Map();
|
35464
35457
|
// TODO: User might want to decide what decoder should be used (e.g. for RLN)
|
35465
35458
|
this.defaultDecoder = new TopicOnlyDecoder();
|
@@ -35472,8 +35465,11 @@ class Relay extends GossipSub {
|
|
35472
35465
|
* @returns {void}
|
35473
35466
|
*/
|
35474
35467
|
async start() {
|
35475
|
-
|
35476
|
-
|
35468
|
+
if (this.gossipSub.isStarted()) {
|
35469
|
+
throw Error("GossipSub already started.");
|
35470
|
+
}
|
35471
|
+
await this.gossipSub.start();
|
35472
|
+
this.gossipSubSubscribe(this.pubSubTopic);
|
35477
35473
|
}
|
35478
35474
|
/**
|
35479
35475
|
* Send Waku message.
|
@@ -35484,24 +35480,33 @@ class Relay extends GossipSub {
|
|
35484
35480
|
log$2("Failed to encode message, aborting publish");
|
35485
35481
|
return { recipients: [] };
|
35486
35482
|
}
|
35487
|
-
return this.publish(this.pubSubTopic, msg);
|
35483
|
+
return this.gossipSub.publish(this.pubSubTopic, msg);
|
35488
35484
|
}
|
35489
35485
|
/**
|
35490
35486
|
* Add an observer and associated Decoder to process incoming messages on a given content topic.
|
35491
35487
|
*
|
35492
35488
|
* @returns Function to delete the observer
|
35493
35489
|
*/
|
35494
|
-
|
35495
|
-
const
|
35496
|
-
|
35497
|
-
callback
|
35498
|
-
|
35499
|
-
|
35500
|
-
|
35490
|
+
subscribe(decoders, callback) {
|
35491
|
+
const contentTopicToObservers = Array.isArray(decoders)
|
35492
|
+
? toObservers(decoders, callback)
|
35493
|
+
: toObservers([decoders], callback);
|
35494
|
+
for (const contentTopic of contentTopicToObservers.keys()) {
|
35495
|
+
const currObservers = this.observers.get(contentTopic) || new Set();
|
35496
|
+
const newObservers = contentTopicToObservers.get(contentTopic) || new Set();
|
35497
|
+
this.observers.set(contentTopic, union(currObservers, newObservers));
|
35498
|
+
}
|
35501
35499
|
return () => {
|
35502
|
-
const
|
35503
|
-
|
35504
|
-
|
35500
|
+
for (const contentTopic of contentTopicToObservers.keys()) {
|
35501
|
+
const currentObservers = this.observers.get(contentTopic) || new Set();
|
35502
|
+
const observersToRemove = contentTopicToObservers.get(contentTopic) || new Set();
|
35503
|
+
const nextObservers = leftMinusJoin(currentObservers, observersToRemove);
|
35504
|
+
if (nextObservers.size) {
|
35505
|
+
this.observers.set(contentTopic, nextObservers);
|
35506
|
+
}
|
35507
|
+
else {
|
35508
|
+
this.observers.delete(contentTopic);
|
35509
|
+
}
|
35505
35510
|
}
|
35506
35511
|
};
|
35507
35512
|
}
|
@@ -35510,6 +35515,9 @@ class Relay extends GossipSub {
|
|
35510
35515
|
map.set(this.pubSubTopic, this.observers.keys());
|
35511
35516
|
return map;
|
35512
35517
|
}
|
35518
|
+
getMeshPeers(topic) {
|
35519
|
+
return this.gossipSub.getMeshPeers(topic ?? this.pubSubTopic);
|
35520
|
+
}
|
35513
35521
|
async processIncomingMessage(pubSubTopic, bytes) {
|
35514
35522
|
const topicOnlyMsg = await this.defaultDecoder.fromWireToProtoObj(bytes);
|
35515
35523
|
if (!topicOnlyMsg || !topicOnlyMsg.contentTopic) {
|
@@ -35540,32 +35548,66 @@ class Relay extends GossipSub {
|
|
35540
35548
|
*
|
35541
35549
|
* @override
|
35542
35550
|
*/
|
35543
|
-
|
35544
|
-
this.addEventListener("gossipsub:message", async (event) => {
|
35551
|
+
gossipSubSubscribe(pubSubTopic) {
|
35552
|
+
this.gossipSub.addEventListener("gossipsub:message", async (event) => {
|
35545
35553
|
if (event.detail.msg.topic !== pubSubTopic)
|
35546
35554
|
return;
|
35547
35555
|
log$2(`Message received on ${pubSubTopic}`);
|
35548
35556
|
this.processIncomingMessage(event.detail.msg.topic, event.detail.msg.data).catch((e) => log$2("Failed to process incoming message", e));
|
35549
35557
|
});
|
35550
|
-
this.topicValidators.set(pubSubTopic, messageValidator);
|
35551
|
-
|
35552
|
-
}
|
35553
|
-
unsubscribe(pubSubTopic) {
|
35554
|
-
super.unsubscribe(pubSubTopic);
|
35555
|
-
this.topicValidators.delete(pubSubTopic);
|
35558
|
+
this.gossipSub.topicValidators.set(pubSubTopic, messageValidator);
|
35559
|
+
this.gossipSub.subscribe(pubSubTopic);
|
35556
35560
|
}
|
35557
|
-
|
35558
|
-
return
|
35561
|
+
isRelayPubSub(pubsub) {
|
35562
|
+
return pubsub?.multicodecs?.includes(Relay.multicodec) || false;
|
35559
35563
|
}
|
35560
35564
|
}
|
35561
35565
|
Relay.multicodec = RelayCodecs[0];
|
35562
|
-
Relay.multicodec = RelayCodecs[RelayCodecs.length - 1];
|
35563
35566
|
function wakuRelay(init = {}) {
|
35564
|
-
return (
|
35567
|
+
return (libp2p) => new Relay(libp2p, init);
|
35568
|
+
}
|
35569
|
+
function wakuGossipSub(init = {}) {
|
35570
|
+
return (components) => {
|
35571
|
+
init = {
|
35572
|
+
...init,
|
35573
|
+
// Ensure that no signature is included nor expected in the messages.
|
35574
|
+
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
|
35575
|
+
fallbackToFloodsub: false,
|
35576
|
+
};
|
35577
|
+
const pubsub = new GossipSub(components, init);
|
35578
|
+
pubsub.multicodecs = RelayCodecs;
|
35579
|
+
return pubsub;
|
35580
|
+
};
|
35581
|
+
}
|
35582
|
+
function toObservers(decoders, callback) {
|
35583
|
+
const contentTopicToDecoders = Array.from(groupByContentTopic(decoders).entries());
|
35584
|
+
const contentTopicToObserversEntries = contentTopicToDecoders.map(([contentTopic, decoders]) => [
|
35585
|
+
contentTopic,
|
35586
|
+
new Set(decoders.map((decoder) => ({
|
35587
|
+
decoder,
|
35588
|
+
callback,
|
35589
|
+
}))),
|
35590
|
+
]);
|
35591
|
+
return new Map(contentTopicToObserversEntries);
|
35592
|
+
}
|
35593
|
+
function union(left, right) {
|
35594
|
+
for (const val of right.values()) {
|
35595
|
+
left.add(val);
|
35596
|
+
}
|
35597
|
+
return left;
|
35598
|
+
}
|
35599
|
+
function leftMinusJoin(left, right) {
|
35600
|
+
for (const val of right.values()) {
|
35601
|
+
if (left.has(val)) {
|
35602
|
+
left.delete(val);
|
35603
|
+
}
|
35604
|
+
}
|
35605
|
+
return left;
|
35565
35606
|
}
|
35566
35607
|
|
35567
35608
|
var index$1 = /*#__PURE__*/Object.freeze({
|
35568
35609
|
__proto__: null,
|
35610
|
+
wakuGossipSub: wakuGossipSub,
|
35569
35611
|
wakuRelay: wakuRelay
|
35570
35612
|
});
|
35571
35613
|
|
@@ -36488,7 +36530,7 @@ async function waitForConnectedPeer(protocol) {
|
|
36488
36530
|
async function waitForGossipSubPeerInMesh(waku) {
|
36489
36531
|
let peers = waku.getMeshPeers();
|
36490
36532
|
while (peers.length == 0) {
|
36491
|
-
await pEvent(waku, "gossipsub:heartbeat");
|
36533
|
+
await pEvent(waku.gossipSub, "gossipsub:heartbeat");
|
36492
36534
|
peers = waku.getMeshPeers();
|
36493
36535
|
}
|
36494
36536
|
}
|
@@ -36513,4 +36555,4 @@ function getEnabledProtocols(waku) {
|
|
36513
36555
|
return protocols;
|
36514
36556
|
}
|
36515
36557
|
|
36516
|
-
export { ConnectionManager, DefaultPubSubTopic, DefaultUserAgent, KeepAliveManager, LightPushCodec, PageDirection, StoreCodec, WakuNode, createCursor, createEncoder, index$4 as message, waitForRemotePeer, waku, wakuFilter, wakuLightPush, wakuRelay, wakuStore, index$3 as waku_filter, index$2 as waku_light_push, index$1 as waku_relay, index as waku_store };
|
36558
|
+
export { ConnectionManager, DefaultPubSubTopic, DefaultUserAgent, KeepAliveManager, LightPushCodec, PageDirection, StoreCodec, WakuNode, createCursor, createEncoder, index$4 as message, waitForRemotePeer, waku, wakuFilter, wakuGossipSub, wakuLightPush, wakuRelay, wakuStore, index$3 as waku_filter, index$2 as waku_light_push, index$1 as waku_relay, index as waku_store };
|
package/dist/index.d.ts
CHANGED
@@ -10,7 +10,7 @@ export { wakuFilter } from "./lib/filter/index.js";
|
|
10
10
|
export * as waku_light_push from "./lib/light_push/index.js";
|
11
11
|
export { wakuLightPush, LightPushCodec } from "./lib/light_push/index.js";
|
12
12
|
export * as waku_relay from "./lib/relay/index.js";
|
13
|
-
export { wakuRelay, RelayCreateOptions } from "./lib/relay/index.js";
|
13
|
+
export { wakuRelay, RelayCreateOptions, wakuGossipSub, } from "./lib/relay/index.js";
|
14
14
|
export * as waku_store from "./lib/store/index.js";
|
15
15
|
export { PageDirection, wakuStore, StoreCodec, createCursor, } from "./lib/store/index.js";
|
16
16
|
export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js";
|
package/dist/index.js
CHANGED
@@ -9,7 +9,7 @@ export { wakuFilter } from "./lib/filter/index.js";
|
|
9
9
|
export * as waku_light_push from "./lib/light_push/index.js";
|
10
10
|
export { wakuLightPush, LightPushCodec } from "./lib/light_push/index.js";
|
11
11
|
export * as waku_relay from "./lib/relay/index.js";
|
12
|
-
export { wakuRelay } from "./lib/relay/index.js";
|
12
|
+
export { wakuRelay, wakuGossipSub, } from "./lib/relay/index.js";
|
13
13
|
export * as waku_store from "./lib/store/index.js";
|
14
14
|
export { PageDirection, wakuStore, StoreCodec, createCursor, } from "./lib/store/index.js";
|
15
15
|
export { waitForRemotePeer } from "./lib/wait_for_remote_peer.js";
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAM1E,OAAO,KAAK,OAAO,MAAM,wBAAwB,CAAC;AAElD,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAe,MAAM,eAAe,CAAC;AAEtD,OAAO,KAAK,WAAW,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,KAAK,eAAe,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE1E,OAAO,KAAK,UAAU,MAAM,sBAAsB,CAAC;AACnD,OAAO,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAM1E,OAAO,KAAK,OAAO,MAAM,wBAAwB,CAAC;AAElD,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAe,MAAM,eAAe,CAAC;AAEtD,OAAO,KAAK,WAAW,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,KAAK,eAAe,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE1E,OAAO,KAAK,UAAU,MAAM,sBAAsB,CAAC;AACnD,OAAO,EACL,SAAS,EAET,aAAa,GACd,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,UAAU,MAAM,sBAAsB,CAAC;AACnD,OAAO,EACL,aAAa,EACb,SAAS,EACT,UAAU,EACV,YAAY,GACb,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAElE,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EACL,gBAAgB,GAEjB,MAAM,6BAA6B,CAAC"}
|
package/dist/lib/filter/index.js
CHANGED
@@ -27,14 +27,15 @@ class Filter extends BaseProtocol {
|
|
27
27
|
.catch((e) => log("Failed to register filter protocol", e));
|
28
28
|
}
|
29
29
|
/**
|
30
|
-
* @param decoders
|
30
|
+
* @param decoders Decoder or array of Decoders to use to decode messages, it also specifies the content topics.
|
31
31
|
* @param callback A function that will be called on each message returned by the filter.
|
32
32
|
* @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to.
|
33
33
|
* @returns Unsubscribe function that can be used to end the subscription.
|
34
34
|
*/
|
35
35
|
async subscribe(decoders, callback, opts) {
|
36
|
+
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
|
36
37
|
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
37
|
-
const contentTopics = Array.from(groupByContentTopic(
|
38
|
+
const contentTopics = Array.from(groupByContentTopic(decodersArray).keys());
|
38
39
|
const contentFilters = contentTopics.map((contentTopic) => ({
|
39
40
|
contentTopic,
|
40
41
|
}));
|
@@ -50,13 +51,27 @@ class Filter extends BaseProtocol {
|
|
50
51
|
log("Error subscribing to peer ", peer.id.toString(), "for content topics", contentTopics, ": ", e);
|
51
52
|
throw e;
|
52
53
|
}
|
53
|
-
const subscription = {
|
54
|
+
const subscription = {
|
55
|
+
callback,
|
56
|
+
decoders: decodersArray,
|
57
|
+
pubSubTopic,
|
58
|
+
};
|
54
59
|
this.subscriptions.set(requestId, subscription);
|
55
60
|
return async () => {
|
56
61
|
await this.unsubscribe(pubSubTopic, contentFilters, requestId, peer);
|
57
62
|
this.subscriptions.delete(requestId);
|
58
63
|
};
|
59
64
|
}
|
65
|
+
getActiveSubscriptions() {
|
66
|
+
const map = new Map();
|
67
|
+
const subscriptions = this.subscriptions;
|
68
|
+
for (const item of subscriptions.values()) {
|
69
|
+
const values = map.get(item.pubSubTopic) || [];
|
70
|
+
const nextValues = item.decoders.map((decoder) => decoder.contentTopic);
|
71
|
+
map.set(item.pubSubTopic, [...values, ...nextValues]);
|
72
|
+
}
|
73
|
+
return map;
|
74
|
+
}
|
60
75
|
onRequest(streamData) {
|
61
76
|
log("Receiving message push");
|
62
77
|
try {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/filter/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/filter/index.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,mBAAmB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AC;YAC1B,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;IAEO,KAAK,CAAC,WAAW,CACvB,KAAa,EACb,cAA+B,EAC/B,SAAiB,EACjB,IAAU;QAEV,MAAM,kBAAkB,GAAG,SAAS,CAAC,aAAa,CAChD,KAAK,EACL,cAAc,EACd,SAAS,EACT,KAAK,CACN,CAAC;QAEF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAC1C,IAAI;YACF,MAAM,IAAI,CAAC,CAAC,kBAAkB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;SACrE;QAAC,OAAO,CAAC,EAAE;YACV,GAAG,CAAC,qBAAqB,EAAE,CAAC,CAAC,CAAC;YAC9B,MAAM,CAAC,CAAC;SACT;IACH,CAAC;CACF;AAED,MAAM,UAAU,UAAU,CACxB,OAAuC,EAAE;IAEzC,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACtD,CAAC"}
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import { GossipSubComponents, GossipsubOpts } from "@chainsafe/libp2p-gossipsub";
|
1
|
+
import { GossipSub, GossipSubComponents, GossipsubOpts } from "@chainsafe/libp2p-gossipsub";
|
2
|
+
import type { Libp2p } from "@libp2p/interface-libp2p";
|
2
3
|
import type { Callback, IDecodedMessage, IDecoder, IRelay, ProtocolCreateOptions } from "@waku/interfaces";
|
3
4
|
export type Observer<T extends IDecodedMessage> = {
|
4
5
|
decoder: IDecoder<T>;
|
@@ -6,4 +7,5 @@ export type Observer<T extends IDecodedMessage> = {
|
|
6
7
|
};
|
7
8
|
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts;
|
8
9
|
export type ContentTopic = string;
|
9
|
-
export declare function wakuRelay(init?: Partial<
|
10
|
+
export declare function wakuRelay(init?: Partial<ProtocolCreateOptions>): (libp2p: Libp2p) => IRelay;
|
11
|
+
export declare function wakuGossipSub(init?: Partial<RelayCreateOptions>): (components: GossipSubComponents) => GossipSub;
|
package/dist/lib/relay/index.js
CHANGED
@@ -2,27 +2,25 @@ import { GossipSub, } from "@chainsafe/libp2p-gossipsub";
|
|
2
2
|
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
|
3
3
|
import debug from "debug";
|
4
4
|
import { DefaultPubSubTopic } from "../constants.js";
|
5
|
+
import { groupByContentTopic } from "../group_by.js";
|
5
6
|
import { TopicOnlyDecoder } from "../message/topic_only_message.js";
|
6
|
-
import { pushOrInitMapSet } from "../push_or_init_map.js";
|
7
7
|
import * as constants from "./constants.js";
|
8
8
|
import { messageValidator } from "./message_validator.js";
|
9
9
|
const log = debug("waku:relay");
|
10
10
|
/**
|
11
11
|
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
|
12
|
-
*
|
13
|
-
*
|
14
|
-
* @implements {require('libp2p-interfaces/src/pubsub')}
|
12
|
+
* Throws if libp2p.pubsub does not support Waku Relay
|
15
13
|
*/
|
16
|
-
class Relay
|
17
|
-
constructor(
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
});
|
23
|
-
super(components, options);
|
24
|
-
this.multicodecs = constants.RelayCodecs;
|
14
|
+
class Relay {
|
15
|
+
constructor(libp2p, options) {
|
16
|
+
if (!this.isRelayPubSub(libp2p.pubsub)) {
|
17
|
+
throw Error(`Failed to initialize Relay. libp2p.pubsub does not support ${Relay.multicodec}`);
|
18
|
+
}
|
19
|
+
this.gossipSub = libp2p.pubsub;
|
25
20
|
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
21
|
+
if (this.gossipSub.isStarted()) {
|
22
|
+
this.gossipSubSubscribe(this.pubSubTopic);
|
23
|
+
}
|
26
24
|
this.observers = new Map();
|
27
25
|
// TODO: User might want to decide what decoder should be used (e.g. for RLN)
|
28
26
|
this.defaultDecoder = new TopicOnlyDecoder();
|
@@ -35,8 +33,11 @@ class Relay extends GossipSub {
|
|
35
33
|
* @returns {void}
|
36
34
|
*/
|
37
35
|
async start() {
|
38
|
-
|
39
|
-
|
36
|
+
if (this.gossipSub.isStarted()) {
|
37
|
+
throw Error("GossipSub already started.");
|
38
|
+
}
|
39
|
+
await this.gossipSub.start();
|
40
|
+
this.gossipSubSubscribe(this.pubSubTopic);
|
40
41
|
}
|
41
42
|
/**
|
42
43
|
* Send Waku message.
|
@@ -47,24 +48,33 @@ class Relay extends GossipSub {
|
|
47
48
|
log("Failed to encode message, aborting publish");
|
48
49
|
return { recipients: [] };
|
49
50
|
}
|
50
|
-
return this.publish(this.pubSubTopic, msg);
|
51
|
+
return this.gossipSub.publish(this.pubSubTopic, msg);
|
51
52
|
}
|
52
53
|
/**
|
53
54
|
* Add an observer and associated Decoder to process incoming messages on a given content topic.
|
54
55
|
*
|
55
56
|
* @returns Function to delete the observer
|
56
57
|
*/
|
57
|
-
|
58
|
-
const
|
59
|
-
|
60
|
-
callback
|
61
|
-
|
62
|
-
|
63
|
-
|
58
|
+
subscribe(decoders, callback) {
|
59
|
+
const contentTopicToObservers = Array.isArray(decoders)
|
60
|
+
? toObservers(decoders, callback)
|
61
|
+
: toObservers([decoders], callback);
|
62
|
+
for (const contentTopic of contentTopicToObservers.keys()) {
|
63
|
+
const currObservers = this.observers.get(contentTopic) || new Set();
|
64
|
+
const newObservers = contentTopicToObservers.get(contentTopic) || new Set();
|
65
|
+
this.observers.set(contentTopic, union(currObservers, newObservers));
|
66
|
+
}
|
64
67
|
return () => {
|
65
|
-
const
|
66
|
-
|
67
|
-
|
68
|
+
for (const contentTopic of contentTopicToObservers.keys()) {
|
69
|
+
const currentObservers = this.observers.get(contentTopic) || new Set();
|
70
|
+
const observersToRemove = contentTopicToObservers.get(contentTopic) || new Set();
|
71
|
+
const nextObservers = leftMinusJoin(currentObservers, observersToRemove);
|
72
|
+
if (nextObservers.size) {
|
73
|
+
this.observers.set(contentTopic, nextObservers);
|
74
|
+
}
|
75
|
+
else {
|
76
|
+
this.observers.delete(contentTopic);
|
77
|
+
}
|
68
78
|
}
|
69
79
|
};
|
70
80
|
}
|
@@ -73,6 +83,9 @@ class Relay extends GossipSub {
|
|
73
83
|
map.set(this.pubSubTopic, this.observers.keys());
|
74
84
|
return map;
|
75
85
|
}
|
86
|
+
getMeshPeers(topic) {
|
87
|
+
return this.gossipSub.getMeshPeers(topic ?? this.pubSubTopic);
|
88
|
+
}
|
76
89
|
async processIncomingMessage(pubSubTopic, bytes) {
|
77
90
|
const topicOnlyMsg = await this.defaultDecoder.fromWireToProtoObj(bytes);
|
78
91
|
if (!topicOnlyMsg || !topicOnlyMsg.contentTopic) {
|
@@ -103,27 +116,60 @@ class Relay extends GossipSub {
|
|
103
116
|
*
|
104
117
|
* @override
|
105
118
|
*/
|
106
|
-
|
107
|
-
this.addEventListener("gossipsub:message", async (event) => {
|
119
|
+
gossipSubSubscribe(pubSubTopic) {
|
120
|
+
this.gossipSub.addEventListener("gossipsub:message", async (event) => {
|
108
121
|
if (event.detail.msg.topic !== pubSubTopic)
|
109
122
|
return;
|
110
123
|
log(`Message received on ${pubSubTopic}`);
|
111
124
|
this.processIncomingMessage(event.detail.msg.topic, event.detail.msg.data).catch((e) => log("Failed to process incoming message", e));
|
112
125
|
});
|
113
|
-
this.topicValidators.set(pubSubTopic, messageValidator);
|
114
|
-
|
115
|
-
}
|
116
|
-
unsubscribe(pubSubTopic) {
|
117
|
-
super.unsubscribe(pubSubTopic);
|
118
|
-
this.topicValidators.delete(pubSubTopic);
|
126
|
+
this.gossipSub.topicValidators.set(pubSubTopic, messageValidator);
|
127
|
+
this.gossipSub.subscribe(pubSubTopic);
|
119
128
|
}
|
120
|
-
|
121
|
-
return
|
129
|
+
isRelayPubSub(pubsub) {
|
130
|
+
return pubsub?.multicodecs?.includes(Relay.multicodec) || false;
|
122
131
|
}
|
123
132
|
}
|
124
133
|
Relay.multicodec = constants.RelayCodecs[0];
|
125
|
-
Relay.multicodec = constants.RelayCodecs[constants.RelayCodecs.length - 1];
|
126
134
|
export function wakuRelay(init = {}) {
|
127
|
-
return (
|
135
|
+
return (libp2p) => new Relay(libp2p, init);
|
136
|
+
}
|
137
|
+
export function wakuGossipSub(init = {}) {
|
138
|
+
return (components) => {
|
139
|
+
init = {
|
140
|
+
...init,
|
141
|
+
// Ensure that no signature is included nor expected in the messages.
|
142
|
+
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
|
143
|
+
fallbackToFloodsub: false,
|
144
|
+
};
|
145
|
+
const pubsub = new GossipSub(components, init);
|
146
|
+
pubsub.multicodecs = constants.RelayCodecs;
|
147
|
+
return pubsub;
|
148
|
+
};
|
149
|
+
}
|
150
|
+
function toObservers(decoders, callback) {
|
151
|
+
const contentTopicToDecoders = Array.from(groupByContentTopic(decoders).entries());
|
152
|
+
const contentTopicToObserversEntries = contentTopicToDecoders.map(([contentTopic, decoders]) => [
|
153
|
+
contentTopic,
|
154
|
+
new Set(decoders.map((decoder) => ({
|
155
|
+
decoder,
|
156
|
+
callback,
|
157
|
+
}))),
|
158
|
+
]);
|
159
|
+
return new Map(contentTopicToObserversEntries);
|
160
|
+
}
|
161
|
+
function union(left, right) {
|
162
|
+
for (const val of right.values()) {
|
163
|
+
left.add(val);
|
164
|
+
}
|
165
|
+
return left;
|
166
|
+
}
|
167
|
+
function leftMinusJoin(left, right) {
|
168
|
+
for (const val of right.values()) {
|
169
|
+
if (left.has(val)) {
|
170
|
+
left.delete(val);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
return left;
|
128
174
|
}
|
129
175
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/relay/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,GAIV,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;
|
1
|
+
{"version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EAAE,KAAmB;IACpD,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE;QAChC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;KACf;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,aAAa,CAAC,IAAkB,EAAE,KAAmB;IAC5D,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,MAAM,EAAE,EAAE;QAChC,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACjB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;SAClB;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
@@ -81,7 +81,7 @@ async function waitForConnectedPeer(protocol) {
|
|
81
81
|
async function waitForGossipSubPeerInMesh(waku) {
|
82
82
|
let peers = waku.getMeshPeers();
|
83
83
|
while (peers.length == 0) {
|
84
|
-
await pEvent(waku, "gossipsub:heartbeat");
|
84
|
+
await pEvent(waku.gossipSub, "gossipsub:heartbeat");
|
85
85
|
peers = waku.getMeshPeers();
|
86
86
|
}
|
87
87
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"wait_for_remote_peer.js","sourceRoot":"","sources":["../../src/lib/wait_for_remote_peer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,MAAM,GAAG,GAAG,KAAK,CAAC,2BAA2B,CAAC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAU,EACV,SAAuB,EACvB,SAAkB;IAElB,SAAS,GAAG,SAAS,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAEnD,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;QAAE,OAAO,OAAO,CAAC,M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ter?: IFilter;
|
37
37
|
lightPush?: ILightPush;
|
38
38
|
connectionManager: ConnectionManager;
|
39
|
-
constructor(options: WakuOptions, libp2p: Libp2p, store?: (libp2p: Libp2p) => IStore, lightPush?: (libp2p: Libp2p) => ILightPush, filter?: (libp2p: Libp2p) => IFilter);
|
39
|
+
constructor(options: WakuOptions, libp2p: Libp2p, store?: (libp2p: Libp2p) => IStore, lightPush?: (libp2p: Libp2p) => ILightPush, filter?: (libp2p: Libp2p) => IFilter, relay?: (libp2p: Libp2p) => IRelay);
|
40
40
|
/**
|
41
41
|
* Dials to the provided peer.
|
42
42
|
*
|
package/dist/lib/waku.js
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
import { Protocols } from "@waku/interfaces";
|
2
2
|
import debug from "debug";
|
3
3
|
import { ConnectionManager } from "./connection_manager.js";
|
4
|
-
import * as relayConstants from "./relay/constants.js";
|
5
4
|
export const DefaultPingKeepAliveValueSecs = 0;
|
6
5
|
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
7
6
|
export const DefaultUserAgent = "js-waku";
|
8
7
|
const log = debug("waku:waku");
|
9
8
|
export class WakuNode {
|
10
|
-
constructor(options, libp2p, store, lightPush, filter) {
|
9
|
+
constructor(options, libp2p, store, lightPush, filter, relay) {
|
11
10
|
this.libp2p = libp2p;
|
12
11
|
if (store) {
|
13
12
|
this.store = store(libp2p);
|
@@ -18,8 +17,8 @@ export class WakuNode {
|
|
18
17
|
if (lightPush) {
|
19
18
|
this.lightPush = lightPush(libp2p);
|
20
19
|
}
|
21
|
-
if (
|
22
|
-
this.relay = libp2p
|
20
|
+
if (relay) {
|
21
|
+
this.relay = relay(libp2p);
|
23
22
|
}
|
24
23
|
const pingKeepAlive = options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
25
24
|
const relayKeepAlive = this.relay
|
@@ -47,7 +46,7 @@ export class WakuNode {
|
|
47
46
|
const codecs = [];
|
48
47
|
if (_protocols.includes(Protocols.Relay)) {
|
49
48
|
if (this.relay) {
|
50
|
-
this.relay.multicodecs.forEach((codec) => codecs.push(codec));
|
49
|
+
this.relay.gossipSub.multicodecs.forEach((codec) => codecs.push(codec));
|
51
50
|
}
|
52
51
|
else {
|
53
52
|
log("Relay codec not included in dial codec: protocol not mounted locally");
|
@@ -105,15 +104,4 @@ export class WakuNode {
|
|
105
104
|
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
|
106
105
|
}
|
107
106
|
}
|
108
|
-
function isRelay(pubsub) {
|
109
|
-
if (pubsub) {
|
110
|
-
try {
|
111
|
-
return pubsub.multicodecs.includes(relayConstants.RelayCodecs[relayConstants.RelayCodecs.length - 1]);
|
112
|
-
// Exception is expected if `libp2p` was not instantiated with pubsub
|
113
|
-
// eslint-disable-next-line no-empty
|
114
|
-
}
|
115
|
-
catch (e) { }
|
116
|
-
}
|
117
|
-
return false;
|
118
|
-
}
|
119
107
|
//# sourceMappingURL=waku.js.map
|
package/dist/lib/waku.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/waku.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/waku.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAC;AAC/C,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,GAAG,EAAE,CAAC;AACrD,MAAM,CAAC,MAAM,gBAAgB,GAAG,SAAS,CAAC;AAE1C,MAAM,GAAG,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;AAwB/B,MAAM,OAAO,QAAQ;IAQnB,YACE,OAAoB,EACpB,MAAc,EACd,KAAkC,EAClC,SAA0C,EAC1C,MAAoC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;gBACf,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;aACrC;iBAAM;gBACL,GAAG,CACD,uEAAuE,CACxE,CAAC;aACH;SACF;QAED,GAAG,CAAC,cAAc,IAAI,CAAC,QAAQ,EAAE,mBAAmB,UAAU,EAAE,CAAC,CAAC;QAElE,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAChD,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC;QAC9B,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,uBAAuB;QACrB,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM;aAC/B,aAAa,EAAE;aACf,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE;YACvD,MAAM,4BAA4B,CAAC;SACpC;QACD,OAAO,cAAc,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAClE,CAAC;CACF"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/core",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.16",
|
4
4
|
"description": "TypeScript implementation of the Waku v2 protocol",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -82,9 +82,9 @@
|
|
82
82
|
"dependencies": {
|
83
83
|
"@chainsafe/libp2p-gossipsub": "^6.1.0",
|
84
84
|
"@noble/hashes": "^1.3.0",
|
85
|
-
"@waku/interfaces": "0.0.
|
85
|
+
"@waku/interfaces": "0.0.11",
|
86
86
|
"@waku/proto": "0.0.4",
|
87
|
-
"@waku/utils": "0.0.
|
87
|
+
"@waku/utils": "0.0.4",
|
88
88
|
"debug": "^4.3.4",
|
89
89
|
"it-all": "^2.0.0",
|
90
90
|
"it-length-prefixed": "^8.0.4",
|
@@ -108,11 +108,11 @@
|
|
108
108
|
"@types/debug": "^4.1.7",
|
109
109
|
"@types/mocha": "^10.0.1",
|
110
110
|
"@types/uuid": "^9.0.1",
|
111
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
111
|
+
"@typescript-eslint/eslint-plugin": "^5.57.0",
|
112
112
|
"@typescript-eslint/parser": "^5.51.0",
|
113
113
|
"@waku/build-utils": "*",
|
114
114
|
"chai": "^4.3.7",
|
115
|
-
"cspell": "^6.
|
115
|
+
"cspell": "^6.31.1",
|
116
116
|
"eslint": "^8.35.0",
|
117
117
|
"eslint-config-prettier": "^8.6.0",
|
118
118
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
@@ -130,7 +130,7 @@
|
|
130
130
|
"npm-run-all": "^4.1.5",
|
131
131
|
"prettier": "^2.8.4",
|
132
132
|
"process": "^0.11.10",
|
133
|
-
"puppeteer": "^19.
|
133
|
+
"puppeteer": "^19.8.2",
|
134
134
|
"rollup": "^3.15.0",
|
135
135
|
"ts-loader": "^9.4.2",
|
136
136
|
"ts-node": "^10.9.1",
|
@@ -158,4 +158,4 @@
|
|
158
158
|
"LICENSE",
|
159
159
|
"README.md"
|
160
160
|
]
|
161
|
-
}
|
161
|
+
}
|
package/src/index.ts
CHANGED
@@ -19,7 +19,11 @@ export * as waku_light_push from "./lib/light_push/index.js";
|
|
19
19
|
export { wakuLightPush, LightPushCodec } from "./lib/light_push/index.js";
|
20
20
|
|
21
21
|
export * as waku_relay from "./lib/relay/index.js";
|
22
|
-
export {
|
22
|
+
export {
|
23
|
+
wakuRelay,
|
24
|
+
RelayCreateOptions,
|
25
|
+
wakuGossipSub,
|
26
|
+
} from "./lib/relay/index.js";
|
23
27
|
|
24
28
|
export * as waku_store from "./lib/store/index.js";
|
25
29
|
export {
|
package/src/lib/filter/index.ts
CHANGED
@@ -2,6 +2,7 @@ import type { Libp2p } from "@libp2p/interface-libp2p";
|
|
2
2
|
import type { Peer } from "@libp2p/interface-peer-store";
|
3
3
|
import type { IncomingStreamData } from "@libp2p/interface-registrar";
|
4
4
|
import type {
|
5
|
+
ActiveSubscriptions,
|
5
6
|
Callback,
|
6
7
|
IDecodedMessage,
|
7
8
|
IDecoder,
|
@@ -58,19 +59,20 @@ class Filter extends BaseProtocol implements IFilter {
|
|
58
59
|
}
|
59
60
|
|
60
61
|
/**
|
61
|
-
* @param decoders
|
62
|
+
* @param decoders Decoder or array of Decoders to use to decode messages, it also specifies the content topics.
|
62
63
|
* @param callback A function that will be called on each message returned by the filter.
|
63
64
|
* @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to.
|
64
65
|
* @returns Unsubscribe function that can be used to end the subscription.
|
65
66
|
*/
|
66
67
|
async subscribe<T extends IDecodedMessage>(
|
67
|
-
decoders: IDecoder<T>[],
|
68
|
+
decoders: IDecoder<T> | IDecoder<T>[],
|
68
69
|
callback: Callback<T>,
|
69
70
|
opts?: ProtocolOptions
|
70
71
|
): Promise<UnsubscribeFunction> {
|
72
|
+
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
|
71
73
|
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
72
74
|
|
73
|
-
const contentTopics = Array.from(groupByContentTopic(
|
75
|
+
const contentTopics = Array.from(groupByContentTopic(decodersArray).keys());
|
74
76
|
|
75
77
|
const contentFilters = contentTopics.map((contentTopic) => ({
|
76
78
|
contentTopic,
|
@@ -109,7 +111,11 @@ class Filter extends BaseProtocol implements IFilter {
|
|
109
111
|
throw e;
|
110
112
|
}
|
111
113
|
|
112
|
-
const subscription: Subscription<T> = {
|
114
|
+
const subscription: Subscription<T> = {
|
115
|
+
callback,
|
116
|
+
decoders: decodersArray,
|
117
|
+
pubSubTopic,
|
118
|
+
};
|
113
119
|
this.subscriptions.set(requestId, subscription);
|
114
120
|
|
115
121
|
return async () => {
|
@@ -118,6 +124,22 @@ class Filter extends BaseProtocol implements IFilter {
|
|
118
124
|
};
|
119
125
|
}
|
120
126
|
|
127
|
+
public getActiveSubscriptions(): ActiveSubscriptions {
|
128
|
+
const map: ActiveSubscriptions = new Map();
|
129
|
+
const subscriptions = this.subscriptions as Map<
|
130
|
+
RequestID,
|
131
|
+
Subscription<IDecodedMessage>
|
132
|
+
>;
|
133
|
+
|
134
|
+
for (const item of subscriptions.values()) {
|
135
|
+
const values = map.get(item.pubSubTopic) || [];
|
136
|
+
const nextValues = item.decoders.map((decoder) => decoder.contentTopic);
|
137
|
+
map.set(item.pubSubTopic, [...values, ...nextValues]);
|
138
|
+
}
|
139
|
+
|
140
|
+
return map;
|
141
|
+
}
|
142
|
+
|
121
143
|
private onRequest(streamData: IncomingStreamData): void {
|
122
144
|
log("Receiving message push");
|
123
145
|
try {
|
package/src/lib/relay/index.ts
CHANGED
@@ -6,6 +6,8 @@ import {
|
|
6
6
|
} from "@chainsafe/libp2p-gossipsub";
|
7
7
|
import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types";
|
8
8
|
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
|
9
|
+
import type { Libp2p } from "@libp2p/interface-libp2p";
|
10
|
+
import type { PubSub } from "@libp2p/interface-pubsub";
|
9
11
|
import type {
|
10
12
|
ActiveSubscriptions,
|
11
13
|
Callback,
|
@@ -20,8 +22,8 @@ import type {
|
|
20
22
|
import debug from "debug";
|
21
23
|
|
22
24
|
import { DefaultPubSubTopic } from "../constants.js";
|
25
|
+
import { groupByContentTopic } from "../group_by.js";
|
23
26
|
import { TopicOnlyDecoder } from "../message/topic_only_message.js";
|
24
|
-
import { pushOrInitMapSet } from "../push_or_init_map.js";
|
25
27
|
|
26
28
|
import * as constants from "./constants.js";
|
27
29
|
import { messageValidator } from "./message_validator.js";
|
@@ -38,14 +40,14 @@ export type ContentTopic = string;
|
|
38
40
|
|
39
41
|
/**
|
40
42
|
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
|
41
|
-
*
|
42
|
-
*
|
43
|
-
* @implements {require('libp2p-interfaces/src/pubsub')}
|
43
|
+
* Throws if libp2p.pubsub does not support Waku Relay
|
44
44
|
*/
|
45
|
-
class Relay
|
45
|
+
class Relay implements IRelay {
|
46
46
|
private readonly pubSubTopic: string;
|
47
|
-
defaultDecoder: IDecoder<IDecodedMessage>;
|
47
|
+
private defaultDecoder: IDecoder<IDecodedMessage>;
|
48
|
+
|
48
49
|
public static multicodec: string = constants.RelayCodecs[0];
|
50
|
+
public readonly gossipSub: GossipSub;
|
49
51
|
|
50
52
|
/**
|
51
53
|
* observers called when receiving new message.
|
@@ -53,21 +55,20 @@ class Relay extends GossipSub implements IRelay {
|
|
53
55
|
*/
|
54
56
|
private observers: Map<ContentTopic, Set<unknown>>;
|
55
57
|
|
56
|
-
constructor(
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
|
63
|
-
fallbackToFloodsub: false,
|
64
|
-
});
|
65
|
-
|
66
|
-
super(components, options);
|
67
|
-
this.multicodecs = constants.RelayCodecs;
|
58
|
+
constructor(libp2p: Libp2p, options?: Partial<RelayCreateOptions>) {
|
59
|
+
if (!this.isRelayPubSub(libp2p.pubsub)) {
|
60
|
+
throw Error(
|
61
|
+
`Failed to initialize Relay. libp2p.pubsub does not support ${Relay.multicodec}`
|
62
|
+
);
|
63
|
+
}
|
68
64
|
|
65
|
+
this.gossipSub = libp2p.pubsub as GossipSub;
|
69
66
|
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
70
67
|
|
68
|
+
if (this.gossipSub.isStarted()) {
|
69
|
+
this.gossipSubSubscribe(this.pubSubTopic);
|
70
|
+
}
|
71
|
+
|
71
72
|
this.observers = new Map();
|
72
73
|
|
73
74
|
// TODO: User might want to decide what decoder should be used (e.g. for RLN)
|
@@ -82,8 +83,12 @@ class Relay extends GossipSub implements IRelay {
|
|
82
83
|
* @returns {void}
|
83
84
|
*/
|
84
85
|
public async start(): Promise<void> {
|
85
|
-
|
86
|
-
|
86
|
+
if (this.gossipSub.isStarted()) {
|
87
|
+
throw Error("GossipSub already started.");
|
88
|
+
}
|
89
|
+
|
90
|
+
await this.gossipSub.start();
|
91
|
+
this.gossipSubSubscribe(this.pubSubTopic);
|
87
92
|
}
|
88
93
|
|
89
94
|
/**
|
@@ -96,7 +101,7 @@ class Relay extends GossipSub implements IRelay {
|
|
96
101
|
return { recipients: [] };
|
97
102
|
}
|
98
103
|
|
99
|
-
return this.publish(this.pubSubTopic, msg);
|
104
|
+
return this.gossipSub.publish(this.pubSubTopic, msg);
|
100
105
|
}
|
101
106
|
|
102
107
|
/**
|
@@ -104,22 +109,38 @@ class Relay extends GossipSub implements IRelay {
|
|
104
109
|
*
|
105
110
|
* @returns Function to delete the observer
|
106
111
|
*/
|
107
|
-
|
108
|
-
|
112
|
+
public subscribe<T extends IDecodedMessage>(
|
113
|
+
decoders: IDecoder<T> | IDecoder<T>[],
|
109
114
|
callback: Callback<T>
|
110
115
|
): () => void {
|
111
|
-
const
|
112
|
-
|
113
|
-
callback
|
114
|
-
|
115
|
-
const contentTopic
|
116
|
+
const contentTopicToObservers = Array.isArray(decoders)
|
117
|
+
? toObservers(decoders, callback)
|
118
|
+
: toObservers([decoders], callback);
|
119
|
+
|
120
|
+
for (const contentTopic of contentTopicToObservers.keys()) {
|
121
|
+
const currObservers = this.observers.get(contentTopic) || new Set();
|
122
|
+
const newObservers =
|
123
|
+
contentTopicToObservers.get(contentTopic) || new Set();
|
116
124
|
|
117
|
-
|
125
|
+
this.observers.set(contentTopic, union(currObservers, newObservers));
|
126
|
+
}
|
118
127
|
|
119
128
|
return () => {
|
120
|
-
const
|
121
|
-
|
122
|
-
|
129
|
+
for (const contentTopic of contentTopicToObservers.keys()) {
|
130
|
+
const currentObservers = this.observers.get(contentTopic) || new Set();
|
131
|
+
const observersToRemove =
|
132
|
+
contentTopicToObservers.get(contentTopic) || new Set();
|
133
|
+
|
134
|
+
const nextObservers = leftMinusJoin(
|
135
|
+
currentObservers,
|
136
|
+
observersToRemove
|
137
|
+
);
|
138
|
+
|
139
|
+
if (nextObservers.size) {
|
140
|
+
this.observers.set(contentTopic, nextObservers);
|
141
|
+
} else {
|
142
|
+
this.observers.delete(contentTopic);
|
143
|
+
}
|
123
144
|
}
|
124
145
|
};
|
125
146
|
}
|
@@ -130,6 +151,10 @@ class Relay extends GossipSub implements IRelay {
|
|
130
151
|
return map;
|
131
152
|
}
|
132
153
|
|
154
|
+
public getMeshPeers(topic?: TopicStr): PeerIdStr[] {
|
155
|
+
return this.gossipSub.getMeshPeers(topic ?? this.pubSubTopic);
|
156
|
+
}
|
157
|
+
|
133
158
|
private async processIncomingMessage<T extends IDecodedMessage>(
|
134
159
|
pubSubTopic: string,
|
135
160
|
bytes: Uint8Array
|
@@ -168,8 +193,8 @@ class Relay extends GossipSub implements IRelay {
|
|
168
193
|
*
|
169
194
|
* @override
|
170
195
|
*/
|
171
|
-
|
172
|
-
this.addEventListener(
|
196
|
+
private gossipSubSubscribe(pubSubTopic: string): void {
|
197
|
+
this.gossipSub.addEventListener(
|
173
198
|
"gossipsub:message",
|
174
199
|
async (event: CustomEvent<GossipsubMessage>) => {
|
175
200
|
if (event.detail.msg.topic !== pubSubTopic) return;
|
@@ -182,24 +207,76 @@ class Relay extends GossipSub implements IRelay {
|
|
182
207
|
}
|
183
208
|
);
|
184
209
|
|
185
|
-
this.topicValidators.set(pubSubTopic, messageValidator);
|
186
|
-
|
210
|
+
this.gossipSub.topicValidators.set(pubSubTopic, messageValidator);
|
211
|
+
this.gossipSub.subscribe(pubSubTopic);
|
187
212
|
}
|
188
213
|
|
189
|
-
|
190
|
-
|
191
|
-
this.topicValidators.delete(pubSubTopic);
|
192
|
-
}
|
193
|
-
|
194
|
-
getMeshPeers(topic?: TopicStr): PeerIdStr[] {
|
195
|
-
return super.getMeshPeers(topic ?? this.pubSubTopic);
|
214
|
+
private isRelayPubSub(pubsub: PubSub): boolean {
|
215
|
+
return pubsub?.multicodecs?.includes(Relay.multicodec) || false;
|
196
216
|
}
|
197
217
|
}
|
198
218
|
|
199
|
-
Relay.multicodec = constants.RelayCodecs[constants.RelayCodecs.length - 1];
|
200
|
-
|
201
219
|
export function wakuRelay(
|
220
|
+
init: Partial<ProtocolCreateOptions> = {}
|
221
|
+
): (libp2p: Libp2p) => IRelay {
|
222
|
+
return (libp2p: Libp2p) => new Relay(libp2p, init);
|
223
|
+
}
|
224
|
+
|
225
|
+
export function wakuGossipSub(
|
202
226
|
init: Partial<RelayCreateOptions> = {}
|
203
|
-
): (components: GossipSubComponents) =>
|
204
|
-
return (components: GossipSubComponents) =>
|
227
|
+
): (components: GossipSubComponents) => GossipSub {
|
228
|
+
return (components: GossipSubComponents) => {
|
229
|
+
init = {
|
230
|
+
...init,
|
231
|
+
// Ensure that no signature is included nor expected in the messages.
|
232
|
+
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
|
233
|
+
fallbackToFloodsub: false,
|
234
|
+
};
|
235
|
+
const pubsub = new GossipSub(components, init);
|
236
|
+
pubsub.multicodecs = constants.RelayCodecs;
|
237
|
+
return pubsub;
|
238
|
+
};
|
239
|
+
}
|
240
|
+
|
241
|
+
function toObservers<T extends IDecodedMessage>(
|
242
|
+
decoders: IDecoder<T>[],
|
243
|
+
callback: Callback<T>
|
244
|
+
): Map<ContentTopic, Set<Observer<T>>> {
|
245
|
+
const contentTopicToDecoders = Array.from(
|
246
|
+
groupByContentTopic(decoders).entries()
|
247
|
+
);
|
248
|
+
|
249
|
+
const contentTopicToObserversEntries = contentTopicToDecoders.map(
|
250
|
+
([contentTopic, decoders]) =>
|
251
|
+
[
|
252
|
+
contentTopic,
|
253
|
+
new Set(
|
254
|
+
decoders.map(
|
255
|
+
(decoder) =>
|
256
|
+
({
|
257
|
+
decoder,
|
258
|
+
callback,
|
259
|
+
} as Observer<T>)
|
260
|
+
)
|
261
|
+
),
|
262
|
+
] as [ContentTopic, Set<Observer<T>>]
|
263
|
+
);
|
264
|
+
|
265
|
+
return new Map(contentTopicToObserversEntries);
|
266
|
+
}
|
267
|
+
|
268
|
+
function union(left: Set<unknown>, right: Set<unknown>): Set<unknown> {
|
269
|
+
for (const val of right.values()) {
|
270
|
+
left.add(val);
|
271
|
+
}
|
272
|
+
return left;
|
273
|
+
}
|
274
|
+
|
275
|
+
function leftMinusJoin(left: Set<unknown>, right: Set<unknown>): Set<unknown> {
|
276
|
+
for (const val of right.values()) {
|
277
|
+
if (left.has(val)) {
|
278
|
+
left.delete(val);
|
279
|
+
}
|
280
|
+
}
|
281
|
+
return left;
|
205
282
|
}
|
@@ -105,7 +105,7 @@ async function waitForGossipSubPeerInMesh(waku: IRelay): Promise<void> {
|
|
105
105
|
let peers = waku.getMeshPeers();
|
106
106
|
|
107
107
|
while (peers.length == 0) {
|
108
|
-
await pEvent(waku, "gossipsub:heartbeat");
|
108
|
+
await pEvent(waku.gossipSub, "gossipsub:heartbeat");
|
109
109
|
peers = waku.getMeshPeers();
|
110
110
|
}
|
111
111
|
}
|
package/src/lib/waku.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import type { Stream } from "@libp2p/interface-connection";
|
2
2
|
import type { Libp2p } from "@libp2p/interface-libp2p";
|
3
3
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
4
|
-
import type { PubSub } from "@libp2p/interface-pubsub";
|
5
4
|
import type { Multiaddr } from "@multiformats/multiaddr";
|
6
5
|
import type {
|
7
6
|
IFilter,
|
@@ -14,7 +13,6 @@ import { Protocols } from "@waku/interfaces";
|
|
14
13
|
import debug from "debug";
|
15
14
|
|
16
15
|
import { ConnectionManager } from "./connection_manager.js";
|
17
|
-
import * as relayConstants from "./relay/constants.js";
|
18
16
|
|
19
17
|
export const DefaultPingKeepAliveValueSecs = 0;
|
20
18
|
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
@@ -57,7 +55,8 @@ export class WakuNode implements Waku {
|
|
57
55
|
libp2p: Libp2p,
|
58
56
|
store?: (libp2p: Libp2p) => IStore,
|
59
57
|
lightPush?: (libp2p: Libp2p) => ILightPush,
|
60
|
-
filter?: (libp2p: Libp2p) => IFilter
|
58
|
+
filter?: (libp2p: Libp2p) => IFilter,
|
59
|
+
relay?: (libp2p: Libp2p) => IRelay
|
61
60
|
) {
|
62
61
|
this.libp2p = libp2p;
|
63
62
|
|
@@ -71,8 +70,8 @@ export class WakuNode implements Waku {
|
|
71
70
|
this.lightPush = lightPush(libp2p);
|
72
71
|
}
|
73
72
|
|
74
|
-
if (
|
75
|
-
this.relay = libp2p
|
73
|
+
if (relay) {
|
74
|
+
this.relay = relay(libp2p);
|
76
75
|
}
|
77
76
|
|
78
77
|
const pingKeepAlive =
|
@@ -120,7 +119,9 @@ export class WakuNode implements Waku {
|
|
120
119
|
const codecs: string[] = [];
|
121
120
|
if (_protocols.includes(Protocols.Relay)) {
|
122
121
|
if (this.relay) {
|
123
|
-
this.relay.multicodecs.forEach((codec) =>
|
122
|
+
this.relay.gossipSub.multicodecs.forEach((codec: string) =>
|
123
|
+
codecs.push(codec)
|
124
|
+
);
|
124
125
|
} else {
|
125
126
|
log(
|
126
127
|
"Relay codec not included in dial codec: protocol not mounted locally"
|
@@ -188,16 +189,3 @@ export class WakuNode implements Waku {
|
|
188
189
|
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
|
189
190
|
}
|
190
191
|
}
|
191
|
-
|
192
|
-
function isRelay(pubsub: PubSub): pubsub is IRelay {
|
193
|
-
if (pubsub) {
|
194
|
-
try {
|
195
|
-
return pubsub.multicodecs.includes(
|
196
|
-
relayConstants.RelayCodecs[relayConstants.RelayCodecs.length - 1]
|
197
|
-
);
|
198
|
-
// Exception is expected if `libp2p` was not instantiated with pubsub
|
199
|
-
// eslint-disable-next-line no-empty
|
200
|
-
} catch (e) {}
|
201
|
-
}
|
202
|
-
return false;
|
203
|
-
}
|