@waku/core 0.0.33-aeb05cd.0 → 0.0.33-b7bdb60.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/bundle/base_protocol-BDjsZTsQ.js +275 -0
  2. package/bundle/{index-tdQNdKHx.js → index-CeEH6b9b.js} +24 -450
  3. package/bundle/index.js +99 -419
  4. package/bundle/lib/base_protocol.js +2 -2
  5. package/bundle/lib/message/version_0.js +2 -2
  6. package/bundle/{version_0-BrbNEwD-.js → version_0-BYg0O3M-.js} +451 -2
  7. package/dist/.tsbuildinfo +1 -1
  8. package/dist/index.d.ts +1 -2
  9. package/dist/index.js +1 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/lib/base_protocol.d.ts +2 -3
  12. package/dist/lib/base_protocol.js +7 -10
  13. package/dist/lib/base_protocol.js.map +1 -1
  14. package/dist/lib/filter/index.d.ts +1 -2
  15. package/dist/lib/filter/index.js +1 -4
  16. package/dist/lib/filter/index.js.map +1 -1
  17. package/dist/lib/stream_manager/stream_manager.d.ts +12 -9
  18. package/dist/lib/stream_manager/stream_manager.js +87 -56
  19. package/dist/lib/stream_manager/stream_manager.js.map +1 -1
  20. package/dist/lib/stream_manager/utils.d.ts +1 -1
  21. package/dist/lib/stream_manager/utils.js +5 -17
  22. package/dist/lib/stream_manager/utils.js.map +1 -1
  23. package/package.json +1 -1
  24. package/src/index.ts +1 -3
  25. package/src/lib/base_protocol.ts +8 -22
  26. package/src/lib/filter/index.ts +0 -2
  27. package/src/lib/stream_manager/stream_manager.ts +124 -66
  28. package/src/lib/stream_manager/utils.ts +5 -17
  29. package/bundle/base_protocol-0xHh0_Hq.js +0 -334
  30. package/dist/lib/wait_for_remote_peer.d.ts +0 -22
  31. package/dist/lib/wait_for_remote_peer.js +0 -142
  32. package/dist/lib/wait_for_remote_peer.js.map +0 -1
  33. package/src/lib/wait_for_remote_peer.ts +0 -200
@@ -1,2 +1,2 @@
1
- import '../index-tdQNdKHx.js';
2
- export { B as BaseProtocol } from '../base_protocol-0xHh0_Hq.js';
1
+ export { B as BaseProtocol } from '../base_protocol-BDjsZTsQ.js';
2
+ import '../index-CeEH6b9b.js';
@@ -1,2 +1,2 @@
1
- export { D as DecodedMessage, j as Decoder, E as Encoder, V as Version, i as createDecoder, g as createEncoder, m as proto } from '../../version_0-BrbNEwD-.js';
2
- import '../../index-tdQNdKHx.js';
1
+ export { D as DecodedMessage, k as Decoder, E as Encoder, V as Version, j as createDecoder, g as createEncoder, m as proto } from '../../version_0-BYg0O3M-.js';
2
+ import '../../index-CeEH6b9b.js';
@@ -1,4 +1,4 @@
1
- import { a as allocUnsafe, f as fromString, b as alloc$1, L as Logger, g as determinePubsubTopic } from './index-tdQNdKHx.js';
1
+ import { a as allocUnsafe, f as fromString, b as alloc$1, e as concat, u as utf8ToBytes$1, L as Logger } from './index-CeEH6b9b.js';
2
2
 
3
3
  /* eslint-disable no-fallthrough */
4
4
  const N1 = Math.pow(2, 7);
@@ -3750,6 +3750,455 @@ var WakuMetadataResponse;
3750
3750
  };
3751
3751
  })(WakuMetadataResponse || (WakuMetadataResponse = {}));
3752
3752
 
