Skip to content

Commit ed015cc

Browse files
committed
feat(group): add new class parameter to add members
re #319
1 parent 0cb0ef3 commit ed015cc

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4440,7 +4440,7 @@ __metadata:
44404440
"@ethersproject/keccak256": ^5.7.0
44414441
"@rollup/plugin-commonjs": ^24.0.1
44424442
"@rollup/plugin-node-resolve": ^15.0.1
4443-
"@zk-kit/incremental-merkle-tree": 1.0.0
4443+
"@zk-kit/incremental-merkle-tree": 1.1.0
44444444
poseidon-lite: ^0.2.0
44454445
rollup-plugin-cleanup: ^3.2.1
44464446
rollup-plugin-typescript2: ^0.31.2
@@ -5643,10 +5643,10 @@ __metadata:
56435643
languageName: node
56445644
linkType: hard
56455645

5646-
"@zk-kit/incremental-merkle-tree@npm:1.0.0":
5647-
version: 1.0.0
5648-
resolution: "@zk-kit/incremental-merkle-tree@npm:1.0.0"
5649-
checksum: 2b5d7b5cc08cf3aea536d6541a1177221fcacca5b93fb22e37cd05926d138fe69035b73cb42e09798505caf1b7a75ad225027d13d35c2f0d7b216147cafd184e
5646+
"@zk-kit/incremental-merkle-tree@npm:1.1.0":
5647+
version: 1.1.0
5648+
resolution: "@zk-kit/incremental-merkle-tree@npm:1.1.0"
5649+
checksum: 5f2d6dd2a4898aa75f72d5b3811ab965c369f0a51561250313849fb9a6a1163064c4887da3bea298d25e80a4bc79b3c6997edf6492a6a8fc157512bc3fcb5e23
56505650
languageName: node
56515651
linkType: hard
56525652

0 commit comments

Comments
 (0)