Skip to content

Commit

Permalink
Merge pull request #707 from semaphore-protocol/feat/create-group-con…
Browse files Browse the repository at this point in the history
…tracts

Add a new `createGroup` function in the contracts
  • Loading branch information
cedoor authored Mar 18, 2024
2 parents 3b167fb + 67a0cec commit b556939
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/contracts/contracts/Semaphore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
verifier = _verifier;
}

/// @dev See {SemaphoreGroups-_createGroup}.
function createGroup() external override returns (uint256 groupId) {
groupId = groupCounter++;
_createGroup(groupId, msg.sender);

groups[groupId].merkleTreeDuration = 1 hours;
}

/// @dev See {SemaphoreGroups-_createGroup}.
function createGroup(address admin) external override returns (uint256 groupId) {
groupId = groupCounter++;
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/contracts/interfaces/ISemaphore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ interface ISemaphore {
uint256[8] points
);

/// @dev See {SemaphoreGroups-_createGroup}.
function createGroup() external returns (uint256);

/// @dev See {SemaphoreGroups-_createGroup}.
function createGroup(address admin) external returns (uint256);

Expand Down
23 changes: 17 additions & 6 deletions packages/contracts/test/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ describe("Semaphore", () => {
.to.emit(semaphoreContract, "GroupAdminUpdated")
.withArgs(groupId, ZeroAddress, accountAddresses[0])
})

it("Should create a group without any parameters", async () => {
const groupId = 2

const transaction = await semaphoreContract["createGroup()"]()

await expect(transaction).to.emit(semaphoreContract, "GroupCreated").withArgs(groupId)
await expect(transaction)
.to.emit(semaphoreContract, "GroupAdminUpdated")
.withArgs(groupId, ZeroAddress, accountAddresses[0])
})
})

describe("# updateGroupMerkleTreeDuration", () => {
Expand Down Expand Up @@ -132,7 +143,7 @@ describe("Semaphore", () => {
})

it("Should add new members to an existing group", async () => {
const groupId = 2
const groupId = 3
const members = [BigInt(1), BigInt(2), BigInt(3)]
const group = new Group()

Expand Down Expand Up @@ -161,7 +172,7 @@ describe("Semaphore", () => {
})

it("Should update a member from an existing group", async () => {
const groupId = 3
const groupId = 4
const members = [BigInt(1), BigInt(2), BigInt(3)]
const group = new Group()

Expand Down Expand Up @@ -195,7 +206,7 @@ describe("Semaphore", () => {
})

it("Should remove a member from an existing group", async () => {
const groupId = 4
const groupId = 5
const members = [BigInt(1), BigInt(2), BigInt(3)]
const group = new Group()

Expand Down Expand Up @@ -229,7 +240,7 @@ describe("Semaphore", () => {
})

describe("# verifyProof", () => {
const groupId = 5
const groupId = 6
const message = 2
const identity = new Identity("0")

Expand Down Expand Up @@ -301,7 +312,7 @@ describe("Semaphore", () => {
})

it("Should not verify a proof if the group has no members", async () => {
const groupId = 6
const groupId = 7
await semaphoreContract["createGroup(address)"](accountAddresses[0])

const transaction = semaphoreContract.verifyProof(groupId, proof)
Expand All @@ -313,7 +324,7 @@ describe("Semaphore", () => {
describe("# validateProof", () => {
const message = 2
const identity = new Identity("0")
const groupOneMemberId = 6
const groupOneMemberId = 7

const group = new Group()
const groupOneMember = new Group()
Expand Down

0 comments on commit b556939

Please sign in to comment.