@waku/core 0.0.4 → 0.0.6

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 (78) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/bundle/{index-a67d7136.js → index-925587a4.js} +1696 -113
  3. package/bundle/index.js +12942 -15253
  4. package/bundle/lib/predefined_bootstrap_nodes.js +24 -4
  5. package/bundle/lib/wait_for_remote_peer.js +4 -6
  6. package/bundle/lib/waku_message/topic_only_message.js +2 -3
  7. package/bundle/lib/waku_message/version_0.js +17 -4
  8. package/bundle/{message-049c8b67.js → message-70dbb19b.js} +1419 -753
  9. package/bundle/{topic_only_message-5ad3a869.js → topic_only_message-a80fe8b2.js} +2 -2
  10. package/dist/index.d.ts +5 -4
  11. package/dist/index.js +5 -4
  12. package/dist/index.js.map +1 -1
  13. package/dist/lib/predefined_bootstrap_nodes.d.ts +1 -2
  14. package/dist/lib/predefined_bootstrap_nodes.js +0 -2
  15. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
  16. package/dist/lib/to_proto_message.js +1 -0
  17. package/dist/lib/to_proto_message.js.map +1 -1
  18. package/dist/lib/wait_for_remote_peer.js +2 -2
  19. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  20. package/dist/lib/waku.d.ts +16 -15
  21. package/dist/lib/waku.js +31 -33
  22. package/dist/lib/waku.js.map +1 -1
  23. package/dist/lib/waku_filter/index.d.ts +10 -35
  24. package/dist/lib/waku_filter/index.js +13 -7
  25. package/dist/lib/waku_filter/index.js.map +1 -1
  26. package/dist/lib/waku_light_push/index.d.ts +8 -24
  27. package/dist/lib/waku_light_push/index.js +12 -6
  28. package/dist/lib/waku_light_push/index.js.map +1 -1
  29. package/dist/lib/waku_message/topic_only_message.d.ts +3 -2
  30. package/dist/lib/waku_message/topic_only_message.js +1 -0
  31. package/dist/lib/waku_message/topic_only_message.js.map +1 -1
  32. package/dist/lib/waku_message/version_0.d.ts +5 -2
  33. package/dist/lib/waku_message/version_0.js +9 -2
  34. package/dist/lib/waku_message/version_0.js.map +1 -1
  35. package/dist/lib/waku_relay/index.d.ts +3 -45
  36. package/dist/lib/waku_relay/index.js +6 -3
  37. package/dist/lib/waku_relay/index.js.map +1 -1
  38. package/dist/lib/waku_store/index.d.ts +11 -71
  39. package/dist/lib/waku_store/index.js +34 -10
  40. package/dist/lib/waku_store/index.js.map +1 -1
  41. package/dist/proto/filter.d.ts +1 -0
  42. package/dist/proto/filter.js +7 -0
  43. package/dist/proto/filter.js.map +1 -1
  44. package/dist/proto/light_push.d.ts +1 -0
  45. package/dist/proto/light_push.js +7 -0
  46. package/dist/proto/light_push.js.map +1 -1
  47. package/dist/proto/message.d.ts +1 -0
  48. package/dist/proto/message.js +7 -0
  49. package/dist/proto/message.js.map +1 -1
  50. package/dist/proto/store.d.ts +1 -0
  51. package/dist/proto/store.js +7 -0
  52. package/dist/proto/store.js.map +1 -1
  53. package/package.json +11 -13
  54. package/src/index.ts +10 -4
  55. package/src/lib/predefined_bootstrap_nodes.ts +1 -5
  56. package/src/lib/to_proto_message.ts +1 -0
  57. package/src/lib/wait_for_remote_peer.ts +2 -2
  58. package/src/lib/waku.ts +51 -47
  59. package/src/lib/waku_filter/index.ts +28 -7
  60. package/src/lib/waku_light_push/index.ts +26 -6
  61. package/src/lib/waku_message/topic_only_message.ts +4 -2
  62. package/src/lib/waku_message/version_0.ts +8 -2
  63. package/src/lib/waku_relay/index.ts +13 -3
  64. package/src/lib/waku_store/index.ts +63 -12
  65. package/src/proto/filter.ts +9 -0
  66. package/src/proto/light_push.ts +9 -0
  67. package/src/proto/message.ts +9 -0
  68. package/src/proto/store.ts +9 -0
  69. package/bundle/browser-1e1a2f27.js +0 -722
  70. package/bundle/events-158407bb.js +0 -1929
  71. package/bundle/index-64ce43f0.js +0 -69
  72. package/bundle/index-8710041d.js +0 -2962
  73. package/bundle/lib/peer_discovery_static_list.js +0 -89
  74. package/bundle/random_subset-75d1c511.js +0 -26
  75. package/dist/lib/peer_discovery_static_list.d.ts +0 -44
  76. package/dist/lib/peer_discovery_static_list.js +0 -72
  77. package/dist/lib/peer_discovery_static_list.js.map +0 -1
  78. package/src/lib/peer_discovery_static_list.ts +0 -118
