// SPDX-License-Identifier: MIT // OpenZeppelin Compact Contracts v0.5.0-alpha.2 (utils/Utils.compact) pragma language_version >= 0.28.0; /** * @module Utils. * @description A library for common utilities used in Compact contracts. */ module Utils { import CompactStandardLibrary; /** * @description Returns whether `keyOrAddress` is the zero address. * * @notice Midnight's burn address is represented as left(default) * in Compact, so we've chosen to represent the zero address as this structure as well. * * @param {Either} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress. * @return {Boolean} - Returns true if `keyOrAddress` is zero. */ export pure circuit isKeyOrAddressZero( keyOrAddress: Either ): Boolean { return isContractAddress(keyOrAddress) ? default == keyOrAddress.right : default == keyOrAddress.left; } /** * @description Returns whether `key` is the zero address. * * @param {ZswapCoinPublicKey} key - A ZswapCoinPublicKey * @return {Boolean} - Returns true if `key` is zero. */ export pure circuit isKeyZero(key: ZswapCoinPublicKey): Boolean { const zero = default; return zero == key; } /** * @description Returns whether `keyOrAddress` is equal to `other`. Assumes that a ZswapCoinPublicKey * and a ContractAddress can never be equal * * @param {Either} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress. * @param {Either} other - The other value to check, either a ZswapCoinPublicKey or a ContractAddress. * @return {Boolean} - Returns true if `keyOrAddress` is equal to `other`. */ export pure circuit isKeyOrAddressEqual( keyOrAddress: Either, other: Either ): Boolean { if (keyOrAddress.is_left && other.is_left) { return keyOrAddress.left == other.left; } else if (!keyOrAddress.is_left && !other.is_left) { return keyOrAddress.right == other.right; } else { return false; } } /** * @description Returns whether `keyOrAddress` is a ContractAddress type. * * @param {Either} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress. * @return {Boolean} - Returns true if `keyOrAddress` is a ContractAddress. */ export pure circuit isContractAddress( keyOrAddress: Either ): Boolean { return !keyOrAddress.is_left; } /** * @description A helper function that returns the empty string: "". * * @return {Opaque<"string">} - The empty string: "". */ export pure circuit emptyString(): Opaque<"string"> { return default>; } /** * @description Zeroes out the unused side of an `Either` value. Prevents crafted * inputs where both `left` and `right` fields carry data from bypassing checks that * only inspect the active side. * * @param {Either} value - The value to canonicalize. * @return {Either} - The canonicalized value. */ export pure circuit canonicalize( value: Either ): Either { return value.is_left ? Either{ is_left: true, left: value.left, right: default } : Either{ is_left: false, left: default, right: value.right }; } /** * @description Returns the canonical zero `Either, ContractAddress>` value: the * left variant (`Bytes<32>`) set to zero. Used as the sentinel for mint/burn operations * (a zero `from` signals a mint, a zero `to` signals a burn) and for cleared approvals. * * The left variant is chosen so that `isTargetZero` checks against `default>`, * and to avoid triggering contract-address guards (e.g. circuits that check `!to.is_left` * before delegating to an `_unsafe` variant). A right variant with `default` * would also pass `isTargetZero`, but the left variant keeps guards and error messages consistent. * * @return {Either, ContractAddress>} - The canonical zero value. */ export pure circuit zeroAccount(): Either, ContractAddress> { return Either, ContractAddress> { is_left: true, left: default>, right: default }; } /** * @description Returns `true` if `target`'s active branch (as indicated by `is_left`) * holds the zero value. * * @param {Either, ContractAddress>} target - The value to check. * @return {Boolean} - `true` if the active branch is zero, `false` otherwise. */ export pure circuit isTargetZero(target: Either, ContractAddress>): Boolean { if (target.is_left) { return target.left == default>; } else { return target.right == default; } } /** * @description Returns the current contract's address as an * `Either` for use as a * recipient in shielded send operations (deposits and receiving change). * * @returns {Either} The contract's address as a recipient. */ export circuit selfAsRecipient(): Either { return right(kernel.self()); } /** * @description The maximum value representable by a `Uint<128>`. * * @notice TODO: Remove once a math module providing this constant is available. * * @returns {Uint<128>} `2^128 - 1`. */ export pure circuit UINT128_MAX(): Uint<128> { return 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; } }