@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.
Files changed (129) hide show
  1. package/CHANGELOG.md +51 -3
  2. package/bundle/index.js +31146 -26342
  3. package/bundle/lib/message/topic_only_message.js +33 -0
  4. package/bundle/lib/message/version_0.js +133 -0
  5. package/bundle/lib/predefined_bootstrap_nodes.js +24 -4
  6. package/bundle/{message-049c8b67.js → peer_exchange-53df2b11.js} +3579 -1007
  7. package/dist/index.d.ts +14 -13
  8. package/dist/index.js +11 -10
  9. package/dist/index.js.map +1 -1
  10. package/dist/lib/{waku_filter → filter}/filter_rpc.d.ts +1 -1
  11. package/dist/lib/{waku_filter → filter}/filter_rpc.js +1 -1
  12. package/dist/lib/filter/filter_rpc.js.map +1 -0
  13. package/dist/lib/filter/index.d.ts +25 -0
  14. package/dist/lib/{waku_filter → filter}/index.js +14 -9
  15. package/dist/lib/filter/index.js.map +1 -0
  16. package/dist/lib/light_push/index.d.ts +22 -0
  17. package/dist/lib/{waku_light_push → light_push}/index.js +14 -9
  18. package/dist/lib/light_push/index.js.map +1 -0
  19. package/dist/lib/{waku_light_push → light_push}/push_rpc.d.ts +1 -1
  20. package/dist/lib/{waku_light_push → light_push}/push_rpc.js +1 -1
  21. package/dist/lib/light_push/push_rpc.js.map +1 -0
  22. package/dist/lib/message/topic_only_message.d.ts +16 -0
  23. package/dist/lib/{waku_message → message}/topic_only_message.js +2 -1
  24. package/dist/lib/message/topic_only_message.js.map +1 -0
  25. package/dist/lib/message/version_0.d.ts +54 -0
  26. package/dist/lib/{waku_message → message}/version_0.js +41 -6
  27. package/dist/lib/message/version_0.js.map +1 -0
  28. package/dist/lib/predefined_bootstrap_nodes.d.ts +1 -2
  29. package/dist/lib/predefined_bootstrap_nodes.js +0 -2
  30. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
  31. package/dist/lib/{waku_relay → relay}/constants.d.ts +0 -0
  32. package/dist/lib/{waku_relay → relay}/constants.js +0 -0
  33. package/dist/lib/relay/constants.js.map +1 -0
  34. package/dist/lib/relay/index.d.ts +24 -0
  35. package/dist/lib/{waku_relay → relay}/index.js +9 -6
  36. package/dist/lib/relay/index.js.map +1 -0
  37. package/dist/lib/{waku_store → store}/history_rpc.d.ts +1 -1
  38. package/dist/lib/{waku_store → store}/history_rpc.js +1 -1
  39. package/dist/lib/store/history_rpc.js.map +1 -0
  40. package/dist/lib/store/index.d.ts +68 -0
  41. package/dist/lib/{waku_store → store}/index.js +41 -16
  42. package/dist/lib/store/index.js.map +1 -0
  43. package/dist/lib/to_proto_message.d.ts +3 -3
  44. package/dist/lib/to_proto_message.js +1 -0
  45. package/dist/lib/to_proto_message.js.map +1 -1
  46. package/dist/lib/wait_for_remote_peer.js +14 -5
  47. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  48. package/dist/lib/waku.d.ts +17 -15
  49. package/dist/lib/waku.js +49 -41
  50. package/dist/lib/waku.js.map +1 -1
  51. package/package.json +28 -31
  52. package/src/index.ts +25 -14
  53. package/src/lib/{waku_filter → filter}/filter_rpc.ts +1 -2
  54. package/src/lib/{waku_filter → filter}/index.ts +51 -30
  55. package/src/lib/{waku_light_push → light_push}/index.ts +38 -19
  56. package/src/lib/{waku_light_push → light_push}/push_rpc.ts +1 -2
  57. package/src/lib/{waku_message → message}/topic_only_message.ts +12 -7
  58. package/src/lib/{waku_message → message}/version_0.ts +59 -18
  59. package/src/lib/predefined_bootstrap_nodes.ts +2 -6
  60. package/src/lib/{waku_relay → relay}/constants.ts +0 -0
  61. package/src/lib/{waku_relay → relay}/index.ts +30 -26
  62. package/src/lib/{waku_store → store}/history_rpc.ts +1 -2
  63. package/src/lib/{waku_store → store}/index.ts +93 -28
  64. package/src/lib/to_proto_message.ts +5 -5
  65. package/src/lib/wait_for_remote_peer.ts +19 -7
  66. package/src/lib/waku.ts +78 -51
  67. package/bundle/browser-1e1a2f27.js +0 -722
  68. package/bundle/events-158407bb.js +0 -1929
  69. package/bundle/index-64ce43f0.js +0 -69
  70. package/bundle/index-8710041d.js +0 -2962
  71. package/bundle/index-a67d7136.js +0 -4059
  72. package/bundle/lib/peer_discovery_static_list.js +0 -89
  73. package/bundle/lib/wait_for_remote_peer.js +0 -326
  74. package/bundle/lib/waku_message/topic_only_message.js +0 -3
  75. package/bundle/lib/waku_message/version_0.js +0 -317
  76. package/bundle/random_subset-75d1c511.js +0 -26
  77. package/bundle/topic_only_message-5ad3a869.js +0 -82
  78. package/dist/lib/multiaddr_to_peer_info.d.ts +0 -3
  79. package/dist/lib/multiaddr_to_peer_info.js +0 -15
  80. package/dist/lib/multiaddr_to_peer_info.js.map +0 -1
  81. package/dist/lib/peer_discovery_static_list.d.ts +0 -44
  82. package/dist/lib/peer_discovery_static_list.js +0 -72
  83. package/dist/lib/peer_discovery_static_list.js.map +0 -1
  84. package/dist/lib/select_connection.d.ts +0 -2
  85. package/dist/lib/select_connection.js +0 -19
  86. package/dist/lib/select_connection.js.map +0 -1
  87. package/dist/lib/select_peer.d.ts +0 -15
  88. package/dist/lib/select_peer.js +0 -59
  89. package/dist/lib/select_peer.js.map +0 -1
  90. package/dist/lib/waku_filter/filter_rpc.js.map +0 -1
  91. package/dist/lib/waku_filter/index.d.ts +0 -50
  92. package/dist/lib/waku_filter/index.js.map +0 -1
  93. package/dist/lib/waku_light_push/index.d.ts +0 -38
  94. package/dist/lib/waku_light_push/index.js.map +0 -1
  95. package/dist/lib/waku_light_push/push_rpc.js.map +0 -1
  96. package/dist/lib/waku_message/topic_only_message.d.ts +0 -15
  97. package/dist/lib/waku_message/topic_only_message.js.map +0 -1
  98. package/dist/lib/waku_message/version_0.d.ts +0 -27
  99. package/dist/lib/waku_message/version_0.js.map +0 -1
  100. package/dist/lib/waku_relay/constants.js.map +0 -1
  101. package/dist/lib/waku_relay/index.d.ts +0 -66
  102. package/dist/lib/waku_relay/index.js.map +0 -1
  103. package/dist/lib/waku_store/history_rpc.js.map +0 -1
  104. package/dist/lib/waku_store/index.d.ts +0 -126
  105. package/dist/lib/waku_store/index.js.map +0 -1
  106. package/dist/proto/filter.d.ts +0 -65
  107. package/dist/proto/filter.js +0 -425
  108. package/dist/proto/filter.js.map +0 -1
  109. package/dist/proto/light_push.d.ts +0 -57
  110. package/dist/proto/light_push.js +0 -369
  111. package/dist/proto/light_push.js.map +0 -1
  112. package/dist/proto/message.d.ts +0 -29
  113. package/dist/proto/message.js +0 -215
  114. package/dist/proto/message.js.map +0 -1
  115. package/dist/proto/store.d.ts +0 -104
  116. package/dist/proto/store.js +0 -602
  117. package/dist/proto/store.js.map +0 -1
  118. package/dist/proto/topic_only_message.d.ts +0 -10
  119. package/dist/proto/topic_only_message.js +0 -46
  120. package/dist/proto/topic_only_message.js.map +0 -1
  121. package/src/lib/multiaddr_to_peer_info.ts +0 -17
  122. package/src/lib/peer_discovery_static_list.ts +0 -118
  123. package/src/lib/select_connection.ts +0 -24
  124. package/src/lib/select_peer.ts +0 -77
  125. package/src/proto/filter.ts +0 -602
  126. package/src/proto/light_push.ts +0 -526
  127. package/src/proto/message.ts +0 -304
  128. package/src/proto/store.ts +0 -844
  129. 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 { multiaddr } from "@multiformats/multiaddr";
