@semaphore-protocol/identity 4.0.0-beta.8 → 4.0.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
@@ -84,6 +84,28 @@ const { privateKey, publicKey, commitment } = new Identity()
84
84
  const identity = new Identity("your-private-key")
85
85
  ```
86
86
 
87
+ \# **identity.export**(): _string_
88
+
89
+ ```typescript
90
+ import { Identity } from "@semaphore-protocol/identity"
91
+
92
+ const identity = new Identity()
93
+
94
+ const privateKey = identity.export()
95
+ ```
96
+
97
+ \# **identity.import**(privateKey: _string_): _Identity_
98
+
99
+ ```typescript
100
+ import { Identity } from "@semaphore-protocol/identity"
101
+
102
+ const identity = new Identity()
103
+
104
+ const privateKey = identity.export()
105
+
106
+ const identity2 = Identity.import(privateKey)
107
+ ```
108
+
87
109
  \# **identity.signMessage**(message: _BigNumberish_): _Signature\<string>_
88
110
 
89
111
  ```typescript
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 4.0.0-beta.8
3
+ * @version 4.0.0
4
4
  * @file A library to create Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -1709,15 +1709,13 @@ ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
1709
1709
  if (value > max || value < min) {
1710
1710
  const n = typeof min === 'bigint' ? 'n' : '';
1711
1711
  let range;
1712
- if (byteLength > 3) {
1712
+ {
1713
1713
  if (min === 0 || min === BigInt(0)) {
1714
1714
  range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
1715
1715
  } else {
1716
1716
  range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
1717
1717
  `${(byteLength + 1) * 8 - 1}${n}`;
1718
1718
  }
1719
- } else {
1720
- range = `>= ${min}${n} and <= ${max}${n}`;
1721
1719
  }
1722
1720
  throw new errors.ERR_OUT_OF_RANGE('value', range, value)
1723
1721
  }
@@ -1731,13 +1729,13 @@ ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
1731
1729
  function boundsError (value, length, type) {
1732
1730
  if (Math.floor(value) !== value) {
1733
1731
  validateNumber(value, type);
1734
- throw new errors.ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value)
1732
+ throw new errors.ERR_OUT_OF_RANGE('offset', 'an integer', value)
1735
1733
  }
1736
1734
  if (length < 0) {
1737
1735
  throw new errors.ERR_BUFFER_OUT_OF_BOUNDS()
1738
1736
  }
1739
- throw new errors.ERR_OUT_OF_RANGE(type || 'offset',
1740
- `>= ${type ? 1 : 0} and <= ${length}`,
1737
+ throw new errors.ERR_OUT_OF_RANGE('offset',
1738
+ `>= ${0} and <= ${length}`,
1741
1739
  value)
1742
1740
  }
1743
1741
  const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
@@ -1871,7 +1869,7 @@ getDefaultExportFromCjs(buffer);
1871
1869
 
1872
1870
  /**
1873
1871
  * @module @zk-kit/utils
1874
- * @version 0.8.1
1872
+ * @version 1.2.0
1875
1873
  * @file Essential zero-knowledge utility library for JavaScript developers.
1876
1874
  * @copyright Ethereum Foundation 2024
1877
1875
  * @license MIT
@@ -1885,6 +1883,7 @@ getDefaultExportFromCjs(buffer);
1885
1883
  * are useful for type checking and validation in the other libraries,
1886
1884
  * enhancing code robustness and reliability.
1887
1885
  */
1886
+ /** @internal */
1888
1887
  const supportedTypes = [
1889
1888
  "number",
1890
1889
  "string",
@@ -1968,7 +1967,7 @@ function isStringifiedBigInt(value) {
1968
1967
  BigInt(value);
1969
1968
  return true;
1970
1969
  }
1971
- catch (_a) {
1970
+ catch {
1972
1971
  return false;
1973
1972
  }
1974
1973
  }
@@ -2017,6 +2016,7 @@ function isBigNumberish(value) {
2017
2016
  /**
2018
2017
  * Returns true if the value type is the same as the type passed
2019
2018
  * as the second parameter, false otherwise.
2019
+ * @param value
2020
2020
  * @param type The expected type.
2021
2021
  */
2022
2022
  function isType(value, type) {
@@ -2060,7 +2060,7 @@ function isSupportedType(type) {
2060
2060
 
2061
2061
  /**
2062
2062
  * @module @zk-kit/utils
2063
- * @version 0.8.1
2063
+ * @version 1.2.0
2064
2064
  * @file Essential zero-knowledge utility library for JavaScript developers.
2065
2065
  * @copyright Ethereum Foundation 2024
2066
2066
  * @license MIT
@@ -2076,20 +2076,6 @@ function requireString(parameterValue, parameterName) {
2076
2076
  throw new TypeError(`Parameter '${parameterName}' is not a string, received type: ${typeof parameterValue}`);
2077
2077
  }
2078
2078
  }
2079
- /**
2080
- * @throws Throws a type error if the parameter value is not a hexadecimal string.
2081
- * If 'prefix' is 'true', the string must start with '0x' or '0X' followed by one or more
2082
- * hexadecimal digits (0-9, a-f, A-F), otherwise no prefix is expected. 'prefix' is optional and
2083
- * if its value it is not explicitly defined it will be set to 'true' by default.
2084
- * @param parameterValue The parameter value.
2085
- * @param parameterName The parameter name.
2086
- * @param prefix A boolean to include or not a '0x' or '0X' prefix.
2087
- */
2088
- function requireHexadecimal(parameterValue, parameterName, prefix = true) {
2089
- if (!isHexadecimal(parameterValue, prefix)) {
2090
- throw new TypeError(`Parameter '${parameterName}' is not a hexadecimal string`);
2091
- }
2092
- }
2093
2079
  /**
2094
2080
  * @throws Throws a type error if the parameter value type is not part of the list of types.
2095
2081
  * @param parameterValue The parameter value.
@@ -2111,34 +2097,40 @@ function requireTypes(parameterValue, parameterName, types) {
2111
2097
 
2112
2098
  /**
2113
2099
  * @module @zk-kit/utils
2114
- * @version 0.8.1
2100
+ * @version 1.2.0
2115
2101
  * @file Essential zero-knowledge utility library for JavaScript developers.
2116
2102
  * @copyright Ethereum Foundation 2024
2117
2103
  * @license MIT
2118
2104
  * @see [Github]{@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/utils}
2119
2105
  */
2120
2106
  /**
2121
- * Converts an hexadecimal string to a buffer. The hexadecimal string
2122
- * should not start with '0x' or '0X'. It keeps the bytes in the same order.
2123
- * @param value The hexadecimal string to convert.
2124
- * @returns The buffer representation of the hexadecimal string.
2107
+ * Converts bytes to a base64 string. It accepts 'Buffer' or 'Uint8Array'.
2108
+ * @param value The bytes to convert.
2109
+ * @returns The converted base64 string.
2125
2110
  */
2126
- function hexadecimalToBuffer(value) {
2127
- requireHexadecimal(value, "value", false);
2128
- if (value.length % 2 !== 0) {
2129
- value = `0${value}`;
2130
- }
2131
- return buffer.Buffer.from(value, "hex");
2111
+ function bufferToBase64(value) {
2112
+ requireTypes(value, "value", ["Buffer", "Uint8Array"]);
2113
+ return buffer.Buffer.from(value).toString("base64");
2132
2114
  }
2133
2115
  /**
2134
- * Converts a buffer to a hexadecimal string. It accepts 'Buffer' or 'Uint8Array'.
2135
- * The hexadecimal string will not start with '0x' or '0X'. It keeps the bytes in the same order.
2136
- * @param value The buffer to convert.
2137
- * @returns The converted hexadecimal string.
2116
+ * Converts a base64 string to bytes (i.e. a buffer). This function does not check
2117
+ * if the input value is a valid base64 string. If there are unsupported characters
2118
+ * they will be ignored.
2119
+ * @param value The base64 string to convert.
2120
+ * @returns The converted buffer.
2138
2121
  */
2139
- function bufferToHexadecimal(value) {
2140
- requireTypes(value, "value", ["Buffer", "Uint8Array"]);
2141
- return buffer.Buffer.from(value).toString("hex");
2122
+ function base64ToBuffer(value) {
2123
+ requireString(value, "value");
2124
+ return buffer.Buffer.from(value, "base64");
2125
+ }
2126
+ /**
2127
+ * Converts text (utf8) to a base64 string.
2128
+ * @param value The text to convert.
2129
+ * @returns The converted base64 string.
2130
+ */
2131
+ function textToBase64(value) {
2132
+ requireString(value, "value");
2133
+ return buffer.Buffer.from(value, "utf8").toString("base64");
2142
2134
  }
2143
2135
 
2144
2136
  var poseidon2$1 = {};
@@ -2245,14 +2237,12 @@ function poseidon2(inputs) {
2245
2237
  * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
2246
2238
  * In addition, the commitment, i.e. the hash of the public key, is used to represent
2247
2239
  * Semaphore identities in groups, adding an additional layer of privacy and security.
2248
- * The private key of the identity is stored as a hexadecimal string or text.
2249
- * The other attributes are stored as stringified bigint.
2240
+ * The private key of the identity can be exported as a base64 string.
2250
2241
  */
2251
2242
  class Identity {
2252
2243
  /**
2253
- * Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
2254
- * Hexadecimal strings must not start with '0x' or '0X'.
2255
- * If the private key is not passed as a parameter, a random hexadecimal key will be generated.
2244
+ * Initializes the class attributes based on a given private key, which must be text or a buffer.
2245
+ * If the private key is not passed as a parameter, a random private key will be generated.
2256
2246
  * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
2257
2247
  * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
2258
2248
  *
@@ -2266,28 +2256,15 @@ class Identity {
2266
2256
  * @param privateKey The private key used to derive the public key (hexadecimal or string).
2267
2257
  */
2268
2258
  constructor(privateKey) {
2269
- let eddsa;
2270
- if (privateKey) {
2271
- requireString(privateKey, "privateKey");
2272
- this._privateKey = privateKey;
2273
- if (isHexadecimal(privateKey, false)) {
2274
- eddsa = new eddsaPoseidon.EdDSAPoseidon(hexadecimalToBuffer(privateKey));
2275
- }
2276
- else {
2277
- eddsa = new eddsaPoseidon.EdDSAPoseidon(privateKey);
2278
- }
2279
- }
2280
- else {
2281
- eddsa = new eddsaPoseidon.EdDSAPoseidon();
2282
- this._privateKey = bufferToHexadecimal(eddsa.privateKey);
2283
- }
2259
+ const eddsa = new eddsaPoseidon.EdDSAPoseidon(privateKey);
2260
+ this._privateKey = eddsa.privateKey;
2284
2261
  this._secretScalar = eddsa.secretScalar;
2285
2262
  this._publicKey = eddsa.publicKey;
2286
2263
  this._commitment = poseidon2_2(this._publicKey);
2287
2264
  }
2288
2265
  /**
2289
2266
  * Returns the private key.
2290
- * @returns The private key as a string (hexadecimal or text).
2267
+ * @returns The private key as a buffer or text.
2291
2268
  */
2292
2269
  get privateKey() {
2293
2270
  return this._privateKey;
@@ -2313,6 +2290,25 @@ class Identity {
2313
2290
  get commitment() {
2314
2291
  return this._commitment;
2315
2292
  }
2293
+ /**
2294
+ * Returns the private key encoded as a base64 string.
2295
+ * @returns The private key as a base64 string.
2296
+ */
2297
+ export() {
2298
+ if (isString(this._privateKey)) {
2299
+ return textToBase64(this._privateKey);
2300
+ }
2301
+ return bufferToBase64(this.privateKey);
2302
+ }
2303
+ /**
2304
+ * Returns a Semaphore identity based on a private key encoded as a base64 string.
2305
+ * The private key will be converted to a buffer, regardless of its original type.
2306
+ * @param privateKey The private key as a base64 string.
2307
+ * @returns The Semaphore identity.
2308
+ */
2309
+ static import(privateKey) {
2310
+ return new Identity(base64ToBuffer(privateKey));
2311
+ }
2316
2312
  /**
2317
2313
  * Generates a signature for a given message using the private key.
2318
2314
  * This method demonstrates how to sign a message and could be used
@@ -2326,10 +2322,7 @@ class Identity {
2326
2322
  * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
2327
2323
  */
2328
2324
  signMessage(message) {
2329
- const privateKey = isHexadecimal(this.privateKey, false)
2330
- ? hexadecimalToBuffer(this.privateKey)
2331
- : this.privateKey;
2332
- return eddsaPoseidon.signMessage(privateKey, message);
2325
+ return eddsaPoseidon.signMessage(this.privateKey, message);
2333
2326
  }
2334
2327
  /**
2335
2328
  * Verifies a signature against a given message and public key.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 4.0.0-beta.8
3
+ * @version 4.0.0
4
4
  * @file A library to create Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -1707,15 +1707,13 @@ ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
1707
1707
  if (value > max || value < min) {
1708
1708
  const n = typeof min === 'bigint' ? 'n' : '';
1709
1709
  let range;
1710
- if (byteLength > 3) {
1710
+ {
1711
1711
  if (min === 0 || min === BigInt(0)) {
1712
1712
  range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
1713
1713
  } else {
1714
1714
  range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
1715
1715
  `${(byteLength + 1) * 8 - 1}${n}`;
1716
1716
  }
1717
- } else {
1718
- range = `>= ${min}${n} and <= ${max}${n}`;
1719
1717
  }
1720
1718
  throw new errors.ERR_OUT_OF_RANGE('value', range, value)
1721
1719
  }
@@ -1729,13 +1727,13 @@ ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
1729
1727
  function boundsError (value, length, type) {
1730
1728
  if (Math.floor(value) !== value) {
1731
1729
  validateNumber(value, type);
1732
- throw new errors.ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value)
1730
+ throw new errors.ERR_OUT_OF_RANGE('offset', 'an integer', value)
1733
1731
  }
1734
1732
  if (length < 0) {
1735
1733
  throw new errors.ERR_BUFFER_OUT_OF_BOUNDS()
1736
1734
  }
1737
- throw new errors.ERR_OUT_OF_RANGE(type || 'offset',
1738
- `>= ${type ? 1 : 0} and <= ${length}`,
1735
+ throw new errors.ERR_OUT_OF_RANGE('offset',
1736
+ `>= ${0} and <= ${length}`,
1739
1737
  value)
1740
1738
  }
1741
1739
  const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
@@ -1869,7 +1867,7 @@ getDefaultExportFromCjs(buffer);
1869
1867
 
1870
1868
  /**
1871
1869
  * @module @zk-kit/utils
1872
- * @version 0.8.1
1870
+ * @version 1.2.0
1873
1871
  * @file Essential zero-knowledge utility library for JavaScript developers.
1874
1872
  * @copyright Ethereum Foundation 2024
1875
1873
  * @license MIT
@@ -1883,6 +1881,7 @@ getDefaultExportFromCjs(buffer);
1883
1881
  * are useful for type checking and validation in the other libraries,
1884
1882
  * enhancing code robustness and reliability.
1885
1883
  */
1884
+ /** @internal */
1886
1885
  const supportedTypes = [
1887
1886
  "number",
1888
1887
  "string",
@@ -1966,7 +1965,7 @@ function isStringifiedBigInt(value) {
1966
1965
  BigInt(value);
1967
1966
  return true;
1968
1967
  }
1969
- catch (_a) {
1968
+ catch {
1970
1969
  return false;
1971
1970
  }
1972
1971
  }
@@ -2015,6 +2014,7 @@ function isBigNumberish(value) {
2015
2014
  /**
2016
2015
  * Returns true if the value type is the same as the type passed
2017
2016
  * as the second parameter, false otherwise.
2017
+ * @param value
2018
2018
  * @param type The expected type.
2019
2019
  */
2020
2020
  function isType(value, type) {
@@ -2058,7 +2058,7 @@ function isSupportedType(type) {
2058
2058
 
2059
2059
  /**
2060
2060
  * @module @zk-kit/utils
2061
- * @version 0.8.1
2061
+ * @version 1.2.0
2062
2062
  * @file Essential zero-knowledge utility library for JavaScript developers.
2063
2063
  * @copyright Ethereum Foundation 2024
2064
2064
  * @license MIT
@@ -2074,20 +2074,6 @@ function requireString(parameterValue, parameterName) {
2074
2074
  throw new TypeError(`Parameter '${parameterName}' is not a string, received type: ${typeof parameterValue}`);
2075
2075
  }
2076
2076
  }
2077
- /**
2078
- * @throws Throws a type error if the parameter value is not a hexadecimal string.
2079
- * If 'prefix' is 'true', the string must start with '0x' or '0X' followed by one or more
2080
- * hexadecimal digits (0-9, a-f, A-F), otherwise no prefix is expected. 'prefix' is optional and
2081
- * if its value it is not explicitly defined it will be set to 'true' by default.
2082
- * @param parameterValue The parameter value.
2083
- * @param parameterName The parameter name.
2084
- * @param prefix A boolean to include or not a '0x' or '0X' prefix.
2085
- */
2086
- function requireHexadecimal(parameterValue, parameterName, prefix = true) {
2087
- if (!isHexadecimal(parameterValue, prefix)) {
2088
- throw new TypeError(`Parameter '${parameterName}' is not a hexadecimal string`);
2089
- }
2090
- }
2091
2077
  /**
2092
2078
  * @throws Throws a type error if the parameter value type is not part of the list of types.
2093
2079
  * @param parameterValue The parameter value.
@@ -2109,34 +2095,40 @@ function requireTypes(parameterValue, parameterName, types) {
2109
2095
 
2110
2096
  /**
2111
2097
  * @module @zk-kit/utils
2112
- * @version 0.8.1
2098
+ * @version 1.2.0
2113
2099
  * @file Essential zero-knowledge utility library for JavaScript developers.
2114
2100
  * @copyright Ethereum Foundation 2024
2115
2101
  * @license MIT
2116
2102
  * @see [Github]{@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/utils}
2117
2103
  */
2118
2104
  /**
2119
- * Converts an hexadecimal string to a buffer. The hexadecimal string
2120
- * should not start with '0x' or '0X'. It keeps the bytes in the same order.
2121
- * @param value The hexadecimal string to convert.
2122
- * @returns The buffer representation of the hexadecimal string.
2105
+ * Converts bytes to a base64 string. It accepts 'Buffer' or 'Uint8Array'.
2106
+ * @param value The bytes to convert.
2107
+ * @returns The converted base64 string.
2123
2108
  */
2124
- function hexadecimalToBuffer(value) {
2125
- requireHexadecimal(value, "value", false);
2126
- if (value.length % 2 !== 0) {
2127
- value = `0${value}`;
2128
- }
2129
- return buffer.Buffer.from(value, "hex");
2109
+ function bufferToBase64(value) {
2110
+ requireTypes(value, "value", ["Buffer", "Uint8Array"]);
2111
+ return buffer.Buffer.from(value).toString("base64");
2130
2112
  }
2131
2113
  /**
2132
- * Converts a buffer to a hexadecimal string. It accepts 'Buffer' or 'Uint8Array'.
2133
- * The hexadecimal string will not start with '0x' or '0X'. It keeps the bytes in the same order.
2134
- * @param value The buffer to convert.
2135
- * @returns The converted hexadecimal string.
2114
+ * Converts a base64 string to bytes (i.e. a buffer). This function does not check
2115
+ * if the input value is a valid base64 string. If there are unsupported characters
2116
+ * they will be ignored.
2117
+ * @param value The base64 string to convert.
2118
+ * @returns The converted buffer.
2136
2119
  */
2137
- function bufferToHexadecimal(value) {
2138
- requireTypes(value, "value", ["Buffer", "Uint8Array"]);
2139
- return buffer.Buffer.from(value).toString("hex");
2120
+ function base64ToBuffer(value) {
2121
+ requireString(value, "value");
2122
+ return buffer.Buffer.from(value, "base64");
2123
+ }
2124
+ /**
2125
+ * Converts text (utf8) to a base64 string.
2126
+ * @param value The text to convert.
2127
+ * @returns The converted base64 string.
2128
+ */
2129
+ function textToBase64(value) {
2130
+ requireString(value, "value");
2131
+ return buffer.Buffer.from(value, "utf8").toString("base64");
2140
2132
  }
2141
2133
 
2142
2134
  var poseidon2$1 = {};
@@ -2243,14 +2235,12 @@ function poseidon2(inputs) {
2243
2235
  * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
2244
2236
  * In addition, the commitment, i.e. the hash of the public key, is used to represent
2245
2237
  * Semaphore identities in groups, adding an additional layer of privacy and security.
2246
- * The private key of the identity is stored as a hexadecimal string or text.
2247
- * The other attributes are stored as stringified bigint.
2238
+ * The private key of the identity can be exported as a base64 string.
2248
2239
  */
2249
2240
  class Identity {
2250
2241
  /**
2251
- * Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
2252
- * Hexadecimal strings must not start with '0x' or '0X'.
2253
- * If the private key is not passed as a parameter, a random hexadecimal key will be generated.
2242
+ * Initializes the class attributes based on a given private key, which must be text or a buffer.
2243
+ * If the private key is not passed as a parameter, a random private key will be generated.
2254
2244
  * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
2255
2245
  * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
2256
2246
  *
@@ -2264,28 +2254,15 @@ class Identity {
2264
2254
  * @param privateKey The private key used to derive the public key (hexadecimal or string).
2265
2255
  */
2266
2256
  constructor(privateKey) {
2267
- let eddsa;
2268
- if (privateKey) {
2269
- requireString(privateKey, "privateKey");
2270
- this._privateKey = privateKey;
2271
- if (isHexadecimal(privateKey, false)) {
2272
- eddsa = new EdDSAPoseidon(hexadecimalToBuffer(privateKey));
2273
- }
2274
- else {
2275
- eddsa = new EdDSAPoseidon(privateKey);
2276
- }
2277
- }
2278
- else {
2279
- eddsa = new EdDSAPoseidon();
2280
- this._privateKey = bufferToHexadecimal(eddsa.privateKey);
2281
- }
2257
+ const eddsa = new EdDSAPoseidon(privateKey);
2258
+ this._privateKey = eddsa.privateKey;
2282
2259
  this._secretScalar = eddsa.secretScalar;
2283
2260
  this._publicKey = eddsa.publicKey;
2284
2261
  this._commitment = poseidon2_2(this._publicKey);
2285
2262
  }
2286
2263
  /**
2287
2264
  * Returns the private key.
2288
- * @returns The private key as a string (hexadecimal or text).
2265
+ * @returns The private key as a buffer or text.
2289
2266
  */
2290
2267
  get privateKey() {
2291
2268
  return this._privateKey;
@@ -2311,6 +2288,25 @@ class Identity {
2311
2288
  get commitment() {
2312
2289
  return this._commitment;
2313
2290
  }
2291
+ /**
2292
+ * Returns the private key encoded as a base64 string.
2293
+ * @returns The private key as a base64 string.
2294
+ */
2295
+ export() {
2296
+ if (isString(this._privateKey)) {
2297
+ return textToBase64(this._privateKey);
2298
+ }
2299
+ return bufferToBase64(this.privateKey);
2300
+ }
2301
+ /**
2302
+ * Returns a Semaphore identity based on a private key encoded as a base64 string.
2303
+ * The private key will be converted to a buffer, regardless of its original type.
2304
+ * @param privateKey The private key as a base64 string.
2305
+ * @returns The Semaphore identity.
2306
+ */
2307
+ static import(privateKey) {
2308
+ return new Identity(base64ToBuffer(privateKey));
2309
+ }
2314
2310
  /**
2315
2311
  * Generates a signature for a given message using the private key.
2316
2312
  * This method demonstrates how to sign a message and could be used
@@ -2324,10 +2320,7 @@ class Identity {
2324
2320
  * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
2325
2321
  */
2326
2322
  signMessage(message) {
2327
- const privateKey = isHexadecimal(this.privateKey, false)
2328
- ? hexadecimalToBuffer(this.privateKey)
2329
- : this.privateKey;
2330
- return signMessage(privateKey, message);
2323
+ return signMessage(this.privateKey, message);
2331
2324
  }
2332
2325
  /**
2333
2326
  * Verifies a signature against a given message and public key.
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import type { Point } from "@zk-kit/baby-jubjub";
2
3
  import { Signature } from "@zk-kit/eddsa-poseidon";
3
4
  import type { BigNumberish } from "@zk-kit/utils";
@@ -8,8 +9,7 @@ import type { BigNumberish } from "@zk-kit/utils";
8
9
  * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
9
10
  * In addition, the commitment, i.e. the hash of the public key, is used to represent
10
11
  * 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.
12
+ * The private key of the identity can be exported as a base64 string.
13
13
  */
14
14
  export declare class Identity {
15
15
  private _privateKey;
@@ -17,9 +17,8 @@ export declare class Identity {
17
17
  private _publicKey;
18
18
  private _commitment;
19
19
  /**
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.
20
+ * Initializes the class attributes based on a given private key, which must be text or a buffer.
21
+ * If the private key is not passed as a parameter, a random private key will be generated.
23
22
  * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
24
23
  * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
25
24
  *
@@ -32,12 +31,12 @@ export declare class Identity {
32
31
  *
33
32
  * @param privateKey The private key used to derive the public key (hexadecimal or string).
34
33
  */
35
- constructor(privateKey?: string);
34
+ constructor(privateKey?: string | Buffer | Uint8Array);
36
35
  /**
37
36
  * Returns the private key.
38
- * @returns The private key as a string (hexadecimal or text).
37
+ * @returns The private key as a buffer or text.
39
38
  */
40
- get privateKey(): string;
39
+ get privateKey(): string | Buffer | Uint8Array;
41
40
  /**
42
41
  * Returns the secret scalar.
43
42
  * @returns The secret scalar as a string.
@@ -53,6 +52,18 @@ export declare class Identity {
53
52
  * @returns The commitment as a string.
54
53
  */
55
54
  get commitment(): bigint;
55
+ /**
56
+ * Returns the private key encoded as a base64 string.
57
+ * @returns The private key as a base64 string.
58
+ */
59
+ export(): string;
60
+ /**
61
+ * Returns a Semaphore identity based on a private key encoded as a base64 string.
62
+ * The private key will be converted to a buffer, regardless of its original type.
63
+ * @param privateKey The private key as a base64 string.
64
+ * @returns The Semaphore identity.
65
+ */
66
+ static import(privateKey: string): Identity;
56
67
  /**
57
68
  * Generates a signature for a given message using the private key.
58
69
  * This method demonstrates how to sign a message and could be used
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "4.0.0-beta.8",
3
+ "version": "4.0.0",
4
4
  "description": "A library to create Semaphore identities.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,9 +39,9 @@
39
39
  "rollup-plugin-cleanup": "^3.2.1"
40
40
  },
41
41
  "dependencies": {
42
- "@zk-kit/baby-jubjub": "0.3.0",
43
- "@zk-kit/eddsa-poseidon": "0.10.0",
44
- "@zk-kit/utils": "0.8.1",
42
+ "@zk-kit/baby-jubjub": "1.0.1",
43
+ "@zk-kit/eddsa-poseidon": "1.0.2",
44
+ "@zk-kit/utils": "1.2.0",
45
45
  "poseidon-lite": "0.2.0"
46
46
  }
47
47
  }
package/src/index.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import type { Point } from "@zk-kit/baby-jubjub"
2
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
+ import { base64ToBuffer, bufferToBase64, textToBase64 } from "@zk-kit/utils/conversions"
5
+ import { isString } from "@zk-kit/utils/type-checks"
7
6
  import { poseidon2 } from "poseidon-lite/poseidon2"
8
7
 
9
8
  /**
@@ -13,12 +12,11 @@ import { poseidon2 } from "poseidon-lite/poseidon2"
13
12
  * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
14
13
  * In addition, the commitment, i.e. the hash of the public key, is used to represent
15
14
  * 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.
15
+ * The private key of the identity can be exported as a base64 string.
18
16
  */
19
17
  export class Identity {
20
18
  // The EdDSA private key, passed as a parameter or generated randomly.
21
- private _privateKey: string
19
+ private _privateKey: string | Buffer | Uint8Array
22
20
  // The secret scalar derived from the private key.
23
21
  // It is used in circuits to derive the public key.
24
22
  private _secretScalar: bigint
@@ -28,9 +26,8 @@ export class Identity {
28
26
  private _commitment: bigint
29
27
 
30
28
  /**
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.
29
+ * Initializes the class attributes based on a given private key, which must be text or a buffer.
30
+ * If the private key is not passed as a parameter, a random private key will be generated.
34
31
  * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
35
32
  * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
36
33
  *
@@ -43,25 +40,10 @@ export class Identity {
43
40
  *
44
41
  * @param privateKey The private key used to derive the public key (hexadecimal or string).
45
42
  */
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
- }
43
+ constructor(privateKey?: string | Buffer | Uint8Array) {
44
+ const eddsa = new EdDSAPoseidon(privateKey)
64
45
 
46
+ this._privateKey = eddsa.privateKey
65
47
  this._secretScalar = eddsa.secretScalar
66
48
  this._publicKey = eddsa.publicKey
67
49
  this._commitment = poseidon2(this._publicKey)
@@ -69,9 +51,9 @@ export class Identity {
69
51
 
70
52
  /**
71
53
  * Returns the private key.
72
- * @returns The private key as a string (hexadecimal or text).
54
+ * @returns The private key as a buffer or text.
73
55
  */
74
- public get privateKey(): string {
56
+ public get privateKey(): string | Buffer | Uint8Array {
75
57
  return this._privateKey
76
58
  }
77
59
 
@@ -99,6 +81,28 @@ export class Identity {
99
81
  return this._commitment
100
82
  }
101
83
 
84
+ /**
85
+ * Returns the private key encoded as a base64 string.
86
+ * @returns The private key as a base64 string.
87
+ */
88
+ public export(): string {
89
+ if (isString(this._privateKey)) {
90
+ return textToBase64(this._privateKey as string)
91
+ }
92
+
93
+ return bufferToBase64(this.privateKey as Buffer | Uint8Array)
94
+ }
95
+
96
+ /**
97
+ * Returns a Semaphore identity based on a private key encoded as a base64 string.
98
+ * The private key will be converted to a buffer, regardless of its original type.
99
+ * @param privateKey The private key as a base64 string.
100
+ * @returns The Semaphore identity.
101
+ */
102
+ static import(privateKey: string): Identity {
103
+ return new Identity(base64ToBuffer(privateKey))
104
+ }
105
+
102
106
  /**
103
107
  * Generates a signature for a given message using the private key.
104
108
  * This method demonstrates how to sign a message and could be used
@@ -112,11 +116,7 @@ export class Identity {
112
116
  * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
113
117
  */
114
118
  public signMessage(message: BigNumberish): Signature<bigint> {
115
- const privateKey = isHexadecimal(this.privateKey, false)
116
- ? hexadecimalToBuffer(this.privateKey)
117
- : this.privateKey
118
-
119
- return signMessage(privateKey, message)
119
+ return signMessage(this.privateKey, message)
120
120
  }
121
121
 
122
122
  /**