@semaphore-protocol/utils 4.0.0-beta.2 → 4.0.0-beta.4

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.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @module @semaphore-protocol/utils
3
+ * @version 4.0.0-beta.4
4
+ * @file A library to provide utility functions to the other Semaphore packages.
5
+ * @copyright Ethereum Foundation 2024
6
+ * @license MIT
7
+ * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
8
+ */
9
+ import { decodeBytes32String } from 'ethers/abi';
10
+ import { toBeHex } from 'ethers/utils';
11
+
12
+ /**
13
+ * Typically used for decoding on-chain Semaphore messages.
14
+ * When Semaphore messages are text they are converted to bigints before
15
+ * the proof is generated (and eventually sent on-chain).
16
+ * This function help devs converting bigint messages to text again.
17
+ * If the original message was not text the output of this
18
+ * function won't probably be human-readable text.
19
+ * @param message The Semaphore message as a bigint.
20
+ * @returns The Semaphore message as a text.
21
+ */
22
+ function decodeMessage(message) {
23
+ return decodeBytes32String(toBeHex(message, 32));
24
+ }
25
+
26
+ export { decodeMessage as default };
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/utils
3
- * @version 4.0.0-beta.2
3
+ * @version 4.0.0-beta.4
4
4
  * @file A library to provide utility functions to the other Semaphore packages.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
8
8
  */
9
- import * as errors from './errors.js';
10
- export { errors };
11
- import * as types from './types.js';
12
- export { types };
9
+ import * as constants from './constants.js';
10
+ export { constants };
11
+ export { default as decodeMessage } from './decode-message.js';
12
+ export { default as supportedNetworks } from './supported-networks.js';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @module @semaphore-protocol/utils
3
+ * @version 4.0.0-beta.4
4
+ * @file A library to provide utility functions to the other Semaphore packages.
5
+ * @copyright Ethereum Foundation 2024
6
+ * @license MIT
7
+ * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
8
+ */
9
+ // List of Semaphore supported networks.
10
+ var supportedNetwork = ["sepolia", "mumbai", "optimism-sepolia", "arbitrum-sepolia", "arbitrum"];
11
+
12
+ export { supportedNetwork as default };
@@ -0,0 +1,2 @@
1
+ export declare const MIN_DEPTH = 1;
2
+ export declare const MAX_DEPTH = 12;
@@ -0,0 +1,12 @@
1
+ import type { BigNumberish } from "ethers";
2
+ /**
3
+ * Typically used for decoding on-chain Semaphore messages.
4
+ * When Semaphore messages are text they are converted to bigints before
5
+ * the proof is generated (and eventually sent on-chain).
6
+ * This function help devs converting bigint messages to text again.
7
+ * If the original message was not text the output of this
8
+ * function won't probably be human-readable text.
9
+ * @param message The Semaphore message as a bigint.
10
+ * @returns The Semaphore message as a text.
11
+ */
12
+ export default function decodeMessage(message: BigNumberish): string;
@@ -1,3 +1,5 @@
1
- import * as errors from "./errors";
2
- import * as types from "./types";
3
- export { errors, types };
1
+ import * as constants from "./constants";
2
+ import decodeMessage from "./decode-message";
3
+ import supportedNetworks from "./supported-networks";
4
+ export * from "./types";
5
+ export { constants, decodeMessage, supportedNetworks };
@@ -0,0 +1,3 @@
1
+ import { SupportedNetwork } from "./types";
2
+ declare const supportedNetwork: SupportedNetwork[];
3
+ export default supportedNetwork;
@@ -0,0 +1 @@
1
+ export type SupportedNetwork = "sepolia" | "mumbai" | "optimism-sepolia" | "arbitrum-sepolia" | "arbitrum";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/utils",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.4",
4
4
  "description": "A library to provide utility functions to the other Semaphore packages.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -12,15 +12,20 @@
12
12
  "require": "./dist/index.cjs",
13
13
  "default": "./dist/index.js"
14
14
  },
