2022-11-25 06:37:06 +00:00
|
|
|
import { expect, assert } from "chai";
|
2022-11-25 06:01:06 +00:00
|
|
|
import { ethers, deployments } from "hardhat";
|
|
|
|
|
|
|
|
describe("RLN", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await deployments.fixture(["RLN"]);
|
2022-11-25 06:08:38 +00:00
|
|
|
});
|
2022-11-25 06:01:06 +00:00
|
|
|
|
|
|
|
it("should register new memberships", async () => {
|
|
|
|
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
|
2022-11-25 06:08:38 +00:00
|
|
|
|
2022-11-25 06:01:06 +00:00
|
|
|
const price = await rln.MEMBERSHIP_DEPOSIT();
|
|
|
|
|
|
|
|
// A valid pair of (id_secret, id_commitment) generated in rust
|
2022-11-25 06:08:38 +00:00
|
|
|
const idCommitment =
|
|
|
|
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
|
2022-11-25 06:01:06 +00:00
|
|
|
|
2022-11-25 06:08:38 +00:00
|
|
|
const registerTx = await rln["register(uint256)"](idCommitment, {
|
|
|
|
value: price,
|
|
|
|
});
|
2022-11-25 06:01:06 +00:00
|
|
|
const txRegisterReceipt = await registerTx.wait();
|
|
|
|
|
2022-11-25 06:08:38 +00:00
|
|
|
const pubkey = txRegisterReceipt.events[0].args.pubkey;
|
2022-11-25 06:01:06 +00:00
|
|
|
|
|
|
|
// We ensure the registered id_commitment is the one we passed
|
2022-11-25 06:08:38 +00:00
|
|
|
expect(
|
|
|
|
pubkey.toHexString() === idCommitment,
|
|
|
|
"registered commitment doesn't match passed commitment"
|
|
|
|
);
|
2022-11-25 06:01:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should withdraw membership", async () => {
|
|
|
|
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
|
2022-11-25 06:08:38 +00:00
|
|
|
|
2022-11-25 06:01:06 +00:00
|
|
|
const price = await rln.MEMBERSHIP_DEPOSIT();
|
|
|
|
|
|
|
|
// A valid pair of (id_secret, id_commitment) generated in rust
|
2022-11-25 06:08:38 +00:00
|
|
|
const idSecret =
|
|
|
|
"0x2a09a9fd93c590c26b91effbb2499f07e8f7aa12e2b4940a3aed2411cb65e11c";
|
|
|
|
const idCommitment =
|
|
|
|
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
|
2022-11-25 06:01:06 +00:00
|
|
|
|
2022-11-25 06:08:38 +00:00
|
|
|
const registerTx = await rln["register(uint256)"](idCommitment, {
|
|
|
|
value: price,
|
|
|
|
});
|
2022-11-25 06:01:06 +00:00
|
|
|
const txRegisterReceipt = await registerTx.wait();
|
|
|
|
|
2022-11-25 06:08:38 +00:00
|
|
|
const treeIndex = txRegisterReceipt.events[0].args.index;
|
2022-11-25 06:01:06 +00:00
|
|
|
|
|
|
|
// We withdraw our id_commitment
|
2022-11-25 06:08:38 +00:00
|
|
|
const receiverAddress = "0x000000000000000000000000000000000000dead";
|
|
|
|
const withdrawTx = await rln.withdraw(idSecret, treeIndex, receiverAddress);
|
|
|
|
|
|
|
|
const txWithdrawReceipt = await withdrawTx.wait();
|
|
|
|
|
|
|
|
const withdrawalPk = txWithdrawReceipt.events[0].args.pubkey;
|
|
|
|
const withdrawalTreeIndex = txWithdrawReceipt.events[0].args.index;
|
|
|
|
|
|
|
|
// We ensure the registered id_commitment is the one we passed and that the index is the same
|
|
|
|
expect(
|
|
|
|
withdrawalPk.toHexString() === idCommitment,
|
|
|
|
"withdraw commitment doesn't match registered commitment"
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
withdrawalTreeIndex.toHexString() === treeIndex.toHexString(),
|
|
|
|
"withdraw index doesn't match registered index"
|
|
|
|
);
|
|
|
|
});
|
2022-11-25 06:37:06 +00:00
|
|
|
|
|
|
|
it("should not allow multiple registrations with same pubkey", async () => {
|
|
|
|
const rln = await ethers.getContract("RLN", ethers.provider.getSigner(0));
|
|
|
|
|
|
|
|
const price = await rln.MEMBERSHIP_DEPOSIT();
|
|
|
|
|
|
|
|
// A valid pair of (id_secret, id_commitment) generated in rust
|
|
|
|
const idCommitment =
|
|
|
|
"0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368";
|
|
|
|
|
|
|
|
const registerTx = await rln["register(uint256)"](idCommitment, {
|
|
|
|
value: price,
|
|
|
|
});
|
|
|
|
const txRegisterReceipt = await registerTx.wait();
|
|
|
|
const index1 = txRegisterReceipt.events[0].args.index;
|
|
|
|
|
|
|
|
// Send the same tx again
|
|
|
|
const registerTx2 = await rln["register(uint256)"](idCommitment, {
|
|
|
|
value: price,
|
|
|
|
});
|
|
|
|
const txRegisterReceipt2 = await registerTx2.wait();
|
|
|
|
const index2 = txRegisterReceipt2.events[0].args.index;
|
|
|
|
|
|
|
|
const pk1 = await rln.members(index1);
|
2022-11-25 06:40:37 +00:00
|
|
|
const pk2 = await rln.members(index2);
|
2022-11-25 06:37:06 +00:00
|
|
|
const samePk = pk1.toHexString() === pk2.toHexString();
|
2022-11-25 06:40:37 +00:00
|
|
|
if (samePk) {
|
2022-11-25 06:37:06 +00:00
|
|
|
assert(false, "same pubkey registered twice");
|
|
|
|
}
|
2022-11-25 06:40:37 +00:00
|
|
|
});
|
2022-11-25 06:37:06 +00:00
|
|
|
|
|
|
|
it("[interep] should register new memberships", () => {
|
|
|
|
// TODO
|
|
|
|
});
|
|
|
|
|
|
|
|
it("[interep] should withdraw membership", () => {
|
|
|
|
// TODO
|
|
|
|
});
|
2022-11-25 06:08:38 +00:00
|
|
|
});
|