@waku/rln 0.0.2-2eccb11.0 → 0.0.2-3ab8023.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.
@@ -10,6 +10,7 @@ import { RATE_LIMIT_PARAMS, DEFAULT_RATE_LIMIT } from './constants.js';
10
10
  import { Contract } from '../../../../node_modules/@ethersproject/contracts/lib.esm/index.js';
11
11
  import { BigNumber } from '../../../../node_modules/@ethersproject/bignumber/lib.esm/bignumber.js';
12
12
 
13
+ /* eslint-disable no-console */
13
14
  const log = new Logger("waku:rln:contract");
14
15
  var MembershipState;
15
16
  (function (MembershipState) {
@@ -48,7 +49,7 @@ class RLNContract {
48
49
  // Use the injected contract if provided; otherwise, instantiate a new one.
49
50
  this.contract = contract || new Contract(address, RLN_ABI, signer);
50
51
  this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
51
- // Initialize event filters for all relevant events
52
+ // Initialize event filters
52
53
  this._membersFilter = this.contract.filters.MembershipRegistered();
53
54
  this._membersRemovedFilter = this.contract.filters.MembershipErased();
54
55
  this._membersExpiredFilter = this.contract.filters.MembershipExpired();
@@ -177,7 +178,6 @@ class RLNContract {
177
178
  evt.event === "MembershipExpired") {
178
179
  // Both MembershipErased and MembershipExpired events should remove members
179
180
  let index = evt.args.index;
180
- // Ensure index is an ethers.BigNumber
181
181
  if (!index) {
182
182
  return;
183
183
  }
@@ -202,7 +202,6 @@ class RLNContract {
202
202
  eventsPerBlock.push(evt);
203
203
  toInsertTable.set(evt.blockNumber, eventsPerBlock);
204
204
  }
205
- // MembershipExtended events don't change the membership set, so we don't need to handle them here
206
205
  });
207
206
  this.removeMembers(rlnInstance, toRemoveTable);
208
207
  this.insertMembers(rlnInstance, toInsertTable);
@@ -260,25 +259,69 @@ class RLNContract {
260
259
  }
261
260
  async registerWithIdentity(identity) {
262
261
  try {
262
+ console.log("registerWithIdentity - starting registration process");
263
+ console.log("registerWithIdentity - identity:", identity);
264
+ console.log("registerWithIdentity - IDCommitmentBigInt:", identity.IDCommitmentBigInt.toString());
265
+ console.log("registerWithIdentity - rate limit:", this.rateLimit);
263
266
  log.info(`Registering identity with rate limit: ${this.rateLimit} messages/epoch`);
264
- const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit: 300000 });
267
+ // Check if the ID commitment is already registered
268
+ const existingIndex = await this.getMemberIndex(identity.IDCommitmentBigInt.toString());
269
+ if (existingIndex) {
270
+ console.error(`ID commitment is already registered with index ${existingIndex}`);
271
+ throw new Error(`ID commitment is already registered with index ${existingIndex}`);
272
+ }
273
+ // Check if there's enough remaining rate limit
274
+ const remainingRateLimit = await this.getRemainingTotalRateLimit();
275
+ if (remainingRateLimit < this.rateLimit) {
276
+ console.error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
277
+ throw new Error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
278
+ }
279
+ console.log("registerWithIdentity - calling contract.register");
280
+ // Estimate gas for the transaction
281
+ const estimatedGas = await this.contract.estimateGas.register(identity.IDCommitmentBigInt, this.rateLimit, []);
282
+ const gasLimit = estimatedGas.add(10000);
283
+ const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit });
284
+ console.log("registerWithIdentity - txRegisterResponse:", txRegisterResponse);
285
+ console.log("registerWithIdentity - hash:", txRegisterResponse.hash);
286
+ console.log("registerWithIdentity - waiting for transaction confirmation...");
265
287
  const txRegisterReceipt = await txRegisterResponse.wait();
288
+ console.log("registerWithIdentity - txRegisterReceipt:", txRegisterReceipt);
289
+ console.log("registerWithIdentity - transaction status:", txRegisterReceipt.status);
290
+ console.log("registerWithIdentity - block number:", txRegisterReceipt.blockNumber);
291
+ console.log("registerWithIdentity - gas used:", txRegisterReceipt.gasUsed.toString());
292
+ // Check transaction status
293
+ if (txRegisterReceipt.status === 0) {
294
+ console.error("Transaction failed on-chain");
295
+ throw new Error("Transaction failed on-chain");
296
+ }
266
297
  const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