7
- import type { Waku } from "@waku/interfaces";
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, WakuFilter } from "./waku_filter";
13
- import { LightPushCodec, WakuLightPush } from "./waku_light_push";
14
- import { EncoderV0 } from "./waku_message/version_0";
15
- import { WakuRelay } from "./waku_relay";
16
- import { RelayCodecs, RelayPingContentTopic } from "./waku_relay/constants";
17
- import * as relayConstants from "./waku_relay/constants";
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?: WakuRelay;
45
- public store?: WakuStore;
46
- public filter?: WakuFilter;
47
- public lightPush?: WakuLightPush;
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?: WakuStore,
60
- lightPush?: WakuLightPush,
61
- filter?: WakuFilter
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
- if (isWakuRelay(libp2p.pubsub)) {
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; Default to Relay
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 ?? [Protocols.Relay];
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
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
135
- // @ts-ignore: new Multiaddr is not backward compatible
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
- const addresses = multiaddrs.map((addr: Multiaddr | string) => {
153
- if (typeof addr === "string") {
154
- return multiaddr(addr);
155
- } else {
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 = new EncoderV0(RelayPingContentTopic);
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 isWakuRelay(pubsub: PubSub): pubsub is WakuRelay {
274
+ function isRelay(pubsub: PubSub): pubsub is IRelay {
248
275
  if (pubsub) {
249
276
  try {
250
277
  return pubsub.multicodecs.includes(