@waku/core 0.0.37 → 0.0.38-88fca77.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/bundle/index.js +1064 -46
- package/bundle/lib/message/version_0.js +1 -1
- package/bundle/{version_0-BHaZD8Qu.js → version_0-BakAFyXb.js} +24 -943
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +3 -2
- package/dist/lib/connection_manager/connection_manager.js +4 -0
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/connection_manager/dialer.js +3 -3
- package/dist/lib/connection_manager/dialer.js.map +1 -1
- package/dist/lib/connection_manager/keep_alive_manager.d.ts +4 -2
- package/dist/lib/connection_manager/keep_alive_manager.js +9 -3
- package/dist/lib/connection_manager/keep_alive_manager.js.map +1 -1
- package/dist/lib/connection_manager/shard_reader.d.ts +8 -9
- package/dist/lib/connection_manager/shard_reader.js +20 -29
- package/dist/lib/connection_manager/shard_reader.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +14 -7
- package/dist/lib/message/version_0.js +22 -11
- package/dist/lib/message/version_0.js.map +1 -1
- package/package.json +1 -125
- package/src/lib/connection_manager/connection_manager.ts +11 -2
- package/src/lib/connection_manager/dialer.ts +3 -3
- package/src/lib/connection_manager/keep_alive_manager.ts +12 -3
- package/src/lib/connection_manager/shard_reader.ts +31 -53
- package/src/lib/message/version_0.ts +23 -19
package/bundle/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { e as equals$2, c as coerce, b as base32, a as base58btc, d as base36, v as version_0,
|
|
2
|
-
export {
|
|
1
|
+
import { e as equals$2, c as coerce, b as base32, a as base58btc, d as base36, B as BASES, f as fromString, v as version_0, g as allocUnsafe, h as alloc, i as encodingLength$1, j as encode$3, k as decode$4, L as Logger, F as FilterSubscribeRequest, l as FilterSubscribeResponse$1, M as MessagePush, P as PushRpc$1, m as PushResponse, S as StoreQueryRequest$1, n as StoreQueryResponse$1, o as createEncoder, p as enumeration, q as message, r as encodeMessage, s as decodeMessage, t as bases, u as base64url, w as encodeUint8Array, W as WakuMetadataRequest, x as WakuMetadataResponse } from './version_0-BakAFyXb.js';
|
|
2
|
+
export { y as createDecoder } from './version_0-BakAFyXb.js';
|
|
3
3
|
|
|
4
4
|
/* eslint-disable */
|
|
5
5
|
var encode_1 = encode$2;
|
|
@@ -191,7 +191,7 @@ class Hasher {
|
|
|
191
191
|
function sha(name) {
|
|
192
192
|
return async (data) => new Uint8Array(await crypto.subtle.digest(name, data));
|
|
193
193
|
}
|
|
194
|
-
const sha256 = from({
|
|
194
|
+
const sha256$2 = from({
|
|
195
195
|
name: 'sha2-256',
|
|
196
196
|
code: 0x12,
|
|
197
197
|
encode: sha('SHA-256')
|
|
@@ -581,6 +581,906 @@ async function isMessageSizeUnderCap(encoder, message) {
|
|
|
581
581
|
}
|
|
582
582
|
const isWireSizeUnderCap = (buf) => buf.length / MB <= SIZE_CAP_IN_MB;
|
|
583
583
|
|
|
584
|
+
const crypto$1 = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
588
|
+
* @module
|
|
589
|
+
*/
|
|
590
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
591
|
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
592
|
+
// node.js versions earlier than v19 don't declare it in global scope.
|
|
593
|
+
// For node.js, package.json#exports field mapping rewrites import
|
|
594
|
+
// from `crypto` to `cryptoNode`, which imports native module.
|
|
595
|
+
// Makes the utils un-importable in browsers without a bundler.
|
|
596
|
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
597
|
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
598
|
+
function isBytes(a) {
|
|
599
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
600
|
+
}
|
|
601
|
+
/** Asserts something is positive integer. */
|
|
602
|
+
function anumber(n) {
|
|
603
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
604
|
+
throw new Error('positive integer expected, got ' + n);
|
|
605
|
+
}
|
|
606
|
+
/** Asserts something is Uint8Array. */
|
|
607
|
+
function abytes(b, ...lengths) {
|
|
608
|
+
if (!isBytes(b))
|
|
609
|
+
throw new Error('Uint8Array expected');
|
|
610
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
611
|
+
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
|
612
|
+
}
|
|
613
|
+
/** Asserts something is hash */
|
|
614
|
+
function ahash(h) {
|
|
615
|
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
616
|
+
throw new Error('Hash should be wrapped by utils.createHasher');
|
|
617
|
+
anumber(h.outputLen);
|
|
618
|
+
anumber(h.blockLen);
|
|
619
|
+
}
|
|
620
|
+
/** Asserts a hash instance has not been destroyed / finished */
|
|
621
|
+
function aexists(instance, checkFinished = true) {
|
|
622
|
+
if (instance.destroyed)
|
|
623
|
+
throw new Error('Hash instance has been destroyed');
|
|
624
|
+
if (checkFinished && instance.finished)
|
|
625
|
+
throw new Error('Hash#digest() has already been called');
|
|
626
|
+
}
|
|
627
|
+
/** Asserts output is properly-sized byte array */
|
|
628
|
+
function aoutput(out, instance) {
|
|
629
|
+
abytes(out);
|
|
630
|
+
const min = instance.outputLen;
|
|
631
|
+
if (out.length < min) {
|
|
632
|
+
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
636
|
+
function clean(...arrays) {
|
|
637
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
638
|
+
arrays[i].fill(0);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
/** Create DataView of an array for easy byte-level manipulation. */
|
|
642
|
+
function createView(arr) {
|
|
643
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
644
|
+
}
|
|
645
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
646
|
+
function rotr(word, shift) {
|
|
647
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
648
|
+
}
|
|
649
|
+
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
|
650
|
+
const hasHexBuiltin = /* @__PURE__ */ (() =>
|
|
651
|
+
// @ts-ignore
|
|
652
|
+
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
653
|
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
654
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
655
|
+
/**
|
|
656
|
+
* Convert byte array to hex string. Uses built-in function, when available.
|
|
657
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
658
|
+
*/
|
|
659
|
+
function bytesToHex$1(bytes) {
|
|
660
|
+
abytes(bytes);
|
|
661
|
+
// @ts-ignore
|
|
662
|
+
if (hasHexBuiltin)
|
|
663
|
+
return bytes.toHex();
|
|
664
|
+
// pre-caching improves the speed 6x
|
|
665
|
+
let hex = '';
|
|
666
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
667
|
+
hex += hexes[bytes[i]];
|
|
668
|
+
}
|
|
669
|
+
return hex;
|
|
670
|
+
}
|
|
671
|
+
// We use optimized technique to convert hex string to byte array
|
|
672
|
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
673
|
+
function asciiToBase16(ch) {
|
|
674
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
675
|
+
return ch - asciis._0; // '2' => 50-48
|
|
676
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
677
|
+
return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
|
678
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
679
|
+
return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Convert hex string to byte array. Uses built-in function, when available.
|
|
684
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
685
|
+
*/
|
|
686
|
+
function hexToBytes(hex) {
|
|
687
|
+
if (typeof hex !== 'string')
|
|
688
|
+
throw new Error('hex string expected, got ' + typeof hex);
|
|
689
|
+
// @ts-ignore
|
|
690
|
+
if (hasHexBuiltin)
|
|
691
|
+
return Uint8Array.fromHex(hex);
|
|
692
|
+
const hl = hex.length;
|
|
693
|
+
const al = hl / 2;
|
|
694
|
+
if (hl % 2)
|
|
695
|
+
throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
|
696
|
+
const array = new Uint8Array(al);
|
|
697
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
698
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
699
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
700
|
+
if (n1 === undefined || n2 === undefined) {
|
|
701
|
+
const char = hex[hi] + hex[hi + 1];
|
|
702
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
703
|
+
}
|
|
704
|
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
|
705
|
+
}
|
|
706
|
+
return array;
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Converts string to bytes using UTF8 encoding.
|
|
710
|
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
|
711
|
+
*/
|
|
712
|
+
function utf8ToBytes$1(str) {
|
|
713
|
+
if (typeof str !== 'string')
|
|
714
|
+
throw new Error('string expected');
|
|
715
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
719
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
720
|
+
* Keep in mind for future mutable operations.
|
|
721
|
+
*/
|
|
722
|
+
function toBytes(data) {
|
|
723
|
+
if (typeof data === 'string')
|
|
724
|
+
data = utf8ToBytes$1(data);
|
|
725
|
+
abytes(data);
|
|
726
|
+
return data;
|
|
727
|
+
}
|
|
728
|
+
/** Copies several Uint8Arrays into one. */
|
|
729
|
+
function concatBytes(...arrays) {
|
|
730
|
+
let sum = 0;
|
|
731
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
732
|
+
const a = arrays[i];
|
|
733
|
+
abytes(a);
|
|
734
|
+
sum += a.length;
|
|
735
|
+
}
|
|
736
|
+
const res = new Uint8Array(sum);
|
|
737
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
738
|
+
const a = arrays[i];
|
|
739
|
+
res.set(a, pad);
|
|
740
|
+
pad += a.length;
|
|
741
|
+
}
|
|
742
|
+
return res;
|
|
743
|
+
}
|
|
744
|
+
/** For runtime check if class implements interface */
|
|
745
|
+
class Hash {
|
|
746
|
+
}
|
|
747
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
748
|
+
function createHasher(hashCons) {
|
|
749
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
750
|
+
const tmp = hashCons();
|
|
751
|
+
hashC.outputLen = tmp.outputLen;
|
|
752
|
+
hashC.blockLen = tmp.blockLen;
|
|
753
|
+
hashC.create = () => hashCons();
|
|
754
|
+
return hashC;
|
|
755
|
+
}
|
|
756
|
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
|
757
|
+
function randomBytes(bytesLength = 32) {
|
|
758
|
+
if (crypto$1 && typeof crypto$1.getRandomValues === 'function') {
|
|
759
|
+
return crypto$1.getRandomValues(new Uint8Array(bytesLength));
|
|
760
|
+
}
|
|
761
|
+
// Legacy Node.js compatibility
|
|
762
|
+
if (crypto$1 && typeof crypto$1.randomBytes === 'function') {
|
|
763
|
+
return Uint8Array.from(crypto$1.randomBytes(bytesLength));
|
|
764
|
+
}
|
|
765
|
+
throw new Error('crypto.getRandomValues must be defined');
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Internal Merkle-Damgard hash utils.
|
|
770
|
+
* @module
|
|
771
|
+
*/
|
|
772
|
+
/** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
|
|
773
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
774
|
+
if (typeof view.setBigUint64 === 'function')
|
|
775
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
776
|
+
const _32n = BigInt(32);
|
|
777
|
+
const _u32_max = BigInt(0xffffffff);
|
|
778
|
+
const wh = Number((value >> _32n) & _u32_max);
|
|
779
|
+
const wl = Number(value & _u32_max);
|
|
780
|
+
const h = isLE ? 4 : 0;
|
|
781
|
+
const l = isLE ? 0 : 4;
|
|
782
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
783
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
784
|
+
}
|
|
785
|
+
/** Choice: a ? b : c */
|
|
786
|
+
function Chi(a, b, c) {
|
|
787
|
+
return (a & b) ^ (~a & c);
|
|
788
|
+
}
|
|
789
|
+
/** Majority function, true if any two inputs is true. */
|
|
790
|
+
function Maj(a, b, c) {
|
|
791
|
+
return (a & b) ^ (a & c) ^ (b & c);
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Merkle-Damgard hash construction base class.
|
|
795
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
796
|
+
*/
|
|
797
|
+
class HashMD extends Hash {
|
|
798
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
799
|
+
super();
|
|
800
|
+
this.finished = false;
|
|
801
|
+
this.length = 0;
|
|
802
|
+
this.pos = 0;
|
|
803
|
+
this.destroyed = false;
|
|
804
|
+
this.blockLen = blockLen;
|
|
805
|
+
this.outputLen = outputLen;
|
|
806
|
+
this.padOffset = padOffset;
|
|
807
|
+
this.isLE = isLE;
|
|
808
|
+
this.buffer = new Uint8Array(blockLen);
|
|
809
|
+
this.view = createView(this.buffer);
|
|
810
|
+
}
|
|
811
|
+
update(data) {
|
|
812
|
+
aexists(this);
|
|
813
|
+
data = toBytes(data);
|
|
814
|
+
abytes(data);
|
|
815
|
+
const { view, buffer, blockLen } = this;
|
|
816
|
+
const len = data.length;
|
|
817
|
+
for (let pos = 0; pos < len;) {
|
|
818
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
819
|
+
// Fast path: we have at least one block in input, cast it to view and process
|
|
820
|
+
if (take === blockLen) {
|
|
821
|
+
const dataView = createView(data);
|
|
822
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
823
|
+
this.process(dataView, pos);
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
827
|
+
this.pos += take;
|
|
828
|
+
pos += take;
|
|
829
|
+
if (this.pos === blockLen) {
|
|
830
|
+
this.process(view, 0);
|
|
831
|
+
this.pos = 0;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
this.length += data.length;
|
|
835
|
+
this.roundClean();
|
|
836
|
+
return this;
|
|
837
|
+
}
|
|
838
|
+
digestInto(out) {
|
|
839
|
+
aexists(this);
|
|
840
|
+
aoutput(out, this);
|
|
841
|
+
this.finished = true;
|
|
842
|
+
// Padding
|
|
843
|
+
// We can avoid allocation of buffer for padding completely if it
|
|
844
|
+
// was previously not allocated here. But it won't change performance.
|
|
845
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
846
|
+
let { pos } = this;
|
|
847
|
+
// append the bit '1' to the message
|
|
848
|
+
buffer[pos++] = 0b10000000;
|
|
849
|
+
clean(this.buffer.subarray(pos));
|
|
850
|
+
// we have less than padOffset left in buffer, so we cannot put length in
|
|
851
|
+
// current block, need process it and pad again
|
|
852
|
+
if (this.padOffset > blockLen - pos) {
|
|
853
|
+
this.process(view, 0);
|
|
854
|
+
pos = 0;
|
|
855
|
+
}
|
|
856
|
+
// Pad until full block byte with zeros
|
|
857
|
+
for (let i = pos; i < blockLen; i++)
|
|
858
|
+
buffer[i] = 0;
|
|
859
|
+
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
|
860
|
+
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
|
861
|
+
// So we just write lowest 64 bits of that value.
|
|
862
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
863
|
+
this.process(view, 0);
|
|
864
|
+
const oview = createView(out);
|
|
865
|
+
const len = this.outputLen;
|
|
866
|
+
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
|
867
|
+
if (len % 4)
|
|
868
|
+
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
869
|
+
const outLen = len / 4;
|
|
870
|
+
const state = this.get();
|
|
871
|
+
if (outLen > state.length)
|
|
872
|
+
throw new Error('_sha2: outputLen bigger than state');
|
|
873
|
+
for (let i = 0; i < outLen; i++)
|
|
874
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
875
|
+
}
|
|
876
|
+
digest() {
|
|
877
|
+
const { buffer, outputLen } = this;
|
|
878
|
+
this.digestInto(buffer);
|
|
879
|
+
const res = buffer.slice(0, outputLen);
|
|
880
|
+
this.destroy();
|
|
881
|
+
return res;
|
|
882
|
+
}
|
|
883
|
+
_cloneInto(to) {
|
|
884
|
+
to || (to = new this.constructor());
|
|
885
|
+
to.set(...this.get());
|
|
886
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
887
|
+
to.destroyed = destroyed;
|
|
888
|
+
to.finished = finished;
|
|
889
|
+
to.length = length;
|
|
890
|
+
to.pos = pos;
|
|
891
|
+
if (length % blockLen)
|
|
892
|
+
to.buffer.set(buffer);
|
|
893
|
+
return to;
|
|
894
|
+
}
|
|
895
|
+
clone() {
|
|
896
|
+
return this._cloneInto();
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
901
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
902
|
+
*/
|
|
903
|
+
/** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
|
|
904
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
905
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
906
|
+
]);
|
|
907
|
+
/** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
|
|
908
|
+
const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
909
|
+
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
|
|
910
|
+
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
|
|
911
|
+
]);
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
|
915
|
+
* @todo re-check https://issues.chromium.org/issues/42212588
|
|
916
|
+
* @module
|
|
917
|
+
*/
|
|
918
|
+
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
919
|
+
const _32n = /* @__PURE__ */ BigInt(32);
|
|
920
|
+
function fromBig(n, le = false) {
|
|
921
|
+
if (le)
|
|
922
|
+
return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
|
923
|
+
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
924
|
+
}
|
|
925
|
+
function split(lst, le = false) {
|
|
926
|
+
const len = lst.length;
|
|
927
|
+
let Ah = new Uint32Array(len);
|
|
928
|
+
let Al = new Uint32Array(len);
|
|
929
|
+
for (let i = 0; i < len; i++) {
|
|
930
|
+
const { h, l } = fromBig(lst[i], le);
|
|
931
|
+
[Ah[i], Al[i]] = [h, l];
|
|
932
|
+
}
|
|
933
|
+
return [Ah, Al];
|
|
934
|
+
}
|
|
935
|
+
// for Shift in [0, 32)
|
|
936
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
937
|
+
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
938
|
+
// Right rotate for Shift in [1, 32)
|
|
939
|
+
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
940
|
+
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
941
|
+
// Right rotate for Shift in (32, 64), NOTE: 32 is special case.
|
|
942
|
+
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
943
|
+
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
944
|
+
// JS uses 32-bit signed integers for bitwise operations which means we cannot
|
|
945
|
+
// simple take carry out of low bit sum by shift, we need to use division.
|
|
946
|
+
function add(Ah, Al, Bh, Bl) {
|
|
947
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
948
|
+
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
949
|
+
}
|
|
950
|
+
// Addition with more than 2 elements
|
|
951
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
952
|
+
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
953
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
954
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
955
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
956
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
960
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
961
|
+
* Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
|
|
962
|
+
* [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
963
|
+
* @module
|
|
964
|
+
*/
|
|
965
|
+
/**
|
|
966
|
+
* Round constants:
|
|
967
|
+
* First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
|
|
968
|
+
*/
|
|
969
|
+
// prettier-ignore
|
|
970
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
971
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
972
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
973
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
974
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
975
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
976
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
977
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
978
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
979
|
+
]);
|
|
980
|
+
/** Reusable temporary buffer. "W" comes straight from spec. */
|
|
981
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
982
|
+
class SHA256 extends HashMD {
|
|
983
|
+
constructor(outputLen = 32) {
|
|
984
|
+
super(64, outputLen, 8, false);
|
|
985
|
+
// We cannot use array here since array allows indexing by variable
|
|
986
|
+
// which means optimizer/compiler cannot use registers.
|
|
987
|
+
this.A = SHA256_IV[0] | 0;
|
|
988
|
+
this.B = SHA256_IV[1] | 0;
|
|
989
|
+
this.C = SHA256_IV[2] | 0;
|
|
990
|
+
this.D = SHA256_IV[3] | 0;
|
|
991
|
+
this.E = SHA256_IV[4] | 0;
|
|
992
|
+
this.F = SHA256_IV[5] | 0;
|
|
993
|
+
this.G = SHA256_IV[6] | 0;
|
|
994
|
+
this.H = SHA256_IV[7] | 0;
|
|
995
|
+
}
|
|
996
|
+
get() {
|
|
997
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
998
|
+
return [A, B, C, D, E, F, G, H];
|
|
999
|
+
}
|
|
1000
|
+
// prettier-ignore
|
|
1001
|
+
set(A, B, C, D, E, F, G, H) {
|
|
1002
|
+
this.A = A | 0;
|
|
1003
|
+
this.B = B | 0;
|
|
1004
|
+
this.C = C | 0;
|
|
1005
|
+
this.D = D | 0;
|
|
1006
|
+
this.E = E | 0;
|
|
1007
|
+
this.F = F | 0;
|
|
1008
|
+
this.G = G | 0;
|
|
1009
|
+
this.H = H | 0;
|
|
1010
|
+
}
|
|
1011
|
+
process(view, offset) {
|
|
1012
|
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
1013
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
1014
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
1015
|
+
for (let i = 16; i < 64; i++) {
|
|
1016
|
+
const W15 = SHA256_W[i - 15];
|
|
1017
|
+
const W2 = SHA256_W[i - 2];
|
|
1018
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
1019
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
1020
|
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
1021
|
+
}
|
|
1022
|
+
// Compression function main loop, 64 rounds
|
|
1023
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
1024
|
+
for (let i = 0; i < 64; i++) {
|
|
1025
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
1026
|
+
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
1027
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
1028
|
+
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
|
1029
|
+
H = G;
|
|
1030
|
+
G = F;
|
|
1031
|
+
F = E;
|
|
1032
|
+
E = (D + T1) | 0;
|
|
1033
|
+
D = C;
|
|
1034
|
+
C = B;
|
|
1035
|
+
B = A;
|
|
1036
|
+
A = (T1 + T2) | 0;
|
|
1037
|
+
}
|
|
1038
|
+
// Add the compressed chunk to the current hash value
|
|
1039
|
+
A = (A + this.A) | 0;
|
|
1040
|
+
B = (B + this.B) | 0;
|
|
1041
|
+
C = (C + this.C) | 0;
|
|
1042
|
+
D = (D + this.D) | 0;
|
|
1043
|
+
E = (E + this.E) | 0;
|
|
1044
|
+
F = (F + this.F) | 0;
|
|
1045
|
+
G = (G + this.G) | 0;
|
|
1046
|
+
H = (H + this.H) | 0;
|
|
1047
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
1048
|
+
}
|
|
1049
|
+
roundClean() {
|
|
1050
|
+
clean(SHA256_W);
|
|
1051
|
+
}
|
|
1052
|
+
destroy() {
|
|
1053
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1054
|
+
clean(this.buffer);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
// SHA2-512 is slower than sha256 in js because u64 operations are slow.
|
|
1058
|
+
// Round contants
|
|
1059
|
+
// First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
|
|
1060
|
+
// prettier-ignore
|
|
1061
|
+
const K512 = /* @__PURE__ */ (() => split([
|
|
1062
|
+
'0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
|
|
1063
|
+
'0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
|
|
1064
|
+
'0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
|
|
1065
|
+
'0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
|
|
1066
|
+
'0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
|
|
1067
|
+
'0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
|
|
1068
|
+
'0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
|
|
1069
|
+
'0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
|
|
1070
|
+
'0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
|
|
1071
|
+
'0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
|
|
1072
|
+
'0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
|
|
1073
|
+
'0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
|
|
1074
|
+
'0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
|
|
1075
|
+
'0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
|
|
1076
|
+
'0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
|
|
1077
|
+
'0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
|
|
1078
|
+
'0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
|
|
1079
|
+
'0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
|
|
1080
|
+
'0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
|
|
1081
|
+
'0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
|
|
1082
|
+
].map(n => BigInt(n))))();
|
|
1083
|
+
const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1084
|
+
const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1085
|
+
// Reusable temporary buffers
|
|
1086
|
+
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1087
|
+
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1088
|
+
class SHA512 extends HashMD {
|
|
1089
|
+
constructor(outputLen = 64) {
|
|
1090
|
+
super(128, outputLen, 16, false);
|
|
1091
|
+
// We cannot use array here since array allows indexing by variable
|
|
1092
|
+
// which means optimizer/compiler cannot use registers.
|
|
1093
|
+
// h -- high 32 bits, l -- low 32 bits
|
|
1094
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
1095
|
+
this.Al = SHA512_IV[1] | 0;
|
|
1096
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
1097
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
1098
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
1099
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
1100
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
1101
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
1102
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
1103
|
+
this.El = SHA512_IV[9] | 0;
|
|
1104
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
1105
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
1106
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
1107
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
1108
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
1109
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
1110
|
+
}
|
|
1111
|
+
// prettier-ignore
|
|
1112
|
+
get() {
|
|
1113
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1114
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
1115
|
+
}
|
|
1116
|
+
// prettier-ignore
|
|
1117
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
1118
|
+
this.Ah = Ah | 0;
|
|
1119
|
+
this.Al = Al | 0;
|
|
1120
|
+
this.Bh = Bh | 0;
|
|
1121
|
+
this.Bl = Bl | 0;
|
|
1122
|
+
this.Ch = Ch | 0;
|
|
1123
|
+
this.Cl = Cl | 0;
|
|
1124
|
+
this.Dh = Dh | 0;
|
|
1125
|
+
this.Dl = Dl | 0;
|
|
1126
|
+
this.Eh = Eh | 0;
|
|
1127
|
+
this.El = El | 0;
|
|
1128
|
+
this.Fh = Fh | 0;
|
|
1129
|
+
this.Fl = Fl | 0;
|
|
1130
|
+
this.Gh = Gh | 0;
|
|
1131
|
+
this.Gl = Gl | 0;
|
|
1132
|
+
this.Hh = Hh | 0;
|
|
1133
|
+
this.Hl = Hl | 0;
|
|
1134
|
+
}
|
|
1135
|
+
process(view, offset) {
|
|
1136
|
+
// Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
|
|
1137
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1138
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
1139
|
+
SHA512_W_L[i] = view.getUint32((offset += 4));
|
|
1140
|
+
}
|
|
1141
|
+
for (let i = 16; i < 80; i++) {
|
|
1142
|
+
// s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
|
|
1143
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
1144
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
1145
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
1146
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
1147
|
+
// s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
|
|
1148
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
1149
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1150
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1151
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1152
|
+
// SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
|
|
1153
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1154
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1155
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
1156
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
1157
|
+
}
|
|
1158
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1159
|
+
// Compression function main loop, 80 rounds
|
|
1160
|
+
for (let i = 0; i < 80; i++) {
|
|
1161
|
+
// S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
|
|
1162
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
1163
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
1164
|
+
//const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
1165
|
+
const CHIh = (Eh & Fh) ^ (~Eh & Gh);
|
|
1166
|
+
const CHIl = (El & Fl) ^ (~El & Gl);
|
|
1167
|
+
// T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
|
|
1168
|
+
// prettier-ignore
|
|
1169
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
1170
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
1171
|
+
const T1l = T1ll | 0;
|
|
1172
|
+
// S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
|
|
1173
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
1174
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
1175
|
+
const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
|
|
1176
|
+
const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
|
|
1177
|
+
Hh = Gh | 0;
|
|
1178
|
+
Hl = Gl | 0;
|
|
1179
|
+
Gh = Fh | 0;
|
|
1180
|
+
Gl = Fl | 0;
|
|
1181
|
+
Fh = Eh | 0;
|
|
1182
|
+
Fl = El | 0;
|
|
1183
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
1184
|
+
Dh = Ch | 0;
|
|
1185
|
+
Dl = Cl | 0;
|
|
1186
|
+
Ch = Bh | 0;
|
|
1187
|
+
Cl = Bl | 0;
|
|
1188
|
+
Bh = Ah | 0;
|
|
1189
|
+
Bl = Al | 0;
|
|
1190
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
1191
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
1192
|
+
Al = All | 0;
|
|
1193
|
+
}
|
|
1194
|
+
// Add the compressed chunk to the current hash value
|
|
1195
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
1196
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
1197
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
1198
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
1199
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
1200
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
1201
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
1202
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
1203
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1204
|
+
}
|
|
1205
|
+
roundClean() {
|
|
1206
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
1207
|
+
}
|
|
1208
|
+
destroy() {
|
|
1209
|
+
clean(this.buffer);
|
|
1210
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* SHA2-256 hash function from RFC 4634.
|
|
1215
|
+
*
|
|
1216
|
+
* It is the fastest JS hash, even faster than Blake3.
|
|
1217
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
1218
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1219
|
+
*/
|
|
1220
|
+
const sha256$1 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
1221
|
+
/** SHA2-512 hash function from RFC 4634. */
|
|
1222
|
+
const sha512 = /* @__PURE__ */ createHasher(() => new SHA512());
|
|
1223
|
+
|
|
1224
|
+
/**
|
|
1225
|
+
* SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
|
|
1226
|
+
*
|
|
1227
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
1228
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1229
|
+
*
|
|
1230
|
+
* Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
1231
|
+
* @module
|
|
1232
|
+
* @deprecated
|
|
1233
|
+
*/
|
|
1234
|
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
1235
|
+
const sha256 = sha256$1;
|
|
1236
|
+
|
|
1237
|
+
/**
|
|
1238
|
+
* Turns a `Uint8Array` into a string.
|
|
1239
|
+
*
|
|
1240
|
+
* Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
|
|
1241
|
+
*
|
|
1242
|
+
* Also `ascii` which is similar to node's 'binary' encoding.
|
|
1243
|
+
*/
|
|
1244
|
+
function toString(array, encoding = 'utf8') {
|
|
1245
|
+
const base = BASES[encoding];
|
|
1246
|
+
if (base == null) {
|
|
1247
|
+
throw new Error(`Unsupported encoding "${encoding}"`);
|
|
1248
|
+
}
|
|
1249
|
+
// strip multibase prefix
|
|
1250
|
+
return base.encoder.encode(array).substring(1);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
function numberToBytes(value) {
|
|
1254
|
+
const buffer = new ArrayBuffer(8);
|
|
1255
|
+
const view = new DataView(buffer);
|
|
1256
|
+
if (typeof value === "number") {
|
|
1257
|
+
view.setFloat64(0, value, false);
|
|
1258
|
+
}
|
|
1259
|
+
else {
|
|
1260
|
+
view.setBigInt64(0, value, false);
|
|
1261
|
+
}
|
|
1262
|
+
return new Uint8Array(buffer);
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Convert byte array to hex string (no `0x` prefix).
|
|
1266
|
+
*/
|
|
1267
|
+
const bytesToHex = (bytes) => toString(bytes, "base16");
|
|
1268
|
+
/**
|
|
1269
|
+
* Decode byte array to utf-8 string.
|
|
1270
|
+
*/
|
|
1271
|
+
const bytesToUtf8 = (b) => toString(b, "utf8");
|
|
1272
|
+
/**
|
|
1273
|
+
* Encode utf-8 string to byte array.
|
|
1274
|
+
*/
|
|
1275
|
+
const utf8ToBytes = (s) => fromString(s, "utf8");
|
|
1276
|
+
/**
|
|
1277
|
+
* Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
|
|
1278
|
+
*/
|
|
1279
|
+
function concat$1(byteArrays, totalLength) {
|
|
1280
|
+
const len = byteArrays.reduce((acc, curr) => acc + curr.length, 0);
|
|
1281
|
+
const res = new Uint8Array(len);
|
|
1282
|
+
let offset = 0;
|
|
1283
|
+
for (const bytes of byteArrays) {
|
|
1284
|
+
res.set(bytes, offset);
|
|
1285
|
+
offset += bytes.length;
|
|
1286
|
+
}
|
|
1287
|
+
return res;
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
function isAutoSharding(config) {
|
|
1291
|
+
return "clusterId" in config && "numShardsInCluster" in config;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
class BaseRoutingInfo {
|
|
1295
|
+
networkConfig;
|
|
1296
|
+
pubsubTopic;
|
|
1297
|
+
shardId;
|
|
1298
|
+
constructor(networkConfig, pubsubTopic, shardId) {
|
|
1299
|
+
this.networkConfig = networkConfig;
|
|
1300
|
+
this.pubsubTopic = pubsubTopic;
|
|
1301
|
+
this.shardId = shardId;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
class AutoShardingRoutingInfo extends BaseRoutingInfo {
|
|
1305
|
+
networkConfig;
|
|
1306
|
+
pubsubTopic;
|
|
1307
|
+
shardId;
|
|
1308
|
+
contentTopic;
|
|
1309
|
+
static fromContentTopic(contentTopic, networkConfig) {
|
|
1310
|
+
ensureValidContentTopic(contentTopic);
|
|
1311
|
+
const shardId = contentTopicToShardIndex(contentTopic, networkConfig.numShardsInCluster);
|
|
1312
|
+
const pubsubTopic = formatPubsubTopic(networkConfig.clusterId, shardId);
|
|
1313
|
+
return new AutoShardingRoutingInfo(networkConfig, pubsubTopic, shardId, contentTopic);
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* No checks are done with this constructor,
|
|
1317
|
+
* Be sure you check that the network config (auto vs static)
|
|
1318
|
+
* matches other parameters.
|
|
1319
|
+
*/
|
|
1320
|
+
constructor(networkConfig, pubsubTopic, shardId, contentTopic) {
|
|
1321
|
+
super(networkConfig, pubsubTopic, shardId);
|
|
1322
|
+
this.networkConfig = networkConfig;
|
|
1323
|
+
this.pubsubTopic = pubsubTopic;
|
|
1324
|
+
this.shardId = shardId;
|
|
1325
|
+
this.contentTopic = contentTopic;
|
|
1326
|
+
}
|
|
1327
|
+
get clusterId() {
|
|
1328
|
+
return this.networkConfig.clusterId;
|
|
1329
|
+
}
|
|
1330
|
+
get isAutoSharding() {
|
|
1331
|
+
return true;
|
|
1332
|
+
}
|
|
1333
|
+
get isStaticSharding() {
|
|
1334
|
+
return false;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
class StaticShardingRoutingInfo extends BaseRoutingInfo {
|
|
1338
|
+
networkConfig;
|
|
1339
|
+
pubsubTopic;
|
|
1340
|
+
shardId;
|
|
1341
|
+
/**
|
|
1342
|
+
* Create Routing Info for static sharding network, using shard
|
|
1343
|
+
*
|
|
1344
|
+
* @param shardId
|
|
1345
|
+
* @param networkConfig
|
|
1346
|
+
*/
|
|
1347
|
+
static fromShard(shardId, networkConfig) {
|
|
1348
|
+
const pubsubTopic = formatPubsubTopic(networkConfig.clusterId, shardId);
|
|
1349
|
+
return new StaticShardingRoutingInfo(networkConfig, pubsubTopic, shardId);
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Create Routing Info for static sharding network, using pubsub topic
|
|
1353
|
+
*
|
|
1354
|
+
* @param pubsubTopic
|
|
1355
|
+
* @param networkConfig
|
|
1356
|
+
*
|
|
1357
|
+
* @throws if the pubsub topic is malformed, or does not match the network config
|
|
1358
|
+
*/
|
|
1359
|
+
static fromPubsubTopic(pubsubTopic, networkConfig) {
|
|
1360
|
+
const { clusterId, shard } = pubsubTopicToSingleShardInfo(pubsubTopic);
|
|
1361
|
+
if (clusterId != networkConfig.clusterId)
|
|
1362
|
+
throw "Pubsub topic does not match network config's cluster id";
|
|
1363
|
+
return new StaticShardingRoutingInfo(networkConfig, pubsubTopic, shard);
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* No checks are done with this constructor,
|
|
1367
|
+
* Be sure you check that the network config (auto vs static)
|
|
1368
|
+
* matches other parameters.
|
|
1369
|
+
*/
|
|
1370
|
+
constructor(networkConfig, pubsubTopic, shardId) {
|
|
1371
|
+
super(networkConfig, pubsubTopic, shardId);
|
|
1372
|
+
this.networkConfig = networkConfig;
|
|
1373
|
+
this.pubsubTopic = pubsubTopic;
|
|
1374
|
+
this.shardId = shardId;
|
|
1375
|
+
}
|
|
1376
|
+
get clusterId() {
|
|
1377
|
+
return this.networkConfig.clusterId;
|
|
1378
|
+
}
|
|
1379
|
+
get isAutoSharding() {
|
|
1380
|
+
return false;
|
|
1381
|
+
}
|
|
1382
|
+
get isStaticSharding() {
|
|
1383
|
+
return true;
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
function createRoutingInfo(networkConfig, options) {
|
|
1387
|
+
if (isAutoSharding(networkConfig)) {
|
|
1388
|
+
if (options.contentTopic) {
|
|
1389
|
+
return AutoShardingRoutingInfo.fromContentTopic(options.contentTopic, networkConfig);
|
|
1390
|
+
}
|
|
1391
|
+
throw new Error("AutoSharding requires contentTopic");
|
|
1392
|
+
}
|
|
1393
|
+
else {
|
|
1394
|
+
if (options.shardId !== undefined) {
|
|
1395
|
+
return StaticShardingRoutingInfo.fromShard(options.shardId, networkConfig);
|
|
1396
|
+
}
|
|
1397
|
+
else if (options.pubsubTopic) {
|
|
1398
|
+
return StaticShardingRoutingInfo.fromPubsubTopic(options.pubsubTopic, networkConfig);
|
|
1399
|
+
}
|
|
1400
|
+
throw new Error("StaticSharding requires shardId or pubsubTopic");
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
const formatPubsubTopic = (clusterId, shard) => {
|
|
1405
|
+
return `/waku/2/rs/${clusterId}/${shard}`;
|
|
1406
|
+
};
|
|
1407
|
+
/**
|
|
1408
|
+
* @deprecated will be removed
|
|
1409
|
+
*/
|
|
1410
|
+
const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
|
|
1411
|
+
const parts = pubsubTopics.split("/");
|
|
1412
|
+
if (parts.length != 6 ||
|
|
1413
|
+
parts[1] !== "waku" ||
|
|
1414
|
+
parts[2] !== "2" ||
|
|
1415
|
+
parts[3] !== "rs")
|
|
1416
|
+
throw new Error("Invalid pubsub topic");
|
|
1417
|
+
const clusterId = parseInt(parts[4]);
|
|
1418
|
+
const shard = parseInt(parts[5]);
|
|
1419
|
+
if (isNaN(clusterId) || isNaN(shard))
|
|
1420
|
+
throw new Error("Invalid clusterId or shard");
|
|
1421
|
+
return {
|
|
1422
|
+
clusterId,
|
|
1423
|
+
shard
|
|
1424
|
+
};
|
|
1425
|
+
};
|
|
1426
|
+
/**
|
|
1427
|
+
* 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/
|
|
1428
|
+
* @param contentTopic String to validate
|
|
1429
|
+
* @returns Object with each content topic field as an attribute
|
|
1430
|
+
*/
|
|
1431
|
+
function ensureValidContentTopic(contentTopic) {
|
|
1432
|
+
const parts = contentTopic.split("/");
|
|
1433
|
+
if (parts.length < 5 || parts.length > 6) {
|
|
1434
|
+
throw Error(`Content topic format is invalid: ${contentTopic}`);
|
|
1435
|
+
}
|
|
1436
|
+
// Validate generation field if present
|
|
1437
|
+
let generation = 0;
|
|
1438
|
+
if (parts.length == 6) {
|
|
1439
|
+
generation = parseInt(parts[1]);
|
|
1440
|
+
if (isNaN(generation)) {
|
|
1441
|
+
throw new Error(`Invalid generation field in content topic: ${contentTopic}`);
|
|
1442
|
+
}
|
|
1443
|
+
if (generation > 0) {
|
|
1444
|
+
throw new Error(`Generation greater than 0 is not supported: ${contentTopic}`);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
// Validate remaining fields
|
|
1448
|
+
const fields = parts.splice(-4);
|
|
1449
|
+
// Validate application field
|
|
1450
|
+
if (fields[0].length == 0) {
|
|
1451
|
+
throw new Error(`Application field cannot be empty: ${contentTopic}`);
|
|
1452
|
+
}
|
|
1453
|
+
// Validate version field
|
|
1454
|
+
if (fields[1].length == 0) {
|
|
1455
|
+
throw new Error(`Version field cannot be empty: ${contentTopic}`);
|
|
1456
|
+
}
|
|
1457
|
+
// Validate topic name field
|
|
1458
|
+
if (fields[2].length == 0) {
|
|
1459
|
+
throw new Error(`Topic name field cannot be empty: ${contentTopic}`);
|
|
1460
|
+
}
|
|
1461
|
+
// Validate encoding field
|
|
1462
|
+
if (fields[3].length == 0) {
|
|
1463
|
+
throw new Error(`Encoding field cannot be empty: ${contentTopic}`);
|
|
1464
|
+
}
|
|
1465
|
+
return {
|
|
1466
|
+
generation,
|
|
1467
|
+
application: fields[0],
|
|
1468
|
+
version: fields[1],
|
|
1469
|
+
topicName: fields[2],
|
|
1470
|
+
encoding: fields[3]
|
|
1471
|
+
};
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Given a string, determines which autoshard index to use for its pubsub topic.
|
|
1475
|
+
* Based on the algorithm described in the RFC: https://rfc.vac.dev/spec/51//#algorithm
|
|
1476
|
+
*/
|
|
1477
|
+
function contentTopicToShardIndex(contentTopic, numShardsInCluster) {
|
|
1478
|
+
const { application, version } = ensureValidContentTopic(contentTopic);
|
|
1479
|
+
const digest = sha256(concat$1([utf8ToBytes(application), utf8ToBytes(version)]));
|
|
1480
|
+
const dataview = new DataView(digest.buffer.slice(-8));
|
|
1481
|
+
return Number(dataview.getBigUint64(0, false) % BigInt(numShardsInCluster));
|
|
1482
|
+
}
|
|
1483
|
+
|
|
584
1484
|
const decodeRelayShard = (bytes) => {
|
|
585
1485
|
// explicitly converting to Uint8Array to avoid Buffer
|
|
586
1486
|
// https://github.com/libp2p/js-libp2p/issues/2146
|
|
@@ -640,6 +1540,123 @@ var index$3 = /*#__PURE__*/Object.freeze({
|
|
|
640
1540
|
version_0: version_0
|
|
641
1541
|
});
|
|
642
1542
|
|
|
1543
|
+
var Protocols;
|
|
1544
|
+
(function (Protocols) {
|
|
1545
|
+
Protocols["Relay"] = "relay";
|
|
1546
|
+
Protocols["Store"] = "store";
|
|
1547
|
+
Protocols["LightPush"] = "lightpush";
|
|
1548
|
+
Protocols["Filter"] = "filter";
|
|
1549
|
+
})(Protocols || (Protocols = {}));
|
|
1550
|
+
var ProtocolError;
|
|
1551
|
+
(function (ProtocolError) {
|
|
1552
|
+
//
|
|
1553
|
+
// GENERAL ERRORS SECTION
|
|
1554
|
+
//
|
|
1555
|
+
/**
|
|
1556
|
+
* Could not determine the origin of the fault. Best to check connectivity and try again
|
|
1557
|
+
* */
|
|
1558
|
+
ProtocolError["GENERIC_FAIL"] = "Generic error";
|
|
1559
|
+
/**
|
|
1560
|
+
* The remote peer rejected the message. Information provided by the remote peer
|
|
1561
|
+
* is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
|
|
1562
|
+
* or `DECODE_FAILED` can be used.
|
|
1563
|
+
*/
|
|
1564
|
+
ProtocolError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
|
|
1565
|
+
/**
|
|
1566
|
+
* Failure to protobuf decode the message. May be due to a remote peer issue,
|
|
1567
|
+
* ensuring that messages are sent via several peer enable mitigation of this error.
|
|
1568
|
+
*/
|
|
1569
|
+
ProtocolError["DECODE_FAILED"] = "Failed to decode";
|
|
1570
|
+
/**
|
|
1571
|
+
* Failure to find a peer with suitable protocols. This may due to a connection issue.
|
|
1572
|
+
* Mitigation can be: retrying after a given time period, display connectivity issue
|
|
1573
|
+
* to user or listening for `peer:connected:bootstrap` or `peer:connected:peer-exchange`
|
|
1574
|
+
* on the connection manager before retrying.
|
|
1575
|
+
*/
|
|
1576
|
+
ProtocolError["NO_PEER_AVAILABLE"] = "No peer available";
|
|
1577
|
+
/**
|
|
1578
|
+
* Failure to find a stream to the peer. This may be because the connection with the peer is not still alive.
|
|
1579
|
+
* Mitigation can be: retrying after a given time period, or mitigation for `NO_PEER_AVAILABLE` can be used.
|
|
1580
|
+
*/
|
|
1581
|
+
ProtocolError["NO_STREAM_AVAILABLE"] = "No stream available";
|
|
1582
|
+
/**
|
|
1583
|
+
* The remote peer did not behave as expected. Mitigation for `NO_PEER_AVAILABLE`
|
|
1584
|
+
* or `DECODE_FAILED` can be used.
|
|
1585
|
+
*/
|
|
1586
|
+
ProtocolError["NO_RESPONSE"] = "No response received";
|
|
1587
|
+
//
|
|
1588
|
+
// SEND ERRORS SECTION
|
|
1589
|
+
//
|
|
1590
|
+
/**
|
|
1591
|
+
* Failure to protobuf encode the message. This is not recoverable and needs
|
|
1592
|
+
* further investigation.
|
|
1593
|
+
*/
|
|
1594
|
+
ProtocolError["ENCODE_FAILED"] = "Failed to encode";
|
|
1595
|
+
/**
|
|
1596
|
+
* The message payload is empty, making the message invalid. Ensure that a non-empty
|
|
1597
|
+
* payload is set on the outgoing message.
|
|
1598
|
+
*/
|
|
1599
|
+
ProtocolError["EMPTY_PAYLOAD"] = "Payload is empty";
|
|
1600
|
+
/**
|
|
1601
|
+
* The message size is above the maximum message size allowed on the Waku Network.
|
|
1602
|
+
* Compressing the message or using an alternative strategy for large messages is recommended.
|
|
1603
|
+
*/
|
|
1604
|
+
ProtocolError["SIZE_TOO_BIG"] = "Size is too big";
|
|
1605
|
+
/**
|
|
1606
|
+
* The PubsubTopic passed to the send function is not configured on the Waku node.
|
|
1607
|
+
* Please ensure that the PubsubTopic is used when initializing the Waku node.
|
|
1608
|
+
*/
|
|
1609
|
+
ProtocolError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
|
|
1610
|
+
/**
|
|
1611
|
+
* Fails when
|
|
1612
|
+
*/
|
|
1613
|
+
ProtocolError["STREAM_ABORTED"] = "Stream aborted";
|
|
1614
|
+
/**
|
|
1615
|
+
* General proof generation error message.
|
|
1616
|
+
* nwaku: https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L201C19-L201C42
|
|
1617
|
+
*/
|
|
1618
|
+
ProtocolError["RLN_PROOF_GENERATION"] = "Proof generation failed";
|
|
1619
|
+
//
|
|
1620
|
+
// RECEIVE ERRORS SECTION
|
|
1621
|
+
//
|
|
1622
|
+
/**
|
|
1623
|
+
* The pubsub topic configured on the decoder does not match the pubsub topic setup on the protocol.
|
|
1624
|
+
* Ensure that the pubsub topic used for decoder creation is the same as the one used for protocol.
|
|
1625
|
+
*/
|
|
1626
|
+
ProtocolError["TOPIC_DECODER_MISMATCH"] = "Topic decoder mismatch";
|
|
1627
|
+
/**
|
|
1628
|
+
* The topics passed in the decoders do not match each other, or don't exist at all.
|
|
1629
|
+
* Ensure that all the pubsub topics used in the decoders are valid and match each other.
|
|
1630
|
+
*/
|
|
1631
|
+
ProtocolError["INVALID_DECODER_TOPICS"] = "Invalid decoder topics";
|
|
1632
|
+
})(ProtocolError || (ProtocolError = {}));
|
|
1633
|
+
|
|
1634
|
+
// Peer tags
|
|
1635
|
+
var Tags;
|
|
1636
|
+
(function (Tags) {
|
|
1637
|
+
Tags["BOOTSTRAP"] = "bootstrap";
|
|
1638
|
+
Tags["PEER_EXCHANGE"] = "peer-exchange";
|
|
1639
|
+
Tags["LOCAL"] = "local-peer-cache";
|
|
1640
|
+
})(Tags || (Tags = {}));
|
|
1641
|
+
// Connection tag
|
|
1642
|
+
const CONNECTION_LOCKED_TAG = "locked";
|
|
1643
|
+
|
|
1644
|
+
var HealthStatus;
|
|
1645
|
+
(function (HealthStatus) {
|
|
1646
|
+
/**
|
|
1647
|
+
* No peer connections
|
|
1648
|
+
*/
|
|
1649
|
+
HealthStatus["Unhealthy"] = "Unhealthy";
|
|
1650
|
+
/**
|
|
1651
|
+
* At least 1 peer supporting both Filter and LightPush protocols
|
|
1652
|
+
*/
|
|
1653
|
+
HealthStatus["MinimallyHealthy"] = "MinimallyHealthy";
|
|
1654
|
+
/**
|
|
1655
|
+
* At least 2 peers supporting both Filter and LightPush protocols
|
|
1656
|
+
*/
|
|
1657
|
+
HealthStatus["SufficientlyHealthy"] = "SufficientlyHealthy";
|
|
1658
|
+
})(HealthStatus || (HealthStatus = {}));
|
|
1659
|
+
|
|
643
1660
|
/**
|
|
644
1661
|
* @packageDocumentation
|
|
645
1662
|
*
|
|
@@ -3347,9 +4364,9 @@ class Dialer {
|
|
|
3347
4364
|
log$5.info(`Skipping peer ${peerId} - no shard info`);
|
|
3348
4365
|
return false;
|
|
3349
4366
|
}
|
|
3350
|
-
const
|
|
3351
|
-
if (!
|
|
3352
|
-
log$5.info(`Skipping peer ${peerId} - not on same
|
|
4367
|
+
const isOnSameCluster = await this.shardReader.isPeerOnCluster(peerId);
|
|
4368
|
+
if (!isOnSameCluster) {
|
|
4369
|
+
log$5.info(`Skipping peer ${peerId} - not on same cluster`);
|
|
3353
4370
|
return true;
|
|
3354
4371
|
}
|
|
3355
4372
|
return false;
|
|
@@ -3448,13 +4465,15 @@ const RelayPingContentTopic = "/relay-ping/1/ping/null";
|
|
|
3448
4465
|
const log$3 = new Logger("keep-alive");
|
|
3449
4466
|
class KeepAliveManager {
|
|
3450
4467
|
relay;
|
|
4468
|
+
networkConfig;
|
|
3451
4469
|
libp2p;
|
|
3452
4470
|
options;
|
|
3453
4471
|
pingKeepAliveTimers = new Map();
|
|
3454
4472
|
relayKeepAliveTimers = new Map();
|
|
3455
|
-
constructor({ options, relay, libp2p }) {
|
|
4473
|
+
constructor({ options, relay, networkConfig, libp2p }) {
|
|
3456
4474
|
this.options = options;
|
|
3457
4475
|
this.relay = relay;
|
|
4476
|
+
this.networkConfig = networkConfig;
|
|
3458
4477
|
this.libp2p = libp2p;
|
|
3459
4478
|
this.onPeerConnect = this.onPeerConnect.bind(this);
|
|
3460
4479
|
this.onPeerDisconnect = this.onPeerDisconnect.bind(this);
|
|
@@ -3538,8 +4557,12 @@ class KeepAliveManager {
|
|
|
3538
4557
|
log$3.warn(`Peer: ${peerId.toString()} is not in the mesh for topic: ${topic}, skipping start for relay ping`);
|
|
3539
4558
|
continue;
|
|
3540
4559
|
}
|
|
4560
|
+
const routingInfo = createRoutingInfo(this.networkConfig, {
|
|
4561
|
+
contentTopic: RelayPingContentTopic,
|
|
4562
|
+
pubsubTopic: topic
|
|
4563
|
+
});
|
|
3541
4564
|
const encoder = createEncoder({
|
|
3542
|
-
|
|
4565
|
+
routingInfo: routingInfo,
|
|
3543
4566
|
contentTopic: RelayPingContentTopic,
|
|
3544
4567
|
ephemeral: true
|
|
3545
4568
|
});
|
|
@@ -3676,28 +4699,28 @@ const log$2 = new Logger("shard-reader");
|
|
|
3676
4699
|
*/
|
|
3677
4700
|
class ShardReader {
|
|
3678
4701
|
libp2p;
|
|
3679
|
-
|
|
4702
|
+
clusterId;
|
|
3680
4703
|
constructor(options) {
|
|
3681
4704
|
this.libp2p = options.libp2p;
|
|
3682
|
-
this.
|
|
4705
|
+
this.clusterId = options.networkConfig.clusterId;
|
|
3683
4706
|
}
|
|
3684
|
-
async
|
|
3685
|
-
const
|
|
3686
|
-
if (!
|
|
4707
|
+
async isPeerOnCluster(id) {
|
|
4708
|
+
const peerRelayShards = await this.getRelayShards(id);
|
|
4709
|
+
if (!peerRelayShards) {
|
|
3687
4710
|
return false;
|
|
3688
4711
|
}
|
|
3689
|
-
|
|
3690
|
-
const shardOverlap = this.staticShard.shards.some((s) => shardInfo.shards.includes(s));
|
|
3691
|
-
return clusterMatch && shardOverlap;
|
|
4712
|
+
return peerRelayShards.clusterId === this.clusterId;
|
|
3692
4713
|
}
|
|
3693
4714
|
async hasShardInfo(id) {
|
|
3694
|
-
const shardInfo = await this.
|
|
4715
|
+
const shardInfo = await this.getRelayShards(id);
|
|
3695
4716
|
return !!shardInfo;
|
|
3696
4717
|
}
|
|
3697
4718
|
async isPeerOnTopic(id, pubsubTopic) {
|
|
3698
4719
|
try {
|
|
3699
|
-
const
|
|
3700
|
-
|
|
4720
|
+
const { clusterId, shard } = pubsubTopicToSingleShardInfo(pubsubTopic);
|
|
4721
|
+
if (clusterId !== this.clusterId)
|
|
4722
|
+
return false;
|
|
4723
|
+
return await this.isPeerOnShard(id, shard);
|
|
3701
4724
|
}
|
|
3702
4725
|
catch (error) {
|
|
3703
4726
|
log$2.error(`Error comparing pubsub topic ${pubsubTopic} with shard info for ${id}`, error);
|
|
@@ -3705,38 +4728,29 @@ class ShardReader {
|
|
|
3705
4728
|
}
|
|
3706
4729
|
}
|
|
3707
4730
|
async isPeerOnShard(id, shard) {
|
|
3708
|
-
const peerShardInfo = await this.
|
|
3709
|
-
if
|
|
4731
|
+
const peerShardInfo = await this.getRelayShards(id);
|
|
4732
|
+
log$2.info(`Checking if peer on same shard: this { clusterId: ${this.clusterId}, shardId: ${shard} },` +
|
|
4733
|
+
`${id} { clusterId: ${peerShardInfo?.clusterId}, shards: ${peerShardInfo?.shards} }`);
|
|
4734
|
+
if (!peerShardInfo) {
|
|
3710
4735
|
return false;
|
|
3711
4736
|
}
|
|
3712
|
-
return (peerShardInfo.clusterId ===
|
|
3713
|
-
peerShardInfo.shards.includes(shard
|
|
4737
|
+
return (peerShardInfo.clusterId === this.clusterId &&
|
|
4738
|
+
peerShardInfo.shards.includes(shard));
|
|
3714
4739
|
}
|
|
3715
|
-
async
|
|
4740
|
+
async getRelayShards(id) {
|
|
3716
4741
|
try {
|
|
3717
4742
|
const peer = await this.libp2p.peerStore.get(id);
|
|
3718
4743
|
const shardInfoBytes = peer.metadata.get("shardInfo");
|
|
3719
4744
|
if (!shardInfoBytes) {
|
|
3720
4745
|
return undefined;
|
|
3721
4746
|
}
|
|
3722
|
-
|
|
3723
|
-
return decodedShardInfo;
|
|
4747
|
+
return decodeRelayShard(shardInfoBytes);
|
|
3724
4748
|
}
|
|
3725
4749
|
catch (error) {
|
|
3726
4750
|
log$2.error(`Error getting shard info for ${id}`, error);
|
|
3727
4751
|
return undefined;
|
|
3728
4752
|
}
|
|
3729
4753
|
}
|
|
3730
|
-
getStaticShardFromNetworkConfig(networkConfig) {
|
|
3731
|
-
if ("shards" in networkConfig) {
|
|
3732
|
-
return networkConfig;
|
|
3733
|
-
}
|
|
3734
|
-
const shards = networkConfig.contentTopics.map((topic) => contentTopicToShardIndex(topic));
|
|
3735
|
-
return {
|
|
3736
|
-
clusterId: networkConfig.clusterId,
|
|
3737
|
-
shards
|
|
3738
|
-
};
|
|
3739
|
-
}
|
|
3740
4754
|
}
|
|
3741
4755
|
|
|
3742
4756
|
/**
|
|
@@ -4144,11 +5158,11 @@ function hexToNumber(hex) {
|
|
|
4144
5158
|
}
|
|
4145
5159
|
// BE: Big Endian, LE: Little Endian
|
|
4146
5160
|
function bytesToNumberBE(bytes) {
|
|
4147
|
-
return hexToNumber(bytesToHex(bytes));
|
|
5161
|
+
return hexToNumber(bytesToHex$1(bytes));
|
|
4148
5162
|
}
|
|
4149
5163
|
function bytesToNumberLE(bytes) {
|
|
4150
5164
|
abytes(bytes);
|
|
4151
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
5165
|
+
return hexToNumber(bytesToHex$1(Uint8Array.from(bytes).reverse()));
|
|
4152
5166
|
}
|
|
4153
5167
|
function numberToBytesBE(n, len) {
|
|
4154
5168
|
return hexToBytes(n.toString(16).padStart(len * 2, '0'));
|
|
@@ -5422,7 +6436,7 @@ function edwards(CURVE, curveOpts = {}) {
|
|
|
5422
6436
|
return this.toBytes();
|
|
5423
6437
|
}
|
|
5424
6438
|
toHex() {
|
|
5425
|
-
return bytesToHex(this.toBytes());
|
|
6439
|
+
return bytesToHex$1(this.toBytes());
|
|
5426
6440
|
}
|
|
5427
6441
|
toString() {
|
|
5428
6442
|
return `<Point ${this.is0() ? 'ZERO' : this.toHex()}>`;
|
|
@@ -6692,7 +7706,7 @@ function weierstrassN(CURVE, curveOpts = {}) {
|
|
|
6692
7706
|
return this.toBytes(isCompressed);
|
|
6693
7707
|
}
|
|
6694
7708
|
toHex(isCompressed = true) {
|
|
6695
|
-
return bytesToHex(this.toBytes(isCompressed));
|
|
7709
|
+
return bytesToHex$1(this.toBytes(isCompressed));
|
|
6696
7710
|
}
|
|
6697
7711
|
toString() {
|
|
6698
7712
|
return `<Point ${this.is0() ? 'ZERO' : this.toHex()}>`;
|
|
@@ -6822,14 +7836,14 @@ function ecdsa(Point, ecdsaOpts, curveOpts = {}) {
|
|
|
6822
7836
|
return this.toBytes('der');
|
|
6823
7837
|
}
|
|
6824
7838
|
toDERHex() {
|
|
6825
|
-
return bytesToHex(this.toBytes('der'));
|
|
7839
|
+
return bytesToHex$1(this.toBytes('der'));
|
|
6826
7840
|
}
|
|
6827
7841
|
// padded bytes of r, then padded bytes of s
|
|
6828
7842
|
toCompactRawBytes() {
|
|
6829
7843
|
return this.toBytes('compact');
|
|
6830
7844
|
}
|
|
6831
7845
|
toCompactHex() {
|
|
6832
|
-
return bytesToHex(this.toBytes('compact'));
|
|
7846
|
+
return bytesToHex$1(this.toBytes('compact'));
|
|
6833
7847
|
}
|
|
6834
7848
|
}
|
|
6835
7849
|
const normPrivateKeyToScalar = _legacyHelperNormPriv(Fn, curveOpts.allowedPrivateKeyLengths, curveOpts.wrapPrivateKey);
|
|
@@ -7283,7 +8297,7 @@ const secp256k1 = createCurve({
|
|
|
7283
8297
|
* Hash message and verify signature with public key
|
|
7284
8298
|
*/
|
|
7285
8299
|
function hashAndVerify(key, sig, msg, options) {
|
|
7286
|
-
const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray());
|
|
8300
|
+
const p = sha256$2.digest(msg instanceof Uint8Array ? msg : msg.subarray());
|
|
7287
8301
|
if (isPromise(p)) {
|
|
7288
8302
|
return p
|
|
7289
8303
|
.then(({ digest }) => {
|
|
@@ -7614,7 +8628,7 @@ function isIdentityMultihash(multihash) {
|
|
|
7614
8628
|
return multihash.code === identity.code;
|
|
7615
8629
|
}
|
|
7616
8630
|
function isSha256Multihash(multihash) {
|
|
7617
|
-
return multihash.code === sha256.code;
|
|
8631
|
+
return multihash.code === sha256$2.code;
|
|
7618
8632
|
}
|
|
7619
8633
|
|
|
7620
8634
|
/**
|
|
@@ -9048,6 +10062,7 @@ class ConnectionManager {
|
|
|
9048
10062
|
this.keepAliveManager = new KeepAliveManager({
|
|
9049
10063
|
relay: options.relay,
|
|
9050
10064
|
libp2p: options.libp2p,
|
|
10065
|
+
networkConfig: options.networkConfig,
|
|
9051
10066
|
options: {
|
|
9052
10067
|
pingKeepAlive: this.options.pingKeepAlive,
|
|
9053
10068
|
relayKeepAlive: this.options.relayKeepAlive
|
|
@@ -9144,6 +10159,9 @@ class ConnectionManager {
|
|
|
9144
10159
|
async isPeerOnTopic(peerId, pubsubTopic) {
|
|
9145
10160
|
return this.shardReader.isPeerOnTopic(peerId, pubsubTopic);
|
|
9146
10161
|
}
|
|
10162
|
+
async isPeerOnShard(peerId, shardId) {
|
|
10163
|
+
return this.shardReader.isPeerOnShard(peerId, shardId);
|
|
10164
|
+
}
|
|
9147
10165
|
}
|
|
9148
10166
|
|
|
9149
10167
|
const log = new Logger("metadata");
|
|
@@ -9301,7 +10319,7 @@ function messageHash(pubsubTopic, message) {
|
|
|
9301
10319
|
message.meta,
|
|
9302
10320
|
timestampBytes
|
|
9303
10321
|
].filter(isDefined));
|
|
9304
|
-
return sha256
|
|
10322
|
+
return sha256(bytes);
|
|
9305
10323
|
}
|
|
9306
10324
|
function tryConvertTimestampToBytes(timestamp) {
|
|
9307
10325
|
if (!timestamp) {
|
|
@@ -9342,7 +10360,7 @@ function tryConvertTimestampToBytes(timestamp) {
|
|
|
9342
10360
|
*/
|
|
9343
10361
|
function messageHashStr(pubsubTopic, message) {
|
|
9344
10362
|
const hash = messageHash(pubsubTopic, message);
|
|
9345
|
-
const hashStr = bytesToHex
|
|
10363
|
+
const hashStr = bytesToHex(hash);
|
|
9346
10364
|
return hashStr;
|
|
9347
10365
|
}
|
|
9348
10366
|
|