@openzeppelin/compact-contracts 0.1.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.
@@ -0,0 +1,98 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // OpenZeppelin Compact Contracts v0.1.0 (utils/Utils.compact)
3
+
4
+ pragma language_version >= 0.21.0;
5
+
6
+ /**
7
+ * @module Utils.
8
+ * @description A library for common utilities used in Compact contracts.
9
+ */
10
+ module Utils {
11
+ import CompactStandardLibrary;
12
+
13
+ /**
14
+ * @description Returns whether `keyOrAddress` is the zero address.
15
+ *
16
+ * @notice Midnight's burn address is represented as left<ZswapCoinPublicKey, ContractAddress>(default<ZswapCoinPublicKey>)
17
+ * in Compact, so we've chosen to represent the zero address as this structure as well.
18
+ *
19
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
20
+ * @return {Boolean} - Returns true if `keyOrAddress` is zero.
21
+ */
22
+ export pure circuit isKeyOrAddressZero(
23
+ keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>
24
+ ): Boolean {
25
+ return isContractAddress(keyOrAddress)
26
+ ? default<ContractAddress> == keyOrAddress.right
27
+ : default<ZswapCoinPublicKey> == keyOrAddress.left;
28
+ }
29
+
30
+ /**
31
+ * @description Returns whether `key` is the zero address.
32
+ *
33
+ * @param {ZswapCoinPublicKey} key - A ZswapCoinPublicKey
34
+ * @return {Boolean} - Returns true if `key` is zero.
35
+ */
36
+ export pure circuit isKeyZero(key: ZswapCoinPublicKey): Boolean {
37
+ const zero = default<ZswapCoinPublicKey>;
38
+ return zero == key;
39
+ }
40
+
41
+ /**
42
+ * @description Returns whether `keyOrAddress` is equal to `other`. Assumes that a ZswapCoinPublicKey
43
+ * and a ContractAddress can never be equal
44
+ *
45
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
46
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} other - The other value to check, either a ZswapCoinPublicKey or a ContractAddress.
47
+ * @return {Boolean} - Returns true if `keyOrAddress` is equal to `other`.
48
+ */
49
+ export pure circuit isKeyOrAddressEqual(
50
+ keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>,
51
+ other: Either<ZswapCoinPublicKey, ContractAddress>
52
+ ): Boolean {
53
+ if (keyOrAddress.is_left && other.is_left) {
54
+ return keyOrAddress.left == other.left;
55
+ } else if (!keyOrAddress.is_left && !other.is_left) {
56
+ return keyOrAddress.right == other.right;
57
+ } else {
58
+ return false;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * @description Returns whether `keyOrAddress` is a ContractAddress type.
64
+ *
65
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
66
+ * @return {Boolean} - Returns true if `keyOrAddress` is a ContractAddress.
67
+ */
68
+ export pure circuit isContractAddress(
69
+ keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>
70
+ ): Boolean {
71
+ return !keyOrAddress.is_left;
72
+ }
73
+
74
+ /**
75
+ * @description A helper function that returns the empty string: "".
76
+ *
77
+ * @return {Opaque<"string">} - The empty string: "".
78
+ */
79
+ export pure circuit emptyString(): Opaque<"string"> {
80
+ return default<Opaque<"string">>;
81
+ }
82
+
83
+ /**
84
+ * @description Zeroes out the unused side of an `Either` value. Prevents crafted
85
+ * inputs where both `left` and `right` fields carry data from bypassing checks that
86
+ * only inspect the active side.
87
+ *
88
+ * @param {Either<T1, T2>} value - The value to canonicalize.
89
+ * @return {Either<T1, T2>} - The canonicalized value.
90
+ */
91
+ export pure circuit canonicalize<T1, T2>(
92
+ value: Either<T1, T2>
93
+ ): Either<T1, T2> {
94
+ return value.is_left
95
+ ? Either<T1, T2>{ is_left: true, left: value.left, right: default<T2> }
96
+ : Either<T1, T2>{ is_left: false, left: default<T1>, right: value.right };
97
+ }
98
+ }