@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
@@ -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,
@@ -25,6 +30,12 @@ export class RLNBaseContract {
25
30
  private minRateLimit?: number;
26
31
  private maxRateLimit?: number;
27
32
 
33
+ /**
34
+ * Default Q value for the RLN contract.
35
+ * @see https://github.com/waku-org/waku-rlnv2-contract/blob/b7e9a9b1bc69256a2a3076c1f099b50ce84e7eff/src/WakuRlnV2.sol#L25
36
+ */
37
+ public idCommitmentBigIntLimit = DEFAULT_Q;
38
+
28
39
  protected _members: Map<number, Member> = new Map();
29
40
  private _membersFilter: ethers.EventFilter;
30
41
  private _membershipErasedFilter: ethers.EventFilter;
@@ -79,7 +90,8 @@ export class RLNBaseContract {
79
90
  const instance = new RLNBaseContract(options);
80
91
  const [min, max] = await Promise.all([
81
92
  instance.contract.minMembershipRateLimit(),
82
- instance.contract.maxMembershipRateLimit()
93
+ instance.contract.maxMembershipRateLimit(),
94
+ instance.contract.Q()
83
95
  ]);
84
96
  instance.minRateLimit = ethers.BigNumber.from(min).toNumber();
85
97
  instance.maxRateLimit = ethers.BigNumber.from(max).toNumber();
@@ -482,15 +494,32 @@ export class RLNBaseContract {
482
494
  return this.contract.register(idCommitmentBigInt, rateLimit, []);
483
495
  }
484
496
 
485
- public async withdraw(token: string, from: string): Promise<void> {
497
+ public async withdraw(token: string, walletAddress: string): Promise<void> {
486
498
  try {
487
- const tx = await this.contract.withdraw(token, from);
499
+ const tx = await this.contract.withdraw(token, walletAddress);
488
500
  await tx.wait();
489
501
  } catch (error) {
490
502
  log.error(`Error in withdraw: ${(error as Error).message}`);
491
503
  }
492
504
  }
493
505
 
506
+ private getIdCommitmentBigInt(bytes: Uint8Array): bigint {
507
+ let idCommitmentBigIntBE = buildBigIntFromUint8ArrayBE(bytes);
508
+
509
+ if (!this.contract) {
510
+ throw Error("RLN contract is not initialized");
511
+ }
512
+
513
+ if (idCommitmentBigIntBE >= this.idCommitmentBigIntLimit) {
514
+ log.warn(
515
+ `ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigIntBE} % ${this.idCommitmentBigIntLimit}`
516
+ );
517
+ idCommitmentBigIntBE =
518
+ idCommitmentBigIntBE % this.idCommitmentBigIntLimit;
519
+ }
520
+ return idCommitmentBigIntBE;
521
+ }
522
+
494
523
  public async registerWithIdentity(
495
524
  identity: IdentityCredential
496
525
  ): Promise<DecryptedCredentials | undefined> {
@@ -499,10 +528,12 @@ export class RLNBaseContract {
499
528
  `Registering identity with rate limit: ${this.rateLimit} messages/epoch`
500
529
  );
501
530
 
502
- // Check if the ID commitment is already registered
503
- const existingIndex = await this.getMemberIndex(
504
- identity.IDCommitmentBigInt
531
+ const idCommitmentBigInt = this.getIdCommitmentBigInt(
532
+ identity.IDCommitment
505
533
  );
534
+
535
+ // Check if the ID commitment is already registered
536
+ const existingIndex = await this.getMemberIndex(idCommitmentBigInt);
506
537
  if (existingIndex) {
507
538
  throw new Error(
508
539
  `ID commitment is already registered with index ${existingIndex}`
@@ -518,19 +549,16 @@ export class RLNBaseContract {
518
549
  }
519
550
 
520
551
  const estimatedGas = await this.contract.estimateGas.register(
521
- identity.IDCommitmentBigInt,
552
+ idCommitmentBigInt,
522
553
  this.rateLimit,
523
554
  []
524
555
  );
525
556
  const gasLimit = estimatedGas.add(10000);
526
557
 
527
558
  const txRegisterResponse: ethers.ContractTransaction =
528
- await this.contract.register(
529
- identity.IDCommitmentBigInt,
530
- this.rateLimit,
531
- [],
532
- { gasLimit }
533
- );
559
+ await this.contract.register(idCommitmentBigInt, this.rateLimit, [], {
560
+ gasLimit
561
+ });
534
562
 
535
563
  const txRegisterReceipt = await txRegisterResponse.wait();
536
564
 
@@ -626,7 +654,7 @@ export class RLNBaseContract {
626
654
  permit.v,
627
655
  permit.r,
628
656
  permit.s,
629
- identity.IDCommitmentBigInt,
657
+ this.getIdCommitmentBigInt(identity.IDCommitment),
630
658
  this.rateLimit,
631
659
  idCommitmentsToErase.map((id) => ethers.BigNumber.from(id))
632
660
  );
@@ -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");
@@ -116,7 +113,9 @@ export class RLNCredentialsManager {
116
113
  );
117
114
  } else {
118
115
  log.info("Using local implementation to generate identity");
119
- identity = this.generateSeededIdentityCredential(options.signature);
116
+ identity = await this.generateSeededIdentityCredential(
117
+ options.signature
118
+ );
120
119
  }
121
120
  }
122
121
 
@@ -249,7 +248,9 @@ export class RLNCredentialsManager {
249
248
  * @param seed A string seed to generate the identity from
250
249
  * @returns IdentityCredential
251
250
  */
252
- private generateSeededIdentityCredential(seed: string): IdentityCredential {
251
+ private async generateSeededIdentityCredential(
252
+ seed: string
253
+ ): Promise<IdentityCredential> {
253
254
  log.info("Generating seeded identity credential");
254
255
  // Convert the seed to bytes
255
256
  const encoder = new TextEncoder();
@@ -257,26 +258,31 @@ export class RLNCredentialsManager {
257
258
 
258
259
  // Generate deterministic values using HMAC-SHA256
259
260
  // We use different context strings for each component to ensure they're different
260
- const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
261
- 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
+ );
262
267
 
263
- // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
264
- const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
265
- const idSecretHash = sha256(combinedBytes);
268
+ const combinedBytes = new Uint8Array([...idTrapdoorBE, ...idNullifierBE]);
269
+ const idSecretHashBE = sha256(combinedBytes);
266
270
 
267
- // Generate IDCommitment as a hash of IDSecretHash
268
- const idCommitment = sha256(idSecretHash);
271
+ const idCommitmentBE = sha256(idSecretHashBE);
269
272
 
270
- // Convert IDCommitment to BigInt
271
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
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);
272
279
 
273
280
  log.info("Successfully generated identity credential");
274
281
  return new IdentityCredential(
275
- idTrapdoor,
276
- idNullifier,
277
- idSecretHash,
278
- idCommitment,
279
- idCommitmentBigInt
282
+ idTrapdoorLE,
283
+ idNullifierLE,
284
+ idSecretHashLE,
285
+ idCommitmentLE
280
286
  );
281
287
  }
282
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 {
@@ -19,14 +19,11 @@ export class IdentityCredential {
19
19
  const idSecretHash = memKeys.subarray(64, 96);
20
20
  const idCommitment = memKeys.subarray(96, 128);
21
21
 
22
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
23
-
24
22
  return new IdentityCredential(
25
23
  idTrapdoor,
26
24
  idNullifier,
27
25
  idSecretHash,
28
- idCommitment,
29
- idCommitmentBigInt
26
+ idCommitment
30
27
  );
31
28
  }
32
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(array: Uint8Array): bigint {
65
- let hex = "";
66
- for (let i = 0; i < array.length; i++) {
67
- hex = array[i].toString(16).padStart(2, "0") + hex; // Reverse order for big-endian
68
- }
69
- return BigInt("0x" + hex);
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,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEpE,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,OAAO,EAAE,EAAE;IACX,OAAO,EAAE,GAAG;IACZ,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE,GAAG;CACtB,CAAC;AAcF,MAAM,UAAU,kBAAkB;IAChC,OAAO;QACL,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE;QACjB,cAAc,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;QAC3C,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;KACzD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,OAAO;QACL,oBAAoB,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,cAAc,CAAC,OAAO,EAAE,CAAC;QACjE,gBAAgB,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,cAAc,CAAC,OAAO,EAAE,CAAC;QAC7D,iBAAiB,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,cAAc,CAAC,OAAO,EAAE,CAAC;KAC/D,CAAC;AACJ,CAAC;AAOD,MAAM,UAAU,0BAA0B,CACxC,YAA+B,EAAE;IAEjC,MAAM,OAAO,GAAG;QACd,oBA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