@semaphore-protocol/group 3.0.0 → 3.2.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/README.md CHANGED
@@ -40,7 +40,7 @@
40
40
  🔎 Issues
41
41
  </a>
42
42
  <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
43
- <a href="https://discord.gg/6mSdGHnstH">
43
+ <a href="https://semaphore.appliedzkp.org/discord">
44
44
  🗣️ Chat &amp; Support
45
45
  </a>
46
46
  </h4>
package/dist/index.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/group
3
- * @version 3.0.0
3
+ * @version 3.2.0
4
4
  * @file A library to create and manage Semaphore groups.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/group}
8
8
  */
9
9
  import { IncrementalMerkleTree } from '@zk-kit/incremental-merkle-tree';
10
- import poseidon from 'poseidon-lite';
10
+ import { poseidon2 } from 'poseidon-lite/poseidon2';
11
11
  import { BigNumber } from '@ethersproject/bignumber';
12
12
  import { zeroPad } from '@ethersproject/bytes';
13
13
  import { keccak256 } from '@ethersproject/keccak256';
@@ -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(poseidon2, 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.2.0
4
4
  * @file A library to create and manage Semaphore groups.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -11,15 +11,11 @@
11
11
  Object.defineProperty(exports, '__esModule', { value: true });
12
12
 
13
13
  var incrementalMerkleTree = require('@zk-kit/incremental-merkle-tree');
14
- var poseidon = require('poseidon-lite');
14
+ var poseidon2 = require('poseidon-lite/poseidon2');
15
15
  var bignumber = require('@ethersproject/bignumber');
16
16
  var bytes = require('@ethersproject/bytes');
17
17
  var keccak256 = require('@ethersproject/keccak256');
18
18
 
19
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
20
-
21
- var poseidon__default = /*#__PURE__*/_interopDefaultLegacy(poseidon);
22
-
23
19
  /**
24
20
  * Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
25
21
  * @param message The message to be hashed.
@@ -34,16 +30,28 @@ function hash(message) {
34
30
  var Group = /** @class */ (function () {
35
31
  /**
36
32
  * Initializes the group with the group id and the tree depth.
37
- * @param groupId Group identifier.
33
+ * @param id Group identifier.
38
34
  * @param treeDepth Tree depth.
39
35
  */
40
- function Group(groupId, treeDepth) {
36
+ function Group(id, treeDepth) {
41
37
  if (treeDepth === void 0) { treeDepth = 20; }
42
38
  if (treeDepth < 16 || treeDepth > 32) {
43
39
  throw new Error("The tree depth must be between 16 and 32");
44
40
  }
45
- this.merkleTree = new incrementalMerkleTree.IncrementalMerkleTree(poseidon__default["default"], treeDepth, hash(groupId), 2);
41
+ this._id = id;
42
+ this.merkleTree = new incrementalMerkleTree.IncrementalMerkleTree(poseidon2.poseidon2, treeDepth, hash(id), 2);
46
43
  }
44
+ Object.defineProperty(Group.prototype, "id", {
45
+ /**
46
+ * Returns the id of the group.
47
+ * @returns Group id.
48
+ */
49
+ get: function () {
50
+ return this._id;
51
+ },
52
+ enumerable: false,
53
+ configurable: true
54
+ });
47
55
  Object.defineProperty(Group.prototype, "root", {
48
56
  /**
49
57
  * 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,44 +1,44 @@
1
1
  {
2
- "name": "@semaphore-protocol/group",
3
- "version": "3.0.0",
4
- "description": "A library to create and manage Semaphore groups.",
5
- "license": "MIT",
6
- "main": "dist/index.node.js",
7
- "exports": {
8
- "import": "./dist/index.mjs",
9
- "require": "./dist/index.node.js"
10
- },
11
- "types": "dist/types/index.d.ts",
12
- "files": [
13
- "dist/",
14
- "src/",
15
- "LICENSE",
16
- "README.md"
17
- ],
18
- "repository": "https://github.com/semaphore-protocol/semaphore",
19
- "homepage": "https://github.com/semaphore-protocol/semaphore/tree/main/packages/group",
20
- "bugs": {
21
- "url": "https://github.com/semaphore-protocol/semaphore.git/issues"
22
- },
23
- "scripts": {
24
- "build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
25
- "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
26
- "prepublishOnly": "yarn build",
27
- "docs": "typedoc src/index.ts --out ../../docs/group"
28
- },
29
- "publishConfig": {
30
- "access": "public"
31
- },
32
- "devDependencies": {
33
- "rollup-plugin-cleanup": "^3.2.1",
34
- "rollup-plugin-typescript2": "^0.31.2",
35
- "typedoc": "^0.22.11"
36
- },
37
- "dependencies": {
38
- "@ethersproject/bignumber": "^5.7.0",
39
- "@ethersproject/bytes": "^5.7.0",
40
- "@ethersproject/keccak256": "^5.7.0",
41
- "@zk-kit/incremental-merkle-tree": "1.0.0",
42
- "poseidon-lite": "^0.0.2"
43
- }
44
- }
2
+ "name": "@semaphore-protocol/group",
3
+ "version": "3.2.0",
4
+ "description": "A library to create and manage Semaphore groups.",
5
+ "license": "MIT",
6
+ "main": "dist/index.node.js",
7
+ "exports": {
8
+ "import": "./dist/index.mjs",
9
+ "require": "./dist/index.node.js"
10
+ },
11
+ "types": "dist/types/index.d.ts",
12
+ "files": [
13
+ "dist/",
14
+ "src/",
15
+ "LICENSE",
16
+ "README.md"
17
+ ],
18
+ "repository": "https://github.com/semaphore-protocol/semaphore",
19
+ "homepage": "https://github.com/semaphore-protocol/semaphore/tree/main/packages/group",
20
+ "bugs": {
21
+ "url": "https://github.com/semaphore-protocol/semaphore.git/issues"
22
+ },
23
+ "scripts": {
24
+ "build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
25
+ "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
26
+ "prepublishOnly": "yarn build",
27
+ "docs": "typedoc src/index.ts --out ../../docs/group"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "devDependencies": {
33
+ "rollup-plugin-cleanup": "^3.2.1",
34
+ "rollup-plugin-typescript2": "^0.31.2",
35
+ "typedoc": "^0.22.11"
36
+ },
37
+ "dependencies": {
38
+ "@ethersproject/bignumber": "^5.7.0",
39
+ "@ethersproject/bytes": "^5.7.0",
40
+ "@ethersproject/keccak256": "^5.7.0",
41
+ "@zk-kit/incremental-merkle-tree": "1.0.0",
42
+ "poseidon-lite": "^0.1.0"
43
+ }
44
+ }
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
- import poseidon from "poseidon-lite"
2
+ import { poseidon2 } from "poseidon-lite/poseidon2"
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(poseidon2, 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