@waku/rln 0.1.6-b4748fd.0 → 0.1.6-b58de3a.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 (44) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
  4. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
  5. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
  6. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
  7. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
  8. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
  9. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
  10. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  11. package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  12. package/bundle/packages/rln/dist/contract/rln_base_contract.js +4 -24
  13. package/bundle/packages/rln/dist/credentials_manager.js +16 -13
  14. package/bundle/packages/rln/dist/identity.js +8 -5
  15. package/bundle/packages/rln/dist/keystore/keystore.js +10 -8
  16. package/bundle/packages/rln/dist/utils/bytes.js +10 -14
  17. package/dist/.tsbuildinfo +1 -1
  18. package/dist/contract/rln_base_contract.d.ts +0 -1
  19. package/dist/contract/rln_base_contract.js +4 -24
  20. package/dist/contract/rln_base_contract.js.map +1 -1
  21. package/dist/contract/test-utils.d.ts +39 -0
  22. package/dist/contract/test-utils.js +118 -0
  23. package/dist/contract/test-utils.js.map +1 -0
  24. package/dist/credentials_manager.js +16 -13
  25. package/dist/credentials_manager.js.map +1 -1
  26. package/dist/identity.d.ts +2 -4
  27. package/dist/identity.js +6 -5
  28. package/dist/identity.js.map +1 -1
  29. package/dist/keystore/keystore.js +10 -8
  30. package/dist/keystore/keystore.js.map +1 -1
  31. package/dist/utils/bytes.d.ts +1 -2
  32. package/dist/utils/bytes.js +9 -13
  33. package/dist/utils/bytes.js.map +1 -1
  34. package/dist/utils/index.d.ts +1 -1
  35. package/dist/utils/index.js +1 -1
  36. package/dist/utils/index.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/contract/rln_base_contract.ts +11 -37
  39. package/src/contract/test-utils.ts +179 -0
  40. package/src/credentials_manager.ts +27 -20
  41. package/src/identity.ts +7 -5
  42. package/src/keystore/keystore.ts +33 -17
  43. package/src/utils/bytes.ts +19 -21
  44. package/src/utils/index.ts +1 -1
@@ -1,3 +1,3 @@
1
- var utils = {};
1
+ var utils = {exports: {}};
2
2
 
3
- export { utils as __exports };
3
+ export { utils as __module };
@@ -1,3 +1,3 @@
1
- var utils = {exports: {}};
1
+ var utils = {};
2
2
 
3
- export { utils as __module };
3
+ export { utils as __exports };
@@ -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/utils.js';
4
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
4
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
5
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
6
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
5
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
7
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
1
+ import { __exports as utils } from '../../../../../../_virtual/utils2.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/utils.js';
3
+ import { __exports as utils } from '../../../../../_virtual/utils2.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/utils2.js';
3
+ import { __module as utils } from '../../../../../_virtual/utils.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/utils.js';
7
+ import { __exports as utils$1 } from '../../../../../_virtual/utils2.js';
8
8
 
9
9
  utils.exports;
10
10
 
@@ -13,7 +13,6 @@ 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';
17
16
  import { RLN_ABI } from './abi.js';
18
17
  import { DEFAULT_Q, DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from './constants.js';
19
18
  import { MembershipState } from './types.js';
@@ -345,28 +344,11 @@ class RLNBaseContract {
345
344
  log.error(`Error in withdraw: ${error.message}`);
346
345
  }
347
346
  }
