@waku/rln 0.1.6-a7f52e4.0 → 0.1.6-acc9100.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 (56) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/index.js +1 -1
  4. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
  5. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
  6. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
  7. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
  8. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
  9. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
  10. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
  11. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  12. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  13. package/bundle/packages/core/dist/lib/message/version_0.js +1 -4
  14. package/bundle/packages/rln/dist/contract/constants.js +7 -1
  15. package/bundle/packages/rln/dist/contract/rln_base_contract.js +30 -8
  16. package/bundle/packages/rln/dist/credentials_manager.js +16 -14
  17. package/bundle/packages/rln/dist/identity.js +5 -8
  18. package/bundle/packages/rln/dist/keystore/keystore.js +15 -11
  19. package/bundle/packages/rln/dist/message.js +11 -0
  20. package/bundle/packages/rln/dist/utils/bytes.js +14 -19
  21. package/dist/.tsbuildinfo +1 -1
  22. package/dist/contract/constants.d.ts +6 -0
  23. package/dist/contract/constants.js +6 -0
  24. package/dist/contract/constants.js.map +1 -1
  25. package/dist/contract/rln_base_contract.d.ts +7 -1
  26. package/dist/contract/rln_base_contract.js +30 -8
  27. package/dist/contract/rln_base_contract.js.map +1 -1
  28. package/dist/credentials_manager.js +16 -14
  29. package/dist/credentials_manager.js.map +1 -1
  30. package/dist/identity.d.ts +4 -2
  31. package/dist/identity.js +5 -6
  32. package/dist/identity.js.map +1 -1
  33. package/dist/keystore/keystore.js +15 -11
  34. package/dist/keystore/keystore.js.map +1 -1
  35. package/dist/message.d.ts +5 -4
  36. package/dist/message.js +2 -0
  37. package/dist/message.js.map +1 -1
  38. package/dist/utils/bytes.d.ts +2 -6
  39. package/dist/utils/bytes.js +13 -18
  40. package/dist/utils/bytes.js.map +1 -1
  41. package/dist/utils/index.d.ts +1 -1
  42. package/dist/utils/index.js +1 -1
  43. package/dist/utils/index.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/contract/constants.ts +9 -0
  46. package/src/contract/rln_base_contract.ts +43 -15
  47. package/src/credentials_manager.ts +27 -21
  48. package/src/identity.ts +5 -8
  49. package/src/keystore/keystore.ts +28 -24
  50. package/src/message.ts +7 -4
  51. package/src/utils/bytes.ts +21 -25
  52. package/src/utils/index.ts +1 -1
  53. package/dist/contract/test-utils.d.ts +0 -39
  54. package/dist/contract/test-utils.js +0 -118
  55. package/dist/contract/test-utils.js.map +0 -1
  56. package/src/contract/test-utils.ts +0 -179
@@ -1,3 +1,3 @@
1
- var utils = {exports: {}};
1
+ var utils = {};
2
2
 
3
- export { utils as __module };
3
+ export { utils as __exports };
@@ -1,3 +1,3 @@
1
- var utils = {};
1
+ var utils = {exports: {}};
2
2
 
3
- export { utils as __exports };
3
+ export { utils as __module };
package/bundle/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export { RLNDecoder, RLNEncoder } from './packages/rln/dist/codec.js';
2
2
  export { RLN_ABI } from './packages/rln/dist/contract/abi.js';
3
3
  export { RLNContract } from './packages/rln/dist/contract/rln_contract.js';
4
- export { DEFAULT_RATE_LIMIT, LINEA_CONTRACT, RATE_LIMIT_PARAMS, RATE_LIMIT_TIERS } from './packages/rln/dist/contract/constants.js';
4
+ export { DEFAULT_Q, DEFAULT_RATE_LIMIT, LINEA_CONTRACT, RATE_LIMIT_PARAMS, RATE_LIMIT_TIERS } from './packages/rln/dist/contract/constants.js';
5
5
  export { MembershipState } from './packages/rln/dist/contract/types.js';
