From 4adc756bfe0e44c6ef6ece776045278665b18143 Mon Sep 17 00:00:00 2001 From: mhchia Date: Sat, 16 Sep 2023 16:11:17 +0800 Subject: [PATCH 1/3] add more checks --- src/circuit-wrapper.ts | 4 ++++ src/registry.ts | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/circuit-wrapper.ts b/src/circuit-wrapper.ts index 96da242f..52f5ae6b 100644 --- a/src/circuit-wrapper.ts +++ b/src/circuit-wrapper.ts @@ -75,6 +75,10 @@ export class RLNProver { x: bigint; epoch: bigint; }): Promise { + if (args.x <= BigInt(0)) { + // y = identitySecret + a1 * x + throw new Error('identity secret is directly leaked if x = 0') + } const witness: RLNWitness = { identitySecret: args.identitySecret, userMessageLimit: args.userMessageLimit, diff --git a/src/registry.ts b/src/registry.ts index 71e9feea..2c253cfe 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -137,6 +137,9 @@ export class ContractRLNRegistry implements IRLNRegistry { identitySecret, address: userAddressBigInt, }) + if (identityCommitment != BigInt(proof.publicSignals[0]) || userAddressBigInt != BigInt(proof.publicSignals[1])) { + throw new Error('Withdraw proof public signals do not match') + } await this.rlnContract.withdraw(identityCommitment, proof.proof) } @@ -163,6 +166,9 @@ export class ContractRLNRegistry implements IRLNRegistry { identitySecret, address: receiverBigInt, }) + if (identityCommitment != BigInt(proof.publicSignals[0]) || receiverBigInt != BigInt(proof.publicSignals[1])) { + throw new Error('Withdraw proof public signals do not match') + } await this.rlnContract.slash(identityCommitment, receiver, proof.proof) } } From c7b1b04025efe26d674d8885d79b84d3e3d4afaf Mon Sep 17 00:00:00 2001 From: mhchia Date: Sat, 16 Sep 2023 16:13:02 +0800 Subject: [PATCH 2/3] expose more contract functions --- src/contract-wrapper.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/contract-wrapper.ts b/src/contract-wrapper.ts index 48934f30..1caa4240 100644 --- a/src/contract-wrapper.ts +++ b/src/contract-wrapper.ts @@ -90,10 +90,6 @@ export class RLNContract { return this.signer || this.provider } - async getTokenAddress() { - return this.rlnContract.token() - } - async getSignerAddress() { if (this.signer === undefined) { throw new Error('Cannot get signer address if signer is not set') @@ -101,6 +97,30 @@ export class RLNContract { return this.signer.getAddress() } + async getMinimalDeposit() { + return this.rlnContract.MINIMAL_DEPOSIT() + } + + async getMaximalRate() { + return this.rlnContract.MAXIMAL_RATE() + } + + async getFeeReceiver() { + return this.rlnContract.FEE_RECEIVER() + } + + async getFeePercentage() { + return this.rlnContract.FEE_PERCENTAGE() + } + + async getFreezePeriod() { + return this.rlnContract.FREEZE_PERIOD() + } + + async getTokenAddress() { + return this.rlnContract.token() + } + async getLogs() { const rlnContractAddress = await this.rlnContract.getAddress() const currentBlockNumber = await this.provider.getBlockNumber() From fdd25a65118170c07c4fff6cdfdb69dc94eb2585 Mon Sep 17 00:00:00 2001 From: mhchia Date: Sat, 16 Sep 2023 16:13:45 +0800 Subject: [PATCH 3/3] approve only when allowance is not enough --- src/contract-wrapper.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/contract-wrapper.ts b/src/contract-wrapper.ts index 1caa4240..aee05ca9 100644 --- a/src/contract-wrapper.ts +++ b/src/contract-wrapper.ts @@ -180,8 +180,11 @@ export class RLNContract { erc20ABI, this.getContractRunner(), ) - const txApprove = await tokenContract.approve(rlnContractAddress, amount) - await txApprove.wait() + const allowance = await tokenContract.allowance(await this.getSignerAddress(), rlnContractAddress) + if (allowance < amount) { + const txApprove = await tokenContract.approve(rlnContractAddress, amount) + await txApprove.wait() + } const txRegister = await this.rlnContract.register(identityCommitment, amount) const receipt = await txRegister.wait() return receipt