@waku/rln 0.0.2-00f2e75.0 → 0.0.2-2380dac.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.
- package/bundle/_virtual/utils.js +2 -2
- package/bundle/_virtual/utils2.js +2 -2
- package/bundle/packages/rln/dist/contract/rln_contract.js +31 -147
- package/bundle/packages/rln/dist/rln.js +0 -2
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
- package/bundle/packages/rln/node_modules/@noble/hashes/_sha2.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/hmac.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/pbkdf2.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/scrypt.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/sha256.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/sha512.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/utils.js +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/rln_contract.d.ts +4 -10
- package/dist/contract/rln_contract.js +31 -147
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/rln.js +0 -2
- package/dist/rln.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/rln_contract.ts +37 -215
- package/src/rln.ts +0 -3
@@ -1,4 +1,3 @@
|
|
1
|
-
/* eslint-disable no-console */
|
2
1
|
import { Logger } from "@waku/utils";
|
3
2
|
import { hexToBytes } from "@waku/utils/bytes";
|
4
3
|
import { ethers } from "ethers";
|
@@ -66,7 +65,7 @@ export class RLNContract {
|
|
66
65
|
|
67
66
|
private _members: Map<number, Member> = new Map();
|
68
67
|
private _membersFilter: ethers.EventFilter;
|
69
|
-
private
|
68
|
+
private _membershipErasedFilter: ethers.EventFilter;
|
70
69
|
private _membersExpiredFilter: ethers.EventFilter;
|
71
70
|
|
72
71
|
/**
|
@@ -115,7 +114,7 @@ export class RLNContract {
|
|
115
114
|
|
116
115
|
// Initialize event filters
|
117
116
|
this._membersFilter = this.contract.filters.MembershipRegistered();
|
118
|
-
this.
|
117
|
+
this._membershipErasedFilter = this.contract.filters.MembershipErased();
|
119
118
|
this._membersExpiredFilter = this.contract.filters.MembershipExpired();
|
120
119
|
}
|
121
120
|
|
@@ -181,12 +180,11 @@ export class RLNContract {
|
|
181
180
|
* @returns Promise<number> The remaining rate limit that can be allocated
|
182
181
|
*/
|
183
182
|
public async getRemainingTotalRateLimit(): Promise<number> {
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
return 10_000;
|
183
|
+
const [maxTotal, currentTotal] = await Promise.all([
|
184
|
+
this.contract.maxTotalRateLimit(),
|
185
|
+
this.contract.currentTotalRateLimit()
|
186
|
+
]);
|
187
|
+
return Number(maxTotal) - Number(currentTotal);
|
190
188
|
}
|
191
189
|
|
192
190
|
/**
|
@@ -211,11 +209,11 @@ export class RLNContract {
|
|
211
209
|
return this._membersFilter;
|
212
210
|
}
|
213
211
|
|
214
|
-
private get
|
215
|
-
if (!this.
|
216
|
-
throw Error("
|
212
|
+
private get membershipErasedFilter(): ethers.EventFilter {
|
213
|
+
if (!this._membershipErasedFilter) {
|
214
|
+
throw Error("MembershipErased filter was not initialized.");
|
217
215
|
}
|
218
|
-
return this.
|
216
|
+
return this._membershipErasedFilter;
|
219
217
|
}
|
220
218
|
|
221
219
|
private get membersExpiredFilter(): ethers.EventFilter {
|
@@ -237,7 +235,7 @@ export class RLNContract {
|
|
237
235
|
const removedMemberEvents = await queryFilter(this.contract, {
|
238
236
|
fromBlock: this.deployBlock,
|
239
237
|
...options,
|
240
|
-
membersFilter: this.
|
238
|
+
membersFilter: this.membershipErasedFilter
|
241
239
|
});
|
242
240
|
const expiredMemberEvents = await queryFilter(this.contract, {
|
243
241
|
fromBlock: this.deployBlock,
|
@@ -368,7 +366,7 @@ export class RLNContract {
|
|
368
366
|
);
|
369
367
|
|
370
368
|
this.contract.on(
|
371
|
-
this.
|
369
|
+
this.membershipErasedFilter,
|
372
370
|
(
|
373
371
|
_idCommitment: string,
|
374
372
|
_membershipRateLimit: ethers.BigNumber,
|
@@ -396,14 +394,6 @@ export class RLNContract {
|
|
396
394
|
identity: IdentityCredential
|
397
395
|
): Promise<DecryptedCredentials | undefined> {
|
398
396
|
try {
|
399
|
-
console.log("registerWithIdentity - starting registration process");
|
400
|
-
console.log("registerWithIdentity - identity:", identity);
|
401
|
-
console.log(
|
402
|
-
"registerWithIdentity - IDCommitmentBigInt:",
|
403
|
-
identity.IDCommitmentBigInt.toString()
|
404
|
-
);
|
405
|
-
console.log("registerWithIdentity - rate limit:", this.rateLimit);
|
406
|
-
|
407
397
|
log.info(
|
408
398
|
`Registering identity with rate limit: ${this.rateLimit} messages/epoch`
|
409
399
|
);
|
@@ -413,9 +403,6 @@ export class RLNContract {
|
|
413
403
|
identity.IDCommitmentBigInt.toString()
|
414
404
|
);
|
415
405
|
if (existingIndex) {
|
416
|
-
console.error(
|
417
|
-
`ID commitment is already registered with index ${existingIndex}`
|
418
|
-
);
|
419
406
|
throw new Error(
|
420
407
|
`ID commitment is already registered with index ${existingIndex}`
|
421
408
|
);
|
@@ -424,16 +411,11 @@ export class RLNContract {
|
|
424
411
|
// Check if there's enough remaining rate limit
|
425
412
|
const remainingRateLimit = await this.getRemainingTotalRateLimit();
|
426
413
|
if (remainingRateLimit < this.rateLimit) {
|
427
|
-
console.error(
|
428
|
-
`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`
|
429
|
-
);
|
430
414
|
throw new Error(
|
431
415
|
`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`
|
432
416
|
);
|
433
417
|
}
|
434
418
|
|
435
|
-
console.log("registerWithIdentity - calling contract.register");
|
436
|
-
// Estimate gas for the transaction
|
437
419
|
const estimatedGas = await this.contract.estimateGas.register(
|
438
420
|
identity.IDCommitmentBigInt,
|
439
421
|
this.rateLimit,
|
@@ -448,97 +430,40 @@ export class RLNContract {
|
|
448
430
|
[],
|
449
431
|
{ gasLimit }
|
450
432
|
);
|
451
|
-
console.log(
|
452
|
-
"registerWithIdentity - txRegisterResponse:",
|
453
|
-
txRegisterResponse
|
454
|
-
);
|
455
|
-
console.log("registerWithIdentity - hash:", txRegisterResponse.hash);
|
456
|
-
console.log(
|
457
|
-
"registerWithIdentity - waiting for transaction confirmation..."
|
458
|
-
);
|
459
433
|
|
460
434
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
461
|
-
console.log(
|
462
|
-
"registerWithIdentity - txRegisterReceipt:",
|
463
|
-
txRegisterReceipt
|
464
|
-
);
|
465
|
-
console.log(
|
466
|
-
"registerWithIdentity - transaction status:",
|
467
|
-
txRegisterReceipt.status
|
468
|
-
);
|
469
|
-
console.log(
|
470
|
-
"registerWithIdentity - block number:",
|
471
|
-
txRegisterReceipt.blockNumber
|
472
|
-
);
|
473
|
-
console.log(
|
474
|
-
"registerWithIdentity - gas used:",
|
475
|
-
txRegisterReceipt.gasUsed.toString()
|
476
|
-
);
|
477
435
|
|
478
|
-
// Check transaction status
|
479
436
|
if (txRegisterReceipt.status === 0) {
|
480
|
-
console.error("Transaction failed on-chain");
|
481
437
|
throw new Error("Transaction failed on-chain");
|
482
438
|
}
|
483
439
|
|
484
440
|
const memberRegistered = txRegisterReceipt.events?.find(
|
485
441
|
(event) => event.event === "MembershipRegistered"
|
486
442
|
);
|
487
|
-
console.log(
|
488
|
-
"registerWithIdentity - memberRegistered event:",
|
489
|
-
memberRegistered
|
490
|
-
);
|
491
443
|
|
492
444
|
if (!memberRegistered || !memberRegistered.args) {
|
493
|
-
|
494
|
-
"registerWithIdentity - ERROR: no memberRegistered event found"
|
495
|
-
);
|
496
|
-
console.log(
|
497
|
-
"registerWithIdentity - all events:",
|
498
|
-
txRegisterReceipt.events
|
499
|
-
);
|
500
|
-
console.error(
|
445
|
+
log.error(
|
501
446
|
"Failed to register membership: No MembershipRegistered event found"
|
502
447
|
);
|
503
448
|
return undefined;
|
504
449
|
}
|
505
450
|
|
506
|
-
console.log(
|
507
|
-
"registerWithIdentity - memberRegistered args:",
|
508
|
-
memberRegistered.args
|
509
|
-
);
|
510
451
|
const decodedData: MembershipRegisteredEvent = {
|
511
452
|
idCommitment: memberRegistered.args.idCommitment,
|
512
453
|
membershipRateLimit: memberRegistered.args.membershipRateLimit,
|
513
454
|
index: memberRegistered.args.index
|
514
455
|
};
|
515
|
-
console.log("registerWithIdentity - decodedData:", decodedData);
|
516
|
-
console.log(
|
517
|
-
"registerWithIdentity - index:",
|
518
|
-
decodedData.index.toString()
|
519
|
-
);
|
520
|
-
console.log(
|
521
|
-
"registerWithIdentity - membershipRateLimit:",
|
522
|
-
decodedData.membershipRateLimit.toString()
|
523
|
-
);
|
524
456
|
|
525
457
|
log.info(
|
526
458
|
`Successfully registered membership with index ${decodedData.index} ` +
|
527
459
|
`and rate limit ${decodedData.membershipRateLimit}`
|
528
460
|
);
|
529
461
|
|
530
|
-
console.log("registerWithIdentity - getting network information");
|
531
462
|
const network = await this.contract.provider.getNetwork();
|
532
|
-
console.log("registerWithIdentity - network:", network);
|
533
|
-
console.log("registerWithIdentity - chainId:", network.chainId);
|
534
|
-
|
535
463
|
const address = this.contract.address;
|
536
|
-
console.log("registerWithIdentity - contract address:", address);
|
537
|
-
|
538
464
|
const membershipId = Number(decodedData.index);
|
539
|
-
console.log("registerWithIdentity - membershipId:", membershipId);
|
540
465
|
|
541
|
-
|
466
|
+
return {
|
542
467
|
identity,
|
543
468
|
membership: {
|
544
469
|
address,
|
@@ -546,41 +471,33 @@ export class RLNContract {
|
|
546
471
|
chainId: network.chainId
|
547
472
|
}
|
548
473
|
};
|
549
|
-
console.log("registerWithIdentity - returning result:", result);
|
550
|
-
|
551
|
-
return result;
|
552
474
|
} catch (error) {
|
553
|
-
console.log("registerWithIdentity - ERROR:", error);
|
554
|
-
|
555
|
-
// Improved error handling to decode contract errors
|
556
475
|
if (error instanceof Error) {
|
557
476
|
const errorMessage = error.message;
|
558
|
-
|
559
|
-
|
477
|
+
log.error("registerWithIdentity - error message:", errorMessage);
|
478
|
+
log.error("registerWithIdentity - error stack:", error.stack);
|
560
479
|
|
561
480
|
// Try to extract more specific error information
|
562
481
|
if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
|
563
|
-
|
482
|
+
throw new Error(
|
564
483
|
"Registration failed: Cannot exceed maximum total rate limit"
|
565
484
|
);
|
566
485
|
} else if (errorMessage.includes("InvalidIdCommitment")) {
|
567
|
-
|
486
|
+
throw new Error("Registration failed: Invalid ID commitment");
|
568
487
|
} else if (errorMessage.includes("InvalidMembershipRateLimit")) {
|
569
|
-
|
488
|
+
throw new Error("Registration failed: Invalid membership rate limit");
|
570
489
|
} else if (errorMessage.includes("execution reverted")) {
|
571
|
-
|
572
|
-
console.error(
|
490
|
+
throw new Error(
|
573
491
|
"Contract execution reverted. Check contract requirements."
|
574
492
|
);
|
575
493
|
} else {
|
576
|
-
|
494
|
+
throw new Error(`Error in registerWithIdentity: ${errorMessage}`);
|
577
495
|
}
|
578
496
|
} else {
|
579
|
-
|
497
|
+
throw new Error("Unknown error in registerWithIdentity", {
|
498
|
+
cause: error
|
499
|
+
});
|
580
500
|
}
|
581
|
-
|
582
|
-
// Re-throw the error to allow callers to handle it
|
583
|
-
throw error;
|
584
501
|
}
|
585
502
|
}
|
586
503
|
|
@@ -735,47 +652,33 @@ export class RLNContract {
|
|
735
652
|
|
736
653
|
public async extendMembership(
|
737
654
|
idCommitment: string
|
738
|
-
): Promise<ethers.
|
739
|
-
|
740
|
-
return await tx.wait();
|
655
|
+
): Promise<ethers.ContractTransaction> {
|
656
|
+
return this.contract.extendMemberships([idCommitment]);
|
741
657
|
}
|
742
658
|
|
743
659
|
public async eraseMembership(
|
744
660
|
idCommitment: string,
|
745
661
|
eraseFromMembershipSet: boolean = true
|
746
|
-
): Promise<ethers.
|
747
|
-
|
662
|
+
): Promise<ethers.ContractTransaction> {
|
663
|
+
return this.contract.eraseMemberships(
|
748
664
|
[idCommitment],
|
749
665
|
eraseFromMembershipSet
|
750
666
|
);
|
751
|
-
return await tx.wait();
|
752
667
|
}
|
753
668
|
|
754
669
|
public async registerMembership(
|
755
670
|
idCommitment: string,
|
756
671
|
rateLimit: number = DEFAULT_RATE_LIMIT
|
757
672
|
): Promise<ethers.ContractTransaction> {
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
);
|
766
|
-
}
|
767
|
-
|
768
|
-
// Try to register
|
769
|
-
return this.contract.register(idCommitment, rateLimit, []);
|
770
|
-
} catch (error) {
|
771
|
-
console.error("Error in registerMembership:", error);
|
772
|
-
|
773
|
-
// Run debug to help diagnose the issue
|
774
|
-
await this.debugRegistration(idCommitment, rateLimit);
|
775
|
-
|
776
|
-
// Re-throw the error
|
777
|
-
throw error;
|
673
|
+
if (
|
674
|
+
rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
|
675
|
+
rateLimit > RATE_LIMIT_PARAMS.MAX_RATE
|
676
|
+
) {
|
677
|
+
throw new Error(
|
678
|
+
`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`
|
679
|
+
);
|
778
680
|
}
|
681
|
+
return this.contract.register(idCommitment, rateLimit, []);
|
779
682
|
}
|
780
683
|
|
781
684
|
private async getMemberIndex(
|
@@ -794,87 +697,6 @@ export class RLNContract {
|
|
794
697
|
return undefined;
|
795
698
|
}
|
796
699
|
}
|
797
|
-
|
798
|
-
/**
|
799
|
-
* Debug function to check why a registration might fail
|
800
|
-
* @param idCommitment The ID commitment to check
|
801
|
-
* @param rateLimit The rate limit to check
|
802
|
-
*/
|
803
|
-
public async debugRegistration(
|
804
|
-
idCommitment: string,
|
805
|
-
rateLimit: number
|
806
|
-
): Promise<void> {
|
807
|
-
console.log("=== DEBUG REGISTRATION ===");
|
808
|
-
console.log(`ID Commitment: ${idCommitment}`);
|
809
|
-
console.log(`Rate Limit: ${rateLimit}`);
|
810
|
-
|
811
|
-
// Check if ID commitment is already registered
|
812
|
-
try {
|
813
|
-
const existingIndex = await this.getMemberIndex(idCommitment);
|
814
|
-
if (existingIndex) {
|
815
|
-
console.error(
|
816
|
-
`ERROR: ID commitment is already registered with index ${existingIndex}`
|
817
|
-
);
|
818
|
-
} else {
|
819
|
-
console.log("ID commitment is not yet registered ✓");
|
820
|
-
}
|
821
|
-
} catch (error) {
|
822
|
-
console.error("Error checking if ID commitment is registered:", error);
|
823
|
-
}
|
824
|
-
|
825
|
-
// Check rate limit constraints
|
826
|
-
try {
|
827
|
-
const minRateLimit = await this.getMinRateLimit();
|
828
|
-
const maxRateLimit = await this.getMaxRateLimit();
|
829
|
-
const maxTotalRateLimit = await this.getMaxTotalRateLimit();
|
830
|
-
const currentTotalRateLimit = await this.getCurrentTotalRateLimit();
|
831
|
-
const remainingRateLimit = await this.getRemainingTotalRateLimit();
|
832
|
-
|
833
|
-
console.log(`Min Rate Limit: ${minRateLimit}`);
|
834
|
-
console.log(`Max Rate Limit: ${maxRateLimit}`);
|
835
|
-
console.log(`Max Total Rate Limit: ${maxTotalRateLimit}`);
|
836
|
-
console.log(`Current Total Rate Limit: ${currentTotalRateLimit}`);
|
837
|
-
console.log(`Remaining Rate Limit: ${remainingRateLimit}`);
|
838
|
-
|
839
|
-
if (rateLimit < minRateLimit) {
|
840
|
-
console.error(
|
841
|
-
`ERROR: Rate limit ${rateLimit} is below minimum ${minRateLimit}`
|
842
|
-
);
|
843
|
-
}
|
844
|
-
if (rateLimit > maxRateLimit) {
|
845
|
-
console.error(
|
846
|
-
`ERROR: Rate limit ${rateLimit} exceeds maximum ${maxRateLimit}`
|
847
|
-
);
|
848
|
-
}
|
849
|
-
if (rateLimit > remainingRateLimit) {
|
850
|
-
console.error(
|
851
|
-
`ERROR: Rate limit ${rateLimit} exceeds remaining capacity ${remainingRateLimit}`
|
852
|
-
);
|
853
|
-
}
|
854
|
-
} catch (error) {
|
855
|
-
console.error("Error checking rate limit constraints:", error);
|
856
|
-
}
|
857
|
-
|
858
|
-
// Try to estimate gas for the transaction to see if it would revert
|
859
|
-
try {
|
860
|
-
await this.contract.estimateGas.register(idCommitment, rateLimit, []);
|
861
|
-
console.log("Transaction gas estimation succeeded ✓");
|
862
|
-
} catch (error) {
|
863
|
-
console.error("Transaction would revert with error:", error);
|
864
|
-
|
865
|
-
// Try to extract more specific error information
|
866
|
-
const errorMessage = (error as Error).message;
|
867
|
-
if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
|
868
|
-
console.error("Cannot exceed maximum total rate limit");
|
869
|
-
} else if (errorMessage.includes("InvalidIdCommitment")) {
|
870
|
-
console.error("Invalid ID commitment format");
|
871
|
-
} else if (errorMessage.includes("InvalidMembershipRateLimit")) {
|
872
|
-
console.error("Invalid membership rate limit");
|
873
|
-
}
|
874
|
-
}
|
875
|
-
|
876
|
-
console.log("=== END DEBUG ===");
|
877
|
-
}
|
878
700
|
}
|
879
701
|
|
880
702
|
interface CustomQueryOptions extends FetchMembersOptions {
|
package/src/rln.ts
CHANGED