6
6
  export { RLNBaseContract } from './packages/rln/dist/contract/rln_base_contract.js';
7
7
  export { createRLN } from './packages/rln/dist/create.js';
@@ -1,7 +1,7 @@
1
1
  import { __exports as _sha2 } from '../../../../../../_virtual/_sha2.js';
2
2
  import './_assert.js';
3
3
  import './utils.js';
4
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
4
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
5
5
  import { __exports as _assert } from '../../../../../../_virtual/_assert.js';
6
6
 
7
7
  Object.defineProperty(_sha2, "__esModule", { value: true });
@@ -1,7 +1,7 @@
1
1
  import { __exports as hmac } from '../../../../../../_virtual/hmac.js';
2
2
  import './_assert.js';
3
3
  import './utils.js';
4
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
4
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
5
5
  import { __exports as _assert } from '../../../../../../_virtual/_assert.js';
6
6
 
7
7
  (function (exports) {
@@ -2,7 +2,7 @@ import { __exports as pbkdf2$1 } from '../../../../../../_virtual/pbkdf22.js';
2
2
  import './_assert.js';
3
3
  import './hmac.js';
4
4
  import './utils.js';
5
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
5
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
6
6
  import { __exports as _assert } from '../../../../../../_virtual/_assert.js';
7
7
  import { __exports as hmac } from '../../../../../../_virtual/hmac.js';
8
8
 
@@ -3,7 +3,7 @@ import './_assert.js';
3
3
  import './sha256.js';
4
4
  import './pbkdf2.js';
5
5
  import './utils.js';
6
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
6
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
7
7
  import { __exports as _assert } from '../../../../../../_virtual/_assert.js';
8
8
  import { __exports as pbkdf2 } from '../../../../../../_virtual/pbkdf22.js';
9
9
  import { __exports as sha256 } from '../../../../../../_virtual/sha2562.js';
@@ -2,7 +2,7 @@ import { __exports as sha256 } from '../../../../../../_virtual/sha2562.js';
2
2
  import './_sha2.js';
3
3
  import './utils.js';
4
4
  import { __exports as _sha2 } from '../../../../../../_virtual/_sha2.js';
5
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
5
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
6
6
 
7
7
  Object.defineProperty(sha256, "__esModule", { value: true });
8
8
  sha256.sha224 = sha256.sha256 = void 0;
@@ -4,7 +4,7 @@ import './_u64.js';
4
4
  import './utils.js';
5
5
  import { __exports as _u64 } from '../../../../../../_virtual/_u64.js';
6
6
  import { __exports as _sha2 } from '../../../../../../_virtual/_sha2.js';
7
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
7
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
8
8
 
9
9
  Object.defineProperty(sha512, "__esModule", { value: true });
10
10
  sha512.sha384 = sha512.sha512_256 = sha512.sha512_224 = sha512.sha512 = sha512.SHA512 = void 0;
@@ -1,4 +1,4 @@
1
- import { __exports as utils } from '../../../../../../_virtual/utils2.js';
1
+ import { __exports as utils } from '../../../../../../_virtual/utils.js';
2
2
  import './cryptoBrowser.js';
3
3
  import { __exports as cryptoBrowser } from '../../../../../../_virtual/cryptoBrowser.js';
4
4
 
@@ -1,6 +1,6 @@
1
1
  import { __exports as random } from '../../../../../_virtual/random.js';
2
2
  import '../@noble/hashes/utils.js';
3
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
3
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
4
4
 
5
5
  Object.defineProperty(random, "__esModule", { value: true });
6
6
  random.getRandomBytes = random.getRandomBytesSync = void 0;
@@ -1,10 +1,10 @@
1
1
  import { commonjsGlobal } from '../../../../../_virtual/_commonjsHelpers.js';
2
2
  import { commonjsRequire } from '../../../../../_virtual/_commonjs-dynamic-modules.js';
3
- import { __module as utils } from '../../../../../_virtual/utils.js';
3
+ import { __module as utils } from '../../../../../_virtual/utils2.js';
4
4
  import '../@noble/hashes/_assert.js';
5
5
  import '../@noble/hashes/utils.js';
6
6
  import { __exports as _assert } from '../../../../../_virtual/_assert.js';
7
- import { __exports as utils$1 } from '../../../../../_virtual/utils2.js';
7
+ import { __exports as utils$1 } from '../../../../../_virtual/utils.js';
8
8
 
9
9
  utils.exports;
10
10
 
@@ -29,9 +29,6 @@ class DecodedMessage {
29
29
  get contentTopic() {
30
30
  return this.proto.contentTopic;
31
31
  }
32
- get _rawTimestamp() {
33
- return this.proto.timestamp;
34
- }
35
32
  get timestamp() {
36
33
  // In the case we receive a value that is bigger than JS's max number,
37
34
  // we catch the error and return undefined.
@@ -53,7 +50,7 @@ class DecodedMessage {
53
50
  get version() {
54
51
  // https://rfc.vac.dev/spec/14/
55
52
  // > If omitted, the value SHOULD be interpreted as version 0.
56
- return this.proto.version ?? 0;
53
+ return this.proto.version ?? Version;
57
54
  }
58
55
  get rateLimitProof() {
59
56
  return this.proto.rateLimitProof;
@@ -22,6 +22,12 @@ const RATE_LIMIT_PARAMS = {
22
22
  MAX_TOTAL_RATE: 160_000, // Maximum total rate limit across all memberships
23
23
  EPOCH_LENGTH: 600 // Epoch length in seconds (10 minutes)
24
24
  };
25
+ /**
26
+ * Default Q value for the RLN contract
27
+ * This is the upper bound for the ID commitment
28
+ * @see https://github.com/waku-org/specs/blob/master/standards/core/rln-contract.md#implementation-suggestions
29
+ */
30
+ const DEFAULT_Q = BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
25
31
  const DEFAULT_RATE_LIMIT = RATE_LIMIT_PARAMS.MAX_RATE;
26
32
 
27
- export { DEFAULT_RATE_LIMIT, LINEA_CONTRACT, RATE_LIMIT_PARAMS, RATE_LIMIT_TIERS };
33
+ export { DEFAULT_Q, DEFAULT_RATE_LIMIT, LINEA_CONTRACT, RATE_LIMIT_PARAMS, RATE_LIMIT_TIERS };
@@ -13,8 +13,9 @@ import '../../../../node_modules/multiformats/dist/src/bases/base8.js';
13
13
  import '../../../../node_modules/multiformats/dist/src/bases/identity.js';
14
14
  import '../../../../node_modules/multiformats/dist/src/codecs/json.js';
15
15
  import { Logger } from '../../../utils/dist/logger/index.js';
16
+ import { buildBigIntFromUint8ArrayBE } from '../utils/bytes.js';
16
17
  import { RLN_ABI } from './abi.js';
17
- import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from './constants.js';
18
+ import { DEFAULT_Q, DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from './constants.js';
18
19
  import { MembershipState } from './types.js';
19
20
  import { Contract } from '../../../../node_modules/@ethersproject/contracts/lib.esm/index.js';
20
21
  import { BigNumber } from '../../../../node_modules/@ethersproject/bignumber/lib.esm/bignumber.js';
@@ -26,6 +27,11 @@ class RLNBaseContract {
26
27
  rateLimit;
27
28
  minRateLimit;
28
29
  maxRateLimit;
30
+ /**
31
+ * Default Q value for the RLN contract.
32
+ * @see https://github.com/waku-org/waku-rlnv2-contract/blob/b7e9a9b1bc69256a2a3076c1f099b50ce84e7eff/src/WakuRlnV2.sol#L25
33
+ */
34
+ idCommitmentBigIntLimit = DEFAULT_Q;
29
35
  _members = new Map();
30
36
  _membersFilter;
31
37
  _membershipErasedFilter;
@@ -66,7 +72,8 @@ class RLNBaseContract {
66
72
  const instance = new RLNBaseContract(options);
67
73
  const [min, max] = await Promise.all([
68
74
  instance.contract.minMembershipRateLimit(),
69
- instance.contract.maxMembershipRateLimit()
75
+ instance.contract.maxMembershipRateLimit(),
76
+ instance.contract.Q()
70
77
  ]);
71
78
  instance.minRateLimit = BigNumber.from(min).toNumber();
72
79
  instance.maxRateLimit = BigNumber.from(max).toNumber();
@@ -328,20 +335,33 @@ class RLNBaseContract {
328
335
  }
329
336
  return this.contract.register(idCommitmentBigInt, rateLimit, []);
330
337
  }
331
- async withdraw(token, from) {
338
+ async withdraw(token, walletAddress) {
332
339
  try {
333
- const tx = await this.contract.withdraw(token, from);
340
+ const tx = await this.contract.withdraw(token, walletAddress);
334
341
  await tx.wait();
335
342
  }
336
343
  catch (error) {
337
344
  log.error(`Error in withdraw: ${error.message}`);
338
345
  }
339
346
  }
347
+ getIdCommitmentBigInt(bytes) {
348
+ let idCommitmentBigIntBE = buildBigIntFromUint8ArrayBE(bytes);
349
+ if (!this.contract) {
350
+ throw Error("RLN contract is not initialized");
351
+ }
352
+ if (idCommitmentBigIntBE >= this.idCommitmentBigIntLimit) {
353
+ log.warn(`ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigIntBE} % ${this.idCommitmentBigIntLimit}`);
354
+ idCommitmentBigIntBE =
355
+ idCommitmentBigIntBE % this.idCommitmentBigIntLimit;
356
+ }
357
+ return idCommitmentBigIntBE;
358
+ }
340
359
  async registerWithIdentity(identity) {
341
360
  try {
342
361
  log.info(`Registering identity with rate limit: ${this.rateLimit} messages/epoch`);
362
+ const idCommitmentBigInt = this.getIdCommitmentBigInt(identity.IDCommitment);
343
363
  // Check if the ID commitment is already registered
344
- const existingIndex = await this.getMemberIndex(identity.IDCommitmentBigInt);
364
+ const existingIndex = await this.getMemberIndex(idCommitmentBigInt);
345
365
  if (existingIndex) {
346
366
  throw new Error(`ID commitment is already registered with index ${existingIndex}`);
347
367
  }
@@ -350,9 +370,11 @@ class RLNBaseContract {
350
370
  if (remainingRateLimit < this.rateLimit) {
351
371
  throw new Error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
352
372
  }
353
- const estimatedGas = await this.contract.estimateGas.register(identity.IDCommitmentBigInt, this.rateLimit, []);
373
+ const estimatedGas = await this.contract.estimateGas.register(idCommitmentBigInt, this.rateLimit, []);
354
374
  const gasLimit = estimatedGas.add(10000);
355
- const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit });
375
+ const txRegisterResponse = await this.contract.register(idCommitmentBigInt, this.rateLimit, [], {
376
+ gasLimit
377
+ });
356
378
  const txRegisterReceipt = await txRegisterResponse.wait();
357
379
  if (txRegisterReceipt.status === 0) {
358
380
  throw new Error("Transaction failed on-chain");
@@ -414,7 +436,7 @@ class RLNBaseContract {
414
436
  async registerWithPermitAndErase(identity, permit, idCommitmentsToErase) {
415
437
  try {
416
438
  log.info(`Registering identity with permit and rate limit: ${this.rateLimit} messages/epoch`);
417
- const txRegisterResponse = await this.contract.registerWithPermit(permit.owner, permit.deadline, permit.v, permit.r, permit.s, identity.IDCommitmentBigInt, this.rateLimit, idCommitmentsToErase.map((id) => BigNumber.from(id)));
439
+ const txRegisterResponse = await this.contract.registerWithPermit(permit.owner, permit.deadline, permit.v, permit.r, permit.s, this.getIdCommitmentBigInt(identity.IDCommitment), this.rateLimit, idCommitmentsToErase.map((id) => BigNumber.from(id)));
418
440
  const txRegisterReceipt = await txRegisterResponse.wait();
419
441
  const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
420
442
  if (!memberRegistered || !memberRegistered.args) {
@@ -1,5 +1,5 @@
1
1
  import { hmac } from '../../../node_modules/@noble/hashes/esm/hmac.js';
2
- import { sha256 } from '../../../node_modules/@noble/hashes/esm/sha256.js';
2
+ import { sha256 } from '../../../node_modules/@noble/hashes/esm/sha2.js';
3
3
  import '../../interfaces/dist/protocols.js';
4
4
  import '../../interfaces/dist/connection_manager.js';
5
5
  import '../../interfaces/dist/health_indicator.js';
@@ -20,7 +20,7 @@ import { RLNBaseContract } from './contract/rln_base_contract.js';
20
20
  import { IdentityCredential } from './identity.js';
21
21
  import { Keystore } from './keystore/keystore.js';
22
22
  import { extractMetaMaskSigner } from './utils/metamask.js';
23
- import { buildBigIntFromUint8Array } from './utils/bytes.js';
23
+ import { switchEndianness } from './utils/bytes.js';
24
24
  import './utils/epoch.js';
25
25
 
26
26
  const log = new Logger("waku:credentials");
@@ -95,7 +95,7 @@ class RLNCredentialsManager {
95
95
  }
96
96
  else {
97
97
  log.info("Using local implementation to generate identity");
98
- identity = this.generateSeededIdentityCredential(options.signature);
98
+ identity = await this.generateSeededIdentityCredential(options.signature);
99
99
  }
100
100
  }
101
101
  if (!identity) {
@@ -191,24 +191,26 @@ class RLNCredentialsManager {
191
191
  * @param seed A string seed to generate the identity from
192
192
  * @returns IdentityCredential
193
193
  */
194
- generateSeededIdentityCredential(seed) {
194
+ async generateSeededIdentityCredential(seed) {
195
195
  log.info("Generating seeded identity credential");
196
196
  // Convert the seed to bytes
197
197
  const encoder = new TextEncoder();
198
198
  const seedBytes = encoder.encode(seed);
199
199
  // Generate deterministic values using HMAC-SHA256
200
200
  // We use different context strings for each component to ensure they're different
201
- const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
202
- const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
203
- // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
204
- const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
205
- const idSecretHash = sha256(combinedBytes);
206
- // Generate IDCommitment as a hash of IDSecretHash
207
- const idCommitment = sha256(idSecretHash);
208
- // Convert IDCommitment to BigInt
209
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
201
+ const idTrapdoorBE = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
202
+ const idNullifierBE = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
203
+ const combinedBytes = new Uint8Array([...idTrapdoorBE, ...idNullifierBE]);
204
+ const idSecretHashBE = sha256(combinedBytes);
205
+ const idCommitmentBE = sha256(idSecretHashBE);
206
+ // All hashing functions return big-endian bytes
207
+ // We need to switch to little-endian for the identity credential
208
+ const idTrapdoorLE = switchEndianness(idTrapdoorBE);
209
+ const idNullifierLE = switchEndianness(idNullifierBE);
210
+ const idSecretHashLE = switchEndianness(idSecretHashBE);
211
+ const idCommitmentLE = switchEndianness(idCommitmentBE);
210
212
  log.info("Successfully generated identity credential");
211
- return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
213
+ return new IdentityCredential(idTrapdoorLE, idNullifierLE, idSecretHashLE, idCommitmentLE);
212
214
  }
213
215
  }
214
216
 
@@ -1,18 +1,16 @@
1
- import { buildBigIntFromUint8Array } from './utils/bytes.js';
2
- import './utils/epoch.js';
3
-
4
1
  class IdentityCredential {
5
2
  IDTrapdoor;
6
3
  IDNullifier;
7
4
  IDSecretHash;
8
5
  IDCommitment;
9
- IDCommitmentBigInt;
10
- constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment, IDCommitmentBigInt) {
6
+ /**
7
+ * All variables are in little-endian format
8
+ */
9
+ constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment) {
11
10
  this.IDTrapdoor = IDTrapdoor;
12
11
  this.IDNullifier = IDNullifier;
13
12
  this.IDSecretHash = IDSecretHash;
14
13
  this.IDCommitment = IDCommitment;
15
- this.IDCommitmentBigInt = IDCommitmentBigInt;
16
14
  }
17
15
  static fromBytes(memKeys) {
18
16
  if (memKeys.length < 128) {
@@ -22,8 +20,7 @@ class IdentityCredential {
22
20
  const idNullifier = memKeys.subarray(32, 64);
23
21
  const idSecretHash = memKeys.subarray(64, 96);
24
22
  const idCommitment = memKeys.subarray(96, 128);
25
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
26
- return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
23
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment);
27
24
  }
28
25
  }
29
26
 
@@ -17,7 +17,6 @@ import { Logger } from '../../../utils/dist/logger/index.js';
17
17
  import { sha256 } from '../../../../node_modules/ethereum-cryptography/esm/sha256.js';
18
18
  import { bytesToUtf8 } from '../../../../node_modules/ethereum-cryptography/esm/utils.js';
19
19
  import _ from '../../../../node_modules/lodash/lodash.js';
20
- import { buildBigIntFromUint8Array } from '../utils/bytes.js';
21
20
  import { keccak256Checksum, decryptEipKeystore } from './cipher.js';
22
21
  import { isKeystoreValid, isCredentialValid } from './schema_validator.js';
23
22
  import { __exports as lib } from '../../../../_virtual/index.js';
@@ -164,14 +163,16 @@ class Keystore {
164
163
  try {
165
164
  const str = bytesToUtf8(bytes);
166
165
  const obj = JSON.parse(str);
167
- // TODO: add runtime validation of nwaku credentials
166
+ const idCommitmentLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []));
167
+ const idTrapdoorLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", []));
168
+ const idNullifierLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", []));
169
+ const idSecretHashLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", []));
168
170
  return {
169
171
  identity: {
170
- IDCommitment: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", [])),
171
- IDTrapdoor: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", [])),
172
- IDNullifier: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", [])),
173
- IDCommitmentBigInt: buildBigIntFromUint8Array(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []))),
174
- IDSecretHash: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", []))
172
+ IDCommitment: idCommitmentLE,
173
+ IDTrapdoor: idTrapdoorLE,
174
+ IDNullifier: idNullifierLE,
175
+ IDSecretHash: idSecretHashLE
175
176
  },
176
177
  membership: {
177
178
  treeIndex: _.get(obj, "treeIndex"),
@@ -208,13 +209,16 @@ class Keystore {
208
209
  // follows nwaku implementation
209
210
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
210
211
  static fromIdentityToBytes(options) {
212
+ function toLittleEndian(bytes) {
213
+ return new Uint8Array(bytes).reverse();
214
+ }
211
215
  return utf8ToBytes(JSON.stringify({
212
216
  treeIndex: options.membership.treeIndex,
213
217
  identityCredential: {
214
- idCommitment: Array.from(options.identity.IDCommitment),
215
- idNullifier: Array.from(options.identity.IDNullifier),
216
- idSecretHash: Array.from(options.identity.IDSecretHash),
217
- idTrapdoor: Array.from(options.identity.IDTrapdoor)
218
+ idCommitment: Array.from(toLittleEndian(options.identity.IDCommitment)),
219
+ idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
220
+ idSecretHash: Array.from(toLittleEndian(options.identity.IDSecretHash)),
221
+ idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
218
222
  },
219
223
  membershipContract: {
220
224
  chainId: options.membership.chainId,
@@ -1,4 +1,14 @@
1
+ import { Version } from '../../core/dist/lib/message/version_0.js';
2
+ import '../../core/dist/lib/filter/filter.js';
3
+ import '../../core/dist/lib/light_push/light_push.js';
4
+ import '../../core/dist/lib/store/store.js';
5
+ import '../../core/dist/lib/connection_manager/connection_manager.js';
6
+ import '../../interfaces/dist/protocols.js';
7
+ import '../../interfaces/dist/connection_manager.js';
8
+ import '../../interfaces/dist/health_indicator.js';
1
9
  import { utf8ToBytes } from '../../utils/dist/bytes/index.js';
10
+ import '../../../node_modules/debug/src/browser.js';
11
+ import '../../core/dist/lib/metadata/metadata.js';
2
12
  import { epochBytesToInt } from './utils/epoch.js';
3
13
 
4
14
  function toRLNSignal(contentTopic, msg) {
@@ -10,6 +20,7 @@ class RlnMessage {
10
20
  msg;
11
21
  rateLimitProof;
12
22
  pubsubTopic = "";
23
+ version = Version;
13
24
  constructor(rlnInstance, msg, rateLimitProof) {
14
25
  this.rlnInstance = rlnInstance;
15
26
  this.msg = msg;
@@ -16,12 +16,12 @@ function concatenate(...input) {
16
16
  }
17
17
  return result;
18
18
  }
19
- // Adapted from https://github.com/feross/buffer
20
- function checkInt(buf, value, offset, ext, max, min) {
21
- if (value > max || value < min)
22
- throw new RangeError('"value" argument is out of bounds');
23
- if (offset + ext > buf.length)
24
- throw new RangeError("Index out of range");
19
+ function switchEndianness(bytes) {
20
+ return new Uint8Array(bytes.reverse());
21
+ }
22
+ function buildBigIntFromUint8ArrayBE(bytes) {
23
+ // Interpret bytes as big-endian
24
+ return bytes.reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
25
25
  }
26
26
  function writeUIntLE(buf, value, offset, byteLength, noAssert) {
27
27
  value = +value;
@@ -39,18 +39,6 @@ function writeUIntLE(buf, value, offset, byteLength, noAssert) {
39
39
  }
40
40
  return buf;
41
41
  }
42
- /**
43
- * Transforms Uint8Array into BigInt
44
- * @param array: Uint8Array
45
- * @returns BigInt
46
- */
47
- function buildBigIntFromUint8Array(array) {
48
- let hex = "";
49
- for (let i = 0; i < array.length; i++) {
50
- hex = array[i].toString(16).padStart(2, "0") + hex; // Reverse order for big-endian
51
- }
52
- return BigInt("0x" + hex);
53
- }
54
42
  /**
55
43
  * Fills with zeros to set length
56
44
  * @param array little endian Uint8Array
@@ -64,5 +52,12 @@ function zeroPadLE(array, length) {
64
52
  }
65
53
  return result;
66
54
  }
55
+ // Adapted from https://github.com/feross/buffer
56
+ function checkInt(buf, value, offset, ext, max, min) {
57
+ if (value > max || value < min)
58
+ throw new RangeError('"value" argument is out of bounds');
59
+ if (offset + ext > buf.length)
60
+ throw new RangeError("Index out of range");
61
+ }
67
62
 
68
- export { buildBigIntFromUint8Array, concatenate, writeUIntLE, zeroPadLE };
63
+ export { buildBigIntFromUint8ArrayBE, concatenate, switchEndianness, writeUIntLE, zeroPadLE };