@semaphore-protocol/group 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/group
3
- * @version 3.0.0
3
+ * @version 3.1.0
4
4
  * @file A library to create and manage Semaphore groups.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -26,16 +26,28 @@ function hash(message) {
26
26
  var Group = /** @class */ (function () {
27
27
  /**
28
28
  * Initializes the group with the group id and the tree depth.
29
- * @param groupId Group identifier.
29
+ * @param id Group identifier.
30
30
  * @param treeDepth Tree depth.
31
31
  */
32
- function Group(groupId, treeDepth) {
32
+ function Group(id, treeDepth) {
33
33
  if (treeDepth === void 0) { treeDepth = 20; }
34
34
  if (treeDepth < 16 || treeDepth > 32) {
35
35
  throw new Error("The tree depth must be between 16 and 32");
36
36
  }
37
- this.merkleTree = new IncrementalMerkleTree(poseidon, treeDepth, hash(groupId), 2);
37
+ this._id = id;
38
+ this.merkleTree = new IncrementalMerkleTree(poseidon, treeDepth, hash(id), 2);
38
39
  }
40
+ Object.defineProperty(Group.prototype, "id", {
41
+ /**
42
+ * Returns the id of the group.
43
+ * @returns Group id.
44
+ */
45
+ get: function () {
46
+ return this._id;
47
+ },
48
+ enumerable: false,
49
+ configurable: true
50
+ });
39
51
  Object.defineProperty(Group.prototype, "root", {
40
52
  /**
41
53
  * Returns the root hash of the tree.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/group
3
- * @version 3.0.0
3
+ * @version 3.1.0
4
4
  * @file A library to create and manage Semaphore groups.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -34,16 +34,28 @@ function hash(message) {
34
34
  var Group = /** @class */ (function () {
35
35
  /**
36
36
  * Initializes the group with the group id and the tree depth.
37
- * @param groupId Group identifier.
37
+ * @param id Group identifier.
38
38
  * @param treeDepth Tree depth.
39
39
  */
40
- function Group(groupId, treeDepth) {
40
+ function Group(id, treeDepth) {
41
41
  if (treeDepth === void 0) { treeDepth = 20; }
42
42
  if (treeDepth < 16 || treeDepth > 32) {
43
43
  throw new Error("The tree depth must be between 16 and 32");
44
44
  }
45
- this.merkleTree = new incrementalMerkleTree.IncrementalMerkleTree(poseidon__default["default"], treeDepth, hash(groupId), 2);
45
+ this._id = id;
46
+ this.merkleTree = new incrementalMerkleTree.IncrementalMerkleTree(poseidon__default["default"], treeDepth, hash(id), 2);
46
47
  }
48
+ Object.defineProperty(Group.prototype, "id", {
49
+ /**
50
+ * Returns the id of the group.
51
+ * @returns Group id.
52
+ */
53
+ get: function () {
54
+ return this._id;
55
+ },
56
+ enumerable: false,
57
+ configurable: true
58
+ });
47
59
  Object.defineProperty(Group.prototype, "root", {
48
60
  /**
49
61
  * Returns the root hash of the tree.
@@ -1,18 +1,24 @@
1
1
  import { IncrementalMerkleTree, MerkleProof } from "@zk-kit/incremental-merkle-tree";
2
- import { Member } from "./types";
2
+ import { BigNumberish } from "./types";
3
3
  export default class Group {
4
+ private _id;
4
5
  merkleTree: IncrementalMerkleTree;
5
6
  /**
6
7
  * Initializes the group with the group id and the tree depth.
7
- * @param groupId Group identifier.
8
+ * @param id Group identifier.
8
9
  * @param treeDepth Tree depth.
9
10
  */
10
- constructor(groupId: Member, treeDepth?: number);
11
+ constructor(id: BigNumberish, treeDepth?: number);
12
+ /**
13
+ * Returns the id of the group.
14
+ * @returns Group id.
15
+ */
16
+ get id(): BigNumberish;
11
17
  /**
12
18
  * Returns the root hash of the tree.
13
19
  * @returns Root hash.
14
20
  */
15
- get root(): Member;
21
+ get root(): BigNumberish;
16
22
  /**
17
23
  * Returns the depth of the tree.
18
24
  * @returns Tree depth.
@@ -22,34 +28,34 @@ export default class Group {
22
28
  * Returns the zero value of the tree.
23
29
  * @returns Tree zero value.
24
30
  */
25
- get zeroValue(): Member;
31
+ get zeroValue(): BigNumberish;
26
32
  /**
27
33
  * Returns the members (i.e. identity commitments) of the group.
28
34
  * @returns List of members.
29
35
  */
30
- get members(): Member[];
36
+ get members(): BigNumberish[];
31
37
  /**
32
38
  * Returns the index of a member. If the member does not exist it returns -1.
33
39
  * @param member Group member.
34
40
  * @returns Index of the member.
35
41
  */
36
- indexOf(member: Member): number;
42
+ indexOf(member: BigNumberish): number;
37
43
  /**
38
44
  * Adds a new member to the group.
39
45
  * @param member New member.
40
46
  */
41
- addMember(member: Member): void;
47
+ addMember(member: BigNumberish): void;
42
48
  /**
43
49
  * Adds new members to the group.
44
50
  * @param members New members.
45
51
  */
46
- addMembers(members: Member[]): void;
52
+ addMembers(members: BigNumberish[]): void;
47
53
  /**
48
54
  * Updates a member in the group.
49
55
  * @param index Index of the member to be updated.
50
56
  * @param member New member value.
51
57
  */
52
- updateMember(index: number, member: Member): void;
58
+ updateMember(index: number, member: BigNumberish): void;
53
59
  /**
54
60
  * Removes a member from the group.
55
61
  * @param index Index of the member to be removed.
@@ -1 +1 @@
1
- export type Member = string | number | bigint;
1
+ export type BigNumberish = string | number | bigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/group",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A library to create and manage Semaphore groups.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
package/src/group.test.ts CHANGED
@@ -6,6 +6,7 @@ describe("Group", () => {
6
6
  it("Should create a group", () => {
7
7
  const group = new Group(1)
8
8
 
9
+ expect(group.id).toBe(1)
9
10
  expect(group.root.toString()).toContain("103543")
10
11
  expect(group.depth).toBe(20)
11
12
  expect(group.zeroValue).toBe(hash(1))
package/src/group.ts CHANGED
@@ -1,29 +1,40 @@
1
1
  import { IncrementalMerkleTree, MerkleProof } from "@zk-kit/incremental-merkle-tree"
2
2
  import poseidon from "poseidon-lite"
3
3
  import hash from "./hash"
4
- import { Member } from "./types"
4
+ import { BigNumberish } from "./types"
5
5
 
6
6
  export default class Group {
7
+ private _id: BigNumberish
8
+
7
9
  merkleTree: IncrementalMerkleTree
8
10
 
9
11
  /**
10
12
  * Initializes the group with the group id and the tree depth.
11
- * @param groupId Group identifier.
13
+ * @param id Group identifier.
12
14
  * @param treeDepth Tree depth.
13
15
  */
14
- constructor(groupId: Member, treeDepth = 20) {
16
+ constructor(id: BigNumberish, treeDepth = 20) {
15
17
  if (treeDepth < 16 || treeDepth > 32) {
16
18
  throw new Error("The tree depth must be between 16 and 32")
17
19
  }
18
20
 
19
- this.merkleTree = new IncrementalMerkleTree(poseidon, treeDepth, hash(groupId), 2)
21
+ this._id = id
22
+ this.merkleTree = new IncrementalMerkleTree(poseidon, treeDepth, hash(id), 2)
23
+ }
24
+
25
+ /**
26
+ * Returns the id of the group.
27
+ * @returns Group id.
28
+ */
29
+ get id(): BigNumberish {
30
+ return this._id
20
31
  }
21
32
 
22
33
  /**
23
34
  * Returns the root hash of the tree.
24
35
  * @returns Root hash.
25
36
  */
26
- get root(): Member {
37
+ get root(): BigNumberish {
27
38
  return this.merkleTree.root
28
39
  }
29
40
 
@@ -39,7 +50,7 @@ export default class Group {
39
50
  * Returns the zero value of the tree.
40
51
  * @returns Tree zero value.
41
52
  */
42
- get zeroValue(): Member {
53
+ get zeroValue(): BigNumberish {
43
54
  return this.merkleTree.zeroes[0]
44
55
  }
45
56
 
@@ -47,7 +58,7 @@ export default class Group {
47
58
  * Returns the members (i.e. identity commitments) of the group.
48
59
  * @returns List of members.
49
60
  */
50
- get members(): Member[] {
61
+ get members(): BigNumberish[] {
51
62
  return this.merkleTree.leaves
52
63
  }
53
64
 
@@ -56,7 +67,7 @@ export default class Group {
56
67
  * @param member Group member.
57
68
  * @returns Index of the member.
58
69
  */
59
- indexOf(member: Member): number {
70
+ indexOf(member: BigNumberish): number {
60
71
  return this.merkleTree.indexOf(member)
61
72
  }
62
73
 
@@ -64,7 +75,7 @@ export default class Group {
64
75
  * Adds a new member to the group.
65
76
  * @param member New member.
66
77
  */
67
- addMember(member: Member) {
78
+ addMember(member: BigNumberish) {
68
79
  this.merkleTree.insert(BigInt(member))
69
80
  }
70
81
 
@@ -72,7 +83,7 @@ export default class Group {
72
83
  * Adds new members to the group.
73
84
  * @param members New members.
74
85
  */
75
- addMembers(members: Member[]) {
86
+ addMembers(members: BigNumberish[]) {
76
87
  for (const member of members) {
77
88
  this.addMember(member)
78
89
  }
@@ -83,7 +94,7 @@ export default class Group {
83
94
  * @param index Index of the member to be updated.
84
95
  * @param member New member value.
85
96
  */
86
- updateMember(index: number, member: Member) {
97
+ updateMember(index: number, member: BigNumberish) {
87
98
  this.merkleTree.update(index, member)
88
99
  }
89
100
 
@@ -1 +1 @@
1
- export type Member = string | number | bigint
1
+ export type BigNumberish = string | number | bigint;