@waku/core 0.0.30-e49e728.0 → 0.0.31-ce62600.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.
- package/CHANGELOG.md +27 -0
- package/bundle/{base_protocol-DCOj0QWD.js → base_protocol-C6HnrRx8.js} +1 -2
- package/bundle/{browser-zSUobdfj.js → index-DnW8ifxc.js} +616 -5
- package/bundle/index.js +26 -23
- package/bundle/lib/base_protocol.js +2 -3
- package/bundle/lib/message/version_0.js +2 -3
- package/bundle/{version_0-TEIsGmpJ.js → version_0-DQ9xsSLk.js} +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/connection_manager.d.ts +3 -2
- package/dist/lib/connection_manager.js +16 -16
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.js +5 -1
- package/dist/lib/filter/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/connection_manager.ts +28 -28
- package/src/lib/filter/index.ts +7 -3
- package/bundle/index-BcSodzY4.js +0 -614
- package/bundle/lib/predefined_bootstrap_nodes.js +0 -81
- package/dist/lib/predefined_bootstrap_nodes.d.ts +0 -34
- package/dist/lib/predefined_bootstrap_nodes.js +0 -54
- package/dist/lib/predefined_bootstrap_nodes.js.map +0 -1
- package/src/lib/predefined_bootstrap_nodes.ts +0 -68
package/bundle/index-BcSodzY4.js
DELETED
@@ -1,614 +0,0 @@
|
|
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-zSUobdfj.js';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Returns a `Uint8Array` of the requested size. Referenced memory will
|
5
|
-
* be initialized to 0.
|
6
|
-
*/
|
7
|
-
function alloc(size = 0) {
|
8
|
-
return new Uint8Array(size);
|
9
|
-
}
|
10
|
-
/**
|
11
|
-
* Where possible returns a Uint8Array of the requested size that references
|
12
|
-
* uninitialized memory. Only use if you are certain you will immediately
|
13
|
-
* overwrite every value in the returned `Uint8Array`.
|
14
|
-
*/
|
15
|
-
function allocUnsafe(size = 0) {
|
16
|
-
return new Uint8Array(size);
|
17
|
-
}
|
18
|
-
|
19
|
-
const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
|
20
|
-
|
21
|
-
function createCodec(name, prefix, encode, decode) {
|
22
|
-
return {
|
23
|
-
name,
|
24
|
-
prefix,
|
25
|
-
encoder: {
|
26
|
-
name,
|
27
|
-
prefix,
|
28
|
-
encode
|
29
|
-
},
|
30
|
-
decoder: {
|
31
|
-
decode
|
32
|
-
}
|
33
|
-
};
|
34
|
-
}
|
35
|
-
const string = createCodec('utf8', 'u', (buf) => {
|
36
|
-
const decoder = new TextDecoder('utf8');
|
37
|
-
return 'u' + decoder.decode(buf);
|
38
|
-
}, (str) => {
|
39
|
-
const encoder = new TextEncoder();
|
40
|
-
return encoder.encode(str.substring(1));
|
41
|
-
});
|
42
|
-
const ascii = createCodec('ascii', 'a', (buf) => {
|
43
|
-
let string = 'a';
|
44
|
-
for (let i = 0; i < buf.length; i++) {
|
45
|
-
string += String.fromCharCode(buf[i]);
|
46
|
-
}
|
47
|
-
return string;
|
48
|
-
}, (str) => {
|
49
|
-
str = str.substring(1);
|
50
|
-
const buf = allocUnsafe(str.length);
|
51
|
-
for (let i = 0; i < str.length; i++) {
|
52
|
-
buf[i] = str.charCodeAt(i);
|
53
|
-
}
|
54
|
-
return buf;
|
55
|
-
});
|
56
|
-
const BASES = {
|
57
|
-
utf8: string,
|
58
|
-
'utf-8': string,
|
59
|
-
hex: bases.base16,
|
60
|
-
latin1: ascii,
|
61
|
-
ascii,
|
62
|
-
binary: ascii,
|
63
|
-
...bases
|
64
|
-
};
|
65
|
-
|
66
|
-
/**
|
67
|
-
* Create a `Uint8Array` from the passed string
|
68
|
-
*
|
69
|
-
* Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module.
|
70
|
-
*
|
71
|
-
* Also `ascii` which is similar to node's 'binary' encoding.
|
72
|
-
*/
|
73
|
-
function fromString(string, encoding = 'utf8') {
|
74
|
-
const base = BASES[encoding];
|
75
|
-
if (base == null) {
|
76
|
-
throw new Error(`Unsupported encoding "${encoding}"`);
|
77
|
-
}
|
78
|
-
// add multibase prefix
|
79
|
-
return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
|
80
|
-
}
|
81
|
-
|
82
|
-
// copied from utils
|
83
|
-
function isBytes(a) {
|
84
|
-
return (a instanceof Uint8Array ||
|
85
|
-
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
|
86
|
-
}
|
87
|
-
function bytes(b, ...lengths) {
|
88
|
-
if (!isBytes(b))
|
89
|
-
throw new Error('Uint8Array expected');
|
90
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
91
|
-
throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
|
92
|
-
}
|
93
|
-
function exists(instance, checkFinished = true) {
|
94
|
-
if (instance.destroyed)
|
95
|
-
throw new Error('Hash instance has been destroyed');
|
96
|
-
if (checkFinished && instance.finished)
|
97
|
-
throw new Error('Hash#digest() has already been called');
|
98
|
-
}
|
99
|
-
function output(out, instance) {
|
100
|
-
bytes(out);
|
101
|
-
const min = instance.outputLen;
|
102
|
-
if (out.length < min) {
|
103
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
104
|
-
}
|
105
|
-
}
|
106
|
-
|
107
|
-
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
108
|
-
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
109
|
-
// node.js versions earlier than v19 don't declare it in global scope.
|
110
|
-
// For node.js, package.json#exports field mapping rewrites import
|
111
|
-
// from `crypto` to `cryptoNode`, which imports native module.
|
112
|
-
// Makes the utils un-importable in browsers without a bundler.
|
113
|
-
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
114
|
-
// Cast array to view
|
115
|
-
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
116
|
-
// The rotate right (circular right shift) operation for uint32
|
117
|
-
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
118
|
-
new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
119
|
-
/**
|
120
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
121
|
-
*/
|
122
|
-
function utf8ToBytes$1(str) {
|
123
|
-
if (typeof str !== 'string')
|
124
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
125
|
-
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
126
|
-
}
|
127
|
-
/**
|
128
|
-
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
129
|
-
* Warning: when Uint8Array is passed, it would NOT get copied.
|
130
|
-
* Keep in mind for future mutable operations.
|
131
|
-
*/
|
132
|
-
function toBytes(data) {
|
133
|
-
if (typeof data === 'string')
|
134
|
-
data = utf8ToBytes$1(data);
|
135
|
-
bytes(data);
|
136
|
-
return data;
|
137
|
-
}
|
138
|
-
// For runtime check if class implements interface
|
139
|
-
class Hash {
|
140
|
-
// Safe version that clones internal state
|
141
|
-
clone() {
|
142
|
-
return this._cloneInto();
|
143
|
-
}
|
144
|
-
}
|
145
|
-
function wrapConstructor(hashCons) {
|
146
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
147
|
-
const tmp = hashCons();
|
148
|
-
hashC.outputLen = tmp.outputLen;
|
149
|
-
hashC.blockLen = tmp.blockLen;
|
150
|
-
hashC.create = () => hashCons();
|
151
|
-
return hashC;
|
152
|
-
}
|
153
|
-
|
154
|
-
// Polyfill for Safari 14
|
155
|
-
function setBigUint64(view, byteOffset, value, isLE) {
|
156
|
-
if (typeof view.setBigUint64 === 'function')
|
157
|
-
return view.setBigUint64(byteOffset, value, isLE);
|
158
|
-
const _32n = BigInt(32);
|
159
|
-
const _u32_max = BigInt(0xffffffff);
|
160
|
-
const wh = Number((value >> _32n) & _u32_max);
|
161
|
-
const wl = Number(value & _u32_max);
|
162
|
-
const h = isLE ? 4 : 0;
|
163
|
-
const l = isLE ? 0 : 4;
|
164
|
-
view.setUint32(byteOffset + h, wh, isLE);
|
165
|
-
view.setUint32(byteOffset + l, wl, isLE);
|
166
|
-
}
|
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 {
|
176
|
-
constructor(blockLen, outputLen, padOffset, isLE) {
|
177
|
-
super();
|
178
|
-
this.blockLen = blockLen;
|
179
|
-
this.outputLen = outputLen;
|
180
|
-
this.padOffset = padOffset;
|
181
|
-
this.isLE = isLE;
|
182
|
-
this.finished = false;
|
183
|
-
this.length = 0;
|
184
|
-
this.pos = 0;
|
185
|
-
this.destroyed = false;
|
186
|
-
this.buffer = new Uint8Array(blockLen);
|
187
|
-
this.view = createView(this.buffer);
|
188
|
-
}
|
189
|
-
update(data) {
|
190
|
-
exists(this);
|
191
|
-
const { view, buffer, blockLen } = this;
|
192
|
-
data = toBytes(data);
|
193
|
-
const len = data.length;
|
194
|
-
for (let pos = 0; pos < len;) {
|
195
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
196
|
-
// Fast path: we have at least one block in input, cast it to view and process
|
197
|
-
if (take === blockLen) {
|
198
|
-
const dataView = createView(data);
|
199
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
200
|
-
this.process(dataView, pos);
|
201
|
-
continue;
|
202
|
-
}
|
203
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
204
|
-
this.pos += take;
|
205
|
-
pos += take;
|
206
|
-
if (this.pos === blockLen) {
|
207
|
-
this.process(view, 0);
|
208
|
-
this.pos = 0;
|
209
|
-
}
|
210
|
-
}
|
211
|
-
this.length += data.length;
|
212
|
-
this.roundClean();
|
213
|
-
return this;
|
214
|
-
}
|
215
|
-
digestInto(out) {
|
216
|
-
exists(this);
|
217
|
-
output(out, this);
|
218
|
-
this.finished = true;
|
219
|
-
// Padding
|
220
|
-
// We can avoid allocation of buffer for padding completely if it
|
221
|
-
// was previously not allocated here. But it won't change performance.
|
222
|
-
const { buffer, view, blockLen, isLE } = this;
|
223
|
-
let { pos } = this;
|
224
|
-
// append the bit '1' to the message
|
225
|
-
buffer[pos++] = 0b10000000;
|
226
|
-
this.buffer.subarray(pos).fill(0);
|
227
|
-
// we have less than padOffset left in buffer, so we cannot put length in
|
228
|
-
// current block, need process it and pad again
|
229
|
-
if (this.padOffset > blockLen - pos) {
|
230
|
-
this.process(view, 0);
|
231
|
-
pos = 0;
|
232
|
-
}
|
233
|
-
// Pad until full block byte with zeros
|
234
|
-
for (let i = pos; i < blockLen; i++)
|
235
|
-
buffer[i] = 0;
|
236
|
-
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
237
|
-
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
238
|
-
// So we just write lowest 64 bits of that value.
|
239
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
240
|
-
this.process(view, 0);
|
241
|
-
const oview = createView(out);
|
242
|
-
const len = this.outputLen;
|
243
|
-
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
244
|
-
if (len % 4)
|
245
|
-
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
246
|
-
const outLen = len / 4;
|
247
|
-
const state = this.get();
|
248
|
-
if (outLen > state.length)
|
249
|
-
throw new Error('_sha2: outputLen bigger than state');
|
250
|
-
for (let i = 0; i < outLen; i++)
|
251
|
-
oview.setUint32(4 * i, state[i], isLE);
|
252
|
-
}
|
253
|
-
digest() {
|
254
|
-
const { buffer, outputLen } = this;
|
255
|
-
this.digestInto(buffer);
|
256
|
-
const res = buffer.slice(0, outputLen);
|
257
|
-
this.destroy();
|
258
|
-
return res;
|
259
|
-
}
|
260
|
-
_cloneInto(to) {
|
261
|
-
to || (to = new this.constructor());
|
262
|
-
to.set(...this.get());
|
263
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
264
|
-
to.length = length;
|
265
|
-
to.pos = pos;
|
266
|
-
to.finished = finished;
|
267
|
-
to.destroyed = destroyed;
|
268
|
-
if (length % blockLen)
|
269
|
-
to.buffer.set(buffer);
|
270
|
-
return to;
|
271
|
-
}
|
272
|
-
}
|
273
|
-
|
274
|
-
// SHA2-256 need to try 2^128 hashes to execute birthday attack.
|
275
|
-
// BTC network is doing 2^67 hashes/sec as per early 2023.
|
276
|
-
// Round constants:
|
277
|
-
// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
|
278
|
-
// prettier-ignore
|
279
|
-
const SHA256_K = /* @__PURE__ */ new Uint32Array([
|
280
|
-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
281
|
-
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
282
|
-
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
283
|
-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
284
|
-
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
285
|
-
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
286
|
-
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
287
|
-
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
288
|
-
]);
|
289
|
-
// Initial state:
|
290
|
-
// first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
|
291
|
-
// prettier-ignore
|
292
|
-
const SHA256_IV = /* @__PURE__ */ new Uint32Array([
|
293
|
-
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
294
|
-
]);
|
295
|
-
// Temporary buffer, not used to store anything between runs
|
296
|
-
// Named this way because it matches specification.
|
297
|
-
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
298
|
-
class SHA256 extends HashMD {
|
299
|
-
constructor() {
|
300
|
-
super(64, 32, 8, false);
|
301
|
-
// We cannot use array here since array allows indexing by variable
|
302
|
-
// which means optimizer/compiler cannot use registers.
|
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;
|
311
|
-
}
|
312
|
-
get() {
|
313
|
-
const { A, B, C, D, E, F, G, H } = this;
|
314
|
-
return [A, B, C, D, E, F, G, H];
|
315
|
-
}
|
316
|
-
// prettier-ignore
|
317
|
-
set(A, B, C, D, E, F, G, H) {
|
318
|
-
this.A = A | 0;
|
319
|
-
this.B = B | 0;
|
320
|
-
this.C = C | 0;
|
321
|
-
this.D = D | 0;
|
322
|
-
this.E = E | 0;
|
323
|
-
this.F = F | 0;
|
324
|
-
this.G = G | 0;
|
325
|
-
this.H = H | 0;
|
326
|
-
}
|
327
|
-
process(view, offset) {
|
328
|
-
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
329
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
330
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
331
|
-
for (let i = 16; i < 64; i++) {
|
332
|
-
const W15 = SHA256_W[i - 15];
|
333
|
-
const W2 = SHA256_W[i - 2];
|
334
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
335
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
336
|
-
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
337
|
-
}
|
338
|
-
// Compression function main loop, 64 rounds
|
339
|
-
let { A, B, C, D, E, F, G, H } = this;
|
340
|
-
for (let i = 0; i < 64; i++) {
|
341
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
342
|
-
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
343
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
344
|
-
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
345
|
-
H = G;
|
346
|
-
G = F;
|
347
|
-
F = E;
|
348
|
-
E = (D + T1) | 0;
|
349
|
-
D = C;
|
350
|
-
C = B;
|
351
|
-
B = A;
|
352
|
-
A = (T1 + T2) | 0;
|
353
|
-
}
|
354
|
-
// Add the compressed chunk to the current hash value
|
355
|
-
A = (A + this.A) | 0;
|
356
|
-
B = (B + this.B) | 0;
|
357
|
-
C = (C + this.C) | 0;
|
358
|
-
D = (D + this.D) | 0;
|
359
|
-
E = (E + this.E) | 0;
|
360
|
-
F = (F + this.F) | 0;
|
361
|
-
G = (G + this.G) | 0;
|
362
|
-
H = (H + this.H) | 0;
|
363
|
-
this.set(A, B, C, D, E, F, G, H);
|
364
|
-
}
|
365
|
-
roundClean() {
|
366
|
-
SHA256_W.fill(0);
|
367
|
-
}
|
368
|
-
destroy() {
|
369
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
370
|
-
this.buffer.fill(0);
|
371
|
-
}
|
372
|
-
}
|
373
|
-
/**
|
374
|
-
* SHA2-256 hash function
|
375
|
-
* @param message - data that would be hashed
|
376
|
-
*/
|
377
|
-
const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
378
|
-
|
379
|
-
/**
|
380
|
-
* DefaultPubsubTopic is the default gossipsub topic to use for Waku.
|
381
|
-
*/
|
382
|
-
/**
|
383
|
-
* The default cluster ID for The Waku Network
|
384
|
-
*/
|
385
|
-
const DEFAULT_CLUSTER_ID = 1;
|
386
|
-
|
387
|
-
/**
|
388
|
-
* Turns a `Uint8Array` into a string.
|
389
|
-
*
|
390
|
-
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
|
391
|
-
*
|
392
|
-
* Also `ascii` which is similar to node's 'binary' encoding.
|
393
|
-
*/
|
394
|
-
function toString(array, encoding = 'utf8') {
|
395
|
-
const base = BASES[encoding];
|
396
|
-
if (base == null) {
|
397
|
-
throw new Error(`Unsupported encoding "${encoding}"`);
|
398
|
-
}
|
399
|
-
// strip multibase prefix
|
400
|
-
return base.encoder.encode(array).substring(1);
|
401
|
-
}
|
402
|
-
|
403
|
-
/**
|
404
|
-
* Decode byte array to utf-8 string.
|
405
|
-
*/
|
406
|
-
const bytesToUtf8 = (b) => toString(b, "utf8");
|
407
|
-
/**
|
408
|
-
* Encode utf-8 string to byte array.
|
409
|
-
*/
|
410
|
-
const utf8ToBytes = (s) => fromString(s, "utf8");
|
411
|
-
/**
|
412
|
-
* Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
|
413
|
-
*/
|
414
|
-
function concat(byteArrays, totalLength) {
|
415
|
-
const len = totalLength ?? byteArrays.reduce((acc, curr) => acc + curr.length, 0);
|
416
|
-
const res = new Uint8Array(len);
|
417
|
-
let offset = 0;
|
418
|
-
for (const bytes of byteArrays) {
|
419
|
-
res.set(bytes, offset);
|
420
|
-
offset += bytes.length;
|
421
|
-
}
|
422
|
-
return res;
|
423
|
-
}
|
424
|
-
|
425
|
-
const singleShardInfoToPubsubTopic = (shardInfo) => {
|
426
|
-
if (shardInfo.shard === undefined)
|
427
|
-
throw new Error("Invalid shard");
|
428
|
-
return `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${shardInfo.shard}`;
|
429
|
-
};
|
430
|
-
const shardInfoToPubsubTopics = (shardInfo) => {
|
431
|
-
if ("contentTopics" in shardInfo && shardInfo.contentTopics) {
|
432
|
-
// Autosharding: explicitly defined content topics
|
433
|
-
return Array.from(new Set(shardInfo.contentTopics.map((contentTopic) => contentTopicToPubsubTopic(contentTopic, shardInfo.clusterId))));
|
434
|
-
}
|
435
|
-
else if ("shards" in shardInfo) {
|
436
|
-
// Static sharding
|
437
|
-
if (shardInfo.shards === undefined)
|
438
|
-
throw new Error("Invalid shard");
|
439
|
-
return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${index}`)));
|
440
|
-
}
|
441
|
-
else if ("application" in shardInfo && "version" in shardInfo) {
|
442
|
-
// Autosharding: single shard from application and version
|
443
|
-
return [
|
444
|
-
contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`, shardInfo.clusterId)
|
445
|
-
];
|
446
|
-
}
|
447
|
-
else {
|
448
|
-
throw new Error("Missing required configuration in shard parameters");
|
449
|
-
}
|
450
|
-
};
|
451
|
-
const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
|
452
|
-
const parts = pubsubTopics.split("/");
|
453
|
-
if (parts.length != 6 ||
|
454
|
-
parts[1] !== "waku" ||
|
455
|
-
parts[2] !== "2" ||
|
456
|
-
parts[3] !== "rs")
|
457
|
-
throw new Error("Invalid pubsub topic");
|
458
|
-
const clusterId = parseInt(parts[4]);
|
459
|
-
const shard = parseInt(parts[5]);
|
460
|
-
if (isNaN(clusterId) || isNaN(shard))
|
461
|
-
throw new Error("Invalid clusterId or shard");
|
462
|
-
return {
|
463
|
-
clusterId,
|
464
|
-
shard
|
465
|
-
};
|
466
|
-
};
|
467
|
-
/**
|
468
|
-
* Given a string, will throw an error if it is not formatted as a valid content topic for autosharding based on https://rfc.vac.dev/spec/51/
|
469
|
-
* @param contentTopic String to validate
|
470
|
-
* @returns Object with each content topic field as an attribute
|
471
|
-
*/
|
472
|
-
function ensureValidContentTopic(contentTopic) {
|
473
|
-
const parts = contentTopic.split("/");
|
474
|
-
if (parts.length < 5 || parts.length > 6) {
|
475
|
-
throw Error("Content topic format is invalid");
|
476
|
-
}
|
477
|
-
// Validate generation field if present
|
478
|
-
let generation = 0;
|
479
|
-
if (parts.length == 6) {
|
480
|
-
generation = parseInt(parts[1]);
|
481
|
-
if (isNaN(generation)) {
|
482
|
-
throw new Error("Invalid generation field in content topic");
|
483
|
-
}
|
484
|
-
if (generation > 0) {
|
485
|
-
throw new Error("Generation greater than 0 is not supported");
|
486
|
-
}
|
487
|
-
}
|
488
|
-
// Validate remaining fields
|
489
|
-
const fields = parts.splice(-4);
|
490
|
-
// Validate application field
|
491
|
-
if (fields[0].length == 0) {
|
492
|
-
throw new Error("Application field cannot be empty");
|
493
|
-
}
|
494
|
-
// Validate version field
|
495
|
-
if (fields[1].length == 0) {
|
496
|
-
throw new Error("Version field cannot be empty");
|
497
|
-
}
|
498
|
-
// Validate topic name field
|
499
|
-
if (fields[2].length == 0) {
|
500
|
-
throw new Error("Topic name field cannot be empty");
|
501
|
-
}
|
502
|
-
// Validate encoding field
|
503
|
-
if (fields[3].length == 0) {
|
504
|
-
throw new Error("Encoding field cannot be empty");
|
505
|
-
}
|
506
|
-
return {
|
507
|
-
generation,
|
508
|
-
application: fields[0],
|
509
|
-
version: fields[1],
|
510
|
-
topicName: fields[2],
|
511
|
-
encoding: fields[3]
|
512
|
-
};
|
513
|
-
}
|
514
|
-
/**
|
515
|
-
* Given a string, determines which autoshard index to use for its pubsub topic.
|
516
|
-
* Based on the algorithm described in the RFC: https://rfc.vac.dev/spec/51//#algorithm
|
517
|
-
*/
|
518
|
-
function contentTopicToShardIndex(contentTopic, networkShards = 8) {
|
519
|
-
const { application, version } = ensureValidContentTopic(contentTopic);
|
520
|
-
const digest = sha256(concat([utf8ToBytes(application), utf8ToBytes(version)]));
|
521
|
-
const dataview = new DataView(digest.buffer.slice(-8));
|
522
|
-
return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
|
523
|
-
}
|
524
|
-
function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
|
525
|
-
if (!contentTopic) {
|
526
|
-
throw Error("Content topic must be specified");
|
527
|
-
}
|
528
|
-
const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
|
529
|
-
return `/waku/2/rs/${clusterId}/${shardIndex}`;
|
530
|
-
}
|
531
|
-
/**
|
532
|
-
* Used when creating encoders/decoders to determine which pubsub topic to use
|
533
|
-
*/
|
534
|
-
function determinePubsubTopic(contentTopic, pubsubTopicShardInfo) {
|
535
|
-
if (typeof pubsubTopicShardInfo == "string") {
|
536
|
-
return pubsubTopicShardInfo;
|
537
|
-
}
|
538
|
-
return pubsubTopicShardInfo?.shard !== undefined
|
539
|
-
? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
|
540
|
-
: contentTopicToPubsubTopic(contentTopic, pubsubTopicShardInfo?.clusterId ?? DEFAULT_CLUSTER_ID);
|
541
|
-
}
|
542
|
-
/**
|
543
|
-
* Validates sharding configuration and sets defaults where possible.
|
544
|
-
* @returns Validated sharding parameters, with any missing values set to defaults
|
545
|
-
*/
|
546
|
-
const ensureShardingConfigured = (shardInfo) => {
|
547
|
-
const clusterId = shardInfo.clusterId ?? DEFAULT_CLUSTER_ID;
|
548
|
-
const shards = "shards" in shardInfo ? shardInfo.shards : [];
|
549
|
-
const contentTopics = "contentTopics" in shardInfo ? shardInfo.contentTopics : [];
|
550
|
-
const [application, version] = "application" in shardInfo && "version" in shardInfo
|
551
|
-
? [shardInfo.application, shardInfo.version]
|
552
|
-
: [undefined, undefined];
|
553
|
-
const isShardsConfigured = shards && shards.length > 0;
|
554
|
-
const isContentTopicsConfigured = contentTopics && contentTopics.length > 0;
|
555
|
-
const isApplicationVersionConfigured = application && version;
|
556
|
-
if (isShardsConfigured) {
|
557
|
-
return {
|
558
|
-
shardingParams: { clusterId, shards },
|
559
|
-
shardInfo: { clusterId, shards },
|
560
|
-
pubsubTopics: shardInfoToPubsubTopics({ clusterId, shards })
|
561
|
-
};
|
562
|
-
}
|
563
|
-
if (isContentTopicsConfigured) {
|
564
|
-
const pubsubTopics = Array.from(new Set(contentTopics.map((topic) => contentTopicToPubsubTopic(topic, clusterId))));
|
565
|
-
const shards = Array.from(new Set(contentTopics.map((topic) => contentTopicToShardIndex(topic))));
|
566
|
-
return {
|
567
|
-
shardingParams: { clusterId, contentTopics },
|
568
|
-
shardInfo: { clusterId, shards },
|
569
|
-
pubsubTopics
|
570
|
-
};
|
571
|
-
}
|
572
|
-
if (isApplicationVersionConfigured) {
|
573
|
-
const pubsubTopic = contentTopicToPubsubTopic(`/${application}/${version}/default/default`, clusterId);
|
574
|
-
return {
|
575
|
-
shardingParams: { clusterId, application, version },
|
576
|
-
shardInfo: {
|
577
|
-
clusterId,
|
578
|
-
shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard]
|
579
|
-
},
|
580
|
-
pubsubTopics: [pubsubTopic]
|
581
|
-
};
|
582
|
-
}
|
583
|
-
throw new Error("Missing minimum required configuration options for static sharding or autosharding.");
|
584
|
-
};
|
585
|
-
|
586
|
-
const APP_NAME = "waku";
|
587
|
-
class Logger {
|
588
|
-
_info;
|
589
|
-
_warn;
|
590
|
-
_error;
|
591
|
-
static createDebugNamespace(level, prefix) {
|
592
|
-
return prefix ? `${APP_NAME}:${level}:${prefix}` : `${APP_NAME}:${level}`;
|
593
|
-
}
|
594
|
-
constructor(prefix) {
|
595
|
-
this._info = debug(Logger.createDebugNamespace("info", prefix));
|
596
|
-
this._warn = debug(Logger.createDebugNamespace("warn", prefix));
|
597
|
-
this._error = debug(Logger.createDebugNamespace("error", prefix));
|
598
|
-
}
|
599
|
-
get info() {
|
600
|
-
return this._info;
|
601
|
-
}
|
602
|
-
get warn() {
|
603
|
-
return this._warn;
|
604
|
-
}
|
605
|
-
get error() {
|
606
|
-
return this._error;
|
607
|
-
}
|
608
|
-
log(level, ...args) {
|
609
|
-
const logger = this[level];
|
610
|
-
logger(...args);
|
611
|
-
}
|
612
|
-
}
|
613
|
-
|
614
|
-
export { Logger as L, allocUnsafe as a, alloc as b, bytesToUtf8 as c, determinePubsubTopic as d, ensureShardingConfigured as e, fromString as f, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, utf8ToBytes as u };
|
@@ -1,81 +0,0 @@
|
|
1
|
-
import '../browser-zSUobdfj.js';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Return pseudo random subset of the input.
|
5
|
-
*/
|
6
|
-
function getPseudoRandomSubset(values, wantedNumber) {
|
7
|
-
if (values.length <= wantedNumber || values.length <= 1) {
|
8
|
-
return values;
|
9
|
-
}
|
10
|
-
return shuffle(values).slice(0, wantedNumber);
|
11
|
-
}
|
12
|
-
function shuffle(arr) {
|
13
|
-
if (arr.length <= 1) {
|
14
|
-
return arr;
|
15
|
-
}
|
16
|
-
const randInt = () => {
|
17
|
-
return Math.floor(Math.random() * Math.floor(arr.length));
|
18
|
-
};
|
19
|
-
for (let i = 0; i < arr.length; i++) {
|
20
|
-
const j = randInt();
|
21
|
-
const tmp = arr[i];
|
22
|
-
arr[i] = arr[j];
|
23
|
-
arr[j] = tmp;
|
24
|
-
}
|
25
|
-
return arr;
|
26
|
-
}
|
27
|
-
|
28
|
-
const DefaultWantedNumber = 1;
|
29
|
-
var Fleet;
|
30
|
-
(function (Fleet) {
|
31
|
-
Fleet["Sandbox"] = "sandbox";
|
32
|
-
Fleet["Test"] = "test";
|
33
|
-
})(Fleet || (Fleet = {}));
|
34
|
-
/**
|
35
|
-
* Return list of pre-defined (hardcoded) bootstrap nodes.
|
36
|
-
*
|
37
|
-
* Default behavior is to return nodes of the nwaku Status Sandbox fleet.
|
38
|
-
*
|
39
|
-
* @param fleet The fleet to be returned. Defaults to sandbox fleet.
|
40
|
-
* @param wantedNumber The number of connections desired. Defaults to {@link DefaultWantedNumber}.
|
41
|
-
*
|
42
|
-
* @returns An array of multiaddresses.
|
43
|
-
*/
|
44
|
-
function getPredefinedBootstrapNodes(fleet = Fleet.Sandbox, wantedNumber = DefaultWantedNumber) {
|
45
|
-
if (wantedNumber <= 0) {
|
46
|
-
return [];
|
47
|
-
}
|
48
|
-
let nodes;
|
49
|
-
switch (fleet) {
|
50
|
-
case Fleet.Sandbox:
|
51
|
-
nodes = fleets.fleets["waku.sandbox"]["waku-websocket"];
|
52
|
-
break;
|
53
|
-
case Fleet.Test:
|
54
|
-
nodes = fleets.fleets["waku.test"]["waku-websocket"];
|
55
|
-
break;
|
56
|
-
default:
|
57
|
-
nodes = fleets.fleets["waku.sandbox"]["waku-websocket"];
|
58
|
-
}
|
59
|
-
nodes = Object.values(nodes);
|
60
|
-
return getPseudoRandomSubset(nodes, wantedNumber);
|
61
|
-
}
|
62
|
-
const fleets = {
|
63
|
-
fleets: {
|
64
|
-
"waku.sandbox": {
|
65
|
-
"waku-websocket": {
|
66
|
-
"node-01.ac-cn-hongkong-c.waku.sandbox": "/dns4/node-01.ac-cn-hongkong-c.waku.sandbox.status.im/tcp/8000/wss/p2p/16Uiu2HAmSJvSJphxRdbnigUV5bjRRZFBhTtWFTSyiKaQByCjwmpV",
|
67
|
-
"node-01.do-ams3.waku.sandbox": "/dns4/node-01.do-ams3.waku.sandbox.status.im/tcp/8000/wss/p2p/16Uiu2HAmQSMNExfUYUqfuXWkD5DaNZnMYnigRxFKbk3tcEFQeQeE",
|
68
|
-
"node-01.gc-us-central1-a.waku.sandbox": "/dns4/node-01.gc-us-central1-a.waku.sandbox.status.im/tcp/8000/wss/p2p/16Uiu2HAm6fyqE1jB5MonzvoMdU8v76bWV8ZeNpncDamY1MQXfjdB"
|
69
|
-
}
|
70
|
-
},
|
71
|
-
"waku.test": {
|
72
|
-
"waku-websocket": {
|
73
|
-
"node-01.ac-cn-hongkong-c.waku.test": "/dns4/node-01.ac-cn-hongkong-c.waku.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAkzHaTP5JsUwfR9NR8Rj9HC24puS6ocaU8wze4QrXr9iXp",
|
74
|
-
"node-01.do-ams3.waku.test": "/dns4/node-01.do-ams3.waku.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAkykgaECHswi3YKJ5dMLbq2kPVCo89fcyTd38UcQD6ej5W",
|
75
|
-
"node-01.gc-us-central1-a.waku.test": "/dns4/node-01.gc-us-central1-a.waku.test.statusim.net/tcp/8000/wss/p2p/16Uiu2HAmDCp8XJ9z1ev18zuv8NHekAsjNyezAvmMfFEJkiharitG"
|
76
|
-
}
|
77
|
-
}
|
78
|
-
}
|
79
|
-
};
|
80
|
-
|
81
|
-
export { DefaultWantedNumber, Fleet, fleets, getPredefinedBootstrapNodes };
|