15
- "./errors": {
16
- "types": "./dist/types/errors.d.ts",
17
- "require": "./dist/lib.commonjs/errors.cjs",
18
- "default": "./dist/lib.esm/errors.js"
15
+ "./supported-networks": {
16
+ "types": "./dist/types/supported-networks.d.ts",
17
+ "require": "./dist/lib.commonjs/supported-networks.cjs",
18
+ "default": "./dist/lib.esm/supported-networks.js"
19
19
  },
20
- "./types": {
21
- "types": "./dist/types/types.d.ts",
22
- "require": "./dist/lib.commonjs/types.cjs",
23
- "default": "./dist/lib.esm/types.js"
20
+ "./constants": {
21
+ "types": "./dist/types/constants.d.ts",
22
+ "require": "./dist/lib.commonjs/constants.cjs",
23
+ "default": "./dist/lib.esm/constants.js"
24
+ },
25
+ "./decode-message": {
26
+ "types": "./dist/types/decode-message.d.ts",
27
+ "require": "./dist/lib.commonjs/decode-message.cjs",
28
+ "default": "./dist/lib.esm/decode-message.js"
24
29
  }
25
30
  },
26
31
  "files": [
@@ -42,9 +47,12 @@
42
47
  "access": "public"
43
48
  },
44
49
  "devDependencies": {
50
+ "@rollup/plugin-typescript": "^11.1.6",
45
51
  "rimraf": "^5.0.5",
46
- "rollup": "^4.9.6",
47
- "rollup-plugin-cleanup": "^3.2.1",
48
- "rollup-plugin-typescript2": "^0.36.0"
52
+ "rollup": "^4.12.0",
53
+ "rollup-plugin-cleanup": "^3.2.1"
54
+ },
55
+ "dependencies": {
56
+ "ethers": "^6.11.1"
49
57
  }
50
58
  }
