Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more checks and expose more contract functions #102

Merged
merged 3 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 Expand Up @@ -257,7 +263,7 @@
this.group.removeMember(index)
}

async slash(identitySecret: bigint, _?: string): Promise<void> {

Check warning on line 266 in src/registry.ts

View workflow job for this annotation

GitHub Actions / build

'_' is defined but never used
const identityCommitment = poseidon([identitySecret])
const rateCommitment = await this.getRateCommitment(identityCommitment)
const index = this.group.indexOf(rateCommitment)
Expand Down
Loading