348
- getIdCommitmentBigInt(bytes) {
349
- let idCommitmentBigIntBE = buildBigIntFromUint8ArrayBE(bytes);
350
- log.info("1");
351
- if (!this.contract) {
352
- throw Error("RLN contract is not initialized");
353
- }
354
- const idCommitmentBigIntLimit = this.contract.idCommitmentBigIntLimit;
355
- log.info("idCommitmentBigIntBE: ", idCommitmentBigIntBE);
356
- log.info("idCommitmentBigIntLimit: ", idCommitmentBigIntLimit);
357
- log.info("idCommitmentBigIntBE >= idCommitmentBigIntLimit: ", idCommitmentBigIntBE >= idCommitmentBigIntLimit);
358
- if (idCommitmentBigIntBE >= idCommitmentBigIntLimit) {
359
- log.warn(`ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigIntBE} % ${idCommitmentBigIntLimit}`);
360
- idCommitmentBigIntBE = idCommitmentBigIntBE % idCommitmentBigIntLimit;
361
- }
362
- return idCommitmentBigIntBE;
363
- }
364
347
  async registerWithIdentity(identity) {
365
348
  try {
366
349
  log.info(`Registering identity with rate limit: ${this.rateLimit} messages/epoch`);
367
- const idCommitmentBigInt = this.getIdCommitmentBigInt(identity.IDCommitment);
368
350
  // Check if the ID commitment is already registered
369
- const existingIndex = await this.getMemberIndex(idCommitmentBigInt);
351
+ const existingIndex = await this.getMemberIndex(identity.IDCommitmentBigInt);
370
352
  if (existingIndex) {
371
353
  throw new Error(`ID commitment is already registered with index ${existingIndex}`);
372
354
  }
@@ -375,11 +357,9 @@ class RLNBaseContract {
375
357
  if (remainingRateLimit < this.rateLimit) {
376
358
  throw new Error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
377
359
  }
378
- const estimatedGas = await this.contract.estimateGas.register(idCommitmentBigInt, this.rateLimit, []);
360
+ const estimatedGas = await this.contract.estimateGas.register(identity.IDCommitmentBigInt, this.rateLimit, []);
379
361
  const gasLimit = estimatedGas.add(10000);
380
- const txRegisterResponse = await this.contract.register(idCommitmentBigInt, this.rateLimit, [], {
381
- gasLimit
382
- });
362
+ const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit });
383
363
  const txRegisterReceipt = await txRegisterResponse.wait();
384
364
  if (txRegisterReceipt.status === 0) {
385
365
  throw new Error("Transaction failed on-chain");
@@ -441,7 +421,7 @@ class RLNBaseContract {
441
421
  async registerWithPermitAndErase(identity, permit, idCommitmentsToErase) {
442
422
  try {
443
423
  log.info(`Registering identity with permit and rate limit: ${this.rateLimit} messages/epoch`);
444
- 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)));
424
+ 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)));
445
425
  const txRegisterReceipt = await txRegisterResponse.wait();
446
426
  const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
447
427
  if (!memberRegistered || !memberRegistered.args) {
@@ -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 { switchEndianness } from './utils/bytes.js';
23
+ import { buildBigIntFromUint8ArrayLE } from './utils/bytes.js';
24
24
  import './utils/epoch.js';
25
25
 
26
26
  const log = new Logger("waku:credentials");
@@ -198,19 +198,22 @@ class RLNCredentialsManager {
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 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);
201
+ const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
202
+ const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
203
+ const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
204
+ const idSecretHash = sha256(combinedBytes);
205
+ const idCommitment = sha256(idSecretHash);
206
+ let idCommitmentBigInt = buildBigIntFromUint8ArrayLE(idCommitment);
207
+ if (!this.contract) {
208
+ throw Error("RLN contract is not initialized");
209
+ }
210
+ const idCommitmentBigIntLimit = this.contract.idCommitmentBigIntLimit;
211
+ if (idCommitmentBigInt >= idCommitmentBigIntLimit) {
212
+ log.warn(`ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigInt} % ${idCommitmentBigIntLimit}`);
213
+ idCommitmentBigInt = idCommitmentBigInt % idCommitmentBigIntLimit;
214
+ }
212
215
  log.info("Successfully generated identity credential");
213
- return new IdentityCredential(idTrapdoorLE, idNullifierLE, idSecretHashLE, idCommitmentLE);
216
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
214
217
  }
215
218
  }
216
219
 