@@ -0,0 +1,5 @@
1
+ // TODO: Support all tree depths after trusted-setup. MAX_DEPTH would be 32.
2
+
3
+ export const MIN_DEPTH = 1
4
+
5
+ export const MAX_DEPTH = 12
@@ -0,0 +1,17 @@
1
+ import type { BigNumberish } from "ethers"
2
+ import { decodeBytes32String } from "ethers/abi"
3
+ import { toBeHex } from "ethers/utils"
4
+
5
+ /**
6
+ * Typically used for decoding on-chain Semaphore messages.
7
+ * When Semaphore messages are text they are converted to bigints before
8
+ * the proof is generated (and eventually sent on-chain).
9
+ * This function help devs converting bigint messages to text again.
10
+ * If the original message was not text the output of this
11
+ * function won't probably be human-readable text.
12
+ * @param message The Semaphore message as a bigint.
13
+ * @returns The Semaphore message as a text.
14
+ */
15
+ export default function decodeMessage(message: BigNumberish) {
16
+ return decodeBytes32String(toBeHex(message, 32))
17
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
- import * as errors from "./errors"
2
- import * as types from "./types"
1
+ import * as constants from "./constants"
2
+ import decodeMessage from "./decode-message"
3
+ import supportedNetworks from "./supported-networks"
3
4
 
4
- export { errors, types }
5
+ export * from "./types"
6
+ export { constants, decodeMessage, supportedNetworks }
@@ -0,0 +1,6 @@
1
+ import { SupportedNetwork } from "./types"
2
+
3
+ // List of Semaphore supported networks.
4
+ const supportedNetwork: SupportedNetwork[] = ["sepolia", "mumbai", "optimism-sepolia", "arbitrum-sepolia", "arbitrum"]
5
+
6
+ export default supportedNetwork
@@ -0,0 +1 @@
1
+ export type SupportedNetwork = "sepolia" | "mumbai" | "optimism-sepolia" | "arbitrum-sepolia" | "arbitrum"
@@ -1,131 +0,0 @@
1
- /**
2
- * @module @semaphore-protocol/utils
3
- * @version 4.0.0-beta.2
4
- * @file A library to provide utility functions to the other Semaphore packages.
5
- * @copyright Ethereum Foundation 2024
6
- * @license MIT
7
- * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
8
- */
9
- 'use strict';
10
-
11
- var types = require('./types.cjs');
12
-
13
- /**
14
- * @module Errors
15
- * This module is designed to provide utility functions for validating
16
- * function parameters. It includes functions that throw type errors if
17
- * the parameters do not meet specified criteria, such as being defined,
18
- * a number, a string, a function, or an array. This module helps ensure
19
- * that functions receive the correct types of inputs, enhancing code
20
- * reliability and reducing runtime errors.
21
- */
22
- /**
23
- * It throws a type error if the parameter value has not been defined.
24
- * @param parameterValue The parameter value.
25
- * @param parameterName The parameter name.
26
- */
27
- function requireDefined(parameterValue, parameterName) {
28
- if (!types.isDefined(parameterValue)) {
29
- throw new TypeError("Parameter '".concat(parameterName, "' is not defined"));
30
- }
31
- }
32
- /**
33
- * It throws a type error if the parameter value is not a number.
34
- * @param parameterValue The parameter value.
35
- * @param parameterName The parameter name.
36
- */
37
- function requireNumber(parameterValue, parameterName) {
38
- if (!types.isNumber(parameterValue)) {
39
- throw new TypeError("Parameter '".concat(parameterName, "' is not a number"));
40
- }
41
- }
42
- /**
43
- * It throws a type error if the parameter value is not a string.
44
- * @param parameterValue The parameter value.
45
- * @param parameterName The parameter name.
46
- */
47
- function requireString(parameterValue, parameterName) {
48
- if (!types.isString(parameterValue)) {
49
- throw new TypeError("Parameter '".concat(parameterName, "' is not a string"));
50
- }
51
- }
52
- /**
53
- * It throws a type error if the parameter value is not a function.
54
- * @param parameterValue The parameter value.
55
- * @param parameterName The parameter name.
56
- */
57
- function requireFunction(parameterValue, parameterName) {
58
- if (!types.isFunction(parameterValue)) {
59
- throw new TypeError("Parameter '".concat(parameterName, "' is not a function"));
60
- }
61
- }
62
- /**
63
- * It throws a type error if the parameter value is not an array.
64
- * @param parameterValue The parameter value.
65
- * @param parameterName The parameter name.
66
- */
67
- function requireArray(parameterValue, parameterName) {
68
- if (!types.isArray(parameterValue)) {
69
- throw new TypeError("Parameter '".concat(parameterName, "' is not an array"));
70
- }
71
- }
72
- /**
73
- * It throws a type error if the parameter value is not a uint8array.
74
- * @param parameterValue The parameter value.
75
- * @param parameterName The parameter name.
76
- */
77
- function requireUint8Array(parameterValue, parameterName) {
78
- if (!types.isUint8Array(parameterValue)) {
79
- throw new TypeError("Parameter '".concat(parameterName, "' is not a Uint8Array"));
80
- }
81
- }
82
- /**
83
- * It throws a type error if the parameter value is not an object.
84
- * @param parameterValue The parameter value.
85
- * @param parameterName The parameter name.
86
- */
87
- function requireObject(parameterValue, parameterName) {
88
- if (!types.isObject(parameterValue)) {
89
- throw new TypeError("Parameter '".concat(parameterName, "' is not an object"));
90
- }
91
- }
92
- /**
93
- * It throws a type error if the parameter value is not a bigint.
94
- * @param parameterValue The parameter value.
95
- * @param parameterName The parameter name.
96
- */
97
- function requireBigInt(parameterValue, parameterName) {
98
- if (!types.isBigInt(parameterValue)) {
99
- throw new TypeError("Parameter '".concat(parameterName, "' is not a bigint"));
100
- }
101
- }
102
- /**
103
- * It throws a type error if the parameter value type is not part of the list of types.
104
- * @param parameterValue The parameter value.
105
- * @param parameterName The parameter name.
106
- */
107
- function requireTypes(parameterValue, parameterName, types$1) {
108
- for (var _i = 0, types_1 = types$1; _i < types_1.length; _i++) {
109
- var type = types_1[_i];
110
- if (!types.isSupportedType(type)) {
111
- throw new Error("Type '".concat(type, "' is not supported"));
112
- }
113
- }
114
- for (var _a = 0, types_2 = types$1; _a < types_2.length; _a++) {
115
- var type = types_2[_a];
116
- if (types.isType(parameterValue, type)) {
117
- return;
118
- }
119
- }
120
- throw new TypeError("Parameter '".concat(parameterName, "' is none of the following types: ").concat(types$1.join(", ")));
121
- }
122
-
123
- exports.requireArray = requireArray;
124
- exports.requireBigInt = requireBigInt;
125
- exports.requireDefined = requireDefined;
126
- exports.requireFunction = requireFunction;
127
- exports.requireNumber = requireNumber;
128
- exports.requireObject = requireObject;
129
- exports.requireString = requireString;
130
- exports.requireTypes = requireTypes;
131
- exports.requireUint8Array = requireUint8Array;
@@ -1,130 +0,0 @@
1
- /**
2
- * @module @semaphore-protocol/utils
3
- * @version 4.0.0-beta.2
4
- * @file A library to provide utility functions to the other Semaphore packages.
5
- * @copyright Ethereum Foundation 2024
6
- * @license MIT
7
- * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
8
- */
9
- 'use strict';
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
- }
120
-
121
- exports.isArray = isArray;
122
- exports.isBigInt = isBigInt;
123
- exports.isDefined = isDefined;
124
- exports.isFunction = isFunction;
125
- exports.isNumber = isNumber;
126
- exports.isObject = isObject;
127
- exports.isString = isString;
128
- exports.isSupportedType = isSupportedType;
129
- exports.isType = isType;
130
- exports.isUint8Array = isUint8Array;
@@ -1,121 +0,0 @@
1
- /**
2
- * @module @semaphore-protocol/utils
3
- * @version 4.0.0-beta.2
4
- * @file A library to provide utility functions to the other Semaphore packages.
5
- * @copyright Ethereum Foundation 2024
6
- * @license MIT
7
- * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/utils}
8
- */
9
- import { isDefined, isNumber, isString, isFunction, isArray, isUint8Array, isObject, isBigInt, isSupportedType, isType } from './types.js';
10
-
11
- /**
12
- * @module Errors
13
- * This module is designed to provide utility functions for validating
14
- * function parameters. It includes functions that throw type errors if
15
- * the parameters do not meet specified criteria, such as being defined,
16
- * a number, a string, a function, or an array. This module helps ensure
17
- * that functions receive the correct types of inputs, enhancing code
18
- * reliability and reducing runtime errors.
19
- */
20
- /**
21
- * It throws a type error if the parameter value has not been defined.
22
- * @param parameterValue The parameter value.
23
- * @param parameterName The parameter name.
24
- */
25
- function requireDefined(parameterValue, parameterName) {
26
- if (!isDefined(parameterValue)) {
27
- throw new TypeError("Parameter '".concat(parameterName, "' is not defined"));
28
- }
29
- }
30
- /**
31
- * It throws a type error if the parameter value is not a number.
32
- * @param parameterValue The parameter value.
33
- * @param parameterName The parameter name.
34
- */
35
- function requireNumber(parameterValue, parameterName) {
36
- if (!isNumber(parameterValue)) {
37
- throw new TypeError("Parameter '".concat(parameterName, "' is not a number"));
38
- }
39
- }
40
- /**
41
- * It throws a type error if the parameter value is not a string.
42
- * @param parameterValue The parameter value.
43
- * @param parameterName The parameter name.
44
- */
45
- function requireString(parameterValue, parameterName) {
46
- if (!isString(parameterValue)) {
47
- throw new TypeError("Parameter '".concat(parameterName, "' is not a string"));
48
- }
49
- }
50
- /**
51
- * It throws a type error if the parameter value is not a function.
52
- * @param parameterValue The parameter value.
53
- * @param parameterName The parameter name.
54
- */
55
- function requireFunction(parameterValue, parameterName) {
56
- if (!isFunction(parameterValue)) {
57
- throw new TypeError("Parameter '".concat(parameterName, "' is not a function"));
58
- }
59
- }
60
- /**
61
- * It throws a type error if the parameter value is not an array.
62
- * @param parameterValue The parameter value.
63
- * @param parameterName The parameter name.
64
- */
65
- function requireArray(parameterValue, parameterName) {
66
- if (!isArray(parameterValue)) {
67
- throw new TypeError("Parameter '".concat(parameterName, "' is not an array"));
68
- }
69
- }
70
- /**
71
- * It throws a type error if the parameter value is not a uint8array.
72
- * @param parameterValue The parameter value.
73
- * @param parameterName The parameter name.
74
- */
75
- function requireUint8Array(parameterValue, parameterName) {
76
- if (!isUint8Array(parameterValue)) {
77
- throw new TypeError("Parameter '".concat(parameterName, "' is not a Uint8Array"));
78
- }
79
- }
80
- /**
81
- * It throws a type error if the parameter value is not an object.
82
- * @param parameterValue The parameter value.
83
- * @param parameterName The parameter name.
84
- */
85
- function requireObject(parameterValue, parameterName) {
86
- if (!isObject(parameterValue)) {
87
- throw new TypeError("Parameter '".concat(parameterName, "' is not an object"));
88
- }
89
- }
90
- /**
91
- * It throws a type error if the parameter value is not a bigint.
92
- * @param parameterValue The parameter value.
93
- * @param parameterName The parameter name.
94
- */
95
- function requireBigInt(parameterValue, parameterName) {
96
- if (!isBigInt(parameterValue)) {
97
- throw new TypeError("Parameter '".concat(parameterName, "' is not a bigint"));
98
- }
99
- }
100
- /**
101
- * It throws a type error if the parameter value type is not part of the list of types.
102
- * @param parameterValue The parameter value.
103
- * @param parameterName The parameter name.
104
- */
105
- function requireTypes(parameterValue, parameterName, types) {
106
- for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
107
- var type = types_1[_i];
108
- if (!isSupportedType(type)) {
109
- throw new Error("Type '".concat(type, "' is not supported"));
110
- }
111
- }
112
- for (var _a = 0, types_2 = types; _a < types_2.length; _a++) {
113
- var type = types_2[_a];
114
- if (isType(parameterValue, type)) {
115
- return;
116
- }
117
- }
118
- throw new TypeError("Parameter '".concat(parameterName, "' is none of the following types: ").concat(types.join(", ")));
119
- }
120
-
121
- export { requireArray, requireBigInt, requireDefined, requireFunction, requireNumber, requireObject, requireString, requireTypes, requireUint8Array };