Skip to content

Commit

Permalink
Merge pull request #102 from Rate-Limiting-Nullifier/feat/more-expose…
Browse files Browse the repository at this point in the history
…d-funcs-and-checks

Add more checks and expose more contract functions
  • Loading branch information
mhchia authored Sep 16, 2023
2 parents 579fbb5 + fdd25a6 commit d60d88e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/circuit-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export class RLNProver {
x: bigint;
epoch: bigint;
}): Promise<RLNFullProof> {
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,
Expand Down
35 changes: 29 additions & 6 deletions src/contract-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,37 @@ 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')
}
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()
Expand Down Expand Up @@ -160,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
Expand Down
6 changes: 6 additions & 0 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
}
Expand Down

0 comments on commit d60d88e

Please sign in to comment.