298
+ console.log("registerWithIdentity - memberRegistered event:", memberRegistered);
267
299
  if (!memberRegistered || !memberRegistered.args) {
268
- log.error("Failed to register membership: No MembershipRegistered event found");
300
+ console.log("registerWithIdentity - ERROR: no memberRegistered event found");
301
+ console.log("registerWithIdentity - all events:", txRegisterReceipt.events);
302
+ console.error("Failed to register membership: No MembershipRegistered event found");
269
303
  return undefined;
270
304
  }
305
+ console.log("registerWithIdentity - memberRegistered args:", memberRegistered.args);
271
306
  const decodedData = {
272
307
  idCommitment: memberRegistered.args.idCommitment,
273
308
  membershipRateLimit: memberRegistered.args.membershipRateLimit,
274
309
  index: memberRegistered.args.index
275
310
  };
311
+ console.log("registerWithIdentity - decodedData:", decodedData);
312
+ console.log("registerWithIdentity - index:", decodedData.index.toString());
313
+ console.log("registerWithIdentity - membershipRateLimit:", decodedData.membershipRateLimit.toString());
276
314
  log.info(`Successfully registered membership with index ${decodedData.index} ` +
277
315
  `and rate limit ${decodedData.membershipRateLimit}`);
316
+ console.log("registerWithIdentity - getting network information");
278
317
  const network = await this.contract.provider.getNetwork();
318
+ console.log("registerWithIdentity - network:", network);
319
+ console.log("registerWithIdentity - chainId:", network.chainId);
279
320
  const address = this.contract.address;
321
+ console.log("registerWithIdentity - contract address:", address);
280
322
  const membershipId = decodedData.index.toNumber();
281
- return {
323
+ console.log("registerWithIdentity - membershipId:", membershipId);
324
+ const result = {
282
325
  identity,
283
326
  membership: {
284
327
  address,
@@ -286,10 +329,39 @@ class RLNContract {
286
329
  chainId: network.chainId
287
330
  }
288
331
  };
332
+ console.log("registerWithIdentity - returning result:", result);
333
+ return result;
289
334
  }
290
335
  catch (error) {
291
- log.error(`Error in registerWithIdentity: ${error.message}`);
292
- return undefined;
336
+ console.log("registerWithIdentity - ERROR:", error);
337
+ // Improved error handling to decode contract errors
338
+ if (error instanceof Error) {
339
+ const errorMessage = error.message;
340
+ console.log("registerWithIdentity - error message:", errorMessage);
341
+ console.log("registerWithIdentity - error stack:", error.stack);
342
+ // Try to extract more specific error information
343
+ if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
344
+ console.error("Registration failed: Cannot exceed maximum total rate limit");
345
+ }
346
+ else if (errorMessage.includes("InvalidIdCommitment")) {
347
+ console.error("Registration failed: Invalid ID commitment");
348
+ }
349
+ else if (errorMessage.includes("InvalidMembershipRateLimit")) {
350
+ console.error("Registration failed: Invalid membership rate limit");
351
+ }
352
+ else if (errorMessage.includes("execution reverted")) {
353
+ // Generic revert
354
+ console.error("Contract execution reverted. Check contract requirements.");
355
+ }
356
+ else {
357
+ console.error(`Error in registerWithIdentity: ${errorMessage}`);
358
+ }
359
+ }
360
+ else {
361
+ console.error("Unknown error in registerWithIdentity");
362
+ }
363
+ // Re-throw the error to allow callers to handle it
364
+ throw error;
293
365
  }
294
366
  }