@@ -1,7 +1,10 @@
1
+ import { ConnectionManager } from "@libp2p/interface-connection-manager";
1
2
  import type { PeerId } from "@libp2p/interface-peer-id";
2
3
  import type { Peer } from "@libp2p/interface-peer-store";
4
+ import type { PeerStore } from "@libp2p/interface-peer-store";
3
5
  import type {
4
6
  Encoder,
7
+ LightPush,
5
8
  Message,
6
9
  ProtocolOptions,
7
10
  SendResult,
@@ -10,7 +13,6 @@ import debug from "debug";
10
13
  import all from "it-all";
11
14
  import * as lp from "it-length-prefixed";
12
15
  import { pipe } from "it-pipe";
13
- import { Libp2p } from "libp2p";
14
16
  import { Uint8ArrayList } from "uint8arraylist";
15
17
 
16
18
  import { PushResponse } from "../../proto/light_push";
@@ -29,6 +31,11 @@ const log = debug("waku:light-push");
29
31
  export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
30
32
  export { PushResponse };
31
33
 
34
+ export interface LightPushComponents {
35
+ peerStore: PeerStore;
36
+ connectionManager: ConnectionManager;
37
+ }
38
+
32
39
  export interface CreateOptions {
33
40
  /**
34
41
  * The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
@@ -44,10 +51,10 @@ export interface CreateOptions {
44
51
  /**
45
52
  * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
46
53
  */
47
- export class WakuLightPush {
54
+ class WakuLightPush implements LightPush {
48
55
  pubSubTopic: string;
49
56
 
50
- constructor(public libp2p: Libp2p, options?: CreateOptions) {
57
+ constructor(public components: LightPushComponents, options?: CreateOptions) {
51
58
  this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
52
59
  }
53
60
 
@@ -59,7 +66,7 @@ export class WakuLightPush {
59
66
  const pubSubTopic = opts?.pubSubTopic ? opts.pubSubTopic : this.pubSubTopic;
60
67
 
61
68
  const res = await selectPeerForProtocol(
62
- this.libp2p.peerStore,
69
+ this.components.peerStore,
63
70
  [LightPushCodec],
64
71
  opts?.peerId
65
72
  );
@@ -69,7 +76,9 @@ export class WakuLightPush {
69
76
  }
70
77
  const { peer } = res;
71
78
 
72
- const connections = this.libp2p.connectionManager.getConnections(peer.id);
79
+ const connections = this.components.connectionManager.getConnections(
80
+ peer.id
81
+ );
73
82
  const connection = selectConnection(connections);
74
83
 
75
84
  if (!connection) throw "Failed to get a connection to the peer";
@@ -123,7 +132,7 @@ export class WakuLightPush {
123
132
  * peers.
124
133
  */
125
134
  async peers(): Promise<Peer[]> {
126
- return getPeersForProtocol(this.libp2p.peerStore, [LightPushCodec]);
135
+ return getPeersForProtocol(this.components.peerStore, [LightPushCodec]);
127
136
  }
128
137
 
129
138
  /**
@@ -134,4 +143,15 @@ export class WakuLightPush {
134
143
  async randomPeer(): Promise<Peer | undefined> {
135
144
  return selectRandomPeer(await this.peers());
136
145
  }
146
+
147
+ get peerStore(): PeerStore {
148
+ return this.components.peerStore;
149
+ }
150
+ }
151
+
152
+ export function wakuLightPush(
153
+ init: Partial<CreateOptions> = {}
154
+ ): (components: LightPushComponents) => LightPush {
155
+ return (components: LightPushComponents) =>
156
+ new WakuLightPush(components, init);
137
157
  }
@@ -1,14 +1,15 @@
1
- import type { Decoder, Message, ProtoMessage } from "@waku/interfaces";
1
+ import type { DecodedMessage, Decoder, ProtoMessage } from "@waku/interfaces";
2
2
  import debug from "debug";
3
3
 
4
4
  import * as proto from "../../proto/topic_only_message";
5
5
 
6
6
  const log = debug("waku:message:topic-only");
7
7
 
8
- export class TopicOnlyMessage implements Message {
8
+ export class TopicOnlyMessage implements DecodedMessage {
9
9
  public payload: undefined;
10
10
  public rateLimitProof: undefined;
11
11
  public timestamp: undefined;
12
+ public ephemeral: undefined;
12
13
 
13
14
  constructor(private proto: proto.TopicOnlyMessage) {}
14
15
 
@@ -29,6 +30,7 @@ export class TopicOnlyDecoder implements Decoder<TopicOnlyMessage> {
29
30
  rateLimitProof: undefined,
30
31
  timestamp: undefined,
31
32
  version: undefined,
33
+ ephemeral: undefined,
32
34
  });
33
35
  }
34
36
 
@@ -26,6 +26,10 @@ export class MessageV0 implements DecodedMessage {
26
26
  return;
27
27
  }
28
28
 
29
+ get ephemeral(): boolean {
30
+ return Boolean(this.proto.ephemeral);
31
+ }
32
+
29
33
  get payload(): Uint8Array | undefined {
30
34
  return this._rawPayload;
31
35
  }
@@ -68,7 +72,7 @@ export class MessageV0 implements DecodedMessage {
68
72
  }
69
73
 
70
74
  export class EncoderV0 implements Encoder {
71
- constructor(public contentTopic: string) {}
75
+ constructor(public contentTopic: string, public ephemeral: boolean = false) {}
72
76
 
73
77
  async toWire(message: Partial<Message>): Promise<Uint8Array> {
74
78
  return proto.WakuMessage.encode(await this.toProtoObj(message));
@@ -83,12 +87,13 @@ export class EncoderV0 implements Encoder {
83
87
  contentTopic: this.contentTopic,
84
88
  timestamp: BigInt(timestamp.valueOf()) * OneMillion,
85
89
  rateLimitProof: message.rateLimitProof,
90
+ ephemeral: this.ephemeral,
86
91
  };
87
92
  }
88
93
  }
89
94
 
90
95
  export class DecoderV0 implements Decoder<MessageV0> {
91
- constructor(public contentTopic: string) {}
96
+ constructor(public contentTopic: string, public ephemeral: boolean = false) {}
92
97
 
93
98
  fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
94
99
  const protoMessage = proto.WakuMessage.decode(bytes);
@@ -99,6 +104,7 @@ export class DecoderV0 implements Decoder<MessageV0> {
99
104
  version: protoMessage.version ?? undefined,
100
105
  timestamp: protoMessage.timestamp ?? undefined,
101
106
  rateLimitProof: protoMessage.rateLimitProof ?? undefined,
107
+ ephemeral: protoMessage.ephemeral ?? false,
102
108
  });
103
109
  }
104
110
 
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  GossipSub,
3
+ GossipSubComponents,
3
4
  GossipsubMessage,
4
5
  GossipsubOpts,
5
6
  } from "@chainsafe/libp2p-gossipsub";
@@ -55,7 +56,7 @@ export type CreateOptions = {
55
56
  *
56
57
  * @implements {require('libp2p-interfaces/src/pubsub')}
57
58
  */
58
- export class WakuRelay extends GossipSub implements Relay {
59
+ class WakuRelay extends GossipSub implements Relay {
59
60
  pubSubTopic: string;
60
61
  defaultDecoder: Decoder<DecodedMessage>;
61
62
  public static multicodec: string = constants.RelayCodecs[0];
@@ -66,13 +67,16 @@ export class WakuRelay extends GossipSub implements Relay {
66
67
  */
67
68
  public observers: Map<string, Set<Observer<any>>>;
68
69
 
69
- constructor(options?: Partial<CreateOptions>) {
70
+ constructor(
71
+ components: GossipSubComponents,
72
+ options?: Partial<CreateOptions>
73
+ ) {
70
74
  options = Object.assign(options ?? {}, {
71
75
  // Ensure that no signature is included nor expected in the messages.
72
76
  globalSignaturePolicy: SignaturePolicy.StrictNoSign,
73
77
  fallbackToFloodsub: false,
74
78
  });
75
- super(options);
79
+ super(components, options);
76
80
  this.multicodecs = constants.RelayCodecs;
77
81
 
78
82
  this.observers = new Map();
@@ -188,3 +192,9 @@ export class WakuRelay extends GossipSub implements Relay {
188
192
  }
189
193
 
190
194
  WakuRelay.multicodec = constants.RelayCodecs[constants.RelayCodecs.length - 1];
195
+
196
+ export function wakuRelay(
197
+ init: Partial<CreateOptions> = {}
198
+ ): (components: GossipSubComponents) => Relay {
199
+ return (components: GossipSubComponents) => new WakuRelay(components, init);
200
+ }
@@ -1,12 +1,14 @@
1
1
  import type { Connection } from "@libp2p/interface-connection";
2
+ import type { ConnectionManager } from "@libp2p/interface-connection-manager";
2
3
  import type { PeerId } from "@libp2p/interface-peer-id";
3
- import { Peer } from "@libp2p/interface-peer-store";
4
- import { DecodedMessage, Decoder } from "@waku/interfaces";
4
+ import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
5
+ import { sha256 } from "@noble/hashes/sha256";
6
+ import { concat, utf8ToBytes } from "@waku/byte-utils";
7
+ import { DecodedMessage, Decoder, Index, Store } from "@waku/interfaces";
5
8
  import debug from "debug";
6
9
  import all from "it-all";
7
10
  import * as lp from "it-length-prefixed";
8
11
  import { pipe } from "it-pipe";
9
- import { Libp2p } from "libp2p";
10
12
  import { Uint8ArrayList } from "uint8arraylist";
11
13
 
12
14
  import * as proto from "../../proto/store";
@@ -27,6 +29,11 @@ export const DefaultPageSize = 10;
27
29
 
28
30
  export { PageDirection };
29
31
 
32
+ export interface StoreComponents {
33
+ peerStore: PeerStore;
34
+ connectionManager: ConnectionManager;
35
+ }
36
+
30
37
  export interface CreateOptions {
31
38
  /**
32
39
  * The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
@@ -75,6 +82,10 @@ export interface QueryOptions {
75
82
  * Retrieve messages with a timestamp within the provided values.
76
83
  */
77
84
  timeFilter?: TimeFilter;
85
+ /**
86
+ * Cursor as an index to start a query from.
87
+ */
88
+ cursor?: Index;
78
89
  }
79
90
 
80
91
  /**
@@ -82,10 +93,10 @@ export interface QueryOptions {
82
93
  *
83
94
  * The Waku Store protocol can be used to retrieved historical messages.
84
95
  */
85
- export class WakuStore {
96
+ class WakuStore implements Store {
86
97
  pubSubTopic: string;
87
98
 
88
- constructor(public libp2p: Libp2p, options?: CreateOptions) {
99
+ constructor(public components: StoreComponents, options?: CreateOptions) {
89
100
  this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
90
101
  }
91
102
 
@@ -227,12 +238,12 @@ export class WakuStore {
227
238
  );
228
239
 
229
240
  log("Querying history with the following options", {
230
- peerId: options?.peerId?.toString(),
231
241
  ...options,
242
+ peerId: options?.peerId?.toString(),
232
243
  });
233
244
 
234
245
  const res = await selectPeerForProtocol(
235
- this.libp2p.peerStore,
246
+ this.components.peerStore,
236
247
  [StoreCodec],
237
248
  options?.peerId
238
249
  );
@@ -242,7 +253,9 @@ export class WakuStore {
242
253
  }
243
254
  const { peer, protocol } = res;
244
255
 
245
- const connections = this.libp2p.connectionManager.getConnections(peer.id);
256
+ const connections = this.components.connectionManager.getConnections(
257
+ peer.id
258
+ );
246
259
  const connection = selectConnection(connections);
247
260
 
248
261
  if (!connection) throw "Failed to get a connection to the peer";
@@ -251,7 +264,8 @@ export class WakuStore {
251
264
  connection,
252
265
  protocol,
253
266
  queryOpts,
254
- decodersAsMap
267
+ decodersAsMap,
268
+ options?.cursor
255
269
  )) {
256
270
  yield messages;
257
271
  }
@@ -262,7 +276,11 @@ export class WakuStore {
262
276
  * store protocol. Waku may or may not be currently connected to these peers.
263
277
  */
264
278
  async peers(): Promise<Peer[]> {
265
- return getPeersForProtocol(this.libp2p.peerStore, [StoreCodec]);
279
+ return getPeersForProtocol(this.components.peerStore, [StoreCodec]);
280
+ }
281
+
282
+ get peerStore(): PeerStore {
283
+ return this.components.peerStore;
266
284
  }
267
285
  }
268
286
 
@@ -270,7 +288,8 @@ async function* paginate<T extends DecodedMessage>(
270
288
  connection: Connection,
271
289
  protocol: string,
272
290
  queryOpts: Params,
273
- decoders: Map<string, Decoder<T>>
291
+ decoders: Map<string, Decoder<T>>,
292
+ cursor?: Index
274
293
  ): AsyncGenerator<Promise<T | undefined>[]> {
275
294
  if (
276
295
  queryOpts.contentTopics.toString() !==
@@ -281,7 +300,6 @@ async function* paginate<T extends DecodedMessage>(
281
300
  );
282
301
  }
283
302
 
284
- let cursor = undefined;
285
303
  while (true) {
286
304
  queryOpts = Object.assign(queryOpts, { cursor });
287
305
 
@@ -370,3 +388,36 @@ async function* paginate<T extends DecodedMessage>(
370
388
  export function isDefined<T>(msg: T | undefined): msg is T {
371
389
  return !!msg;
372
390
  }
391
+
392
+ export async function createCursor(
393
+ message: DecodedMessage,
394
+ pubsubTopic: string = DefaultPubSubTopic
395
+ ): Promise<Index> {
396
+ if (
397
+ !message ||
398
+ !message.timestamp ||
399
+ !message.payload ||
400
+ !message.contentTopic
401
+ ) {
402
+ throw new Error("Message is missing required fields");
403
+ }
404
+
405
+ const contentTopicBytes = utf8ToBytes(message.contentTopic);
406
+
407
+ const digest = sha256(concat([contentTopicBytes, message.payload]));
408
+
409
+ const messageTime = BigInt(message.timestamp.getTime()) * BigInt(1000000);
410
+
411
+ return {
412
+ digest,
413
+ pubsubTopic,
414
+ senderTime: messageTime,
415
+ receivedTime: messageTime,
416
+ };
417
+ }
418
+
419
+ export function wakuStore(
420
+ init: Partial<CreateOptions> = {}
421
+ ): (components: StoreComponents) => Store {
422
+ return (components: StoreComponents) => new WakuStore(components, init);
423
+ }
@@ -501,6 +501,7 @@ export interface WakuMessage {
501
501
  timestampDeprecated?: number;
502
502
  timestamp?: bigint;
503
503
  rateLimitProof?: RateLimitProof;
504
+ ephemeral?: boolean;
504
505
  }
505
506
 
506
507
  export namespace WakuMessage {
@@ -544,6 +545,11 @@ export namespace WakuMessage {
544
545
  RateLimitProof.codec().encode(obj.rateLimitProof, writer);
545
546
  }
546
547
 
548
+ if (obj.ephemeral != null) {
549
+ writer.uint32(248);
550
+ writer.bool(obj.ephemeral);
551
+ }
552
+
547
553
  if (opts.lengthDelimited !== false) {
548
554
  writer.ldelim();
549
555
  }
@@ -578,6 +584,9 @@ export namespace WakuMessage {
578
584
  reader.uint32()
579
585
  );
580
586
  break;
587
+ case 31:
588
+ obj.ephemeral = reader.bool();
589
+ break;
581
590
  default:
582
591
  reader.skipType(tag & 7);
583
592
  break;
@@ -425,6 +425,7 @@ export interface WakuMessage {
425
425
  timestampDeprecated?: number;
426
426
  timestamp?: bigint;
427
427
  rateLimitProof?: RateLimitProof;
428
+ ephemeral?: boolean;
428
429
  }
429
430
 
430
431
  export namespace WakuMessage {
@@ -468,6 +469,11 @@ export namespace WakuMessage {
468
469
  RateLimitProof.codec().encode(obj.rateLimitProof, writer);
469
470
  }
470
471
 
472
+ if (obj.ephemeral != null) {
473
+ writer.uint32(248);
474
+ writer.bool(obj.ephemeral);
475
+ }
476
+
471
477
  if (opts.lengthDelimited !== false) {
472
478
  writer.ldelim();
473
479
  }
@@ -502,6 +508,9 @@ export namespace WakuMessage {
502
508
  reader.uint32()
503
509
  );
504
510
  break;
511
+ case 31:
512
+ obj.ephemeral = reader.bool();
513
+ break;
505
514
  default:
506
515
  reader.skipType(tag & 7);
507
516
  break;
@@ -203,6 +203,7 @@ export interface WakuMessage {
203
203
  timestampDeprecated?: number;
204
204
  timestamp?: bigint;
205
205
  rateLimitProof?: RateLimitProof;
206
+ ephemeral?: boolean;
206
207
  }
207
208
 
208
209
  export namespace WakuMessage {
@@ -246,6 +247,11 @@ export namespace WakuMessage {
246
247
  RateLimitProof.codec().encode(obj.rateLimitProof, writer);
247
248
  }
248
249
 
250
+ if (obj.ephemeral != null) {
251
+ writer.uint32(248);
252
+ writer.bool(obj.ephemeral);
253
+ }
254
+
249
255
  if (opts.lengthDelimited !== false) {
250
256
  writer.ldelim();
251
257
  }
@@ -280,6 +286,9 @@ export namespace WakuMessage {
280
286
  reader.uint32()
281
287
  );
282
288
  break;
289
+ case 31:
290
+ obj.ephemeral = reader.bool();
291
+ break;
283
292
  default:
284
293
  reader.skipType(tag & 7);
285
294
  break;
@@ -743,6 +743,7 @@ export interface WakuMessage {
743
743
  timestampDeprecated?: number;
744
744
  timestamp?: bigint;
745
745
  rateLimitProof?: RateLimitProof;
746
+ ephemeral?: boolean;
746
747
  }
747
748
 
748
749
  export namespace WakuMessage {
@@ -786,6 +787,11 @@ export namespace WakuMessage {
786
787
  RateLimitProof.codec().encode(obj.rateLimitProof, writer);
787
788
  }
788
789
 
790
+ if (obj.ephemeral != null) {
791
+ writer.uint32(248);
792
+ writer.bool(obj.ephemeral);
793
+ }
794
+
789
795
  if (opts.lengthDelimited !== false) {
790
796
  writer.ldelim();
791
797
  }
@@ -820,6 +826,9 @@ export namespace WakuMessage {
820
826
  reader.uint32()
821
827
  );
822
828
  break;
829
+ case 31:
830
+ obj.ephemeral = reader.bool();
831
+ break;
823
832
  default:
824
833
  reader.skipType(tag & 7);
825
834
  break;