Skip to content

Commit

Permalink
Merge pull request #732 from semaphore-protocol/fix/group-checks
Browse files Browse the repository at this point in the history
Add checks for removed members
  • Loading branch information
cedoor authored Mar 28, 2024
2 parents 4adc328 + 0014365 commit 2faac4e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
24 changes: 21 additions & 3 deletions packages/group/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export class Group {
* @param member The new member to be added.
*/
public addMember(member: BigNumber) {
if (member === 0n || member === "0") {
throw new Error("Failed to add member: value cannot be 0")
}

this.leanIMT.insert(BigInt(member))
}

Expand All @@ -80,6 +84,12 @@ export class Group {
* @param members New members.
*/
public addMembers(members: BigNumber[]) {
for (const member of members) {
if (member === 0n || member === "0") {
throw new Error("Failed to add member: value cannot be 0")
}
}

this.leanIMT.insertMany(members.map(BigInt))
}

Expand All @@ -89,6 +99,10 @@ export class Group {
* @param member New member value.
*/
public updateMember(index: number, member: BigNumber) {
if (this.members[index] === 0n) {
throw new Error("Failed to update member: it has been removed")
}

this.leanIMT.update(index, BigInt(member))
}

Expand All @@ -97,16 +111,20 @@ export class Group {
* @param index The index of the member to be removed.
*/
public removeMember(index: number) {
this.leanIMT.update(index, BigInt(0))
if (this.members[index] === 0n) {
throw new Error("Failed to remove member: it has already been removed")
}

this.leanIMT.update(index, 0n)
}

/**
* Creates a proof of membership for a member of the group.
* @param index The index of the member.
* @returns The {@link MerkleProof} object.
*/
public generateMerkleProof(_index: number): LeanIMTMerkleProof {
return this.leanIMT.generateProof(_index)
public generateMerkleProof(index: number): LeanIMTMerkleProof {
return this.leanIMT.generateProof(index)
}

/**
Expand Down
37 changes: 36 additions & 1 deletion packages/group/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe("Group", () => {

expect(group.size).toBe(1)
})

it("Should not add a member to a group if its value is 0", () => {
const group = new Group()

const fun = () => group.addMember(0n)

expect(fun).toThrow("Failed to add member: value cannot be 0")
})
})

describe("# addMembers", () => {
Expand All @@ -43,6 +51,14 @@ describe("Group", () => {

expect(group.size).toBe(2)
})

it("Should not add many members to a group if any value is 0", () => {
const group = new Group()

const fun = () => group.addMembers([1n, 0n])

expect(fun).toThrow("Failed to add member: value cannot be 0")
})
})

describe("# indexOf", () => {
Expand All @@ -66,6 +82,16 @@ describe("Group", () => {
expect(group.size).toBe(2)
expect(group.members[0]).toBe(1n)
})

it("Should not update a member in a group if it has previously been removed", () => {
const group = new Group()
group.addMembers([1n, 3n])
group.removeMember(0)

const fun = () => group.updateMember(0, 1n)

expect(fun).toThrow("Failed to update member: it has been removed")
})
})

describe("# removeMember", () => {
Expand All @@ -78,12 +104,21 @@ describe("Group", () => {
expect(group.size).toBe(2)
expect(group.members[0]).toBe(0n)
})

it("Should not remove a member from a group if it has already been removed", () => {
const group = new Group()
group.addMembers([1n, 3n])
group.removeMember(0)

const fun = () => group.removeMember(0)

expect(fun).toThrow("Failed to remove member: it has already been removed")
})
})

describe("# generateMerkleProof", () => {
it("Should generate a proof of membership", () => {
const group = new Group()

group.addMembers([1n, 3n])

const proof = group.generateMerkleProof(0)
Expand Down

0 comments on commit 2faac4e

Please sign in to comment.