@semaphore-protocol/identity 4.0.0-alpha.8 → 4.0.0-beta
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 +3 -14
- package/dist/index.browser.js +56 -12
- package/dist/index.node.cjs +56 -12
- package/dist/index.node.js +56 -12
- package/dist/types/identity.d.ts +55 -9
- package/package.json +1 -1
- package/src/identity.ts +60 -14
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<a href="https://npmjs.org/package/@semaphore-protocol/identity">
|
|
19
19
|
<img alt="Downloads" src="https://img.shields.io/npm/dm/@semaphore-protocol/identity.svg?style=flat-square" />
|
|
20
20
|
</a>
|
|
21
|
-
<a href="https://js.semaphore.pse.dev/
|
|
21
|
+
<a href="https://js.semaphore.pse.dev/modules/_semaphore_protocol_identity">
|
|
22
22
|
<img alt="Documentation typedoc" src="https://img.shields.io/badge/docs-typedoc-744C7C?style=flat-square">
|
|
23
23
|
</a>
|
|
24
24
|
<a href="https://eslint.org/">
|
|
@@ -70,6 +70,8 @@ yarn add @semaphore-protocol/identity
|
|
|
70
70
|
|
|
71
71
|
## 📜 Usage
|
|
72
72
|
|
|
73
|
+
For more information on the functions provided by `@semaphore-protocol/identity`, please refer to the [TypeDoc documentation](https://js.semaphore.pse.dev/modules/_semaphore_protocol_identity).
|
|
74
|
+
|
|
73
75
|
\# **new Identity**(privateKey?: _BigNumberish_): _Identity_
|
|
74
76
|
|
|
75
77
|
```typescript
|
|
@@ -93,19 +95,6 @@ const identity = new Identity()
|
|
|
93
95
|
const signature = identity.signMessage(message)
|
|
94
96
|
```
|
|
95
97
|
|
|
96
|
-
\# **identity.verifySignature**(message: _BigNumberish_, signature: _Signature_): _boolean_
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
import { Identity } from "@semaphore-protocol/identity"
|
|
100
|
-
|
|
101
|
-
const message = "message"
|
|
102
|
-
const identity = new Identity()
|
|
103
|
-
|
|
104
|
-
const signature = identity.signMessage(message)
|
|
105
|
-
|
|
106
|
-
identity.verifySignature(message, signature)
|
|
107
|
-
```
|
|
108
|
-
|
|
109
98
|
\# **Identity.verifySignature**(message: _BigNumberish_, signature: _Signature_, publicKey: _Point_): _boolean_
|
|
110
99
|
|
|
111
100
|
```typescript
|
package/dist/index.browser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version 4.0.0-
|
|
3
|
+
* @version 4.0.0-beta
|
|
4
4
|
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2024
|
|
6
6
|
* @license MIT
|
|
@@ -914,10 +914,29 @@ function randomNumber() {
|
|
|
914
914
|
return bytesToBigint(bytes);
|
|
915
915
|
}
|
|
916
916
|
|
|
917
|
+
/**
|
|
918
|
+
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
919
|
+
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
920
|
+
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
921
|
+
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
922
|
+
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
923
|
+
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
924
|
+
*/
|
|
917
925
|
var Identity = /** @class */ (function () {
|
|
918
926
|
/**
|
|
919
|
-
* Initializes the class attributes based on
|
|
920
|
-
*
|
|
927
|
+
* Initializes the class attributes based on a given private key.
|
|
928
|
+
* If the private key is not passed as a parameter, a random key is generated.
|
|
929
|
+
* The constructor calculates the secret scalar and public key from the private key,
|
|
930
|
+
* and computes a commitment of the public key using a hash function (Poseidon).
|
|
931
|
+
*
|
|
932
|
+
* @example
|
|
933
|
+
* // Generates an identity.
|
|
934
|
+
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
935
|
+
* @example
|
|
936
|
+
* // Generates an identity with a random private key.
|
|
937
|
+
* const { privateKey, publicKey, commitment } = new Identity()
|
|
938
|
+
*
|
|
939
|
+
* @param privateKey The private key used to derive the public key.
|
|
921
940
|
*/
|
|
922
941
|
function Identity(privateKey) {
|
|
923
942
|
if (privateKey === void 0) { privateKey = randomNumber().toString(); }
|
|
@@ -929,7 +948,7 @@ var Identity = /** @class */ (function () {
|
|
|
929
948
|
Object.defineProperty(Identity.prototype, "privateKey", {
|
|
930
949
|
/**
|
|
931
950
|
* Returns the private key.
|
|
932
|
-
* @returns The private key.
|
|
951
|
+
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
|
|
933
952
|
*/
|
|
934
953
|
get: function () {
|
|
935
954
|
return this._privateKey;
|
|
@@ -940,7 +959,7 @@ var Identity = /** @class */ (function () {
|
|
|
940
959
|
Object.defineProperty(Identity.prototype, "secretScalar", {
|
|
941
960
|
/**
|
|
942
961
|
* Returns the secret scalar.
|
|
943
|
-
* @returns The secret scalar.
|
|
962
|
+
* @returns The secret scalar as a string.
|
|
944
963
|
*/
|
|
945
964
|
get: function () {
|
|
946
965
|
return this._secretScalar;
|
|
@@ -950,8 +969,8 @@ var Identity = /** @class */ (function () {
|
|
|
950
969
|
});
|
|
951
970
|
Object.defineProperty(Identity.prototype, "publicKey", {
|
|
952
971
|
/**
|
|
953
|
-
* Returns the public key.
|
|
954
|
-
* @returns The public key.
|
|
972
|
+
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
973
|
+
* @returns The public key as a point.
|
|
955
974
|
*/
|
|
956
975
|
get: function () {
|
|
957
976
|
return this._publicKey;
|
|
@@ -961,8 +980,8 @@ var Identity = /** @class */ (function () {
|
|
|
961
980
|
});
|
|
962
981
|
Object.defineProperty(Identity.prototype, "commitment", {
|
|
963
982
|
/**
|
|
964
|
-
* Returns the
|
|
965
|
-
* @returns The
|
|
983
|
+
* Returns the commitment hash of the public key.
|
|
984
|
+
* @returns The commitment as a string.
|
|
966
985
|
*/
|
|
967
986
|
get: function () {
|
|
968
987
|
return this._commitment;
|
|
@@ -970,12 +989,37 @@ var Identity = /** @class */ (function () {
|
|
|
970
989
|
enumerable: false,
|
|
971
990
|
configurable: true
|
|
972
991
|
});
|
|
992
|
+
/**
|
|
993
|
+
* Generates a signature for a given message using the private key.
|
|
994
|
+
* This method demonstrates how to sign a message and could be used
|
|
995
|
+
* for authentication or data integrity.
|
|
996
|
+
*
|
|
997
|
+
* @example
|
|
998
|
+
* const identity = new Identity()
|
|
999
|
+
* const signature = identity.signMessage("message")
|
|
1000
|
+
*
|
|
1001
|
+
* @param message The message to be signed.
|
|
1002
|
+
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
1003
|
+
*/
|
|
973
1004
|
Identity.prototype.signMessage = function (message) {
|
|
974
1005
|
return signMessage(this.privateKey, message);
|
|
975
1006
|
};
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
1007
|
+
/**
|
|
1008
|
+
* Verifies a signature against a given message and public key.
|
|
1009
|
+
* This static method allows for the verification of signatures without needing
|
|
1010
|
+
* an instance of the Identity class. It's useful for cases where you only have
|
|
1011
|
+
* the public key, the message and a signature, and need to verify if they match.
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* const identity = new Identity()
|
|
1015
|
+
* const signature = identity.signMessage("message")
|
|
1016
|
+
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
1017
|
+
*
|
|
1018
|
+
* @param message The message that was signed.
|
|
1019
|
+
* @param signature The signature to verify.
|
|
1020
|
+
* @param publicKey The public key to use for verification.
|
|
1021
|
+
* @returns A boolean indicating whether the signature is valid.
|
|
1022
|
+
*/
|
|
979
1023
|
Identity.verifySignature = function (message, signature, publicKey) {
|
|
980
1024
|
return verifySignature(message, signature, publicKey);
|
|
981
1025
|
};
|
package/dist/index.node.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version 4.0.0-
|
|
3
|
+
* @version 4.0.0-beta
|
|
4
4
|
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2024
|
|
6
6
|
* @license MIT
|
|
@@ -118,10 +118,29 @@ function randomNumber() {
|
|
|
118
118
|
return BigInt(hex);
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
123
|
+
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
124
|
+
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
125
|
+
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
126
|
+
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
127
|
+
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
128
|
+
*/
|
|
121
129
|
var Identity = /** @class */ (function () {
|
|
122
130
|
/**
|
|
123
|
-
* Initializes the class attributes based on
|
|
124
|
-
*
|
|
131
|
+
* Initializes the class attributes based on a given private key.
|
|
132
|
+
* If the private key is not passed as a parameter, a random key is generated.
|
|
133
|
+
* The constructor calculates the secret scalar and public key from the private key,
|
|
134
|
+
* and computes a commitment of the public key using a hash function (Poseidon).
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* // Generates an identity.
|
|
138
|
+
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
139
|
+
* @example
|
|
140
|
+
* // Generates an identity with a random private key.
|
|
141
|
+
* const { privateKey, publicKey, commitment } = new Identity()
|
|
142
|
+
*
|
|
143
|
+
* @param privateKey The private key used to derive the public key.
|
|
125
144
|
*/
|
|
126
145
|
function Identity(privateKey) {
|
|
127
146
|
if (privateKey === void 0) { privateKey = randomNumber().toString(); }
|
|
@@ -133,7 +152,7 @@ var Identity = /** @class */ (function () {
|
|
|
133
152
|
Object.defineProperty(Identity.prototype, "privateKey", {
|
|
134
153
|
/**
|
|
135
154
|
* Returns the private key.
|
|
136
|
-
* @returns The private key.
|
|
155
|
+
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
|
|
137
156
|
*/
|
|
138
157
|
get: function () {
|
|
139
158
|
return this._privateKey;
|
|
@@ -144,7 +163,7 @@ var Identity = /** @class */ (function () {
|
|
|
144
163
|
Object.defineProperty(Identity.prototype, "secretScalar", {
|
|
145
164
|
/**
|
|
146
165
|
* Returns the secret scalar.
|
|
147
|
-
* @returns The secret scalar.
|
|
166
|
+
* @returns The secret scalar as a string.
|
|
148
167
|
*/
|
|
149
168
|
get: function () {
|
|
150
169
|
return this._secretScalar;
|
|
@@ -154,8 +173,8 @@ var Identity = /** @class */ (function () {
|
|
|
154
173
|
});
|
|
155
174
|
Object.defineProperty(Identity.prototype, "publicKey", {
|
|
156
175
|
/**
|
|
157
|
-
* Returns the public key.
|
|
158
|
-
* @returns The public key.
|
|
176
|
+
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
177
|
+
* @returns The public key as a point.
|
|
159
178
|
*/
|
|
160
179
|
get: function () {
|
|
161
180
|
return this._publicKey;
|
|
@@ -165,8 +184,8 @@ var Identity = /** @class */ (function () {
|
|
|
165
184
|
});
|
|
166
185
|
Object.defineProperty(Identity.prototype, "commitment", {
|
|
167
186
|
/**
|
|
168
|
-
* Returns the
|
|
169
|
-
* @returns The
|
|
187
|
+
* Returns the commitment hash of the public key.
|
|
188
|
+
* @returns The commitment as a string.
|
|
170
189
|
*/
|
|
171
190
|
get: function () {
|
|
172
191
|
return this._commitment;
|
|
@@ -174,12 +193,37 @@ var Identity = /** @class */ (function () {
|
|
|
174
193
|
enumerable: false,
|
|
175
194
|
configurable: true
|
|
176
195
|
});
|
|
196
|
+
/**
|
|
197
|
+
* Generates a signature for a given message using the private key.
|
|
198
|
+
* This method demonstrates how to sign a message and could be used
|
|
199
|
+
* for authentication or data integrity.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* const identity = new Identity()
|
|
203
|
+
* const signature = identity.signMessage("message")
|
|
204
|
+
*
|
|
205
|
+
* @param message The message to be signed.
|
|
206
|
+
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
207
|
+
*/
|
|
177
208
|
Identity.prototype.signMessage = function (message) {
|
|
178
209
|
return eddsaPoseidon.signMessage(this.privateKey, message);
|
|
179
210
|
};
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Verifies a signature against a given message and public key.
|
|
213
|
+
* This static method allows for the verification of signatures without needing
|
|
214
|
+
* an instance of the Identity class. It's useful for cases where you only have
|
|
215
|
+
* the public key, the message and a signature, and need to verify if they match.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* const identity = new Identity()
|
|
219
|
+
* const signature = identity.signMessage("message")
|
|
220
|
+
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
221
|
+
*
|
|
222
|
+
* @param message The message that was signed.
|
|
223
|
+
* @param signature The signature to verify.
|
|
224
|
+
* @param publicKey The public key to use for verification.
|
|
225
|
+
* @returns A boolean indicating whether the signature is valid.
|
|
226
|
+
*/
|
|
183
227
|
Identity.verifySignature = function (message, signature, publicKey) {
|
|
184
228
|
return eddsaPoseidon.verifySignature(message, signature, publicKey);
|
|
185
229
|
};
|
package/dist/index.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version 4.0.0-
|
|
3
|
+
* @version 4.0.0-beta
|
|
4
4
|
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2024
|
|
6
6
|
* @license MIT
|
|
@@ -116,10 +116,29 @@ function randomNumber() {
|
|
|
116
116
|
return BigInt(hex);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
/**
|
|
120
|
+
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
121
|
+
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
122
|
+
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
123
|
+
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
124
|
+
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
125
|
+
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
126
|
+
*/
|
|
119
127
|
var Identity = /** @class */ (function () {
|
|
120
128
|
/**
|
|
121
|
-
* Initializes the class attributes based on
|
|
122
|
-
*
|
|
129
|
+
* Initializes the class attributes based on a given private key.
|
|
130
|
+
* If the private key is not passed as a parameter, a random key is generated.
|
|
131
|
+
* The constructor calculates the secret scalar and public key from the private key,
|
|
132
|
+
* and computes a commitment of the public key using a hash function (Poseidon).
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* // Generates an identity.
|
|
136
|
+
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
137
|
+
* @example
|
|
138
|
+
* // Generates an identity with a random private key.
|
|
139
|
+
* const { privateKey, publicKey, commitment } = new Identity()
|
|
140
|
+
*
|
|
141
|
+
* @param privateKey The private key used to derive the public key.
|
|
123
142
|
*/
|
|
124
143
|
function Identity(privateKey) {
|
|
125
144
|
if (privateKey === void 0) { privateKey = randomNumber().toString(); }
|
|
@@ -131,7 +150,7 @@ var Identity = /** @class */ (function () {
|
|
|
131
150
|
Object.defineProperty(Identity.prototype, "privateKey", {
|
|
132
151
|
/**
|
|
133
152
|
* Returns the private key.
|
|
134
|
-
* @returns The private key.
|
|
153
|
+
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
|
|
135
154
|
*/
|
|
136
155
|
get: function () {
|
|
137
156
|
return this._privateKey;
|
|
@@ -142,7 +161,7 @@ var Identity = /** @class */ (function () {
|
|
|
142
161
|
Object.defineProperty(Identity.prototype, "secretScalar", {
|
|
143
162
|
/**
|
|
144
163
|
* Returns the secret scalar.
|
|
145
|
-
* @returns The secret scalar.
|
|
164
|
+
* @returns The secret scalar as a string.
|
|
146
165
|
*/
|
|
147
166
|
get: function () {
|
|
148
167
|
return this._secretScalar;
|
|
@@ -152,8 +171,8 @@ var Identity = /** @class */ (function () {
|
|
|
152
171
|
});
|
|
153
172
|
Object.defineProperty(Identity.prototype, "publicKey", {
|
|
154
173
|
/**
|
|
155
|
-
* Returns the public key.
|
|
156
|
-
* @returns The public key.
|
|
174
|
+
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
175
|
+
* @returns The public key as a point.
|
|
157
176
|
*/
|
|
158
177
|
get: function () {
|
|
159
178
|
return this._publicKey;
|
|
@@ -163,8 +182,8 @@ var Identity = /** @class */ (function () {
|
|
|
163
182
|
});
|
|
164
183
|
Object.defineProperty(Identity.prototype, "commitment", {
|
|
165
184
|
/**
|
|
166
|
-
* Returns the
|
|
167
|
-
* @returns The
|
|
185
|
+
* Returns the commitment hash of the public key.
|
|
186
|
+
* @returns The commitment as a string.
|
|
168
187
|
*/
|
|
169
188
|
get: function () {
|
|
170
189
|
return this._commitment;
|
|
@@ -172,12 +191,37 @@ var Identity = /** @class */ (function () {
|
|
|
172
191
|
enumerable: false,
|
|
173
192
|
configurable: true
|
|
174
193
|
});
|
|
194
|
+
/**
|
|
195
|
+
* Generates a signature for a given message using the private key.
|
|
196
|
+
* This method demonstrates how to sign a message and could be used
|
|
197
|
+
* for authentication or data integrity.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* const identity = new Identity()
|
|
201
|
+
* const signature = identity.signMessage("message")
|
|
202
|
+
*
|
|
203
|
+
* @param message The message to be signed.
|
|
204
|
+
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
205
|
+
*/
|
|
175
206
|
Identity.prototype.signMessage = function (message) {
|
|
176
207
|
return signMessage(this.privateKey, message);
|
|
177
208
|
};
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Verifies a signature against a given message and public key.
|
|
211
|
+
* This static method allows for the verification of signatures without needing
|
|
212
|
+
* an instance of the Identity class. It's useful for cases where you only have
|
|
213
|
+
* the public key, the message and a signature, and need to verify if they match.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* const identity = new Identity()
|
|
217
|
+
* const signature = identity.signMessage("message")
|
|
218
|
+
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
219
|
+
*
|
|
220
|
+
* @param message The message that was signed.
|
|
221
|
+
* @param signature The signature to verify.
|
|
222
|
+
* @param publicKey The public key to use for verification.
|
|
223
|
+
* @returns A boolean indicating whether the signature is valid.
|
|
224
|
+
*/
|
|
181
225
|
Identity.verifySignature = function (message, signature, publicKey) {
|
|
182
226
|
return verifySignature(message, signature, publicKey);
|
|
183
227
|
};
|
package/dist/types/identity.d.ts
CHANGED
|
@@ -1,35 +1,81 @@
|
|
|
1
1
|
import { BigNumberish, Point, Signature } from "@zk-kit/eddsa-poseidon";
|
|
2
|
+
/**
|
|
3
|
+
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
4
|
+
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
5
|
+
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
6
|
+
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
7
|
+
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
8
|
+
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
9
|
+
*/
|
|
2
10
|
export default class Identity {
|
|
3
11
|
private _privateKey;
|
|
4
12
|
private _secretScalar;
|
|
5
13
|
private _publicKey;
|
|
6
14
|
private _commitment;
|
|
7
15
|
/**
|
|
8
|
-
* Initializes the class attributes based on
|
|
9
|
-
*
|
|
16
|
+
* Initializes the class attributes based on a given private key.
|
|
17
|
+
* If the private key is not passed as a parameter, a random key is generated.
|
|
18
|
+
* The constructor calculates the secret scalar and public key from the private key,
|
|
19
|
+
* and computes a commitment of the public key using a hash function (Poseidon).
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Generates an identity.
|
|
23
|
+
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
24
|
+
* @example
|
|
25
|
+
* // Generates an identity with a random private key.
|
|
26
|
+
* const { privateKey, publicKey, commitment } = new Identity()
|
|
27
|
+
*
|
|
28
|
+
* @param privateKey The private key used to derive the public key.
|
|
10
29
|
*/
|
|
11
30
|
constructor(privateKey?: BigNumberish);
|
|
12
31
|
/**
|
|
13
32
|
* Returns the private key.
|
|
14
|
-
* @returns The private key.
|
|
33
|
+
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
|
|
15
34
|
*/
|
|
16
35
|
get privateKey(): BigNumberish;
|
|
17
36
|
/**
|
|
18
37
|
* Returns the secret scalar.
|
|
19
|
-
* @returns The secret scalar.
|
|
38
|
+
* @returns The secret scalar as a string.
|
|
20
39
|
*/
|
|
21
40
|
get secretScalar(): string;
|
|
22
41
|
/**
|
|
23
|
-
* Returns the public key.
|
|
24
|
-
* @returns The public key.
|
|
42
|
+
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
43
|
+
* @returns The public key as a point.
|
|
25
44
|
*/
|
|
26
45
|
get publicKey(): Point<string>;
|
|
27
46
|
/**
|
|
28
|
-
* Returns the
|
|
29
|
-
* @returns The
|
|
47
|
+
* Returns the commitment hash of the public key.
|
|
48
|
+
* @returns The commitment as a string.
|
|
30
49
|
*/
|
|
31
50
|
get commitment(): string;
|
|
51
|
+
/**
|
|
52
|
+
* Generates a signature for a given message using the private key.
|
|
53
|
+
* This method demonstrates how to sign a message and could be used
|
|
54
|
+
* for authentication or data integrity.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const identity = new Identity()
|
|
58
|
+
* const signature = identity.signMessage("message")
|
|
59
|
+
*
|
|
60
|
+
* @param message The message to be signed.
|
|
61
|
+
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
62
|
+
*/
|
|
32
63
|
signMessage(message: BigNumberish): Signature<string>;
|
|
33
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Verifies a signature against a given message and public key.
|
|
66
|
+
* This static method allows for the verification of signatures without needing
|
|
67
|
+
* an instance of the Identity class. It's useful for cases where you only have
|
|
68
|
+
* the public key, the message and a signature, and need to verify if they match.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const identity = new Identity()
|
|
72
|
+
* const signature = identity.signMessage("message")
|
|
73
|
+
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
74
|
+
*
|
|
75
|
+
* @param message The message that was signed.
|
|
76
|
+
* @param signature The signature to verify.
|
|
77
|
+
* @param publicKey The public key to use for verification.
|
|
78
|
+
* @returns A boolean indicating whether the signature is valid.
|
|
79
|
+
*/
|
|
34
80
|
static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean;
|
|
35
81
|
}
|
package/package.json
CHANGED
package/src/identity.ts
CHANGED
|
@@ -10,28 +10,50 @@ import {
|
|
|
10
10
|
import { poseidon2 } from "poseidon-lite/poseidon2"
|
|
11
11
|
import { randomNumber } from "./random-number.node"
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
15
|
+
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
16
|
+
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
17
|
+
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
18
|
+
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
19
|
+
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
20
|
+
*/
|
|
13
21
|
export default class Identity {
|
|
22
|
+
// The EdDSA private key, passed as a parameter or generated randomly.
|
|
14
23
|
private _privateKey: BigNumberish
|
|
24
|
+
// The secret scalar derived from the private key.
|
|
25
|
+
// It is used in circuits to derive the public key.
|
|
15
26
|
private _secretScalar: string
|
|
27
|
+
// The EdDSA public key, derived from the private key.
|
|
16
28
|
private _publicKey: Point<string>
|
|
29
|
+
// The identity commitment used as a public value in Semaphore groups.
|
|
17
30
|
private _commitment: string
|
|
18
31
|
|
|
19
32
|
/**
|
|
20
|
-
* Initializes the class attributes based on
|
|
21
|
-
*
|
|
33
|
+
* Initializes the class attributes based on a given private key.
|
|
34
|
+
* If the private key is not passed as a parameter, a random key is generated.
|
|
35
|
+
* The constructor calculates the secret scalar and public key from the private key,
|
|
36
|
+
* and computes a commitment of the public key using a hash function (Poseidon).
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // Generates an identity.
|
|
40
|
+
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
41
|
+
* @example
|
|
42
|
+
* // Generates an identity with a random private key.
|
|
43
|
+
* const { privateKey, publicKey, commitment } = new Identity()
|
|
44
|
+
*
|
|
45
|
+
* @param privateKey The private key used to derive the public key.
|
|
22
46
|
*/
|
|
23
47
|
constructor(privateKey: BigNumberish = randomNumber().toString()) {
|
|
24
48
|
this._privateKey = privateKey
|
|
25
49
|
this._secretScalar = deriveSecretScalar(privateKey)
|
|
26
|
-
|
|
27
50
|
this._publicKey = derivePublicKey(privateKey)
|
|
28
|
-
|
|
29
51
|
this._commitment = poseidon2(this._publicKey).toString()
|
|
30
52
|
}
|
|
31
53
|
|
|
32
54
|
/**
|
|
33
55
|
* Returns the private key.
|
|
34
|
-
* @returns The private key.
|
|
56
|
+
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
|
|
35
57
|
*/
|
|
36
58
|
public get privateKey(): BigNumberish {
|
|
37
59
|
return this._privateKey
|
|
@@ -39,36 +61,60 @@ export default class Identity {
|
|
|
39
61
|
|
|
40
62
|
/**
|
|
41
63
|
* Returns the secret scalar.
|
|
42
|
-
* @returns The secret scalar.
|
|
64
|
+
* @returns The secret scalar as a string.
|
|
43
65
|
*/
|
|
44
66
|
public get secretScalar(): string {
|
|
45
67
|
return this._secretScalar
|
|
46
68
|
}
|
|
47
69
|
|
|
48
70
|
/**
|
|
49
|
-
* Returns the public key.
|
|
50
|
-
* @returns The public key.
|
|
71
|
+
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
72
|
+
* @returns The public key as a point.
|
|
51
73
|
*/
|
|
52
74
|
public get publicKey(): Point<string> {
|
|
53
75
|
return this._publicKey
|
|
54
76
|
}
|
|
55
77
|
|
|
56
78
|
/**
|
|
57
|
-
* Returns the
|
|
58
|
-
* @returns The
|
|
79
|
+
* Returns the commitment hash of the public key.
|
|
80
|
+
* @returns The commitment as a string.
|
|
59
81
|
*/
|
|
60
82
|
public get commitment(): string {
|
|
61
83
|
return this._commitment
|
|
62
84
|
}
|
|
63
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Generates a signature for a given message using the private key.
|
|
88
|
+
* This method demonstrates how to sign a message and could be used
|
|
89
|
+
* for authentication or data integrity.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* const identity = new Identity()
|
|
93
|
+
* const signature = identity.signMessage("message")
|
|
94
|
+
*
|
|
95
|
+
* @param message The message to be signed.
|
|
96
|
+
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
97
|
+
*/
|
|
64
98
|
public signMessage(message: BigNumberish): Signature<string> {
|
|
65
99
|
return signMessage(this.privateKey, message)
|
|
66
100
|
}
|
|
67
101
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Verifies a signature against a given message and public key.
|
|
104
|
+
* This static method allows for the verification of signatures without needing
|
|
105
|
+
* an instance of the Identity class. It's useful for cases where you only have
|
|
106
|
+
* the public key, the message and a signature, and need to verify if they match.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* const identity = new Identity()
|
|
110
|
+
* const signature = identity.signMessage("message")
|
|
111
|
+
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
112
|
+
*
|
|
113
|
+
* @param message The message that was signed.
|
|
114
|
+
* @param signature The signature to verify.
|
|
115
|
+
* @param publicKey The public key to use for verification.
|
|
116
|
+
* @returns A boolean indicating whether the signature is valid.
|
|
117
|
+
*/
|
|
72
118
|
static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
|
|
73
119
|
return verifySignature(message, signature, publicKey)
|
|
74
120
|
}
|