@waku/rln 0.1.6-7fba26d.0 → 0.1.6-8c47a91.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 +26 -19
  16. package/bundle/packages/rln/dist/credentials_manager.js +14 -16
  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 -16
  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 +2 -6
  26. package/dist/contract/rln_base_contract.js +26 -19
  27. package/dist/contract/rln_base_contract.js.map +1 -1
  28. package/dist/credentials_manager.js +14 -16
  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 -15
  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 +39 -26
  47. package/src/credentials_manager.ts +21 -24
  48. package/src/identity.ts +5 -7
  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
@@ -3,9 +3,14 @@ import { ethers } from "ethers";
3
3
 
4
4
  import { IdentityCredential } from "../identity.js";
5
5
  import { DecryptedCredentials } from "../keystore/types.js";
6
+ import { buildBigIntFromUint8ArrayBE } from "../utils/bytes.js";
6
7
 
7
8
  import { RLN_ABI } from "./abi.js";
8
- import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from "./constants.js";
9
+ import {
10
+ DEFAULT_Q,
11
+ DEFAULT_RATE_LIMIT,
12
+ RATE_LIMIT_PARAMS
13
+ } from "./constants.js";
9
14
  import {
10
15
  CustomQueryOptions,
11
16
  FetchMembersOptions,
@@ -29,7 +34,7 @@ export class RLNBaseContract {
29
34
  * Default Q value for the RLN contract.
30
35
  * @see https://github.com/waku-org/waku-rlnv2-contract/blob/b7e9a9b1bc69256a2a3076c1f099b50ce84e7eff/src/WakuRlnV2.sol#L25
31
36
  */
32
- private Q: undefined | bigint;
37
+ public idCommitmentBigIntLimit = DEFAULT_Q;
33
38
 
34
39
  protected _members: Map<number, Member> = new Map();
35
40
  private _membersFilter: ethers.EventFilter;
@@ -83,28 +88,21 @@ export class RLNBaseContract {
83
88
  options: RLNContractInitOptions
84
89
  ): Promise<RLNBaseContract> {
85
90
  const instance = new RLNBaseContract(options);
86
- const [min, max] = await Promise.all([
91
+ const [min, max, idCommitmentBigIntLimit] = await Promise.all([
87
92
  instance.contract.minMembershipRateLimit(),
88
- instance.contract.maxMembershipRateLimit()
93
+ instance.contract.maxMembershipRateLimit(),
94
+ instance.contract.Q()
89
95
  ]);
90
96
  instance.minRateLimit = ethers.BigNumber.from(min).toNumber();
91
97
  instance.maxRateLimit = ethers.BigNumber.from(max).toNumber();
98
+ instance.idCommitmentBigIntLimit = BigInt(
99
+ idCommitmentBigIntLimit.toString()
100
+ );
92
101
 
93
102
  instance.validateRateLimit(instance.rateLimit);
94
103
  return instance;
95
104
  }
96
105
 
97
- /**
98
- * Fetches and caches the Q value from the contract.
99
- * @returns Promise<bigint> The Q value from the contract
100
- */
101
- public async getQ(): Promise<bigint> {
102
- if (this.Q !== undefined) return this.Q;
103
- const q = await this.contract.Q();
104
- this.Q = BigInt(q.toString());
105
- return this.Q;
106
- }
107
-
108
106
  /**
109
107
  * Gets the current rate limit for this contract instance
110
108
  */
@@ -508,6 +506,22 @@ export class RLNBaseContract {
508
506
  }
509
507
  }
510
508
 
509
+ private getIdCommitmentBigInt(bytes: Uint8Array): bigint {
510
+ let idCommitmentBigIntBE = buildBigIntFromUint8ArrayBE(bytes);
511
+ if (!this.contract) {
512
+ throw Error("RLN contract is not initialized");
513
+ }
514
+ const idCommitmentBigIntLimit = this.contract.idCommitmentBigIntLimit;
515
+
516
+ if (idCommitmentBigIntBE >= idCommitmentBigIntLimit) {
517
+ log.warn(
518
+ `ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigIntBE} % ${idCommitmentBigIntLimit}`
519
+ );
520
+ idCommitmentBigIntBE = idCommitmentBigIntBE % idCommitmentBigIntLimit;
521
+ }
522
+ return idCommitmentBigIntBE;
523
+ }
524
+
511
525
  public async registerWithIdentity(
512
526
  identity: IdentityCredential
513
527
  ): Promise<DecryptedCredentials | undefined> {
@@ -516,10 +530,12 @@ export class RLNBaseContract {
516
530
  `Registering identity with rate limit: ${this.rateLimit} messages/epoch`
517
531
  );
518
532
 
519
- // Check if the ID commitment is already registered
520
- const existingIndex = await this.getMemberIndex(
521
- identity.IDCommitmentBigInt
533
+ const idCommitmentBigInt = this.getIdCommitmentBigInt(
534
+ identity.IDCommitment
522
535
  );
536
+
537
+ // Check if the ID commitment is already registered
538
+ const existingIndex = await this.getMemberIndex(idCommitmentBigInt);
523
539
  if (existingIndex) {
524
540
  throw new Error(
525
541
  `ID commitment is already registered with index ${existingIndex}`
@@ -535,19 +551,16 @@ export class RLNBaseContract {
535
551
  }
536
552
 
537
553
  const estimatedGas = await this.contract.estimateGas.register(
538
- identity.IDCommitmentBigInt,
554
+ idCommitmentBigInt,
539
555
  this.rateLimit,
540
556
  []
541
557
  );
542
558
  const gasLimit = estimatedGas.add(10000);
543
559
 
544
560
  const txRegisterResponse: ethers.ContractTransaction =
545
- await this.contract.register(
546
- identity.IDCommitmentBigInt,
547
- this.rateLimit,
548
- [],
549
- { gasLimit }
550
- );
561
+ await this.contract.register(idCommitmentBigInt, this.rateLimit, [], {
562
+ gasLimit
563
+ });
551
564
 
552
565
  const txRegisterReceipt = await txRegisterResponse.wait();
553
566
 
@@ -643,7 +656,7 @@ export class RLNBaseContract {
643
656
  permit.v,
644
657
  permit.r,
645
658
  permit.s,
646
- identity.IDCommitmentBigInt,
659
+ this.getIdCommitmentBigInt(identity.IDCommitment),
647
660
  this.rateLimit,
648
661
  idCommitmentsToErase.map((id) => ethers.BigNumber.from(id))
649
662
  );
@@ -1,5 +1,5 @@
1
1
  import { hmac } from "@noble/hashes/hmac";
2
- import { sha256 } from "@noble/hashes/sha256";
2
+ import { sha256 } from "@noble/hashes/sha2";
3
3
  import { Logger } from "@waku/utils";
4
4
  import { ethers } from "ethers";
5
5
 
@@ -13,10 +13,7 @@ import type {
13
13
  } from "./keystore/index.js";
14
14
  import { KeystoreEntity, Password } from "./keystore/types.js";
15
15
  import { RegisterMembershipOptions, StartRLNOptions } from "./types.js";
16
- import {
17
- buildBigIntFromUint8Array,
18
- extractMetaMaskSigner
19
- } from "./utils/index.js";
16
+ import { extractMetaMaskSigner, switchEndianness } from "./utils/index.js";
20
17
  import { Zerokit } from "./zerokit.js";
21
18
 
22
19
  const log = new Logger("waku:credentials");
@@ -261,31 +258,31 @@ export class RLNCredentialsManager {
261
258
 
262
259
  // Generate deterministic values using HMAC-SHA256
263
260
  // We use different context strings for each component to ensure they're different
264
- const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
265
- const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
261
+ const idTrapdoorBE = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
262
+ const idNullifierBE = hmac(
263
+ sha256,
264
+ seedBytes,
265
+ encoder.encode("IDNullifier")
266
+ );
266
267
 
267
- const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
268
- const idSecretHash = sha256(combinedBytes);
268
+ const combinedBytes = new Uint8Array([...idTrapdoorBE, ...idNullifierBE]);
269
+ const idSecretHashBE = sha256(combinedBytes);
269
270
 
270
- const idCommitment = sha256(idSecretHash);
271
+ const idCommitmentBE = sha256(idSecretHashBE);
271
272
 
272
- let idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
273
- if (!this.contract) {
274
- throw Error("RLN contract is not initialized");
275
- }
276
-
277
- const Q = await this.contract.getQ();
278
- if (idCommitmentBigInt >= Q) {
279
- idCommitmentBigInt = idCommitmentBigInt % Q;
280
- }
273
+ // All hashing functions return big-endian bytes
274
+ // We need to switch to little-endian for the identity credential
275
+ const idTrapdoorLE = switchEndianness(idTrapdoorBE);
276
+ const idNullifierLE = switchEndianness(idNullifierBE);
277
+ const idSecretHashLE = switchEndianness(idSecretHashBE);
278
+ const idCommitmentLE = switchEndianness(idCommitmentBE);
281
279
 
282
280
  log.info("Successfully generated identity credential");
283
281
  return new IdentityCredential(
284
- idTrapdoor,
285
- idNullifier,
286
- idSecretHash,
287
- idCommitment,
288
- idCommitmentBigInt
282
+ idTrapdoorLE,
283
+ idNullifierLE,
284
+ idSecretHashLE,
285
+ idCommitmentLE
289
286
  );
290
287
  }
291
288
  }
package/src/identity.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { buildBigIntFromUint8Array } from "./utils/index.js";
2
-
3
1
  export class IdentityCredential {
2
+ /**
3
+ * All variables are in little-endian format
4
+ */
4
5
  public constructor(
5
6
  public readonly IDTrapdoor: Uint8Array,
6
7
  public readonly IDNullifier: Uint8Array,
7
8
  public readonly IDSecretHash: Uint8Array,
8
- public readonly IDCommitment: Uint8Array,
9
- public readonly IDCommitmentBigInt: bigint
9
+ public readonly IDCommitment: Uint8Array
10
10
  ) {}
11
11
 
12
12
  public static fromBytes(memKeys: Uint8Array): IdentityCredential {
@@ -18,14 +18,12 @@ export class IdentityCredential {
18
18
  const idNullifier = memKeys.subarray(32, 64);
19
19
  const idSecretHash = memKeys.subarray(64, 96);
20
20
  const idCommitment = memKeys.subarray(96, 128);
21
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment, 32);
22
21
 
23
22
  return new IdentityCredential(
24
23
  idTrapdoor,
25
24
  idNullifier,
26
25
  idSecretHash,
27
- idCommitment,
28
- idCommitmentBigInt
26
+ idCommitment
29
27
  );
30
28
  }
31
29
  }
@@ -14,8 +14,6 @@ import {
14
14
  import _ from "lodash";
15
15
  import { v4 as uuidV4 } from "uuid";
16
16
 
17
- import { buildBigIntFromUint8Array } from "../utils/bytes.js";
18
-
19
17
  import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
20
18
  import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
21
19
  import type {
@@ -250,26 +248,25 @@ export class Keystore {
250
248
  const str = bytesToUtf8(bytes);
251
249
  const obj = JSON.parse(str);
252
250
 
253
- // TODO: add runtime validation of nwaku credentials
251
+ const idCommitmentLE = Keystore.fromArraylikeToBytes(
252
+ _.get(obj, "identityCredential.idCommitment", [])
253
+ );
254
+ const idTrapdoorLE = Keystore.fromArraylikeToBytes(
255
+ _.get(obj, "identityCredential.idTrapdoor", [])
256
+ );
257
+ const idNullifierLE = Keystore.fromArraylikeToBytes(
258
+ _.get(obj, "identityCredential.idNullifier", [])
259
+ );
260
+ const idSecretHashLE = Keystore.fromArraylikeToBytes(
261
+ _.get(obj, "identityCredential.idSecretHash", [])
262
+ );
263
+
254
264
  return {
255
265
  identity: {
256
- IDCommitment: Keystore.fromArraylikeToBytes(
257
- _.get(obj, "identityCredential.idCommitment", [])
258
- ),
259
- IDTrapdoor: Keystore.fromArraylikeToBytes(
260
- _.get(obj, "identityCredential.idTrapdoor", [])
261
- ),
262
- IDNullifier: Keystore.fromArraylikeToBytes(
263
- _.get(obj, "identityCredential.idNullifier", [])
264
- ),
265
- IDCommitmentBigInt: buildBigIntFromUint8Array(
266
- Keystore.fromArraylikeToBytes(
267
- _.get(obj, "identityCredential.idCommitment", [])
268
- )
269
- ),
270
- IDSecretHash: Keystore.fromArraylikeToBytes(
271
- _.get(obj, "identityCredential.idSecretHash", [])
272
- )
266
+ IDCommitment: idCommitmentLE,
267
+ IDTrapdoor: idTrapdoorLE,
268
+ IDNullifier: idNullifierLE,
269
+ IDSecretHash: idSecretHashLE
273
270
  },
274
271
  membership: {
275
272
  treeIndex: _.get(obj, "treeIndex"),
@@ -321,14 +318,21 @@ export class Keystore {
321
318
  // follows nwaku implementation
322
319
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
323
320
  private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
321
+ function toLittleEndian(bytes: Uint8Array): Uint8Array {
322
+ return new Uint8Array(bytes).reverse();
323
+ }
324
324
  return utf8ToBytes(
325
325
  JSON.stringify({
326
326
  treeIndex: options.membership.treeIndex,
327
327
  identityCredential: {
328
- idCommitment: Array.from(options.identity.IDCommitment),
329
- idNullifier: Array.from(options.identity.IDNullifier),
330
- idSecretHash: Array.from(options.identity.IDSecretHash),
331
- idTrapdoor: Array.from(options.identity.IDTrapdoor)
328
+ idCommitment: Array.from(
329
+ toLittleEndian(options.identity.IDCommitment)
330
+ ),
331
+ idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
332
+ idSecretHash: Array.from(
333
+ toLittleEndian(options.identity.IDSecretHash)
334
+ ),
335
+ idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
332
336
  },
333
337
  membershipContract: {
334
338
  chainId: options.membership.chainId,
package/src/message.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import { message } from "@waku/core";
1
2
  import type {
2
3
  IDecodedMessage,
3
4
  IMessage,
4
- IRateLimitProof
5
+ IRateLimitProof,
6
+ IRlnMessage
5
7
  } from "@waku/interfaces";
6
8
  import * as utils from "@waku/utils/bytes";
7
9
 
@@ -13,12 +15,13 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
13
15
  return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
14
16
  }
15
17
 
16
- export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
18
+ export class RlnMessage<T extends IDecodedMessage> implements IRlnMessage {
17
19
  public pubsubTopic = "";
20
+ public version = message.version_0.Version;
18
21
 
19
22
  public constructor(
20
- public rlnInstance: RLNInstance,
21
- public msg: T,
23
+ private rlnInstance: RLNInstance,
24
+ private msg: T,
22
25
  public rateLimitProof: IRateLimitProof | undefined
23
26
  ) {}
24
27
 
@@ -17,18 +17,13 @@ export function concatenate(...input: Uint8Array[]): Uint8Array {
17
17
  return result;
18
18
  }
19
19
 
20
- // Adapted from https://github.com/feross/buffer
21
- function checkInt(
22
- buf: Uint8Array,
23
- value: number,
24
- offset: number,
25
- ext: number,
26
- max: number,
27
- min: number
28
- ): void {
29
- if (value > max || value < min)
30
- throw new RangeError('"value" argument is out of bounds');
31
- if (offset + ext > buf.length) throw new RangeError("Index out of range");
20
+ export function switchEndianness(bytes: Uint8Array): Uint8Array {
21
+ return new Uint8Array(bytes.reverse());
22
+ }
23
+
24
+ export function buildBigIntFromUint8ArrayBE(bytes: Uint8Array): bigint {
25
+ // Interpret bytes as big-endian
26
+ return bytes.reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
32
27
  }
33
28
 
34
29
  export function writeUIntLE(
@@ -56,19 +51,6 @@ export function writeUIntLE(
56
51
  return buf;
57
52
  }
58
53
 
59
- /**
60
- * Transforms Uint8Array into BigInt
61
- * @param array: Uint8Array
62
- * @returns BigInt
63
- */
64
- export function buildBigIntFromUint8Array(
65
- array: Uint8Array,
66
- byteOffset: number = 0
67
- ): bigint {
68
- const dataView = new DataView(array.buffer);
69
- return dataView.getBigUint64(byteOffset, true);
70
- }
71
-
72
54
  /**
73
55
  * Fills with zeros to set length
74
56
  * @param array little endian Uint8Array
@@ -82,3 +64,17 @@ export function zeroPadLE(array: Uint8Array, length: number): Uint8Array {
82
64
  }
83
65
  return result;
84
66
  }
67
+
68
+ // Adapted from https://github.com/feross/buffer
69
+ function checkInt(
70
+ buf: Uint8Array,
71
+ value: number,
72
+ offset: number,
73
+ ext: number,
74
+ max: number,
75
+ min: number
76
+ ): void {
77
+ if (value > max || value < min)
78
+ throw new RangeError('"value" argument is out of bounds');
79
+ if (offset + ext > buf.length) throw new RangeError("Index out of range");
80
+ }
@@ -2,7 +2,7 @@ export { extractMetaMaskSigner } from "./metamask.js";
2
2
  export {
3
3
  concatenate,
4
4
  writeUIntLE,
5
- buildBigIntFromUint8Array,
5
+ switchEndianness,
6
6
  zeroPadLE
7
7
  } from "./bytes.js";
8
8
  export { sha256, poseidonHash } from "./hash.js";
@@ -1,39 +0,0 @@
1
- import * as ethers from "ethers";
2
- import sinon from "sinon";
3
- import type { IdentityCredential } from "../identity.js";
4
- export declare const mockRateLimits: {
5
- minRate: number;
6
- maxRate: number;
7
- maxTotalRate: number;
8
- currentTotalRate: number;
9
- };
10
- type MockProvider = {
11
- getLogs: () => never[];
12
- getBlockNumber: () => Promise<number>;
13
- getNetwork: () => Promise<{
14
- chainId: number;
15
- }>;
16
- };
17
- type MockFilters = {
18
- MembershipRegistered: () => {
19
- address: string;
20
- };
21
- MembershipErased: () => {
22
- address: string;
23
- };
24
- MembershipExpired: () => {
25
- address: string;
26
- };
27
- };
28
- export declare function createMockProvider(): MockProvider;
29
- export declare function createMockFilters(): MockFilters;
30
- type ContractOverrides = Partial<{
31
- filters: Record<string, unknown>;
32
- [key: string]: unknown;
33
- }>;
34
- export declare function createMockRegistryContract(overrides?: ContractOverrides): ethers.Contract;
35
- export declare function mockRLNRegisteredEvent(idCommitment?: string): ethers.Event;
36
- export declare function formatIdCommitment(idCommitmentBigInt: bigint): string;
37
- export declare function createRegisterStub(identity: IdentityCredential): sinon.SinonStub;
38
- export declare function verifyRegistration(decryptedCredentials: any, identity: IdentityCredential, registerStub: sinon.SinonStub, insertMemberSpy: sinon.SinonStub): void;
39
- export {};
@@ -1,118 +0,0 @@
1
- import { hexToBytes } from "@waku/utils/bytes";
2
- import { expect } from "chai";
3
- import * as ethers from "ethers";
4
- import sinon from "sinon";
5
- import { DEFAULT_RATE_LIMIT, LINEA_CONTRACT } from "./constants.js";
6
- export const mockRateLimits = {
7
- minRate: 20,
8
- maxRate: 600,
9
- maxTotalRate: 1200,
10
- currentTotalRate: 500
11
- };
12
- export function createMockProvider() {
13
- return {
14
- getLogs: () => [],
15
- getBlockNumber: () => Promise.resolve(1000),
16
- getNetwork: () => Promise.resolve({ chainId: 11155111 })
17
- };
18
- }
19
- export function createMockFilters() {
20
- return {
21
- MembershipRegistered: () => ({ address: LINEA_CONTRACT.address }),
22
- MembershipErased: () => ({ address: LINEA_CONTRACT.address }),
23
- MembershipExpired: () => ({ address: LINEA_CONTRACT.address })
24
- };
25
- }
26
- export function createMockRegistryContract(overrides = {}) {
27
- const filters = {
28
- MembershipRegistered: () => ({ address: LINEA_CONTRACT.address }),
29
- MembershipErased: () => ({ address: LINEA_CONTRACT.address }),
30
- MembershipExpired: () => ({ address: LINEA_CONTRACT.address })
31
- };
32
- const baseContract = {
33
- minMembershipRateLimit: () => Promise.resolve(ethers.BigNumber.from(mockRateLimits.minRate)),
34
- maxMembershipRateLimit: () => Promise.resolve(ethers.BigNumber.from(mockRateLimits.maxRate)),
35
- maxTotalRateLimit: () => Promise.resolve(ethers.BigNumber.from(mockRateLimits.maxTotalRate)),
36
- currentTotalRateLimit: () => Promise.resolve(ethers.BigNumber.from(mockRateLimits.currentTotalRate)),
37
- queryFilter: () => [],
38
- provider: createMockProvider(),
39
- filters,
40
- on: () => ({}),
41
- removeAllListeners: () => ({}),
42
- register: () => ({
43
- wait: () => Promise.resolve({
44
- events: [mockRLNRegisteredEvent()]
45
- })
46
- }),
47
- estimateGas: {
48
- register: () => Promise.resolve(ethers.BigNumber.from(100000))
49
- },
50
- functions: {
51
- register: () => Promise.resolve()
52
- },
53
- getMemberIndex: () => Promise.resolve(null),
54
- interface: {
55
- getEvent: (eventName) => ({
56
- name: eventName,
57
- format: () => { }
58
- })
59
- },
60
- address: LINEA_CONTRACT.address
61
- };
62
- // Merge overrides while preserving filters
63
- const merged = {
64
- ...baseContract,
65
- ...overrides,
66
- filters: { ...filters, ...(overrides.filters || {}) }
67
- };
68
- return merged;
69
- }
70
- export function mockRLNRegisteredEvent(idCommitment) {
71
- return {
72
- args: {
73
- idCommitment: idCommitment ||
74
- "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
75
- membershipRateLimit: ethers.BigNumber.from(DEFAULT_RATE_LIMIT),
76
- index: ethers.BigNumber.from(1)
77
- },
78
- event: "MembershipRegistered"
79
- };
80
- }
81
- export function formatIdCommitment(idCommitmentBigInt) {
82
- return "0x" + idCommitmentBigInt.toString(16).padStart(64, "0");
83
- }
84
- export function createRegisterStub(identity) {
85
- return sinon.stub().callsFake(() => ({
86
- wait: () => Promise.resolve({
87
- events: [
88
- {
89
- event: "MembershipRegistered",
90
- args: {
91
- idCommitment: formatIdCommitment(identity.IDCommitmentBigInt),
92
- membershipRateLimit: ethers.BigNumber.from(DEFAULT_RATE_LIMIT),
93
- index: ethers.BigNumber.from(1)
94
- }
95
- }
96
- ]
97
- })
98
- }));
99
- }
100
- export function verifyRegistration(decryptedCredentials, identity, registerStub, insertMemberSpy) {
101
- if (!decryptedCredentials) {
102
- throw new Error("Decrypted credentials should not be undefined");
103
- }
104
- // Verify registration call
105
- expect(registerStub.calledWith(sinon.match.same(identity.IDCommitmentBigInt), sinon.match.same(DEFAULT_RATE_LIMIT), sinon.match.array, sinon.match.object)).to.be.true;
106
- // Verify credential properties
107
- expect(decryptedCredentials).to.have.property("identity");
108
- expect(decryptedCredentials).to.have.property("membership");
109
- expect(decryptedCredentials.membership).to.include({
110
- address: LINEA_CONTRACT.address,
111
- treeIndex: 1
112
- });
113
- // Verify member insertion
114
- const expectedIdCommitment = ethers.utils.zeroPad(hexToBytes(formatIdCommitment(identity.IDCommitmentBigInt)), 32);
115
- expect(insertMemberSpy.callCount).to.equal(1);
116
- expect(insertMemberSpy.getCall(0).args[0]).to.deep.equal(expectedIdCommitment);
117
- }
118
- //# sourceMappingURL=test-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"test-utils.js","sourceRoot":"","sources":["../../src/contract/test-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO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