@semaphore-protocol/identity 4.0.0-beta.2 → 4.0.0-beta.3

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.
@@ -8,6 +8,8 @@ import type { BigNumberish } from "@zk-kit/utils";
8
8
  * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
9
9
  * In addition, the commitment, i.e. the hash of the public key, is used to represent
10
10
  * Semaphore identities in groups, adding an additional layer of privacy and security.
11
+ * The private key of the identity is stored as a hexadecimal string or text.
12
+ * The other attributes are stored as stringified bigint.
11
13
  */
12
14
  export default class Identity {
13
15
  private _privateKey;
@@ -15,10 +17,11 @@ export default class Identity {
15
17
  private _publicKey;
16
18
  private _commitment;
17
19
  /**
18
- * Initializes the class attributes based on a given private key.
19
- * If the private key is not passed as a parameter, a random key is generated.
20
- * The constructor calculates the secret scalar and public key from the private key,
21
- * and computes a commitment of the public key using a hash function (Poseidon).
20
+ * Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
21
+ * Hexadecimal strings must not start with '0x' or '0X'.
22
+ * If the private key is not passed as a parameter, a random hexadecimal key will be generated.
23
+ * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
24
+ * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
22
25
  *
23
26
  * @example
24
27
  * // Generates an identity.
@@ -27,29 +30,29 @@ export default class Identity {
27
30
  * // Generates an identity with a random private key.
28
31
  * const { privateKey, publicKey, commitment } = new Identity()
29
32
  *
30
- * @param privateKey The private key used to derive the public key.
33
+ * @param privateKey The private key used to derive the public key (hexadecimal or string).
31
34
  */
32
- constructor(privateKey?: BigNumberish);
35
+ constructor(privateKey?: string);
33
36
  /**
34
37
  * Returns the private key.
35
- * @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
38
+ * @returns The private key as a string (hexadecimal or text).
36
39
  */
37
- get privateKey(): BigNumberish;
40
+ get privateKey(): string;
38
41
  /**
39
42
  * Returns the secret scalar.
40
43
  * @returns The secret scalar as a string.
41
44
  */
42
- get secretScalar(): string;
45
+ get secretScalar(): bigint;
43
46
  /**
44
47
  * Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
45
48
  * @returns The public key as a point.
46
49
  */
47
- get publicKey(): Point<string>;
50
+ get publicKey(): Point<bigint>;
48
51
  /**
49
52
  * Returns the commitment hash of the public key.
50
53
  * @returns The commitment as a string.
51
54
  */
52
- get commitment(): string;
55
+ get commitment(): bigint;
53
56
  /**
54
57
  * Generates a signature for a given message using the private key.
55
58
  * This method demonstrates how to sign a message and could be used
@@ -62,7 +65,7 @@ export default class Identity {
62
65
  * @param message The message to be signed.
63
66
  * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
64
67
  */
65
- signMessage(message: BigNumberish): Signature<string>;
68
+ signMessage(message: BigNumberish): Signature<bigint>;
66
69
  /**
67
70
  * Verifies a signature against a given message and public key.
68
71
  * This static method allows for the verification of signatures without needing
package/package.json CHANGED
@@ -1,21 +1,15 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.3",
4
4
  "description": "A library to create Semaphore identities.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
- "main": "dist/index.browser.js",
7
+ "main": "dist/index.js",
8
8
  "types": "dist/types/index.d.ts",
9
9
  "exports": {
10
- ".": {
11
- "types": "./dist/types/index.d.ts",
12
- "node": {
13
- "require": "./dist/index.node.cjs",
14
- "default": "./dist/index.node.js"
15
- },
16
- "browser": "./dist/index.browser.js",
17
- "default": "./dist/index.browser.js"
18
- }
10
+ "types": "./dist/types/index.d.ts",
11
+ "require": "./dist/index.cjs",
12
+ "default": "./dist/index.js"
19
13
  },
20
14
  "files": [
21
15
  "dist/",
@@ -29,9 +23,7 @@
29
23
  "url": "https://github.com/semaphore-protocol/semaphore.git/issues"
30
24
  },
31
25
  "scripts": {
32
- "build": "rimraf dist && yarn build:browser && yarn build:node",
33
- "build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
34
- "build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
26
+ "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
35
27
  "prepublishOnly": "yarn build"
36
28
  },
37
29
  "publishConfig": {
@@ -41,13 +33,15 @@
41
33
  "@rollup/plugin-alias": "^5.1.0",
42
34
  "@rollup/plugin-commonjs": "^25.0.7",
43
35
  "@rollup/plugin-node-resolve": "^15.2.3",
44
- "rollup-plugin-cleanup": "^3.2.1",
45
- "rollup-plugin-typescript2": "^0.36.0"
36
+ "@rollup/plugin-typescript": "^11.1.6",
37
+ "rimraf": "^5.0.5",
38
+ "rollup": "^4.12.0",
39
+ "rollup-plugin-cleanup": "^3.2.1"
46
40
  },
47
41
  "dependencies": {
48
- "@zk-kit/baby-jubjub": "0.2.0",
49
- "@zk-kit/eddsa-poseidon": "0.6.0",
50
- "@zk-kit/utils": "0.3.0",
42
+ "@zk-kit/baby-jubjub": "0.3.0",
43
+ "@zk-kit/eddsa-poseidon": "0.10.0",
44
+ "@zk-kit/utils": "0.8.1",
51
45
  "poseidon-lite": "0.2.0"
52
46
  }
53
47
  }
package/src/identity.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import type { Point } from "@zk-kit/baby-jubjub"
2
- import { Signature, derivePublicKey, deriveSecretScalar, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"
2
+ import { EdDSAPoseidon, Signature, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"
3
3
  import type { BigNumberish } from "@zk-kit/utils"
4
+ import { bufferToHexadecimal, hexadecimalToBuffer } from "@zk-kit/utils/conversions"
5
+ import { requireString } from "@zk-kit/utils/error-handlers"
6
+ import { isHexadecimal } from "@zk-kit/utils/type-checks"
4
7
  import { poseidon2 } from "poseidon-lite/poseidon2"
5
- import { randomNumber } from "./random-number.node"
6
8
 
7
9
  /**
8
10
  * The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
@@ -11,23 +13,26 @@ import { randomNumber } from "./random-number.node"
11
13
  * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
12
14
  * In addition, the commitment, i.e. the hash of the public key, is used to represent
13
15
  * Semaphore identities in groups, adding an additional layer of privacy and security.
16
+ * The private key of the identity is stored as a hexadecimal string or text.
17
+ * The other attributes are stored as stringified bigint.
14
18
  */
15
19
  export default class Identity {
16
20
  // The EdDSA private key, passed as a parameter or generated randomly.
17
- private _privateKey: BigNumberish
21
+ private _privateKey: string
18
22
  // The secret scalar derived from the private key.
19
23
  // It is used in circuits to derive the public key.
20
- private _secretScalar: string
24
+ private _secretScalar: bigint
21
25
  // The EdDSA public key, derived from the private key.
22
- private _publicKey: Point<string>
26
+ private _publicKey: Point<bigint>
23
27
  // The identity commitment used as a public value in Semaphore groups.
24
- private _commitment: string
28
+ private _commitment: bigint
25
29
 
26
30
  /**
27
- * Initializes the class attributes based on a given private key.
28
- * If the private key is not passed as a parameter, a random key is generated.
29
- * The constructor calculates the secret scalar and public key from the private key,
30
- * and computes a commitment of the public key using a hash function (Poseidon).
31
+ * Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
32
+ * Hexadecimal strings must not start with '0x' or '0X'.
33
+ * If the private key is not passed as a parameter, a random hexadecimal key will be generated.
34
+ * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
35
+ * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
31
36
  *
32
37
  * @example
33
38
  * // Generates an identity.
@@ -36,20 +41,37 @@ export default class Identity {
36
41
  * // Generates an identity with a random private key.
37
42
  * const { privateKey, publicKey, commitment } = new Identity()
38
43
  *
39
- * @param privateKey The private key used to derive the public key.
44
+ * @param privateKey The private key used to derive the public key (hexadecimal or string).
40
45
  */
41
- constructor(privateKey: BigNumberish = randomNumber().toString()) {
42
- this._privateKey = privateKey
43
- this._secretScalar = deriveSecretScalar(privateKey)
44
- this._publicKey = derivePublicKey(privateKey)
45
- this._commitment = poseidon2(this._publicKey).toString()
46
+ constructor(privateKey?: string) {
47
+ let eddsa: EdDSAPoseidon
48
+
49
+ if (privateKey) {
50
+ requireString(privateKey, "privateKey")
51
+
52
+ this._privateKey = privateKey
53
+
54
+ if (isHexadecimal(privateKey, false)) {
55
+ eddsa = new EdDSAPoseidon(hexadecimalToBuffer(privateKey))
56
+ } else {
57
+ eddsa = new EdDSAPoseidon(privateKey)
58
+ }
59
+ } else {
60
+ eddsa = new EdDSAPoseidon()
61
+
62
+ this._privateKey = bufferToHexadecimal(eddsa.privateKey as any)
63
+ }
64
+
65
+ this._secretScalar = eddsa.secretScalar
66
+ this._publicKey = eddsa.publicKey
67
+ this._commitment = poseidon2(this._publicKey)
46
68
  }
47
69
 
48
70
  /**
49
71
  * Returns the private key.
50
- * @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
72
+ * @returns The private key as a string (hexadecimal or text).
51
73
  */
52
- public get privateKey(): BigNumberish {
74
+ public get privateKey(): string {
53
75
  return this._privateKey
54
76
  }
55
77
 
@@ -57,7 +79,7 @@ export default class Identity {
57
79
  * Returns the secret scalar.
58
80
  * @returns The secret scalar as a string.
59
81
  */
60
- public get secretScalar(): string {
82
+ public get secretScalar(): bigint {
61
83
  return this._secretScalar
62
84
  }
63
85
 
@@ -65,7 +87,7 @@ export default class Identity {
65
87
  * Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
66
88
  * @returns The public key as a point.
67
89
  */
68
- public get publicKey(): Point<string> {
90
+ public get publicKey(): Point<bigint> {
69
91
  return this._publicKey
70
92
  }
71
93
 
@@ -73,7 +95,7 @@ export default class Identity {
73
95
  * Returns the commitment hash of the public key.
74
96
  * @returns The commitment as a string.
75
97
  */
76
- public get commitment(): string {
98
+ public get commitment(): bigint {
77
99
  return this._commitment
78
100
  }
79
101
 
@@ -89,7 +111,7 @@ export default class Identity {
89
111
  * @param message The message to be signed.
90
112
  * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
91
113
  */
92
- public signMessage(message: BigNumberish): Signature<string> {
114
+ public signMessage(message: BigNumberish): Signature<bigint> {
93
115
  return signMessage(this.privateKey, message)
94
116
  }
95
117