@waku/rln 0.1.6-d3a80d8.0 → 0.1.6-ef2a162.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/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/rln/dist/contract/constants.js +7 -1
  14. package/bundle/packages/rln/dist/contract/rln_base_contract.js +5 -2
  15. package/bundle/packages/rln/dist/contract/rln_contract.js +2 -2
  16. package/bundle/packages/rln/dist/credentials_manager.js +24 -16
  17. package/bundle/packages/rln/dist/identity.js +8 -6
  18. package/bundle/packages/rln/dist/keystore/keystore.js +38 -11
  19. package/bundle/packages/rln/dist/proof.js +2 -2
  20. package/bundle/packages/rln/dist/utils/bytes.js +103 -58
  21. package/bundle/packages/rln/dist/utils/hash.js +3 -3
  22. package/bundle/packages/rln/dist/zerokit.js +17 -17
  23. package/dist/.tsbuildinfo +1 -1
  24. package/dist/contract/constants.d.ts +6 -0
  25. package/dist/contract/constants.js +6 -0
  26. package/dist/contract/constants.js.map +1 -1
  27. package/dist/contract/rln_base_contract.js +5 -2
  28. package/dist/contract/rln_base_contract.js.map +1 -1
  29. package/dist/contract/rln_contract.js +2 -2
  30. package/dist/contract/rln_contract.js.map +1 -1
  31. package/dist/credentials_manager.d.ts +4 -0
  32. package/dist/credentials_manager.js +25 -16
  33. package/dist/credentials_manager.js.map +1 -1
  34. package/dist/identity.d.ts +5 -2
  35. package/dist/identity.js +8 -5
  36. package/dist/identity.js.map +1 -1
  37. package/dist/keystore/keystore.js +38 -11
  38. package/dist/keystore/keystore.js.map +1 -1
  39. package/dist/proof.js +2 -2
  40. package/dist/proof.js.map +1 -1
  41. package/dist/utils/bytes.d.ts +42 -20
  42. package/dist/utils/bytes.js +102 -57
  43. package/dist/utils/bytes.js.map +1 -1
  44. package/dist/utils/hash.js +5 -5
  45. package/dist/utils/hash.js.map +1 -1
  46. package/dist/utils/index.d.ts +1 -1
  47. package/dist/utils/index.js +1 -1
  48. package/dist/utils/index.js.map +1 -1
  49. package/dist/zerokit.js +17 -17
  50. package/dist/zerokit.js.map +1 -1
  51. package/package.json +1 -1
  52. package/src/contract/constants.ts +9 -0
  53. package/src/contract/rln_base_contract.ts +5 -3
  54. package/src/contract/rln_contract.ts +5 -2
  55. package/src/credentials_manager.ts +46 -24
  56. package/src/identity.ts +11 -7
  57. package/src/keystore/keystore.ts +52 -23
  58. package/src/proof.ts +2 -2
  59. package/src/utils/bytes.ts +118 -72
  60. package/src/utils/hash.ts +15 -5
  61. package/src/utils/index.ts +1 -6
  62. package/src/zerokit.ts +30 -22
  63. package/dist/contract/test-utils.d.ts +0 -39
  64. package/dist/contract/test-utils.js +0 -118
  65. package/dist/contract/test-utils.js.map +0 -1
  66. 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
 
@@ -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,6 +13,7 @@ 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 { BytesUtils } from '../utils/bytes.js';
16
17
  import { RLN_ABI } from './abi.js';
17
18
  import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from './constants.js';
18
19
  import { MembershipState } from './types.js';
@@ -352,7 +353,9 @@ class RLNBaseContract {
352
353
  }
353
354
  const estimatedGas = await this.contract.estimateGas.register(identity.IDCommitmentBigInt, this.rateLimit, []);
354
355
  const gasLimit = estimatedGas.add(10000);
355
- const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit });
356
+ const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], {
357
+ gasLimit
358
+ });
356
359
  const txRegisterReceipt = await txRegisterResponse.wait();
