From 5a5d7b3e26e2525f5e743f7ed431a20b742c43bf Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Thu, 15 May 2025 16:24:43 +0530 Subject: [PATCH] chore: cannot erase membership if it isn't expired/in grace period --- .../rln/src/contract/rln_base_contract.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/rln/src/contract/rln_base_contract.ts b/packages/rln/src/contract/rln_base_contract.ts index 46a1e75e42..1b0730b8ae 100644 --- a/packages/rln/src/contract/rln_base_contract.ts +++ b/packages/rln/src/contract/rln_base_contract.ts @@ -432,6 +432,13 @@ export class RLNBaseContract { idCommitmentBigInt: bigint, eraseFromMembershipSet: boolean = true ): Promise { + if ( + !(await this.isExpired(idCommitmentBigInt)) && + !(await this.isInGracePeriod(idCommitmentBigInt)) + ) { + throw new Error("Membership is not expired or in grace period"); + } + const estimatedGas = await this.contract.estimateGas[ "eraseMemberships(uint256[],bool)" ]([idCommitmentBigInt], eraseFromMembershipSet); @@ -711,4 +718,32 @@ export class RLNBaseContract { return undefined; } } + + /** + * Checks if a membership is expired for the given idCommitment + * @param idCommitmentBigInt The idCommitment as bigint + * @returns Promise True if expired, false otherwise + */ + public async isExpired(idCommitmentBigInt: bigint): Promise { + try { + return await this.contract.isExpired(idCommitmentBigInt); + } catch (error) { + log.error("Error in isExpired:", error); + return false; + } + } + + /** + * Checks if a membership is in grace period for the given idCommitment + * @param idCommitmentBigInt The idCommitment as bigint + * @returns Promise True if in grace period, false otherwise + */ + public async isInGracePeriod(idCommitmentBigInt: bigint): Promise { + try { + return await this.contract.isInGracePeriod(idCommitmentBigInt); + } catch (error) { + log.error("Error in isInGracePeriod:", error); + return false; + } + } }