@waku/core 0.0.5 → 0.0.7
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 +51 -3
- package/bundle/index.js +31146 -26342
- package/bundle/lib/message/topic_only_message.js +33 -0
- package/bundle/lib/message/version_0.js +133 -0
- package/bundle/lib/predefined_bootstrap_nodes.js +24 -4
- package/bundle/{message-049c8b67.js → peer_exchange-53df2b11.js} +3579 -1007
- package/dist/index.d.ts +14 -13
- package/dist/index.js +11 -10
- package/dist/index.js.map +1 -1
- package/dist/lib/{waku_filter → filter}/filter_rpc.d.ts +1 -1
- package/dist/lib/{waku_filter → filter}/filter_rpc.js +1 -1
- package/dist/lib/filter/filter_rpc.js.map +1 -0
- package/dist/lib/filter/index.d.ts +25 -0
- package/dist/lib/{waku_filter → filter}/index.js +14 -9
- package/dist/lib/filter/index.js.map +1 -0
- package/dist/lib/light_push/index.d.ts +22 -0
- package/dist/lib/{waku_light_push → light_push}/index.js +14 -9
- package/dist/lib/light_push/index.js.map +1 -0
- package/dist/lib/{waku_light_push → light_push}/push_rpc.d.ts +1 -1
- package/dist/lib/{waku_light_push → light_push}/push_rpc.js +1 -1
- package/dist/lib/light_push/push_rpc.js.map +1 -0
- package/dist/lib/message/topic_only_message.d.ts +16 -0
- package/dist/lib/{waku_message → message}/topic_only_message.js +2 -1
- package/dist/lib/message/topic_only_message.js.map +1 -0
- package/dist/lib/message/version_0.d.ts +54 -0
- package/dist/lib/{waku_message → message}/version_0.js +41 -6
- package/dist/lib/message/version_0.js.map +1 -0
- package/dist/lib/predefined_bootstrap_nodes.d.ts +1 -2
- package/dist/lib/predefined_bootstrap_nodes.js +0 -2
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/{waku_relay → relay}/constants.d.ts +0 -0
- package/dist/lib/{waku_relay → relay}/constants.js +0 -0
- package/dist/lib/relay/constants.js.map +1 -0
- package/dist/lib/relay/index.d.ts +24 -0
- package/dist/lib/{waku_relay → relay}/index.js +9 -6
- package/dist/lib/relay/index.js.map +1 -0
- package/dist/lib/{waku_store → store}/history_rpc.d.ts +1 -1
- package/dist/lib/{waku_store → store}/history_rpc.js +1 -1
- package/dist/lib/store/history_rpc.js.map +1 -0
- package/dist/lib/store/index.d.ts +68 -0
- package/dist/lib/{waku_store → store}/index.js +41 -16
- package/dist/lib/store/index.js.map +1 -0
- package/dist/lib/to_proto_message.d.ts +3 -3
- package/dist/lib/to_proto_message.js +1 -0
- package/dist/lib/to_proto_message.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +14 -5
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/dist/lib/waku.d.ts +17 -15
- package/dist/lib/waku.js +49 -41
- package/dist/lib/waku.js.map +1 -1
- package/package.json +28 -31
- package/src/index.ts +25 -14
- package/src/lib/{waku_filter → filter}/filter_rpc.ts +1 -2
- package/src/lib/{waku_filter → filter}/index.ts +51 -30
- package/src/lib/{waku_light_push → light_push}/index.ts +38 -19
- package/src/lib/{waku_light_push → light_push}/push_rpc.ts +1 -2
- package/src/lib/{waku_message → message}/topic_only_message.ts +12 -7
- package/src/lib/{waku_message → message}/version_0.ts +59 -18
- package/src/lib/predefined_bootstrap_nodes.ts +2 -6
- package/src/lib/{waku_relay → relay}/constants.ts +0 -0
- package/src/lib/{waku_relay → relay}/index.ts +30 -26
- package/src/lib/{waku_store → store}/history_rpc.ts +1 -2
- package/src/lib/{waku_store → store}/index.ts +93 -28
- package/src/lib/to_proto_message.ts +5 -5
- package/src/lib/wait_for_remote_peer.ts +19 -7
- package/src/lib/waku.ts +78 -51
- package/bundle/browser-1e1a2f27.js +0 -722
- package/bundle/events-158407bb.js +0 -1929
- package/bundle/index-64ce43f0.js +0 -69
- package/bundle/index-8710041d.js +0 -2962
- package/bundle/index-a67d7136.js +0 -4059
- package/bundle/lib/peer_discovery_static_list.js +0 -89
- package/bundle/lib/wait_for_remote_peer.js +0 -326
- package/bundle/lib/waku_message/topic_only_message.js +0 -3
- package/bundle/lib/waku_message/version_0.js +0 -317
- package/bundle/random_subset-75d1c511.js +0 -26
- package/bundle/topic_only_message-5ad3a869.js +0 -82
- package/dist/lib/multiaddr_to_peer_info.d.ts +0 -3
- package/dist/lib/multiaddr_to_peer_info.js +0 -15
- package/dist/lib/multiaddr_to_peer_info.js.map +0 -1
- package/dist/lib/peer_discovery_static_list.d.ts +0 -44
- package/dist/lib/peer_discovery_static_list.js +0 -72
- package/dist/lib/peer_discovery_static_list.js.map +0 -1
- package/dist/lib/select_connection.d.ts +0 -2
- package/dist/lib/select_connection.js +0 -19
- package/dist/lib/select_connection.js.map +0 -1
- package/dist/lib/select_peer.d.ts +0 -15
- package/dist/lib/select_peer.js +0 -59
- package/dist/lib/select_peer.js.map +0 -1
- package/dist/lib/waku_filter/filter_rpc.js.map +0 -1
- package/dist/lib/waku_filter/index.d.ts +0 -50
- package/dist/lib/waku_filter/index.js.map +0 -1
- package/dist/lib/waku_light_push/index.d.ts +0 -38
- package/dist/lib/waku_light_push/index.js.map +0 -1
- package/dist/lib/waku_light_push/push_rpc.js.map +0 -1
- package/dist/lib/waku_message/topic_only_message.d.ts +0 -15
- package/dist/lib/waku_message/topic_only_message.js.map +0 -1
- package/dist/lib/waku_message/version_0.d.ts +0 -27
- package/dist/lib/waku_message/version_0.js.map +0 -1
- package/dist/lib/waku_relay/constants.js.map +0 -1
- package/dist/lib/waku_relay/index.d.ts +0 -66
- package/dist/lib/waku_relay/index.js.map +0 -1
- package/dist/lib/waku_store/history_rpc.js.map +0 -1
- package/dist/lib/waku_store/index.d.ts +0 -126
- package/dist/lib/waku_store/index.js.map +0 -1
- package/dist/proto/filter.d.ts +0 -65
- package/dist/proto/filter.js +0 -425
- package/dist/proto/filter.js.map +0 -1
- package/dist/proto/light_push.d.ts +0 -57
- package/dist/proto/light_push.js +0 -369
- package/dist/proto/light_push.js.map +0 -1
- package/dist/proto/message.d.ts +0 -29
- package/dist/proto/message.js +0 -215
- package/dist/proto/message.js.map +0 -1
- package/dist/proto/store.d.ts +0 -104
- package/dist/proto/store.js +0 -602
- package/dist/proto/store.js.map +0 -1
- package/dist/proto/topic_only_message.d.ts +0 -10
- package/dist/proto/topic_only_message.js +0 -46
- package/dist/proto/topic_only_message.js.map +0 -1
- package/src/lib/multiaddr_to_peer_info.ts +0 -17
- package/src/lib/peer_discovery_static_list.ts +0 -118
- package/src/lib/select_connection.ts +0 -24
- package/src/lib/select_peer.ts +0 -77
- package/src/proto/filter.ts +0 -602
- package/src/proto/light_push.ts +0 -526
- package/src/proto/message.ts +0 -304
- package/src/proto/store.ts +0 -844
- package/src/proto/topic_only_message.ts +0 -67
package/src/lib/waku.ts
CHANGED
@@ -1,24 +1,31 @@
|
|
1
1
|
import type { Stream } from "@libp2p/interface-connection";
|
2
2
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
3
3
|
import type { PubSub } from "@libp2p/interface-pubsub";
|
4
|
-
import { peerIdFromString } from "@libp2p/peer-id";
|
5
4
|
import type { Multiaddr } from "@multiformats/multiaddr";
|
6
|
-
import {
|
7
|
-
|
5
|
+
import type {
|
6
|
+
IFilter,
|
7
|
+
ILightPush,
|
8
|
+
IPeerExchange,
|
9
|
+
IRelay,
|
10
|
+
IStore,
|
11
|
+
PeerExchangeComponents,
|
12
|
+
Waku,
|
13
|
+
} from "@waku/interfaces";
|
8
14
|
import { Protocols } from "@waku/interfaces";
|
15
|
+
import { PeerExchangeCodec } from "@waku/peer-exchange";
|
9
16
|
import debug from "debug";
|
10
17
|
import type { Libp2p } from "libp2p";
|
11
18
|
|
12
|
-
import { FilterCodec,
|
13
|
-
import { LightPushCodec,
|
14
|
-
import {
|
15
|
-
import
|
16
|
-
import { RelayCodecs, RelayPingContentTopic } from "./
|
17
|
-
import
|
18
|
-
import { StoreCodec, WakuStore } from "./waku_store";
|
19
|
+
import { FilterCodec, FilterComponents } from "./filter/index.js";
|
20
|
+
import { LightPushCodec, LightPushComponents } from "./light_push/index.js";
|
21
|
+
import { createEncoder } from "./message/version_0.js";
|
22
|
+
import * as relayConstants from "./relay/constants.js";
|
23
|
+
import { RelayCodecs, RelayPingContentTopic } from "./relay/constants.js";
|
24
|
+
import { StoreCodec, StoreComponents } from "./store/index.js";
|
19
25
|
|
20
26
|
export const DefaultPingKeepAliveValueSecs = 0;
|
21
27
|
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
28
|
+
export const DefaultUserAgent = "js-waku";
|
22
29
|
|
23
30
|
const log = debug("waku:waku");
|
24
31
|
|
@@ -37,14 +44,20 @@ export interface WakuOptions {
|
|
37
44
|
* @default {@link DefaultRelayKeepAliveValueSecs}
|
38
45
|
*/
|
39
46
|
relayKeepAlive?: number;
|
47
|
+
/**
|
48
|
+
* Set the user agent string to be used in identification of the node.
|
49
|
+
* @default {@link DefaultUserAgent}
|
50
|
+
*/
|
51
|
+
userAgent?: string;
|
40
52
|
}
|
41
53
|
|
42
54
|
export class WakuNode implements Waku {
|
43
55
|
public libp2p: Libp2p;
|
44
|
-
public relay?:
|
45
|
-
public store?:
|
46
|
-
public filter?:
|
47
|
-
public lightPush?:
|
56
|
+
public relay?: IRelay;
|
57
|
+
public store?: IStore;
|
58
|
+
public filter?: IFilter;
|
59
|
+
public lightPush?: ILightPush;
|
60
|
+
public peerExchange?: IPeerExchange;
|
48
61
|
|
49
62
|
private pingKeepAliveTimers: {
|
50
63
|
[peer: string]: ReturnType<typeof setInterval>;
|
@@ -56,16 +69,31 @@ export class WakuNode implements Waku {
|
|
56
69
|
constructor(
|
57
70
|
options: WakuOptions,
|
58
71
|
libp2p: Libp2p,
|
59
|
-
store?:
|
60
|
-
lightPush?:
|
61
|
-
filter?:
|
72
|
+
store?: (components: StoreComponents) => IStore,
|
73
|
+
lightPush?: (components: LightPushComponents) => ILightPush,
|
74
|
+
filter?: (components: FilterComponents) => IFilter,
|
75
|
+
peerExchange?: (components: PeerExchangeComponents) => IPeerExchange
|
62
76
|
) {
|
63
77
|
this.libp2p = libp2p;
|
64
|
-
this.store = store;
|
65
|
-
this.filter = filter;
|
66
|
-
this.lightPush = lightPush;
|
67
78
|
|
68
|
-
|
79
|
+
const { peerStore, connectionManager, registrar } = libp2p;
|
80
|
+
const components = { peerStore, connectionManager, registrar };
|
81
|
+
|
82
|
+
if (store) {
|
83
|
+
this.store = store(components);
|
84
|
+
}
|
85
|
+
if (filter) {
|
86
|
+
this.filter = filter(components);
|
87
|
+
}
|
88
|
+
if (lightPush) {
|
89
|
+
this.lightPush = lightPush(components);
|
90
|
+
}
|
91
|
+
|
92
|
+
if (peerExchange) {
|
93
|
+
this.peerExchange = peerExchange(components);
|
94
|
+
}
|
95
|
+
|
96
|
+
if (isRelay(libp2p.pubsub)) {
|
69
97
|
this.relay = libp2p.pubsub;
|
70
98
|
}
|
71
99
|
|
@@ -73,7 +101,8 @@ export class WakuNode implements Waku {
|
|
73
101
|
"Waku node created",
|
74
102
|
this.libp2p.peerId.toString(),
|
75
103
|
`relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
|
76
|
-
.lightPush}, filter: ${!!this.filter}
|
104
|
+
.lightPush}, filter: ${!!this.filter}, peer exchange: ${!!this
|
105
|
+
.peerExchange} `
|
77
106
|
);
|
78
107
|
|
79
108
|
this.pingKeepAliveTimers = {};
|
@@ -103,19 +132,36 @@ export class WakuNode implements Waku {
|
|
103
132
|
libp2p.connectionManager.addEventListener("peer:disconnect", (evt) => {
|
104
133
|
this.stopKeepAlive(evt.detail.remotePeer);
|
105
134
|
});
|
135
|
+
|
136
|
+
// Trivial handling of discovered peers, to be refined.
|
137
|
+
libp2p.addEventListener("peer:discovery", (evt) => {
|
138
|
+
const peerId = evt.detail.id;
|
139
|
+
log(`Found peer ${peerId.toString()}, dialing.`);
|
140
|
+
libp2p.dial(peerId).catch((err) => {
|
141
|
+
log(`Fail to dial ${peerId}`, err);
|
142
|
+
});
|
143
|
+
});
|
106
144
|
}
|
107
145
|
|
108
146
|
/**
|
109
147
|
* Dials to the provided peer.
|
110
148
|
*
|
111
149
|
* @param peer The peer to dial
|
112
|
-
* @param protocols Waku protocols we expect from the peer;
|
150
|
+
* @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
|
113
151
|
*/
|
114
152
|
async dial(
|
115
153
|
peer: PeerId | Multiaddr,
|
116
154
|
protocols?: Protocols[]
|
117
155
|
): Promise<Stream> {
|
118
|
-
const _protocols = protocols ?? [
|
156
|
+
const _protocols = protocols ?? [];
|
157
|
+
|
158
|
+
if (typeof protocols === "undefined") {
|
159
|
+
this.relay && _protocols.push(Protocols.Relay);
|
160
|
+
this.store && _protocols.push(Protocols.Store);
|
161
|
+
this.filter && _protocols.push(Protocols.Filter);
|
162
|
+
this.lightPush && _protocols.push(Protocols.LightPush);
|
163
|
+
this.peerExchange && _protocols.push(Protocols.PeerExchange);
|
164
|
+
}
|
119
165
|
|
120
166
|
const codecs: string[] = [];
|
121
167
|
if (_protocols.includes(Protocols.Relay)) {
|
@@ -131,32 +177,13 @@ export class WakuNode implements Waku {
|
|
131
177
|
codecs.push(FilterCodec);
|
132
178
|
}
|
133
179
|
|
134
|
-
|
135
|
-
|
136
|
-
return this.libp2p.dialProtocol(peer, codecs);
|
137
|
-
}
|
138
|
-
|
139
|
-
/**
|
140
|
-
* Add peer to address book, it will be auto-dialed in the background.
|
141
|
-
*/
|
142
|
-
async addPeerToAddressBook(
|
143
|
-
peerId: PeerId | string,
|
144
|
-
multiaddrs: Multiaddr[] | string[]
|
145
|
-
): Promise<void> {
|
146
|
-
let peer;
|
147
|
-
if (typeof peerId === "string") {
|
148
|
-
peer = peerIdFromString(peerId);
|
149
|
-
} else {
|
150
|
-
peer = peerId;
|
180
|
+
if (_protocols.includes(Protocols.PeerExchange)) {
|
181
|
+
codecs.push(PeerExchangeCodec);
|
151
182
|
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
return addr;
|
157
|
-
}
|
158
|
-
});
|
159
|
-
await this.libp2p.peerStore.addressBook.set(peer, addresses);
|
183
|
+
|
184
|
+
log(`Dialing to ${peer.toString()} with protocols ${_protocols}`);
|
185
|
+
|
186
|
+
return this.libp2p.dialProtocol(peer, codecs);
|
160
187
|
}
|
161
188
|
|
162
189
|
async start(): Promise<void> {
|
@@ -207,7 +234,7 @@ export class WakuNode implements Waku {
|
|
207
234
|
|
208
235
|
const relay = this.relay;
|
209
236
|
if (relay && relayPeriodSecs !== 0) {
|
210
|
-
const encoder =
|
237
|
+
const encoder = createEncoder(RelayPingContentTopic);
|
211
238
|
this.relayKeepAliveTimers[peerIdStr] = setInterval(() => {
|
212
239
|
log("Sending Waku Relay ping message");
|
213
240
|
relay
|
@@ -244,7 +271,7 @@ export class WakuNode implements Waku {
|
|
244
271
|
}
|
245
272
|
}
|
246
273
|
|
247
|
-
function
|
274
|
+
function isRelay(pubsub: PubSub): pubsub is IRelay {
|
248
275
|
if (pubsub) {
|
249
276
|
try {
|
250
277
|
return pubsub.multicodecs.includes(
|