357
360
  if (txRegisterReceipt.status === 0) {
358
361
  throw new Error("Transaction failed on-chain");
@@ -414,7 +417,7 @@ class RLNBaseContract {
414
417
  async registerWithPermitAndErase(identity, permit, idCommitmentsToErase) {
415
418
  try {
416
419
  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)));
420
+ const txRegisterResponse = await this.contract.registerWithPermit(permit.owner, permit.deadline, permit.v, permit.r, permit.s, BytesUtils.buildBigIntFromUint8ArrayBE(identity.IDCommitment), this.rateLimit, idCommitmentsToErase.map((id) => BigNumber.from(id)));
418
421
  const txRegisterReceipt = await txRegisterResponse.wait();
419
422
  const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
420
423
  if (!memberRegistered || !memberRegistered.args) {
@@ -4,7 +4,7 @@ import '../../../interfaces/dist/health_indicator.js';
4
4
  import { hexToBytes } from '../../../utils/dist/bytes/index.js';
5
5
  import { Logger } from '../../../utils/dist/logger/index.js';
6
6
  import { MerkleRootTracker } from '../root_tracker.js';
7
- import { zeroPadLE } from '../utils/bytes.js';
7
+ import { BytesUtils } from '../utils/bytes.js';
8
8
  import { RLNBaseContract } from './rln_base_contract.js';
9
9
  import { BigNumber } from '../../../../node_modules/@ethersproject/bignumber/lib.esm/bignumber.js';
10
10
 
@@ -83,7 +83,7 @@ class RLNContract extends RLNBaseContract {
83
83
  if (typeof index === "number" || typeof index === "string") {
84
84
  index = BigNumber.from(index);
85
85
  }
86
- const idCommitment = zeroPadLE(hexToBytes(_idCommitment), 32);
86
+ const idCommitment = BytesUtils.zeroPadLE(hexToBytes(_idCommitment), 32);
87
87
  rlnInstance.zerokit.insertMember(idCommitment);
88
88
  const numericIndex = index.toNumber();
89
89
  this._members.set(numericIndex, {
@@ -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';
@@ -15,12 +15,12 @@ import '../../../node_modules/multiformats/dist/src/bases/base8.js';
15
15
  import '../../../node_modules/multiformats/dist/src/bases/identity.js';
16
16
  import '../../../node_modules/multiformats/dist/src/codecs/json.js';
17
17
  import { Logger } from '../../utils/dist/logger/index.js';
18
- import { LINEA_CONTRACT } from './contract/constants.js';
18
+ import { LINEA_CONTRACT, DEFAULT_Q } from './contract/constants.js';
19
19
  import { RLNBaseContract } from './contract/rln_base_contract.js';
20
20
  import { IdentityCredential } from './identity.js';
21
21
  import { Keystore } from './keystore/keystore.js';
22
+ import { BytesUtils } from './utils/bytes.js';
22
23
  import { extractMetaMaskSigner } from './utils/metamask.js';
23
- import { buildBigIntFromUint8Array } 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,32 @@ 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);
210
- log.info("Successfully generated identity credential");
211
- return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
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 idCommitmentRawBE = sha256(idSecretHashBE);
206
+ const idCommitmentBE = this.reduceIdCommitment(idCommitmentRawBE);
207
+ log.info("Successfully generated identity credential, storing in Big Endian format");
208
+ return new IdentityCredential(idTrapdoorBE, idNullifierBE, idSecretHashBE, idCommitmentBE);
209
+ }
210
+ /**
211
+ * Helper: take 32-byte BE, reduce mod Q, return 32-byte BE
212
+ */
213
+ reduceIdCommitment(bytesBE, limit = DEFAULT_Q) {
214
+ const nBE = BytesUtils.buildBigIntFromUint8ArrayBE(bytesBE);
215
+ if (nBE >= limit) {
216
+ log.warn(`ID commitment is greater than Q, reducing it by Q: ${nBE} % ${limit}`);
217
+ return BytesUtils.bigIntToUint8Array32BE(nBE % limit);
218
+ }
219
+ return bytesBE;
212
220
  }
213
221
  }
214
222
 
@@ -1,5 +1,4 @@
1
- import { buildBigIntFromUint8Array } from './utils/bytes.js';
2
- import './utils/epoch.js';
1
+ import { BytesUtils } from './utils/bytes.js';
3
2
 
4
3
  class IdentityCredential {
5
4
  IDTrapdoor;
@@ -7,12 +6,16 @@ class IdentityCredential {
7
6
  IDSecretHash;
8
7
  IDCommitment;
9
8
  IDCommitmentBigInt;
10
- constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment, IDCommitmentBigInt) {
9
+ /**
10
+ * All variables are in little-endian format
11
+ */
12
+ constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment) {
11
13
  this.IDTrapdoor = IDTrapdoor;
12
14
  this.IDNullifier = IDNullifier;
13
15
  this.IDSecretHash = IDSecretHash;
14
16
  this.IDCommitment = IDCommitment;
15
- this.IDCommitmentBigInt = IDCommitmentBigInt;
17
+ this.IDCommitmentBigInt =
18
+ BytesUtils.buildBigIntFromUint8ArrayBE(IDCommitment);
16
19
  }
17
20
  static fromBytes(memKeys) {
18
21
  if (memKeys.length < 128) {
@@ -22,8 +25,7 @@ class IdentityCredential {
22
25
  const idNullifier = memKeys.subarray(32, 64);
23
26
  const idSecretHash = memKeys.subarray(64, 96);
24
27
  const idCommitment = memKeys.subarray(96, 128);
25
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment, 32);
26
- return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
28
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment);
27
29
  }
28
30
  }
29
31
 
@@ -17,7 +17,7 @@ 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';
20
+ import { BytesUtils } from '../utils/bytes.js';
21
21
  import { keccak256Checksum, decryptEipKeystore } from './cipher.js';
22
22
  import { isKeystoreValid, isCredentialValid } from './schema_validator.js';
23
23
  import { __exports as lib } from '../../../../_virtual/index.js';
@@ -164,14 +164,24 @@ class Keystore {
164
164
  try {
165
165
  const str = bytesToUtf8(bytes);
166
166
  const obj = JSON.parse(str);
167
- // TODO: add runtime validation of nwaku credentials
167
+ // Little Endian
168
+ const idCommitmentLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []));
169
+ const idTrapdoorLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", []));
170
+ const idNullifierLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", []));
171
+ const idSecretHashLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", []));
172
+ // Big Endian
173
+ const idCommitmentBE = BytesUtils.switchEndianness(idCommitmentLE);
174
+ const idTrapdoorBE = BytesUtils.switchEndianness(idTrapdoorLE);
175
+ const idNullifierBE = BytesUtils.switchEndianness(idNullifierLE);
176
+ const idSecretHashBE = BytesUtils.switchEndianness(idSecretHashLE);
177
+ const idCommitmentBigInt = BytesUtils.buildBigIntFromUint8ArrayBE(idCommitmentBE);
168
178
  return {
169
179
  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", []))
180
+ IDCommitment: idCommitmentBE,
181
+ IDTrapdoor: idTrapdoorBE,
182
+ IDNullifier: idNullifierBE,
183
+ IDSecretHash: idSecretHashBE,
184
+ IDCommitmentBigInt: idCommitmentBigInt
175
185
  },
176
186
  membership: {
177
187
  treeIndex: _.get(obj, "treeIndex"),
@@ -207,14 +217,31 @@ class Keystore {
207
217
  }
208
218
  // follows nwaku implementation
209
219
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
220
+ // IdentityCredential is stored in Big Endian format => switch to Little Endian
210
221
  static fromIdentityToBytes(options) {
222
+ const { IDCommitment, IDNullifier, IDSecretHash, IDTrapdoor } = options.identity;
223
+ const idCommitmentLE = BytesUtils.switchEndianness(IDCommitment);
224
+ const idNullifierLE = BytesUtils.switchEndianness(IDNullifier);
225
+ const idSecretHashLE = BytesUtils.switchEndianness(IDSecretHash);
226
+ const idTrapdoorLE = BytesUtils.switchEndianness(IDTrapdoor);
227
+ // eslint-disable-next-line no-console
228
+ console.log({
229
+ idCommitmentBE: IDCommitment,
230
+ idCommitmentLE,
231
+ idNullifierBE: IDNullifier,
232
+ idNullifierLE,
233
+ idSecretHashBE: IDSecretHash,
234
+ idSecretHashLE,
235
+ idTrapdoorBE: IDTrapdoor,
236
+ idTrapdoorLE
237
+ });
211
238
  return utf8ToBytes(JSON.stringify({
212
239
  treeIndex: options.membership.treeIndex,
213
240
  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)
241
+ idCommitment: Array.from(idCommitmentLE),
242
+ idNullifier: Array.from(idNullifierLE),
243
+ idSecretHash: Array.from(idSecretHashLE),
244
+ idTrapdoor: Array.from(idTrapdoorLE)
218
245
  },
219
246
  membershipContract: {
220
247
  chainId: options.membership.chainId,
@@ -1,4 +1,4 @@
1
- import { concatenate } from './utils/bytes.js';
1
+ import { BytesUtils } from './utils/bytes.js';
2
2
  import { poseidonHash } from './utils/hash.js';
3
3
  import './utils/epoch.js';
4
4
 
@@ -48,7 +48,7 @@ class Proof {
48
48
  }
49
49
  }
50
50
  function proofToBytes(p) {
51
- return concatenate(p.proof, p.merkleRoot, p.epoch, p.shareX, p.shareY, p.nullifier, p.rlnIdentifier);
51
+ return BytesUtils.concatenate(p.proof, p.merkleRoot, p.epoch, p.shareX, p.shareY, p.nullifier, p.rlnIdentifier);
52
52
  }
53
53
 
54
54
  export { Proof, proofToBytes };
@@ -1,65 +1,110 @@
1
- /**
2
- * Concatenate Uint8Arrays
3
- * @param input
4
- * @returns concatenation of all Uint8Array received as input
5
- */
6
- function concatenate(...input) {
7
- let totalLength = 0;
8
- for (const arr of input) {
9
- totalLength += arr.length;
1
+ class BytesUtils {
2
+ /**
3
+ * Switches endianness of a byte array
4
+ */
5
+ static switchEndianness(bytes) {
6
+ return new Uint8Array(bytes.reverse());
10
7
  }
11
- const result = new Uint8Array(totalLength);
12
- let offset = 0;
13
- for (const arr of input) {
14
- result.set(arr, offset);
15
- offset += arr.length;
8
+ /**
9
+ * Builds a BigInt from a big-endian Uint8Array
10
+ * @param bytes The big-endian bytes to convert
11
+ * @returns The resulting BigInt in big-endian format
12
+ */
13
+ static buildBigIntFromUint8ArrayBE(bytes) {
14
+ let result = 0n;
15
+ for (let i = 0; i < bytes.length; i++) {
16
+ result = (result << 8n) + BigInt(bytes[i]);
17
+ }
18
+ return result;
16
19
  }
17
- return result;
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");
25
- }
26
- function writeUIntLE(buf, value, offset, byteLength, noAssert) {
27
- value = +value;
28
- offset = offset >>> 0;
29
- byteLength = byteLength >>> 0;
30
- {
31
- const maxBytes = Math.pow(2, 8 * byteLength) - 1;
32
- checkInt(buf, value, offset, byteLength, maxBytes, 0);
20
+ /**
21
+ * Switches endianness of a bigint value
22
+ * @param value The bigint value to switch endianness for
23
+ * @returns The bigint value with reversed endianness
24
+ */
25
+ static switchEndiannessBigInt(value) {
26
+ // Convert bigint to byte array
27
+ const bytes = [];
28
+ let tempValue = value;
29
+ while (tempValue > 0n) {
30
+ bytes.push(Number(tempValue & 0xffn));
31
+ tempValue >>= 8n;
32
+ }
33
+ // Reverse bytes and convert back to bigint
34
+ return bytes
35
+ .reverse()
36
+ .reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
33
37
  }
34
- let mul = 1;
35
- let i = 0;
36
- buf[offset] = value & 0xff;
37
- while (++i < byteLength && (mul *= 0x100)) {
38
- buf[offset + i] = (value / mul) & 0xff;
38
+ /**
39
+ * Converts a big-endian bigint to a 32-byte big-endian Uint8Array
40
+ * @param value The big-endian bigint to convert
41
+ * @returns A 32-byte big-endian Uint8Array
42
+ */
43
+ static bigIntToUint8Array32BE(value) {
44
+ const bytes = new Uint8Array(32);
45
+ for (let i = 31; i >= 0; i--) {
46
+ bytes[i] = Number(value & 0xffn);
47
+ value >>= 8n;
48
+ }
49
+ return bytes;
39
50
  }
40
- return buf;
41
- }
42
- /**
43
- * Transforms Uint8Array into BigInt
44
- * @param array: Uint8Array
45
- * @returns BigInt
46
- */
47
- function buildBigIntFromUint8Array(array, byteOffset = 0) {
48
- const dataView = new DataView(array.buffer);
49
- return dataView.getBigUint64(byteOffset, true);
50
- }
51
- /**
52
- * Fills with zeros to set length
53
- * @param array little endian Uint8Array
54
- * @param length amount to pad
55
- * @returns little endian Uint8Array padded with zeros to set length
56
- */
57
- function zeroPadLE(array, length) {
58
- const result = new Uint8Array(length);
59
- for (let i = 0; i < length; i++) {
60
- result[i] = array[i] || 0;
51
+ /**
52
+ * Writes an unsigned integer to a buffer in little-endian format
53
+ */
54
+ static writeUIntLE(buf, value, offset, byteLength, noAssert) {
55
+ value = +value;
56
+ offset = offset >>> 0;
57
+ byteLength = byteLength >>> 0;
58
+ if (!noAssert) {
59
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
60
+ BytesUtils.checkInt(buf, value, offset, byteLength, maxBytes, 0);
61
+ }
62
+ let mul = 1;
63
+ let i = 0;
64
+ buf[offset] = value & 0xff;
65
+ while (++i < byteLength && (mul *= 0x100)) {
66
+ buf[offset + i] = (value / mul) & 0xff;
67
+ }
68
+ return buf;
69
+ }
70
+ /**
71
+ * Fills with zeros to set length
72
+ * @param array little endian Uint8Array
73
+ * @param length amount to pad
74
+ * @returns little endian Uint8Array padded with zeros to set length
75
+ */
76
+ static zeroPadLE(array, length) {
77
+ const result = new Uint8Array(length);
78
+ for (let i = 0; i < length; i++) {
79
+ result[i] = array[i] || 0;
80
+ }
81
+ return result;
82
+ }
83
+ // Adapted from https://github.com/feross/buffer
84
+ static checkInt(buf, value, offset, ext, max, min) {
85
+ if (value > max || value < min)
86
+ throw new RangeError('"value" argument is out of bounds');
87
+ if (offset + ext > buf.length)
88
+ throw new RangeError("Index out of range");
89
+ }
90
+ /**
91
+ * Concatenate Uint8Arrays
92
+ * @param input
93
+ * @returns concatenation of all Uint8Array received as input
94
+ */
95
+ static concatenate(...input) {
96
+ let totalLength = 0;
97
+ for (const arr of input) {
98
+ totalLength += arr.length;
99
+ }
100
+ const result = new Uint8Array(totalLength);
101
+ let offset = 0;
102
+ for (const arr of input) {
103
+ result.set(arr, offset);
104
+ offset += arr.length;
105
+ }
106
+ return result;
61
107
  }
62
- return result;
63
108
  }
64
109
 
65
- export { buildBigIntFromUint8Array, concatenate, writeUIntLE, zeroPadLE };
110
+ export { BytesUtils };
@@ -1,9 +1,9 @@
1
1
  import { poseidonHash as poseidonHash$1 } from '../../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
2
- import { writeUIntLE, concatenate } from './bytes.js';
2
+ import { BytesUtils } from './bytes.js';
3
3
 
4
4
  function poseidonHash(...input) {
5
- const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
6
- const lenPrefixedData = concatenate(inputLen, ...input);
5
+ const inputLen = BytesUtils.writeUIntLE(new Uint8Array(8), input.length, 0, 8);
6
+ const lenPrefixedData = BytesUtils.concatenate(inputLen, ...input);
7
7
  return poseidonHash$1(lenPrefixedData);
8
8
  }
9
9