@semaphore-protocol/utils 4.0.0-beta.1 → 4.0.0-beta.10

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.
Files changed (35) hide show
  1. package/README.md +4 -4
  2. package/dist/index.cjs +171 -227
  3. package/dist/index.js +164 -226
  4. package/dist/lib.commonjs/constants.cjs +17 -0
  5. package/dist/lib.commonjs/decode-message.cjs +28 -0
  6. package/dist/lib.commonjs/index.cjs +14 -5
  7. package/dist/lib.commonjs/networks/deployed-contracts.json.cjs +94 -0
  8. package/dist/lib.commonjs/networks/index.cjs +66 -0
  9. package/dist/lib.commonjs/networks/supported-networks.cjs +38 -0
  10. package/dist/lib.esm/constants.js +14 -0
  11. package/dist/lib.esm/decode-message.js +26 -0
  12. package/dist/lib.esm/index.js +7 -5
  13. package/dist/lib.esm/networks/deployed-contracts.json.js +92 -0
  14. package/dist/lib.esm/networks/index.js +59 -0
  15. package/dist/lib.esm/networks/supported-networks.js +36 -0
  16. package/dist/types/constants.d.ts +2 -0
  17. package/dist/types/decode-message.d.ts +12 -0
  18. package/dist/types/index.d.ts +4 -3
  19. package/dist/types/networks/index.d.ts +34 -0
  20. package/dist/types/networks/supported-networks.d.ts +27 -0
  21. package/package.json +21 -12
  22. package/src/constants.ts +5 -0
  23. package/src/decode-message.ts +17 -0
  24. package/src/index.ts +4 -3
  25. package/src/networks/deployed-contracts.json +82 -0
  26. package/src/networks/index.ts +66 -0
  27. package/src/networks/supported-networks.ts +26 -0
  28. package/dist/lib.commonjs/errors.cjs +0 -131
  29. package/dist/lib.commonjs/types.cjs +0 -130
  30. package/dist/lib.esm/errors.js +0 -121
  31. package/dist/lib.esm/types.js +0 -119
  32. package/dist/types/errors.d.ts +0 -64
  33. package/dist/types/types.d.ts +0 -73
  34. package/src/errors.ts +0 -132
  35. package/src/types.ts +0 -123
