From 6997987a60a52f1382230ff7b64b25606610534f Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Thu, 3 Apr 2025 19:56:11 +0530 Subject: [PATCH] chore: public methods written above private methods --- .../rln/src/contract/rln_base_contract.ts | 106 ++++++------- packages/rln/src/credentials_manager.ts | 150 +++++++++--------- 2 files changed, 128 insertions(+), 128 deletions(-) diff --git a/packages/rln/src/contract/rln_base_contract.ts b/packages/rln/src/contract/rln_base_contract.ts index 9393476709..2c46810fbe 100644 --- a/packages/rln/src/contract/rln_base_contract.ts +++ b/packages/rln/src/contract/rln_base_contract.ts @@ -60,21 +60,6 @@ export class RLNBaseContract { this._membersExpiredFilter = this.contract.filters.MembershipExpired(); } - /** - * Validates that the rate limit is within the allowed range - * @throws Error if the rate limit is outside the allowed range - */ - private validateRateLimit(rateLimit: number): void { - if ( - rateLimit < RATE_LIMIT_PARAMS.MIN_RATE || - rateLimit > RATE_LIMIT_PARAMS.MAX_RATE - ) { - throw new Error( - `Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE} messages per epoch` - ); - } - } - /** * Gets the current rate limit for this contract instance */ @@ -160,27 +145,6 @@ export class RLNBaseContract { return sortedMembers; } - private get membersFilter(): ethers.EventFilter { - if (!this._membersFilter) { - throw Error("Members filter was not initialized."); - } - return this._membersFilter; - } - - private get membershipErasedFilter(): ethers.EventFilter { - if (!this._membershipErasedFilter) { - throw Error("MembershipErased filter was not initialized."); - } - return this._membershipErasedFilter; - } - - private get membersExpiredFilter(): ethers.EventFilter { - if (!this._membersExpiredFilter) { - throw Error("MembersExpired filter was not initialized."); - } - return this._membersExpiredFilter; - } - public async fetchMembers(options: FetchMembersOptions = {}): Promise { const registeredMemberEvents = await RLNBaseContract.queryFilter( this.contract, @@ -480,23 +444,6 @@ export class RLNBaseContract { ); } - private async getMemberIndex( - idCommitment: string - ): Promise { - try { - const events = await this.contract.queryFilter( - this.contract.filters.MembershipRegistered(idCommitment) - ); - if (events.length === 0) return undefined; - - // Get the most recent registration event - const event = events[events.length - 1]; - return event.args?.index; - } catch (error) { - return undefined; - } - } - public async registerMembership( idCommitment: string, rateLimit: number = DEFAULT_RATE_LIMIT @@ -704,4 +651,57 @@ export class RLNBaseContract { return undefined; } } + + /** + * Validates that the rate limit is within the allowed range + * @throws Error if the rate limit is outside the allowed range + */ + private validateRateLimit(rateLimit: number): void { + if ( + rateLimit < RATE_LIMIT_PARAMS.MIN_RATE || + rateLimit > RATE_LIMIT_PARAMS.MAX_RATE + ) { + throw new Error( + `Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE} messages per epoch` + ); + } + } + + private get membersFilter(): ethers.EventFilter { + if (!this._membersFilter) { + throw Error("Members filter was not initialized."); + } + return this._membersFilter; + } + + private get membershipErasedFilter(): ethers.EventFilter { + if (!this._membershipErasedFilter) { + throw Error("MembershipErased filter was not initialized."); + } + return this._membershipErasedFilter; + } + + private get membersExpiredFilter(): ethers.EventFilter { + if (!this._membersExpiredFilter) { + throw Error("MembersExpired filter was not initialized."); + } + return this._membersExpiredFilter; + } + + private async getMemberIndex( + idCommitment: string + ): Promise { + try { + const events = await this.contract.queryFilter( + this.contract.filters.MembershipRegistered(idCommitment) + ); + if (events.length === 0) return undefined; + + // Get the most recent registration event + const event = events[events.length - 1]; + return event.args?.index; + } catch (error) { + return undefined; + } + } } diff --git a/packages/rln/src/credentials_manager.ts b/packages/rln/src/credentials_manager.ts index 86e78c3af6..1cf0495d59 100644 --- a/packages/rln/src/credentials_manager.ts +++ b/packages/rln/src/credentials_manager.ts @@ -120,6 +120,54 @@ export class RLNCredentialsManager { } } + public async registerMembership( + options: RegisterMembershipOptions + ): Promise { + if (!this.contract) { + log.error("RLN Contract is not initialized"); + throw Error("RLN Contract is not initialized."); + } + + log.info("Registering membership"); + let identity = "identity" in options && options.identity; + + if ("signature" in options) { + log.info("Generating identity from signature"); + if (this.zerokit) { + log.info("Using Zerokit to generate identity"); + identity = this.zerokit.generateSeededIdentityCredential( + options.signature + ); + } else { + log.info("Using local implementation to generate identity"); + identity = this.generateSeededIdentityCredential(options.signature); + } + } + + if (!identity) { + log.error("Missing signature or identity to register membership"); + throw Error("Missing signature or identity to register membership."); + } + + log.info("Registering identity with contract"); + return this.contract.registerWithIdentity(identity); + } + + /** + * Changes credentials in use by relying on provided Keystore earlier in rln.start + * @param id: string, hash of credentials to select from Keystore + * @param password: string or bytes to use to decrypt credentials from Keystore + */ + public async useCredentials(id: string, password: Password): Promise { + log.info(`Attempting to use credentials with ID: ${id}`); + this._credentials = await this.keystore?.readCredential(id, password); + if (this._credentials) { + log.info("Successfully loaded credentials"); + } else { + log.warn("Failed to load credentials"); + } + } + protected async determineStartOptions( options: StartRLNOptions, credentials: KeystoreEntity | undefined @@ -192,6 +240,33 @@ export class RLNCredentialsManager { } } + protected async verifyCredentialsAgainstContract( + credentials: KeystoreEntity + ): Promise { + if (!this.contract) { + throw Error( + "Failed to verify chain coordinates: no contract initialized." + ); + } + + const registryAddress = credentials.membership.address; + const currentRegistryAddress = this.contract.address; + if (registryAddress !== currentRegistryAddress) { + throw Error( + `Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}` + ); + } + + const chainId = credentials.membership.chainId; + const network = await this.contract.provider.getNetwork(); + const currentChainId = network.chainId; + if (chainId !== currentChainId) { + throw Error( + `Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}` + ); + } + } + /** * Generates an identity credential from a seed string * This is a pure implementation that doesn't rely on Zerokit @@ -228,79 +303,4 @@ export class RLNCredentialsManager { idCommitmentBigInt ); } - - public async registerMembership( - options: RegisterMembershipOptions - ): Promise { - if (!this.contract) { - log.error("RLN Contract is not initialized"); - throw Error("RLN Contract is not initialized."); - } - - log.info("Registering membership"); - let identity = "identity" in options && options.identity; - - if ("signature" in options) { - log.info("Generating identity from signature"); - if (this.zerokit) { - log.info("Using Zerokit to generate identity"); - identity = this.zerokit.generateSeededIdentityCredential( - options.signature - ); - } else { - log.info("Using local implementation to generate identity"); - identity = this.generateSeededIdentityCredential(options.signature); - } - } - - if (!identity) { - log.error("Missing signature or identity to register membership"); - throw Error("Missing signature or identity to register membership."); - } - - log.info("Registering identity with contract"); - return this.contract.registerWithIdentity(identity); - } - - /** - * Changes credentials in use by relying on provided Keystore earlier in rln.start - * @param id: string, hash of credentials to select from Keystore - * @param password: string or bytes to use to decrypt credentials from Keystore - */ - public async useCredentials(id: string, password: Password): Promise { - log.info(`Attempting to use credentials with ID: ${id}`); - this._credentials = await this.keystore?.readCredential(id, password); - if (this._credentials) { - log.info("Successfully loaded credentials"); - } else { - log.warn("Failed to load credentials"); - } - } - - protected async verifyCredentialsAgainstContract( - credentials: KeystoreEntity - ): Promise { - if (!this.contract) { - throw Error( - "Failed to verify chain coordinates: no contract initialized." - ); - } - - const registryAddress = credentials.membership.address; - const currentRegistryAddress = this.contract.address; - if (registryAddress !== currentRegistryAddress) { - throw Error( - `Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}` - ); - } - - const chainId = credentials.membership.chainId; - const network = await this.contract.provider.getNetwork(); - const currentChainId = network.chainId; - if (chainId !== currentChainId) { - throw Error( - `Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}` - ); - } - } }