@waku/core 0.0.26 → 0.0.28-070b625.0

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 (66) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/bundle/{base_protocol-pDODy0G6.js → base_protocol-D0Zdzb-v.js} +134 -89
  3. package/bundle/{browser-mTOOnVZp.js → browser-DoQRY-an.js} +518 -712
  4. package/bundle/{index-cmONXM-V.js → index-BJwgMx4y.js} +116 -88
  5. package/bundle/index.js +2967 -21667
  6. package/bundle/lib/base_protocol.js +3 -3
  7. package/bundle/lib/message/version_0.js +3 -3
  8. package/bundle/lib/predefined_bootstrap_nodes.js +17 -17
  9. package/bundle/version_0-C6o0DvNW.js +4055 -0
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/index.d.ts +3 -6
  12. package/dist/index.js +3 -6
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/base_protocol.d.ts +15 -13
  15. package/dist/lib/base_protocol.js +35 -22
  16. package/dist/lib/base_protocol.js.map +1 -1
  17. package/dist/lib/connection_manager.d.ts +2 -2
  18. package/dist/lib/connection_manager.js +16 -6
  19. package/dist/lib/connection_manager.js.map +1 -1
  20. package/dist/lib/filter/index.d.ts +1 -1
  21. package/dist/lib/filter/index.js +144 -82
  22. package/dist/lib/filter/index.js.map +1 -1
  23. package/dist/lib/filterPeers.d.ts +8 -5
  24. package/dist/lib/filterPeers.js +12 -5
  25. package/dist/lib/filterPeers.js.map +1 -1
  26. package/dist/lib/keep_alive_manager.d.ts +2 -3
  27. package/dist/lib/keep_alive_manager.js.map +1 -1
  28. package/dist/lib/light_push/index.d.ts +12 -2
  29. package/dist/lib/light_push/index.js +80 -80
  30. package/dist/lib/light_push/index.js.map +1 -1
  31. package/dist/lib/message/version_0.js +1 -1
  32. package/dist/lib/message/version_0.js.map +1 -1
  33. package/dist/lib/metadata/index.d.ts +2 -2
  34. package/dist/lib/metadata/index.js +58 -16
  35. package/dist/lib/metadata/index.js.map +1 -1
  36. package/dist/lib/predefined_bootstrap_nodes.d.ts +11 -11
  37. package/dist/lib/predefined_bootstrap_nodes.js +16 -16
  38. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
  39. package/dist/lib/store/history_rpc.js +1 -1
  40. package/dist/lib/store/history_rpc.js.map +1 -1
  41. package/dist/lib/store/index.d.ts +14 -6
  42. package/dist/lib/store/index.js +51 -235
  43. package/dist/lib/store/index.js.map +1 -1
  44. package/dist/lib/stream_manager.d.ts +2 -2
  45. package/dist/lib/stream_manager.js.map +1 -1
  46. package/dist/lib/wait_for_remote_peer.d.ts +1 -1
  47. package/dist/lib/wait_for_remote_peer.js +42 -10
  48. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  49. package/package.json +1 -127
  50. package/src/index.ts +3 -7
  51. package/src/lib/base_protocol.ts +57 -37
  52. package/src/lib/connection_manager.ts +17 -10
  53. package/src/lib/filter/index.ts +234 -136
  54. package/src/lib/filterPeers.ts +15 -7
  55. package/src/lib/keep_alive_manager.ts +2 -3
  56. package/src/lib/light_push/index.ts +104 -124
  57. package/src/lib/metadata/index.ts +92 -30
  58. package/src/lib/predefined_bootstrap_nodes.ts +22 -22
  59. package/src/lib/store/index.ts +79 -344
  60. package/src/lib/stream_manager.ts +2 -3
  61. package/src/lib/wait_for_remote_peer.ts +68 -12
  62. package/bundle/version_0-LQTFNC7k.js +0 -5008
  63. package/dist/lib/waku.d.ts +0 -57
  64. package/dist/lib/waku.js +0 -130
  65. package/dist/lib/waku.js.map +0 -1
  66. package/src/lib/waku.ts +0 -214