package/README.md CHANGED
@@ -70,10 +70,10 @@ yarn add @semaphore-protocol/utils
70
70
  For more information on the functions and modules provided by `@semaphore-protocol/utils`, please refer to the [TypeDoc documentation](https://js.semaphore.pse.dev/modules/_semaphore_protocol_utils).
71
71
 
72
72
  ```typescript
73
- // You can import modules from the main bundle.
74
- import { errors, types } from "@semaphore-protocol/utils"
73
+ // You can import functions/parameters from the main bundle.
74
+ import { supportedNetworks, decodeMessage } from "@semaphore-protocol/utils"
75
75
 
76
76
  // Or by using conditional exports.
77
- import { requireNumber } from "@semaphore-protocol/utils/errors"
78
- import { isNumber } from "@semaphore-protocol/utils/types"
77
+ import supportedNetworks from "@semaphore-protocol/utils/supported-networks"
78
+ import decodeMessage from "@semaphore-protocol/utils/decode-message"
79
79
  ```
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/utils
3
- * @version 4.0.0-beta.1
3
+ * @version 4.0.0-beta.10
4
4
  * @file A library to provide utility functions to the other Semaphore packages.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -8,252 +8,196 @@
8
8
  */
9
9
  'use strict';
10
10
 
11
- /**
12
- * @module Types
13
- * This module provides utility functions to check data types.
14
- * It defines a set of supported types and includes functions to check if
15
- * a value is defined and if it matches a supported type. These functions
16
- * are useful for type checking and validation in the other libraries,
17
- * enhancing code robustness and reliability.
18
- */
19
- // The list of types supported by this utility functions.
20
- var supportedTypes = ["number", "string", "function", "array", "uint8array", "object", "bigint"];
21
- /**
22
- * It returns true if the value is defined, false otherwise.
23
- * @param value The value to be checked.
24
- * @returns True or false.
25
- */
26
- function isDefined(value) {
27
- return typeof value !== "undefined";
28
- }
29
- /**
30
- * It returns true if the value is a number, false otherwise.
31
- * @param value The value to be checked.
32
- * @returns True or false.
33
- */
34
- function isNumber(value) {
35
- return typeof value === "number";
36
- }
37
- /**
38
- * It returns true if the value is a string, false otherwise.
39
- * @param value The value to be checked.
40
- * @returns True or false.
41
- */
42
- function isString(value) {
43
- return typeof value === "string";
44
- }
45
- /**
46
- * It returns true if the value is a function, false otherwise.
47
- * @param value The value to be checked.
48
- * @returns True or false.
49
- */
50
- function isFunction(value) {
51
- return typeof value === "function";
52
- }
53
- /**
54
- * It returns true if the value is an array, false otherwise.
55
- * @param value The value to be checked.
56
- * @returns True or false.
57
- */
58
- function isArray(value) {
59
- return typeof value === "object" && Array.isArray(value);
60
- }
61
- /**
62
- * It returns true if the value is a uint8array, false otherwise.
63
- * @param value The value to be checked.
64
- * @returns True or false.
65
- */
66
- function isUint8Array(value) {
67
- return value instanceof Uint8Array;
68
- }
69
- /**
70
- * It returns true if the value is an object, false otherwise.
71
- * @param value The value to be checked.
72
- * @returns True or false.
73
- */
74
- function isObject(value) {
75
- return typeof value === "object";
76
- }
77
- /**
78
- * It returns true if the value is a bigint, false otherwise.
79
- * @param value The value to be checked.
80
- * @returns True or false.
81
- */
82
- function isBigInt(value) {
83
- return typeof value === "bigint";
84
- }
85
- /**
86
- * It returns true if the value type is the same as the type passed
87
- * as the second parameter, false otherwise.
88
- * @param type The expected type.
89
- * @returns True or false.
90
- */
91
- function isType(value, type) {
92
- switch (type) {
93
- case "number":
94
- return isNumber(value);
95
- case "string":
96
- return isString(value);
97
- case "function":
98
- return isFunction(value);
99
- case "array":
100
- return isArray(value);
101
- case "uint8array":
102
- return isUint8Array(value);
103
- case "object":
104
- return isObject(value);
105
- case "bigint":
106
- return isBigInt(value);
107
- default:
108
- return false;
109
- }
110
- }
111
- /**
112
- * Return true if the type is being supported by this utility
113
- * functions, false otherwise.
114
- * @param type The type to be checked.
115
- * @returns True or false
116
- */
117
- function isSupportedType(type) {
118
- return supportedTypes.includes(type);
119
- }
11
+ var abi = require('ethers/abi');
12
+ var utils = require('ethers/utils');
13
+
14
+ // Minimum supported tree depth.
15
+ const MIN_DEPTH = 1;
16
+ // Maximum supported tree depth.
17
+ const MAX_DEPTH = 32;
120
18
 
121
- var types = /*#__PURE__*/Object.freeze({
19
+ var constants = /*#__PURE__*/Object.freeze({
122
20
  __proto__: null,
123
- isArray: isArray,
124
- isBigInt: isBigInt,
125
- isDefined: isDefined,
126
- isFunction: isFunction,
127
- isNumber: isNumber,
128
- isObject: isObject,
129
- isString: isString,
130
- isSupportedType: isSupportedType,
131
- isType: isType,
132
- isUint8Array: isUint8Array
21
+ MAX_DEPTH: MAX_DEPTH,
22
+ MIN_DEPTH: MIN_DEPTH
133
23
  });
134
24
 
135
25
  /**
136
- * @module Errors
137
- * This module is designed to provide utility functions for validating
138
- * function parameters. It includes functions that throw type errors if
139
- * the parameters do not meet specified criteria, such as being defined,
140
- * a number, a string, a function, or an array. This module helps ensure
141
- * that functions receive the correct types of inputs, enhancing code
142
- * reliability and reducing runtime errors.
143
- */
144
- /**
145
- * It throws a type error if the parameter value has not been defined.
146
- * @param parameterValue The parameter value.
147
- * @param parameterName The parameter name.
148
- */
149
- function requireDefined(parameterValue, parameterName) {
150
- if (!isDefined(parameterValue)) {
151
- throw new TypeError("Parameter '".concat(parameterName, "' is not defined"));
152
- }
153
- }
154
- /**
155
- * It throws a type error if the parameter value is not a number.
156
- * @param parameterValue The parameter value.
157
- * @param parameterName The parameter name.
26
+ * Typically used for decoding on-chain Semaphore messages.
27
+ * When Semaphore messages are text they are converted to bigints before
28
+ * the proof is generated (and eventually sent on-chain).
29
+ * This function help devs converting bigint messages to text again.
30
+ * If the original message was not text the output of this
31
+ * function won't probably be human-readable text.
32
+ * @param message The Semaphore message as a bigint.
33
+ * @returns The Semaphore message as a text.
158
34
  */
159
- function requireNumber(parameterValue, parameterName) {
160
- if (!isNumber(parameterValue)) {
161
- throw new TypeError("Parameter '".concat(parameterName, "' is not a number"));
162
- }
163
- }
164
- /**
165
- * It throws a type error if the parameter value is not a string.
166
- * @param parameterValue The parameter value.
167
- * @param parameterName The parameter name.
168
- */
169
- function requireString(parameterValue, parameterName) {
170
- if (!isString(parameterValue)) {
171
- throw new TypeError("Parameter '".concat(parameterName, "' is not a string"));
172
- }
35
+ function decodeMessage(message) {
36
+ return abi.decodeBytes32String(utils.toBeHex(message, 32));
173
37
  }
174
- /**
175
- * It throws a type error if the parameter value is not a function.
176
- * @param parameterValue The parameter value.
177
- * @param parameterName The parameter name.
178
- */
179
- function requireFunction(parameterValue, parameterName) {
180
- if (!isFunction(parameterValue)) {
181
- throw new TypeError("Parameter '".concat(parameterName, "' is not a function"));
38
+
39
+ var deployedContracts = [
40
+ {
41
+ network: "sepolia",
42
+ contracts: [
43
+ {
44
+ name: "SemaphoreVerifier",
45
+ address: "0x58AD7a4389E0F5d814815E5D5A3Ab903dAE9284a",
46
+ startBlock: 5539276
47
+ },
48
+ {
49
+ name: "PoseidonT3",
50
+ address: "0x4EFfF6a6a7BB31cdAd9Ee742b0faAA6Bb9BF096B",
51
+ startBlock: 5539276
52
+ },
53
+ {
54
+ name: "Semaphore",
55
+ address: "0x0A09FB3f63c13F1C54F2fA41AFB1e7a98cffc774",
56
+ startBlock: 5539276
57
+ }
58
+ ]
59
+ },
60
+ {
61
+ network: "arbitrum-sepolia",
62
+ contracts: [
63
+ {
64
+ name: "SemaphoreVerifier",
65
+ address: "0x5f4edC58142f4395D1D536e793137A0252dA5a49",
66
+ startBlock: 25837653
67
+ },
68
+ {
69
+ name: "PoseidonT3",
70
+ address: "0x181B7f34538cE3BceC68597d4A212aB3f7881648",
71
+ startBlock: 25837653
72
+ },
73
+ {
74
+ name: "Semaphore",
75
+ address: "0x220fBdB6F996827b1Cf12f0C181E8d5e6de3a36F",
76
+ startBlock: 25837653
77
+ }
78
+ ]
79
+ },
80
+ {
81
+ network: "optimism-sepolia",
82
+ contracts: [
83
+ {
84
+ name: "SemaphoreVerifier",
85
+ address: "0x3d3df6CFc6BFf68d9693e097F32bF4a9903E77a5",
86
+ startBlock: 9663266
87
+ },
88
+ {
89
+ name: "PoseidonT3",
90
+ address: "0x5f4edC58142f4395D1D536e793137A0252dA5a49",
91
+ startBlock: 9663266
92
+ },
93
+ {
94
+ name: "Semaphore",
95
+ address: "0x181B7f34538cE3BceC68597d4A212aB3f7881648",
96
+ startBlock: 9663266
97
+ }
98
+ ]
99
+ },
100
+ {
101
+ network: "matic-mumbai",
102
+ contracts: [
103
+ {
104
+ name: "SemaphoreVerifier",
105
+ address: "0xE5269ae1E996C6FEEf6E832c7032401E73087568",
106
+ startBlock: 47356015
107
+ },
108
+ {
109
+ name: "PoseidonT3",
110
+ address: "0xC0ae1a8D3505B2bE9DCe0e826abd722Afd13F1c9",
111
+ startBlock: 47356015
112
+ },
113
+ {
114
+ name: "Semaphore",
115
+ address: "0x346a936b19071b2f619200848B8ADbb938D72250",
116
+ startBlock: 47356015
117
+ }
118
+ ]
119
+ }
120
+ ];
121
+
122
+ var supportedNetworks = {
123
+ sepolia: {
124
+ name: "Sepolia",
125
+ url: "https://rpc2.sepolia.org",
126
+ chainId: 11155111,
127
+ explorer: "https://sepolia.etherscan.io"
128
+ },
129
+ "arbitrum-sepolia": {
130
+ name: "Arbitrum Sepolia",
131
+ url: "https://sepolia-rollup.arbitrum.io/rpc",
132
+ chainId: 421614,
133
+ explorer: "https://sepolia.arbiscan.io"
134
+ },
135
+ "optimism-sepolia": {
136
+ name: "Optimism Sepolia",
137
+ url: "https://sepolia.optimism.io",
138
+ chainId: 11155420,
139
+ explorer: "https://sepolia-optimism.etherscan.io"
140
+ },
141
+ "matic-mumbai": {
142
+ name: "Matic Mumbai",
143
+ url: "https://rpc-mumbai.polygon.technology",
144
+ chainId: 80001,
145
+ explorer: "https://mumbai.polygonscan.com"
182
146
  }
183
- }
147
+ };
148
+
184
149
  /**
185
- * It throws a type error if the parameter value is not an array.
186
- * @param parameterValue The parameter value.
187
- * @param parameterName The parameter name.
150
+ * @module Networks
151
+ * This module provides a collection of utility functions to provide the other internal
152
+ * packages and developers with information on deployed contracts and networks supported
153
+ * by Semaphore.
188
154
  */
189
- function requireArray(parameterValue, parameterName) {
190
- if (!isArray(parameterValue)) {
191
- throw new TypeError("Parameter '".concat(parameterName, "' is not an array"));
192
- }
193
- }
155
+ // Default Semaphore network.
156
+ const defaultNetwork = "sepolia";
194
157
  /**
195
- * It throws a type error if the parameter value is not a uint8array.
196
- * @param parameterValue The parameter value.
197
- * @param parameterName The parameter name.
158
+ * Returns true if a network is supported by Semaphore, false otherwise.
159
+ * @param supportedNetwork The network to be checked.
198
160
  */
199
- function requireUint8Array(parameterValue, parameterName) {
200
- if (!isUint8Array(parameterValue)) {
201
- throw new TypeError("Parameter '".concat(parameterName, "' is not a Uint8Array"));
202
- }
161
+ function isSupportedNetwork(supportedNetwork) {
162
+ return Object.keys(supportedNetworks).includes(supportedNetwork);
203
163
  }
204
164
  /**
205
- * It throws a type error if the parameter value is not an object.
206
- * @param parameterValue The parameter value.
207
- * @param parameterName The parameter name.
165
+ * Utility function to get an object compatible with the Hardhat 'networks' option.
166
+ * If the private key is not defined it returns an empty object.
167
+ * @param privateKey Private key to be used with networks.
168
+ * @returns An object compatible with the Hardhat 'networks' option.
208
169
  */
209
- function requireObject(parameterValue, parameterName) {
210
- if (!isObject(parameterValue)) {
211
- throw new TypeError("Parameter '".concat(parameterName, "' is not an object"));
170
+ function getHardhatNetworks(privateKey) {
171
+ if (!privateKey) {
172
+ return {};
212
173
  }
213
- }
214
- /**
215
- * It throws a type error if the parameter value is not a bigint.
216
- * @param parameterValue The parameter value.
217
- * @param parameterName The parameter name.
218
- */
219
- function requireBigInt(parameterValue, parameterName) {
220
- if (!isBigInt(parameterValue)) {
221
- throw new TypeError("Parameter '".concat(parameterName, "' is not a bigint"));
174
+ const supportedNetworksCopy = JSON.parse(JSON.stringify(supportedNetworks));
175
+ for (const key in supportedNetworksCopy) {
176
+ if (Object.prototype.hasOwnProperty.call(supportedNetworksCopy, key)) {
177
+ supportedNetworksCopy[key].accounts = [`0x${privateKey}`];
178
+ }
222
179
  }
180
+ return supportedNetworksCopy;
223
181
  }
224
182
  /**
225
- * It throws a type error if the parameter value type is not part of the list of types.
226
- * @param parameterValue The parameter value.
227
- * @param parameterName The parameter name.
183
+ * Returns name, address and start block of a Semaphore contract deployed
184
+ * on a specific supported network.
185
+ * @param supportedNetwork The network supported by Semaphore.
186
+ * @returns An object with name, address and start block of the deployed contract.
228
187
  */
229
- function requireTypes(parameterValue, parameterName, types) {
230
- for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
231
- var type = types_1[_i];
232
- if (!isSupportedType(type)) {
233
- throw new Error("Type '".concat(type, "' is not supported"));
234
- }
235
- }
236
- for (var _a = 0, types_2 = types; _a < types_2.length; _a++) {
237
- var type = types_2[_a];
238
- if (isType(parameterValue, type)) {
239
- return;
240
- }
188
+ function getDeployedContract(supportedNetwork) {
189
+ if (!isSupportedNetwork(supportedNetwork)) {
190
+ throw new Error(`Semaphore has not been deployed on '${supportedNetwork}' yet`);
241
191
  }
242
- throw new TypeError("Parameter '".concat(parameterName, "' is none of the following types: ").concat(types.join(", ")));
192
+ const deployedContract = deployedContracts.find(({ network }) => network === supportedNetwork);
193
+ return deployedContract.contracts.find(({ name }) => name === "Semaphore");
243
194
  }
244
195
 
245
- var errors = /*#__PURE__*/Object.freeze({
246
- __proto__: null,
247
- requireArray: requireArray,
248
- requireBigInt: requireBigInt,
249
- requireDefined: requireDefined,
250
- requireFunction: requireFunction,
251
- requireNumber: requireNumber,
252
- requireObject: requireObject,
253
- requireString: requireString,
254
- requireTypes: requireTypes,
255
- requireUint8Array: requireUint8Array
256
- });
257
-
258
- exports.errors = errors;
259
- exports.types = types;
196
+ exports.constants = constants;
197
+ exports.decodeMessage = decodeMessage;
198
+ exports.defaultNetwork = defaultNetwork;
199
+ exports.deployedContracts = deployedContracts;
200
+ exports.getDeployedContract = getDeployedContract;
201
+ exports.getHardhatNetworks = getHardhatNetworks;
202
+ exports.isSupportedNetwork = isSupportedNetwork;
203
+ exports.supportedNetworks = supportedNetworks;