295
367
  /**
@@ -398,12 +470,21 @@ class RLNContract {
398
470
  return await tx.wait();
399
471
  }
400
472
  async registerMembership(idCommitment, rateLimit = DEFAULT_RATE_LIMIT) {
401
- if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
402
- rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
403
- throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
473
+ try {
474
+ if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
475
+ rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
476
+ throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
477
+ }
478
+ // Try to register
479
+ return this.contract.register(idCommitment, rateLimit, []);
480
+ }
481
+ catch (error) {
482
+ console.error("Error in registerMembership:", error);
483
+ // Run debug to help diagnose the issue
484
+ await this.debugRegistration(idCommitment, rateLimit);
485
+ // Re-throw the error
486
+ throw error;
404
487
  }
405
- const tx = await this.contract.register(idCommitment, rateLimit, []);
406
- return await tx.wait();
407
488
  }
408
489
  async getMemberIndex(idCommitment) {
409
490
  try {
@@ -418,6 +499,74 @@ class RLNContract {
418
499
  return undefined;
419
500
  }
420
501
  }
502
+ /**
503
+ * Debug function to check why a registration might fail
504
+ * @param idCommitment The ID commitment to check
505
+ * @param rateLimit The rate limit to check
506
+ */
507
+ async debugRegistration(idCommitment, rateLimit) {
508
+ console.log("=== DEBUG REGISTRATION ===");
509
+ console.log(`ID Commitment: ${idCommitment}`);
510
+ console.log(`Rate Limit: ${rateLimit}`);
511
+ // Check if ID commitment is already registered
512
+ try {
513
+ const existingIndex = await this.getMemberIndex(idCommitment);
514
+ if (existingIndex) {
515
+ console.error(`ERROR: ID commitment is already registered with index ${existingIndex}`);
516
+ }
517
+ else {
518
+ console.log("ID commitment is not yet registered ✓");
519
+ }
520
+ }
521
+ catch (error) {
522
+ console.error("Error checking if ID commitment is registered:", error);
523
+ }
524
+ // Check rate limit constraints
525
+ try {
526
+ const minRateLimit = await this.getMinRateLimit();
527
+ const maxRateLimit = await this.getMaxRateLimit();
528
+ const maxTotalRateLimit = await this.getMaxTotalRateLimit();
529
+ const currentTotalRateLimit = await this.getCurrentTotalRateLimit();
530
+ const remainingRateLimit = await this.getRemainingTotalRateLimit();
531
+ console.log(`Min Rate Limit: ${minRateLimit}`);
532
+ console.log(`Max Rate Limit: ${maxRateLimit}`);
533
+ console.log(`Max Total Rate Limit: ${maxTotalRateLimit}`);
534
+ console.log(`Current Total Rate Limit: ${currentTotalRateLimit}`);
535
+ console.log(`Remaining Rate Limit: ${remainingRateLimit}`);
536
+ if (rateLimit < minRateLimit) {
537
+ console.error(`ERROR: Rate limit ${rateLimit} is below minimum ${minRateLimit}`);
538
+ }
539
+ if (rateLimit > maxRateLimit) {
540
+ console.error(`ERROR: Rate limit ${rateLimit} exceeds maximum ${maxRateLimit}`);
541
+ }
542
+ if (rateLimit > remainingRateLimit) {
543
+ console.error(`ERROR: Rate limit ${rateLimit} exceeds remaining capacity ${remainingRateLimit}`);
544
+ }
545
+ }
546
+ catch (error) {
547
+ console.error("Error checking rate limit constraints:", error);
548
+ }
549
+ // Try to estimate gas for the transaction to see if it would revert
550
+ try {
551
+ await this.contract.estimateGas.register(idCommitment, rateLimit, []);
552
+ console.log("Transaction gas estimation succeeded ✓");
553
+ }
554
+ catch (error) {
555
+ console.error("Transaction would revert with error:", error);
556
+ // Try to extract more specific error information
557
+ const errorMessage = error.message;
558
+ if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
559
+ console.error("Cannot exceed maximum total rate limit");
560
+ }
561
+ else if (errorMessage.includes("InvalidIdCommitment")) {
562
+ console.error("Invalid ID commitment format");
563
+ }
564
+ else if (errorMessage.includes("InvalidMembershipRateLimit")) {
565
+ console.error("Invalid membership rate limit");
566
+ }
567
+ }
568
+ console.log("=== END DEBUG ===");
569
+ }
421
570
  }
422
571
  // These values should be tested on other networks
423
572
  const FETCH_CHUNK = 5;
@@ -100,8 +100,6 @@ class RLNInstance {
100
100
  return this._signer;
101
101
  }
102
102
  async start(options = {}) {
103
- // eslint-disable-next-line no-console
104
- console.log("starting", options);
105
103
  if (this.started || this.starting) {
106
104
  return;
107
105
  }
@@ -133,12 +131,6 @@ class RLNInstance {
133
131
  if (address === SEPOLIA_CONTRACT.address) {
134
132
  chainId = SEPOLIA_CONTRACT.chainId;
135
133
  }
136
- // eslint-disable-next-line no-console
137
- console.log({
138
- chainId,
139
- address,
140
- SEPOLIA_CONTRACT
141
- });
142
134
  const signer = options.signer || (await extractMetaMaskSigner());
143
135
  const currentChainId = await signer.getChainId();
144
136
  if (chainId && chainId !== currentChainId) {
@@ -174,6 +166,8 @@ class RLNInstance {
174
166
  if ("signature" in options) {
175
167
  identity = this.zerokit.generateSeededIdentityCredential(options.signature);
176
168
  }
169
+ // eslint-disable-next-line no-console
170
+ console.log("registering membership", identity);
177
171
  if (!identity) {
178
172
  throw Error("Missing signature or identity to register membership.");
179
173
  }