3753
+ // copied from utils
3754
+ function isBytes(a) {
3755
+ return (a instanceof Uint8Array ||
3756
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
3757
+ }
3758
+ function bytes(b, ...lengths) {
3759
+ if (!isBytes(b))
3760
+ throw new Error('Uint8Array expected');
3761
+ if (lengths.length > 0 && !lengths.includes(b.length))
3762
+ throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
3763
+ }
3764
+ function exists(instance, checkFinished = true) {
3765
+ if (instance.destroyed)
3766
+ throw new Error('Hash instance has been destroyed');
3767
+ if (checkFinished && instance.finished)
3768
+ throw new Error('Hash#digest() has already been called');
3769
+ }
3770
+ function output(out, instance) {
3771
+ bytes(out);
3772
+ const min = instance.outputLen;
3773
+ if (out.length < min) {
3774
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
3775
+ }
3776
+ }
3777
+
3778
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
3779
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
3780
+ // node.js versions earlier than v19 don't declare it in global scope.
3781
+ // For node.js, package.json#exports field mapping rewrites import
3782
+ // from `crypto` to `cryptoNode`, which imports native module.
3783
+ // Makes the utils un-importable in browsers without a bundler.
3784
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
3785
+ // Cast array to view
3786
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
3787
+ // The rotate right (circular right shift) operation for uint32
3788
+ const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
3789
+ new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
3790
+ /**
3791
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
3792
+ */
3793
+ function utf8ToBytes(str) {
3794
+ if (typeof str !== 'string')
3795
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
3796
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
3797
+ }
3798
+ /**
3799
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
3800
+ * Warning: when Uint8Array is passed, it would NOT get copied.
3801
+ * Keep in mind for future mutable operations.
3802
+ */
3803
+ function toBytes(data) {
3804
+ if (typeof data === 'string')
3805
+ data = utf8ToBytes(data);
3806
+ bytes(data);
3807
+ return data;
3808
+ }
3809
+ // For runtime check if class implements interface
3810
+ class Hash {
3811
+ // Safe version that clones internal state
3812
+ clone() {
3813
+ return this._cloneInto();
3814
+ }
3815
+ }
3816
+ function wrapConstructor(hashCons) {
3817
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
3818
+ const tmp = hashCons();
3819
+ hashC.outputLen = tmp.outputLen;
3820
+ hashC.blockLen = tmp.blockLen;
3821
+ hashC.create = () => hashCons();
3822
+ return hashC;
3823
+ }
3824
+
3825
+ /**
3826
+ * Polyfill for Safari 14
3827
+ */
3828
+ function setBigUint64(view, byteOffset, value, isLE) {
3829
+ if (typeof view.setBigUint64 === 'function')
3830
+ return view.setBigUint64(byteOffset, value, isLE);
3831
+ const _32n = BigInt(32);
3832
+ const _u32_max = BigInt(0xffffffff);
3833
+ const wh = Number((value >> _32n) & _u32_max);
3834
+ const wl = Number(value & _u32_max);
3835
+ const h = isLE ? 4 : 0;
3836
+ const l = isLE ? 0 : 4;
3837
+ view.setUint32(byteOffset + h, wh, isLE);
3838
+ view.setUint32(byteOffset + l, wl, isLE);
3839
+ }
3840
+ /**
3841
+ * Choice: a ? b : c
3842
+ */
3843
+ const Chi = (a, b, c) => (a & b) ^ (~a & c);
3844
+ /**
3845
+ * Majority function, true if any two inputs is true
3846
+ */
3847
+ const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
3848
+ /**
3849
+ * Merkle-Damgard hash construction base class.
3850
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
3851
+ */
3852
+ class HashMD extends Hash {
3853
+ constructor(blockLen, outputLen, padOffset, isLE) {
3854
+ super();
3855
+ this.blockLen = blockLen;
3856
+ this.outputLen = outputLen;
3857
+ this.padOffset = padOffset;
3858
+ this.isLE = isLE;
3859
+ this.finished = false;
3860
+ this.length = 0;
3861
+ this.pos = 0;
3862
+ this.destroyed = false;
3863
+ this.buffer = new Uint8Array(blockLen);
3864
+ this.view = createView(this.buffer);
3865
+ }
3866
+ update(data) {
3867
+ exists(this);
3868
+ const { view, buffer, blockLen } = this;
3869
+ data = toBytes(data);
3870
+ const len = data.length;
3871
+ for (let pos = 0; pos < len;) {
3872
+ const take = Math.min(blockLen - this.pos, len - pos);
3873
+ // Fast path: we have at least one block in input, cast it to view and process
3874
+ if (take === blockLen) {
3875
+ const dataView = createView(data);
3876
+ for (; blockLen <= len - pos; pos += blockLen)
3877
+ this.process(dataView, pos);
3878
+ continue;
3879
+ }
3880
+ buffer.set(data.subarray(pos, pos + take), this.pos);
3881
+ this.pos += take;
3882
+ pos += take;
3883
+ if (this.pos === blockLen) {
3884
+ this.process(view, 0);
3885
+ this.pos = 0;
3886
+ }
3887
+ }
3888
+ this.length += data.length;
3889
+ this.roundClean();
3890
+ return this;
3891
+ }
3892
+ digestInto(out) {
3893
+ exists(this);
3894
+ output(out, this);
3895
+ this.finished = true;
3896
+ // Padding
3897
+ // We can avoid allocation of buffer for padding completely if it
3898
+ // was previously not allocated here. But it won't change performance.
3899
+ const { buffer, view, blockLen, isLE } = this;
3900
+ let { pos } = this;
3901
+ // append the bit '1' to the message
3902
+ buffer[pos++] = 0b10000000;
3903
+ this.buffer.subarray(pos).fill(0);
3904
+ // we have less than padOffset left in buffer, so we cannot put length in
3905
+ // current block, need process it and pad again
3906
+ if (this.padOffset > blockLen - pos) {
3907
+ this.process(view, 0);
3908
+ pos = 0;
3909
+ }
3910
+ // Pad until full block byte with zeros
3911
+ for (let i = pos; i < blockLen; i++)
3912
+ buffer[i] = 0;
3913
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
3914
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
3915
+ // So we just write lowest 64 bits of that value.
3916
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
3917
+ this.process(view, 0);
3918
+ const oview = createView(out);
3919
+ const len = this.outputLen;
3920
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
3921
+ if (len % 4)
3922
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
3923
+ const outLen = len / 4;
3924
+ const state = this.get();
3925
+ if (outLen > state.length)
3926
+ throw new Error('_sha2: outputLen bigger than state');
3927
+ for (let i = 0; i < outLen; i++)
3928
+ oview.setUint32(4 * i, state[i], isLE);
3929
+ }
3930
+ digest() {
3931
+ const { buffer, outputLen } = this;
3932
+ this.digestInto(buffer);
3933
+ const res = buffer.slice(0, outputLen);
3934
+ this.destroy();
3935
+ return res;
3936
+ }
3937
+ _cloneInto(to) {
3938
+ to || (to = new this.constructor());
3939
+ to.set(...this.get());
3940
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
3941
+ to.length = length;
3942
+ to.pos = pos;
3943
+ to.finished = finished;
3944
+ to.destroyed = destroyed;
3945
+ if (length % blockLen)
3946
+ to.buffer.set(buffer);
3947
+ return to;
3948
+ }
3949
+ }
3950
+
3951
+ // SHA2-256 need to try 2^128 hashes to execute birthday attack.
3952
+ // BTC network is doing 2^67 hashes/sec as per early 2023.
3953
+ // Round constants:
3954
+ // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
3955
+ // prettier-ignore
3956
+ const SHA256_K = /* @__PURE__ */ new Uint32Array([
3957
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
3958
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
3959
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
3960
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
3961
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
3962
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
3963
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
3964
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
3965
+ ]);
3966
+ // Initial state:
3967
+ // first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
3968
+ // prettier-ignore
3969
+ const SHA256_IV = /* @__PURE__ */ new Uint32Array([
3970
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
3971
+ ]);
3972
+ // Temporary buffer, not used to store anything between runs
3973
+ // Named this way because it matches specification.
3974
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
3975
+ class SHA256 extends HashMD {
3976
+ constructor() {
3977
+ super(64, 32, 8, false);
3978
+ // We cannot use array here since array allows indexing by variable
3979
+ // which means optimizer/compiler cannot use registers.
3980
+ this.A = SHA256_IV[0] | 0;
3981
+ this.B = SHA256_IV[1] | 0;
3982
+ this.C = SHA256_IV[2] | 0;
3983
+ this.D = SHA256_IV[3] | 0;
3984
+ this.E = SHA256_IV[4] | 0;
3985
+ this.F = SHA256_IV[5] | 0;
3986
+ this.G = SHA256_IV[6] | 0;
3987
+ this.H = SHA256_IV[7] | 0;
3988
+ }
3989
+ get() {
3990
+ const { A, B, C, D, E, F, G, H } = this;
3991
+ return [A, B, C, D, E, F, G, H];
3992
+ }
3993
+ // prettier-ignore
3994
+ set(A, B, C, D, E, F, G, H) {
3995
+ this.A = A | 0;
3996
+ this.B = B | 0;
3997
+ this.C = C | 0;
3998
+ this.D = D | 0;
3999
+ this.E = E | 0;
4000
+ this.F = F | 0;
4001
+ this.G = G | 0;
4002
+ this.H = H | 0;
4003
+ }
4004
+ process(view, offset) {
4005
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
4006
+ for (let i = 0; i < 16; i++, offset += 4)
4007
+ SHA256_W[i] = view.getUint32(offset, false);
4008
+ for (let i = 16; i < 64; i++) {
4009
+ const W15 = SHA256_W[i - 15];
4010
+ const W2 = SHA256_W[i - 2];
4011
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
4012
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
4013
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
4014
+ }
4015
+ // Compression function main loop, 64 rounds
4016
+ let { A, B, C, D, E, F, G, H } = this;
4017
+ for (let i = 0; i < 64; i++) {
4018
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
4019
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
4020
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
4021
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
4022
+ H = G;
4023
+ G = F;
4024
+ F = E;
4025
+ E = (D + T1) | 0;
4026
+ D = C;
4027
+ C = B;
4028
+ B = A;
4029
+ A = (T1 + T2) | 0;
4030
+ }
4031
+ // Add the compressed chunk to the current hash value
4032
+ A = (A + this.A) | 0;
4033
+ B = (B + this.B) | 0;
4034
+ C = (C + this.C) | 0;
4035
+ D = (D + this.D) | 0;
4036
+ E = (E + this.E) | 0;
4037
+ F = (F + this.F) | 0;
4038
+ G = (G + this.G) | 0;
4039
+ H = (H + this.H) | 0;
4040
+ this.set(A, B, C, D, E, F, G, H);
4041
+ }
4042
+ roundClean() {
4043
+ SHA256_W.fill(0);
4044
+ }
4045
+ destroy() {
4046
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
4047
+ this.buffer.fill(0);
4048
+ }
4049
+ }
4050
+ /**
4051
+ * SHA2-256 hash function
4052
+ * @param message - data that would be hashed
4053
+ */
4054
+ const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
4055
+
4056
+ /**
4057
+ * The default cluster ID for The Waku Network
4058
+ */
4059
+ const DEFAULT_CLUSTER_ID = 1;
4060
+
4061
+ const singleShardInfoToPubsubTopic = (shardInfo) => {
4062
+ if (shardInfo.shard === undefined)
4063
+ throw new Error("Invalid shard");
4064
+ return `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${shardInfo.shard}`;
4065
+ };
4066
+ const shardInfoToPubsubTopics = (shardInfo) => {
4067
+ if ("contentTopics" in shardInfo && shardInfo.contentTopics) {
4068
+ // Autosharding: explicitly defined content topics
4069
+ return Array.from(new Set(shardInfo.contentTopics.map((contentTopic) => contentTopicToPubsubTopic(contentTopic, shardInfo.clusterId))));
4070
+ }
4071
+ else if ("shards" in shardInfo) {
4072
+ // Static sharding
4073
+ if (shardInfo.shards === undefined)
4074
+ throw new Error("Invalid shard");
4075
+ return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${index}`)));
4076
+ }
4077
+ else if ("application" in shardInfo && "version" in shardInfo) {
4078
+ // Autosharding: single shard from application and version
4079
+ return [
4080
+ contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`, shardInfo.clusterId)
4081
+ ];
4082
+ }
4083
+ else {
4084
+ throw new Error("Missing required configuration in shard parameters");
4085
+ }
4086
+ };
4087
+ const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
4088
+ const parts = pubsubTopics.split("/");
4089
+ if (parts.length != 6 ||
4090
+ parts[1] !== "waku" ||
4091
+ parts[2] !== "2" ||
4092
+ parts[3] !== "rs")
4093
+ throw new Error("Invalid pubsub topic");
4094
+ const clusterId = parseInt(parts[4]);
4095
+ const shard = parseInt(parts[5]);
4096
+ if (isNaN(clusterId) || isNaN(shard))
4097
+ throw new Error("Invalid clusterId or shard");
4098
+ return {
4099
+ clusterId,
4100
+ shard
4101
+ };
4102
+ };
4103
+ const pubsubTopicsToShardInfo = (pubsubTopics) => {
4104
+ const shardInfoSet = new Set();
4105
+ const clusterIds = new Set();
4106
+ for (const topic of pubsubTopics) {
4107
+ const { clusterId, shard } = pubsubTopicToSingleShardInfo(topic);
4108
+ shardInfoSet.add(`${clusterId}:${shard}`);
4109
+ clusterIds.add(clusterId);
4110
+ }
4111
+ if (shardInfoSet.size === 0) {
4112
+ throw new Error("No valid pubsub topics provided");
4113
+ }
4114
+ if (clusterIds.size > 1) {
4115
+ throw new Error("Pubsub topics from multiple cluster IDs are not supported");
4116
+ }
4117
+ const clusterId = clusterIds.values().next().value;
4118
+ const shards = Array.from(shardInfoSet).map((info) => parseInt(info.split(":")[1]));
4119
+ return {
4120
+ clusterId,
4121
+ shards
4122
+ };
4123
+ };
4124
+ /**
4125
+ * 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/
4126
+ * @param contentTopic String to validate
4127
+ * @returns Object with each content topic field as an attribute
4128
+ */
4129
+ function ensureValidContentTopic(contentTopic) {
4130
+ const parts = contentTopic.split("/");
4131
+ if (parts.length < 5 || parts.length > 6) {
4132
+ throw Error("Content topic format is invalid");
4133
+ }
4134
+ // Validate generation field if present
4135
+ let generation = 0;
4136
+ if (parts.length == 6) {
4137
+ generation = parseInt(parts[1]);
4138
+ if (isNaN(generation)) {
4139
+ throw new Error("Invalid generation field in content topic");
4140
+ }
4141
+ if (generation > 0) {
4142
+ throw new Error("Generation greater than 0 is not supported");
4143
+ }
4144
+ }
4145
+ // Validate remaining fields
4146
+ const fields = parts.splice(-4);
4147
+ // Validate application field
4148
+ if (fields[0].length == 0) {
4149
+ throw new Error("Application field cannot be empty");
4150
+ }
4151
+ // Validate version field
4152
+ if (fields[1].length == 0) {
4153
+ throw new Error("Version field cannot be empty");
4154
+ }
4155
+ // Validate topic name field
4156
+ if (fields[2].length == 0) {
4157
+ throw new Error("Topic name field cannot be empty");
4158
+ }
4159
+ // Validate encoding field
4160
+ if (fields[3].length == 0) {
4161
+ throw new Error("Encoding field cannot be empty");
4162
+ }
4163
+ return {
4164
+ generation,
4165
+ application: fields[0],
4166
+ version: fields[1],
4167
+ topicName: fields[2],
4168
+ encoding: fields[3]
4169
+ };
4170
+ }
4171
+ /**
4172
+ * Given a string, determines which autoshard index to use for its pubsub topic.
4173
+ * Based on the algorithm described in the RFC: https://rfc.vac.dev/spec/51//#algorithm
4174
+ */
4175
+ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
4176
+ const { application, version } = ensureValidContentTopic(contentTopic);
4177
+ const digest = sha256(concat([utf8ToBytes$1(application), utf8ToBytes$1(version)]));
4178
+ const dataview = new DataView(digest.buffer.slice(-8));
4179
+ return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
4180
+ }
4181
+ function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
4182
+ if (!contentTopic) {
4183
+ throw Error("Content topic must be specified");
4184
+ }
4185
+ const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
4186
+ return `/waku/2/rs/${clusterId}/${shardIndex}`;
4187
+ }
4188
+ /**
4189
+ * Used when creating encoders/decoders to determine which pubsub topic to use
4190
+ */
4191
+ function determinePubsubTopic(contentTopic,
4192
+ // TODO: make it accept ShardInfo https://github.com/waku-org/js-waku/issues/2086
4193
+ pubsubTopicShardInfo) {
4194
+ if (typeof pubsubTopicShardInfo == "string") {
4195
+ return pubsubTopicShardInfo;
4196
+ }
4197
+ return pubsubTopicShardInfo?.shard !== undefined
4198
+ ? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
4199
+ : contentTopicToPubsubTopic(contentTopic, pubsubTopicShardInfo?.clusterId ?? DEFAULT_CLUSTER_ID);
4200
+ }
4201
+
3753
4202
  const log = new Logger("message:version-0");