@@ -1,29 +1,10 @@
1
- import { i as identityBase, j as base2, k as base8, l as base10, m as base16, n as base32, o as base36, p as base58, q as base64, r as base256emoji, s as debug } from './browser-mTOOnVZp.js';
2
-
3
- // @ts-check
4
-
5
-
6
- const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
7
-
8
- /**
9
- * To guarantee Uint8Array semantics, convert nodejs Buffers
10
- * into vanilla Uint8Arrays
11
- */
12
- function asUint8Array(buf) {
13
- if (globalThis.Buffer != null) {
14
- return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
15
- }
16
- return buf;
17
- }
1
+ import { i as identityBase, c as base2, d as base8, e as base10, f as base16, h as base32, j as base36, k as base58, l as base64, m as base256emoji, n as debug } from './browser-DoQRY-an.js';
18
2
 
19
3
  /**
20
4
  * Returns a `Uint8Array` of the requested size. Referenced memory will
21
5
  * be initialized to 0.
22
6
  */
23
7
  function alloc(size = 0) {
24
- if (globalThis.Buffer?.alloc != null) {
25
- return asUint8Array(globalThis.Buffer.alloc(size));
26
- }
27
8
  return new Uint8Array(size);
28
9
  }
29
10
  /**
@@ -32,12 +13,11 @@ function alloc(size = 0) {
32
13
  * overwrite every value in the returned `Uint8Array`.
33
14
  */
34
15
  function allocUnsafe(size = 0) {
35
- if (globalThis.Buffer?.allocUnsafe != null) {
36
- return asUint8Array(globalThis.Buffer.allocUnsafe(size));
37
- }
38
16
  return new Uint8Array(size);
39
17
  }
40
18
 
