Skip to content

Commit 6580b66

Browse files
authored
Merge pull request #322 from semaphore-protocol/feat/init-members
New class parameter to add members Former-commit-id: 5915ea0
2 parents 594c612 + 42d33e2 commit 6580b66

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

packages/group/README.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ yarn add @semaphore-protocol/group
7474

7575
```typescript
7676
import { Group } from "@semaphore-protocol/group"
77+
import { Identity } from "@semaphore-protocol/identity"
7778

7879
// Group with max 1048576 members (20^²).
7980
const group1 = new Group(1)
@@ -83,6 +84,13 @@ const group2 = new Group(1, 16)
8384

8485
// Group with max 16777216 members (24^²).
8586
const group3 = new Group(1, 24)
87+
88+
// Group with a list of predefined members.
89+
const identity1 = new Identity()
90+
const identity2 = new Identity()
91+
const identity3 = new Identity()
92+
93+
const group3 = new Group(1, 16, [identity1.commitment, identity2.commitment, identity3.commitment])
8694
```
8795

8896
\# **addMember**(identityCommitment: _Member_)
@@ -96,21 +104,6 @@ const commitment = identity.generateCommitment()
96104
group.addMember(commitment)
97105
```
98106

99-
\# **addMembers**(identityCommitments: _Member\[]_)
100-
101-
```typescript
102-
let identityCommitments: bigint[]
103-
104-
for (let i = 0; i < 10; i++) {
105-
const identity = new Identity()
106-
const commitment = identity.generateCommitment()
107-
108-
identityCommitments.push(commitment)
109-
}
110-
111-
group.addMember(identityCommitments)
112-
```
113-
114107
\# **removeMember**(index: _number_)
115108

116109
```typescript

packages/group/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@
4141
"@ethersproject/bignumber": "^5.7.0",
4242
"@ethersproject/bytes": "^5.7.0",
4343
"@ethersproject/keccak256": "^5.7.0",
44-
"@zk-kit/incremental-merkle-tree": "1.0.0"
44+
"@zk-kit/incremental-merkle-tree": "1.1.0"
4545
}
4646
}

packages/group/src/group.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,29 @@ describe("Group", () => {
1919
expect(fun).toThrow("The tree depth must be between 16 and 32")
2020
})
2121

22-
it("Should create a group with different parameters", () => {
22+
it("Should create a group with a different tree depth", () => {
2323
const group = new Group(1, 32)
2424

2525
expect(group.root.toString()).toContain("460373")
2626
expect(group.depth).toBe(32)
2727
expect(group.zeroValue).toBe(hash(1))
2828
expect(group.members).toHaveLength(0)
2929
})
30+
31+
it("Should create a group with a list of members", () => {
32+
const group = new Group(2, 20, [1, 2, 3])
33+
34+
const group2 = new Group(2, 20)
35+
36+
group2.addMember(1)
37+
group2.addMember(2)
38+
group2.addMember(3)
39+
40+
expect(group.root.toString()).toContain(group2.root.toString())
41+
expect(group.depth).toBe(20)
42+
expect(group.zeroValue).toBe(hash(2))
43+
expect(group.members).toHaveLength(3)
44+
})
3045
})
3146

3247
describe("# addMember", () => {

packages/group/src/group.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ export default class Group {
1212
* Initializes the group with the group id and the tree depth.
1313
* @param id Group identifier.
1414
* @param treeDepth Tree depth.
15+
* @param members List of group members.
1516
*/
16-
constructor(id: BigNumberish, treeDepth = 20) {
17+
constructor(id: BigNumberish, treeDepth = 20, members: BigNumberish[] = []) {
1718
if (treeDepth < 16 || treeDepth > 32) {
1819
throw new Error("The tree depth must be between 16 and 32")
1920
}
2021

2122
this._id = id
22-
this.merkleTree = new IncrementalMerkleTree(poseidon2, treeDepth, hash(id), 2)
23+
this.merkleTree = new IncrementalMerkleTree(poseidon2, treeDepth, hash(id), 2, members)
2324
}
2425

2526
/**
@@ -82,6 +83,7 @@ export default class Group {
8283
/**
8384
* Adds new members to the group.
8485
* @param members New members.
86+
* @deprecated Use the new class parameter to add a list of members.
8587
*/
8688
addMembers(members: BigNumberish[]) {
8789
for (const member of members) {

yarn.lock.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
41a594b655244878cf24a7d982a9f8c3bc0988c2
1+
4cd9ed331cad58cdd4d33c9535028b24d411bd56

0 commit comments

Comments
 (0)