3754
4203
  const OneMillion = BigInt(1_000_000);
3755
4204
  const Version = 0;
@@ -3902,4 +4351,4 @@ var version_0 = /*#__PURE__*/Object.freeze({
3902
4351
  proto: message
3903
4352
  });
3904
4353
 
3905
- export { DecodedMessage as D, Encoder as E, FilterSubscribeRequest as F, MessagePush as M, PushRpc$1 as P, StoreQueryRequest$1 as S, Version as V, WakuMetadataRequest as W, encode as a, FilterSubscribeResponse$1 as b, PushResponse as c, decode as d, encodingLength as e, StoreQueryResponse$1 as f, createEncoder as g, WakuMetadataResponse as h, createDecoder as i, Decoder as j, message as m, version_0 as v };
4354
+ export { DecodedMessage as D, Encoder as E, FilterSubscribeRequest as F, MessagePush as M, PushRpc$1 as P, StoreQueryRequest$1 as S, Version as V, WakuMetadataRequest as W, encode as a, FilterSubscribeResponse$1 as b, PushResponse as c, decode as d, encodingLength as e, StoreQueryResponse$1 as f, createEncoder as g, pubsubTopicsToShardInfo as h, WakuMetadataResponse as i, createDecoder as j, Decoder as k, message as m, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, version_0 as v };