19
+ const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
20
+
41
21
  function createCodec(name, prefix, encode, decode) {
42
22
  return {
43
23
  name,
@@ -83,25 +63,6 @@ const BASES = {
83
63
  ...bases
84
64
  };
85
65
 
86
- /**
87
- * Turns a `Uint8Array` into a string.
88
- *
89
- * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
90
- *
91
- * Also `ascii` which is similar to node's 'binary' encoding.
92
- */
93
- function toString(array, encoding = 'utf8') {
94
- const base = BASES[encoding];
95
- if (base == null) {
96
- throw new Error(`Unsupported encoding "${encoding}"`);
97
- }
98
- if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
99
- return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8');
100
- }
101
- // strip multibase prefix
102
- return base.encoder.encode(array).substring(1);
103
- }
104
-
105
66
  /**
106
67
  * Create a `Uint8Array` from the passed string
107
68
  *
@@ -114,23 +75,20 @@ function fromString(string, encoding = 'utf8') {
114
75
  if (base == null) {
115
76
  throw new Error(`Unsupported encoding "${encoding}"`);
116
77
  }
117
- if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
118
- return asUint8Array(globalThis.Buffer.from(string, 'utf-8'));
119
- }
120
78
  // add multibase prefix
121
79
  return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
122
80
  }
123
81
 
124
- /**
125
- * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
126
- */
127
- const DefaultPubsubTopic = "/waku/2/default-waku/proto";
128
-
82
+ // copied from utils
83
+ function isBytes(a) {
84
+ return (a instanceof Uint8Array ||
85
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
86
+ }
129
87
  function bytes(b, ...lengths) {
130
- if (!(b instanceof Uint8Array))
131
- throw new Error('Expected Uint8Array');
88
+ if (!isBytes(b))
89
+ throw new Error('Uint8Array expected');
132
90
  if (lengths.length > 0 && !lengths.includes(b.length))
133
- throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
91
+ throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
134
92
  }
135
93
  function exists(instance, checkFinished = true) {
136
94
  if (instance.destroyed)
@@ -152,17 +110,12 @@ function output(out, instance) {
152
110
  // For node.js, package.json#exports field mapping rewrites import
153
111
  // from `crypto` to `cryptoNode`, which imports native module.
154
112
  // Makes the utils un-importable in browsers without a bundler.
155
- // Once node.js 18 is deprecated, we can just drop the import.
156
- const u8a = (a) => a instanceof Uint8Array;
113
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
157
114
  // Cast array to view
158
115
  const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
159
116
  // The rotate right (circular right shift) operation for uint32
160
117
  const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
161
- // big-endian hardware is rare. Just in case someone still decides to run hashes:
162
- // early-throw an error because we don't support BE yet.
163
- const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
164
- if (!isLE)
165
- throw new Error('Non little-endian hardware is not supported');
118
+ new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
166
119
  /**
167
120
  * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
168
121
  */
@@ -179,8 +132,7 @@ function utf8ToBytes$1(str) {
179
132
  function toBytes(data) {
180
133
  if (typeof data === 'string')
181
134
  data = utf8ToBytes$1(data);
182
- if (!u8a(data))
183
- throw new Error(`expected Uint8Array, got ${typeof data}`);
135
+ bytes(data);
184
136
  return data;
185
137
  }
186
138
  // For runtime check if class implements interface
@@ -212,8 +164,15 @@ function setBigUint64(view, byteOffset, value, isLE) {
212
164
  view.setUint32(byteOffset + h, wh, isLE);
213
165
  view.setUint32(byteOffset + l, wl, isLE);
214
166
  }
215
- // Base SHA2 class (RFC 6234)
216
- class SHA2 extends Hash {
167
+ // Choice: a ? b : c
168
+ const Chi = (a, b, c) => (a & b) ^ (~a & c);
169
+ // Majority function, true if any two inpust is true
170
+ const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
171
+ /**
172
+ * Merkle-Damgard hash construction base class.
173
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
174
+ */
175
+ class HashMD extends Hash {
217
176
  constructor(blockLen, outputLen, padOffset, isLE) {
218
177
  super();
219
178
  this.blockLen = blockLen;
@@ -265,7 +224,8 @@ class SHA2 extends Hash {
265
224
  // append the bit '1' to the message
266
225
  buffer[pos++] = 0b10000000;
267
226
  this.buffer.subarray(pos).fill(0);
268
- // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
227
+ // we have less than padOffset left in buffer, so we cannot put length in
228
+ // current block, need process it and pad again
269
229
  if (this.padOffset > blockLen - pos) {
270
230
  this.process(view, 0);
271
231
  pos = 0;
@@ -313,10 +273,6 @@ class SHA2 extends Hash {
313
273
 
314
274
  // SHA2-256 need to try 2^128 hashes to execute birthday attack.
315
275
  // BTC network is doing 2^67 hashes/sec as per early 2023.
316
- // Choice: a ? b : c
317
- const Chi = (a, b, c) => (a & b) ^ (~a & c);
318
- // Majority function, true if any two inpust is true
319
- const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
320
276
  // Round constants:
321
277
  // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
322
278
  // prettier-ignore
@@ -330,27 +286,28 @@ const SHA256_K = /* @__PURE__ */ new Uint32Array([
330
286
  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
331
287
  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
332
288
  ]);
333
- // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
289
+ // Initial state:
290
+ // first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
334
291
  // prettier-ignore
335
- const IV = /* @__PURE__ */ new Uint32Array([
292
+ const SHA256_IV = /* @__PURE__ */ new Uint32Array([
336
293
  0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
337
294
  ]);
338
295
  // Temporary buffer, not used to store anything between runs
339
296
  // Named this way because it matches specification.
340
297
  const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
341
- class SHA256 extends SHA2 {
298
+ class SHA256 extends HashMD {
342
299
  constructor() {
343
300
  super(64, 32, 8, false);
344
301
  // We cannot use array here since array allows indexing by variable
345
302
  // which means optimizer/compiler cannot use registers.
346
- this.A = IV[0] | 0;
347
- this.B = IV[1] | 0;
348
- this.C = IV[2] | 0;
349
- this.D = IV[3] | 0;
350
- this.E = IV[4] | 0;
351
- this.F = IV[5] | 0;
352
- this.G = IV[6] | 0;
353
- this.H = IV[7] | 0;
303
+ this.A = SHA256_IV[0] | 0;
304
+ this.B = SHA256_IV[1] | 0;
305
+ this.C = SHA256_IV[2] | 0;
306
+ this.D = SHA256_IV[3] | 0;
307
+ this.E = SHA256_IV[4] | 0;
308
+ this.F = SHA256_IV[5] | 0;
309
+ this.G = SHA256_IV[6] | 0;
310
+ this.H = SHA256_IV[7] | 0;
354
311
  }
355
312
  get() {
356
313
  const { A, B, C, D, E, F, G, H } = this;
@@ -419,6 +376,31 @@ class SHA256 extends SHA2 {
419
376
  */
420
377
  const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
421
378
 
379
+ /**
380
+ * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
381
+ */
382
+ const DefaultPubsubTopic = "/waku/2/default-waku/proto";
383
+ /**
384
+ * The default cluster ID for The Waku Network
385
+ */
386
+ const DEFAULT_CLUSTER_ID = 1;
387
+
388
+ /**
389
+ * Turns a `Uint8Array` into a string.
390
+ *
391
+ * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
392
+ *
393
+ * Also `ascii` which is similar to node's 'binary' encoding.
394
+ */
395
+ function toString(array, encoding = 'utf8') {
396
+ const base = BASES[encoding];
397
+ if (base == null) {
398
+ throw new Error(`Unsupported encoding "${encoding}"`);
399
+ }
400
+ // strip multibase prefix
401
+ return base.encoder.encode(array).substring(1);
402
+ }
403
+
422
404
  /**
423
405
  * Decode byte array to utf-8 string.
424
406
  */
@@ -447,9 +429,7 @@ const singleShardInfoToPubsubTopic = (shardInfo) => {
447
429
  return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`;
448
430
  };
449
431
  const shardInfoToPubsubTopics = (shardInfo) => {
450
- if (shardInfo.clusterId === undefined)
451
- throw new Error("Cluster ID must be specified");
452
- if ("contentTopics" in shardInfo) {
432
+ if ("contentTopics" in shardInfo && shardInfo.contentTopics) {
453
433
  // Autosharding: explicitly defined content topics
454
434
  return Array.from(new Set(shardInfo.contentTopics.map((contentTopic) => contentTopicToPubsubTopic(contentTopic, shardInfo.clusterId))));
455
435
  }
@@ -457,14 +437,17 @@ const shardInfoToPubsubTopics = (shardInfo) => {
457
437
  // Static sharding
458
438
  if (shardInfo.shards === undefined)
459
439
  throw new Error("Invalid shard");
460
- return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId}/${index}`)));
440
+ return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${index}`)));
461
441
  }
462
- else {
442
+ else if ("application" in shardInfo && "version" in shardInfo) {
463
443
  // Autosharding: single shard from application and version
464
444
  return [
465
- contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`)
445
+ contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`, shardInfo.clusterId)
466
446
  ];
467
447
  }
448
+ else {
449
+ throw new Error("Missing required configuration in shard parameters");
450
+ }
468
451
  };
469
452
  const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
470
453
  const parts = pubsubTopics.split("/");
@@ -482,6 +465,8 @@ const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
482
465
  shard
483
466
  };
484
467
  };
468
+ //TODO: move part of BaseProtocol instead of utils
469
+ // return `ProtocolError.TOPIC_NOT_CONFIGURED` instead of throwing
485
470
  function ensurePubsubTopicIsConfigured(pubsubTopic, configuredTopics) {
486
471
  if (!configuredTopics.includes(pubsubTopic)) {
487
472
  throw new Error(`Pubsub topic ${pubsubTopic} has not been configured on this instance. Configured topics are: ${configuredTopics}. Please update your configuration by passing in the topic during Waku node instantiation.`);
@@ -544,7 +529,7 @@ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
544
529
  const dataview = new DataView(digest.buffer.slice(-8));
545
530
  return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
546
531
  }
547
- function contentTopicToPubsubTopic(contentTopic, clusterId = 1, networkShards = 8) {
532
+ function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
548
533
  const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
549
534
  return `/waku/2/rs/${clusterId}/${shardIndex}`;
550
535
  }
@@ -563,6 +548,49 @@ function determinePubsubTopic(contentTopic, pubsubTopicShardInfo = DefaultPubsub
563
548
  : DefaultPubsubTopic;
564
549
  }
565
550
  }
551
+ /**
552
+ * Validates sharding configuration and sets defaults where possible.
553
+ * @returns Validated sharding parameters, with any missing values set to defaults
554
+ */
555
+ const ensureShardingConfigured = (shardInfo) => {
556
+ const clusterId = shardInfo.clusterId ?? DEFAULT_CLUSTER_ID;
557
+ const shards = "shards" in shardInfo ? shardInfo.shards : [];
558
+ const contentTopics = "contentTopics" in shardInfo ? shardInfo.contentTopics : [];
559
+ const [application, version] = "application" in shardInfo && "version" in shardInfo
560
+ ? [shardInfo.application, shardInfo.version]
561
+ : [undefined, undefined];
562
+ const isShardsConfigured = shards && shards.length > 0;
563
+ const isContentTopicsConfigured = contentTopics && contentTopics.length > 0;
564
+ const isApplicationVersionConfigured = application && version;
565
+ if (isShardsConfigured) {
566
+ return {
567
+ shardingParams: { clusterId, shards },
568
+ shardInfo: { clusterId, shards },
569
+ pubsubTopics: shardInfoToPubsubTopics({ clusterId, shards })
570
+ };
571
+ }
572
+ if (isContentTopicsConfigured) {
573
+ const pubsubTopics = Array.from(new Set(contentTopics.map((topic) => contentTopicToPubsubTopic(topic, clusterId))));
574
+ const shards = Array.from(new Set(contentTopics.map((topic) => contentTopicToShardIndex(topic))));
575
+ return {
576
+ shardingParams: { clusterId, contentTopics },
577
+ shardInfo: { clusterId, shards },
578
+ pubsubTopics
579
+ };
580
+ }
581
+ if (isApplicationVersionConfigured) {
582
+ const pubsubTopic = contentTopicToPubsubTopic(`/${application}/${version}/default/default`, clusterId);
583
+ return {
584
+ shardingParams: { clusterId, application, version },
585
+ shardInfo: {
586
+ clusterId,
587
+ shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard]
588
+ },
589
+ pubsubTopics: [pubsubTopic]
590
+ };
591
+ }
592
+ throw new Error("Missing minimum required configuration options for static sharding or autosharding.");
593
+ };
566
594
 
567
595
  const APP_NAME = "waku";
568
596
  class Logger {
@@ -592,4 +620,4 @@ class Logger {
592
620
  }
593
621
  }
594
622
 
595
- export { DefaultPubsubTopic as D, Logger as L, allocUnsafe as a, asUint8Array as b, bases as c, alloc as d, singleShardInfoToPubsubTopic as e, fromString as f, ensurePubsubTopicIsConfigured as g, sha256 as h, concat as i, determinePubsubTopic as j, bytesToUtf8 as k, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, toString as t, utf8ToBytes as u };
623
+ export { DefaultPubsubTopic as D, Logger as L, allocUnsafe as a, bytesToUtf8 as b, concat as c, alloc as d, singleShardInfoToPubsubTopic as e, ensurePubsubTopicIsConfigured as f, shardInfoToPubsubTopics as g, fromString as h, determinePubsubTopic as i, ensureShardingConfigured as j, pubsubTopicToSingleShardInfo as p, sha256 as s, utf8ToBytes as u };