@@ -1,16 +1,18 @@
1
+ import { buildBigIntFromUint8ArrayLE } from './utils/bytes.js';
2
+ import './utils/epoch.js';
3
+
1
4
  class IdentityCredential {
2
5
  IDTrapdoor;
3
6
  IDNullifier;
4
7
  IDSecretHash;
5
8
  IDCommitment;
6
- /**
7
- * All variables are in little-endian format
8
- */
9
- constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment) {
9
+ IDCommitmentBigInt;
10
+ constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment, IDCommitmentBigInt) {
10
11
  this.IDTrapdoor = IDTrapdoor;
11
12
  this.IDNullifier = IDNullifier;
12
13
  this.IDSecretHash = IDSecretHash;
13
14
  this.IDCommitment = IDCommitment;
15
+ this.IDCommitmentBigInt = IDCommitmentBigInt;
14
16
  }
15
17
  static fromBytes(memKeys) {
16
18
  if (memKeys.length < 128) {
@@ -20,7 +22,8 @@ class IdentityCredential {
20
22
  const idNullifier = memKeys.subarray(32, 64);
21
23
  const idSecretHash = memKeys.subarray(64, 96);
22
24
  const idCommitment = memKeys.subarray(96, 128);
23
- return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment);
25
+ const idCommitmentBigInt = buildBigIntFromUint8ArrayLE(idCommitment);
26
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
24
27
  }
25
28
  }
26
29
 
@@ -17,6 +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 { buildBigIntFromUint8ArrayLE } from '../utils/bytes.js';
20
21
  import { keccak256Checksum, decryptEipKeystore } from './cipher.js';
21
22
  import { isKeystoreValid, isCredentialValid } from './schema_validator.js';
22
23
  import { __exports as lib } from '../../../../_virtual/index.js';
@@ -160,19 +161,20 @@ class Keystore {
160
161
  };
161
162
  }
162
163
  static fromBytesToIdentity(bytes) {
164
+ function fromLittleEndian(bytes) {
165
+ return new Uint8Array(bytes).reverse();
166
+ }
163
167
  try {
164
168
  const str = bytesToUtf8(bytes);
165
169
  const obj = JSON.parse(str);
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", []));
170
+ // TODO: add runtime validation of nwaku credentials
170
171
  return {
171
172
  identity: {
172
- IDCommitment: idCommitmentLE,
173
- IDTrapdoor: idTrapdoorLE,
174
- IDNullifier: idNullifierLE,
175
- IDSecretHash: idSecretHashLE
173
+ IDCommitment: fromLittleEndian(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []))),
174
+ IDTrapdoor: fromLittleEndian(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", []))),
175
+ IDNullifier: fromLittleEndian(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", []))),
176
+ IDCommitmentBigInt: buildBigIntFromUint8ArrayLE(fromLittleEndian(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", [])))),
177
+ IDSecretHash: fromLittleEndian(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", [])))
176
178
  },
177
179
  membership: {
178
180
  treeIndex: _.get(obj, "treeIndex"),
@@ -16,12 +16,12 @@ function concatenate(...input) {
16
16
  }
17
17
  return result;
18
18
  }
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);
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
25
  }
26
26
  function writeUIntLE(buf, value, offset, byteLength, noAssert) {
27
27
  value = +value;
@@ -39,6 +39,9 @@ function writeUIntLE(buf, value, offset, byteLength, noAssert) {
39
39
  }
40
40
  return buf;
41
41
  }
42
+ function buildBigIntFromUint8ArrayLE(bytes) {
43
+ return bytes.reduce((acc, byte, i) => acc + BigInt(byte) * (1n << (8n * BigInt(i))), 0n);
44
+ }
42
45
  /**
43
46
  * Fills with zeros to set length
44
47
  * @param array little endian Uint8Array
@@ -52,12 +55,5 @@ function zeroPadLE(array, length) {
52
55
  }
53
56
  return result;
54
57
  }
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
- }
62
58
 
63
- export { buildBigIntFromUint8ArrayBE, concatenate, switchEndianness, writeUIntLE, zeroPadLE };
59
+ export { buildBigIntFromUint8ArrayLE, concatenate, writeUIntLE, zeroPadLE };