Skip to content

Commit cf2355f

Browse files
committed
docs(group): add more documentation to code
The commit adds more documentation to the group package functions and updates the TypeDoc link. re #483
1 parent 2c144fc commit cf2355f

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

packages/group/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<a href="https://npmjs.org/package/@semaphore-protocol/group">
1919
<img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-protocol/group.svg?style=flat-square" />
2020
</a>
21-
<a href="https://js.semaphore.pse.dev/group">
21+
<a href="https://js.semaphore.pse.dev/modules/_semaphore_protocol_group">
2222
<img alt="Documentation typedoc" src="https://img.shields.io/badge/docs-typedoc-744C7C?style=flat-square">
2323
</a>
2424
<a href="https://eslint.org/">
@@ -70,6 +70,8 @@ yarn add @semaphore-protocol/group
7070

7171
## 📜 Usage
7272

73+
For more information on the functions provided by `@semaphore-protocol/group`, please refer to the [TypeDoc documentation](https://js.semaphore.pse.dev/modules/_semaphore_protocol_group).
74+
7375
\# **new Group**(members: _BigNumberish[]_ = []): _Group_
7476

7577
```typescript

packages/group/src/group.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,84 @@ import { LeanIMT } from "@zk-kit/imt"
22
import { poseidon2 } from "poseidon-lite/poseidon2"
33
import { BigNumberish, MerkleProof } from "./types"
44

5+
/**
6+
* The Semaphore group is a {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT}
7+
* (Lean Incremental Merkle Tree), i.e. an optimized version of the incremental binary Merkle tree
8+
* used by Semaphore V3. The new tree does not use zero hashes, and its depth is dynamic.
9+
* The members of a Semaphore group, or the leaves of a tree, are the identity commitments.
10+
* Thanks to the properties of Merkle trees, it can be efficiently demonstrated that a group
11+
* member belongs to the group.
12+
* This class supports operations such as member addition, update, removal and Merkle proof
13+
* generation and verification. Groups can also be exported or imported.
14+
*/
515
export default class Group {
6-
leanIMT: LeanIMT
16+
// The {@link https://zkkit.pse.dev/classes/_zk_kit_imt.LeanIMT.html | LeanIMT} instance.
17+
public leanIMT: LeanIMT
718

819
/**
9-
* Initializes the group with the group id and the tree depth.
10-
* @param members List of group members.
20+
* Creates a new instance of the Group. Optionally, a list of identity commitments can
21+
* be passed as a parameter. Adding members in chunks is more efficient than adding
22+
* them one by one with the `addMember` function.
23+
* @param members A list of identity commitments.
1124
*/
1225
constructor(members: BigNumberish[] = []) {
1326
this.leanIMT = new LeanIMT((a, b) => poseidon2([a, b]), members.map(BigInt))
1427
}
1528

1629
/**
1730
* Returns the root hash of the tree.
18-
* @returns Root hash.
31+
* @returns The root hash as a string.
1932
*/
20-
get root(): string {
33+
public get root(): string {
2134
return this.leanIMT.root ? this.leanIMT.root.toString() : "0"
2235
}
2336

2437
/**
2538
* Returns the depth of the tree.
26-
* @returns Tree depth.
39+
* @returns The tree depth as a number.
2740
*/
28-
get depth(): number {
41+
public get depth(): number {
2942
return this.leanIMT.depth
3043
}
3144

3245
/**
3346
* Returns the size of the tree (i.e. number of leaves).
34-
* @returns Tree depth.
47+
* @returns The tree size as a number.
3548
*/
36-
get size(): number {
49+
public get size(): number {
3750
return this.leanIMT.size
3851
}
3952

4053
/**
4154
* Returns the members (i.e. identity commitments) of the group.
42-
* @returns List of members.
55+
* @returns The list of members of the group.
4356
*/
44-
get members(): string[] {
57+
public get members(): string[] {
4558
return this.leanIMT.leaves.map(String)
4659
}
4760

4861
/**
4962
* Returns the index of a member. If the member does not exist it returns -1.
50-
* @param member Group member.
51-
* @returns Index of the member.
63+
* @param member A member of the group.
64+
* @returns The index of the member, or -1 if it does not exist.
5265
*/
53-
indexOf(member: BigNumberish): number {
66+
public indexOf(member: BigNumberish): number {
5467
return this.leanIMT.indexOf(BigInt(member))
5568
}
5669

5770
/**
5871
* Adds a new member to the group.
59-
* @param member New member.
72+
* @param member The new member to be added.
6073
*/
61-
addMember(member: BigNumberish) {
74+
public addMember(member: BigNumberish) {
6275
this.leanIMT.insert(BigInt(member))
6376
}
6477

6578
/**
6679
* Adds new members to the group.
6780
* @param members New members.
6881
*/
69-
addMembers(members: BigNumberish[]) {
82+
public addMembers(members: BigNumberish[]) {
7083
this.leanIMT.insertMany(members.map(BigInt))
7184
}
7285

@@ -75,24 +88,24 @@ export default class Group {
7588
* @param index Index of the member to be updated.
7689
* @param member New member value.
7790
*/
78-
updateMember(index: number, member: BigNumberish) {
91+
public updateMember(index: number, member: BigNumberish) {
7992
this.leanIMT.update(index, BigInt(member))
8093
}
8194

8295
/**
8396
* Removes a member from the group.
84-
* @param index Index of the member to be removed.
97+
* @param index The index of the member to be removed.
8598
*/
86-
removeMember(index: number) {
99+
public removeMember(index: number) {
87100
this.leanIMT.update(index, BigInt(0))
88101
}
89102

90103
/**
91-
* Creates a proof of membership.
92-
* @param index Index of the proof's member.
93-
* @returns Proof object.
104+
* Creates a proof of membership for a member of the group.
105+
* @param index The index of the member.
106+
* @returns The {@link MerkleProof} object.
94107
*/
95-
generateMerkleProof(_index: number): MerkleProof {
108+
public generateMerkleProof(_index: number): MerkleProof {
96109
const { index, leaf, root, siblings } = this.leanIMT.generateProof(_index)
97110

98111
return {
@@ -104,21 +117,21 @@ export default class Group {
104117
}
105118

106119
/**
107-
* It enables the conversion of the group into a JSON string that
120+
* Enables the conversion of the group into a JSON string that
108121
* can be re-used for future imports. This approach is beneficial for
109122
* large groups, as it avoids re-calculating the tree hashes.
110123
* @returns The stringified JSON of the group.
111124
*/
112-
export(): string {
125+
public export(): string {
113126
return this.leanIMT.export()
114127
}
115128

116129
/**
117-
* It imports an entire group by initializing the tree without calculating
130+
* Imports an entire group by initializing the tree without calculating
118131
* any hashes. Note that it is crucial to ensure the integrity of the
119132
* exported group.
120133
* @param nodes The stringified JSON of the group.
121-
* @returns The group instance.
134+
* @returns The {@link Group} instance.
122135
*/
123136
static import(exportedGroup: string): Group {
124137
const group = new Group()

0 commit comments

Comments
 (0)