draft-ERC7579Utils.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Execution} from "../../interfaces/draft-IERC7579.sol";
  4. import {Packing} from "../../utils/Packing.sol";
  5. import {Address} from "../../utils/Address.sol";
  6. type Mode is bytes32;
  7. type CallType is bytes1;
  8. type ExecType is bytes1;
  9. type ModeSelector is bytes4;
  10. type ModePayload is bytes22;
  11. /**
  12. * @dev Library with common ERC-7579 utility functions.
  13. *
  14. * See https://eips.ethereum.org/EIPS/eip-7579[ERC-7579].
  15. */
  16. // slither-disable-next-line unused-state
  17. library ERC7579Utils {
  18. using Packing for *;
  19. /// @dev A single `call` execution.
  20. CallType internal constant CALLTYPE_SINGLE = CallType.wrap(0x00);
  21. /// @dev A batch of `call` executions.
  22. CallType internal constant CALLTYPE_BATCH = CallType.wrap(0x01);
  23. /// @dev A `delegatecall` execution.
  24. CallType internal constant CALLTYPE_DELEGATECALL = CallType.wrap(0xFF);
  25. /// @dev Default execution type that reverts on failure.
  26. ExecType internal constant EXECTYPE_DEFAULT = ExecType.wrap(0x00);
  27. /// @dev Execution type that does not revert on failure.
  28. ExecType internal constant EXECTYPE_TRY = ExecType.wrap(0x01);
  29. /**
  30. * @dev Emits when an {EXECTYPE_TRY} execution fails.
  31. * @param batchExecutionIndex The index of the failed call in the execution batch.
  32. * @param returndata The returned data from the failed call.
  33. */
  34. event ERC7579TryExecuteFail(uint256 batchExecutionIndex, bytes returndata);
  35. /// @dev The provided {CallType} is not supported.
  36. error ERC7579UnsupportedCallType(CallType callType);
  37. /// @dev The provided {ExecType} is not supported.
  38. error ERC7579UnsupportedExecType(ExecType execType);
  39. /// @dev The provided module doesn't match the provided module type.
  40. error ERC7579MismatchedModuleTypeId(uint256 moduleTypeId, address module);
  41. /// @dev The module is not installed.
  42. error ERC7579UninstalledModule(uint256 moduleTypeId, address module);
  43. /// @dev The module is already installed.
  44. error ERC7579AlreadyInstalledModule(uint256 moduleTypeId, address module);
  45. /// @dev The module type is not supported.
  46. error ERC7579UnsupportedModuleType(uint256 moduleTypeId);
  47. /// @dev Input calldata not properly formatted and possibly malicious.
  48. error ERC7579DecodingError();
  49. /// @dev Executes a single call.
  50. function execSingle(
  51. bytes calldata executionCalldata,
  52. ExecType execType
  53. ) internal returns (bytes[] memory returnData) {
  54. (address target, uint256 value, bytes calldata callData) = decodeSingle(executionCalldata);
  55. returnData = new bytes[](1);
  56. returnData[0] = _call(0, execType, target, value, callData);
  57. }
  58. /// @dev Executes a batch of calls.
  59. function execBatch(
  60. bytes calldata executionCalldata,
  61. ExecType execType
  62. ) internal returns (bytes[] memory returnData) {
  63. Execution[] calldata executionBatch = decodeBatch(executionCalldata);
  64. returnData = new bytes[](executionBatch.length);
  65. for (uint256 i = 0; i < executionBatch.length; ++i) {
  66. returnData[i] = _call(
  67. i,
  68. execType,
  69. executionBatch[i].target,
  70. executionBatch[i].value,
  71. executionBatch[i].callData
  72. );
  73. }
  74. }
  75. /// @dev Executes a delegate call.
  76. function execDelegateCall(
  77. bytes calldata executionCalldata,
  78. ExecType execType
  79. ) internal returns (bytes[] memory returnData) {
  80. (address target, bytes calldata callData) = decodeDelegate(executionCalldata);
  81. returnData = new bytes[](1);
  82. returnData[0] = _delegatecall(0, execType, target, callData);
  83. }
  84. /// @dev Encodes the mode with the provided parameters. See {decodeMode}.
  85. function encodeMode(
  86. CallType callType,
  87. ExecType execType,
  88. ModeSelector selector,
  89. ModePayload payload
  90. ) internal pure returns (Mode mode) {
  91. return
  92. Mode.wrap(
  93. CallType
  94. .unwrap(callType)
  95. .pack_1_1(ExecType.unwrap(execType))
  96. .pack_2_4(bytes4(0))
  97. .pack_6_4(ModeSelector.unwrap(selector))
  98. .pack_10_22(ModePayload.unwrap(payload))
  99. );
  100. }
  101. /// @dev Decodes the mode into its parameters. See {encodeMode}.
  102. function decodeMode(
  103. Mode mode
  104. ) internal pure returns (CallType callType, ExecType execType, ModeSelector selector, ModePayload payload) {
  105. return (
  106. CallType.wrap(Packing.extract_32_1(Mode.unwrap(mode), 0)),
  107. ExecType.wrap(Packing.extract_32_1(Mode.unwrap(mode), 1)),
  108. ModeSelector.wrap(Packing.extract_32_4(Mode.unwrap(mode), 6)),
  109. ModePayload.wrap(Packing.extract_32_22(Mode.unwrap(mode), 10))
  110. );
  111. }
  112. /// @dev Encodes a single call execution. See {decodeSingle}.
  113. function encodeSingle(
  114. address target,
  115. uint256 value,
  116. bytes calldata callData
  117. ) internal pure returns (bytes memory executionCalldata) {
  118. return abi.encodePacked(target, value, callData);
  119. }
  120. /// @dev Decodes a single call execution. See {encodeSingle}.
  121. function decodeSingle(
  122. bytes calldata executionCalldata
  123. ) internal pure returns (address target, uint256 value, bytes calldata callData) {
  124. target = address(bytes20(executionCalldata[0:20]));
  125. value = uint256(bytes32(executionCalldata[20:52]));
  126. callData = executionCalldata[52:];
  127. }
  128. /// @dev Encodes a delegate call execution. See {decodeDelegate}.
  129. function encodeDelegate(
  130. address target,
  131. bytes calldata callData
  132. ) internal pure returns (bytes memory executionCalldata) {
  133. return abi.encodePacked(target, callData);
  134. }
  135. /// @dev Decodes a delegate call execution. See {encodeDelegate}.
  136. function decodeDelegate(
  137. bytes calldata executionCalldata
  138. ) internal pure returns (address target, bytes calldata callData) {
  139. target = address(bytes20(executionCalldata[0:20]));
  140. callData = executionCalldata[20:];
  141. }
  142. /// @dev Encodes a batch of executions. See {decodeBatch}.
  143. function encodeBatch(Execution[] memory executionBatch) internal pure returns (bytes memory executionCalldata) {
  144. return abi.encode(executionBatch);
  145. }
  146. /// @dev Decodes a batch of executions. See {encodeBatch}.
  147. ///
  148. /// NOTE: This function runs some checks and will throw a {ERC7579DecodingError} if the input is not properly formatted.
  149. function decodeBatch(bytes calldata executionCalldata) internal pure returns (Execution[] calldata executionBatch) {
  150. unchecked {
  151. uint256 bufferLength = executionCalldata.length;
  152. // Check executionCalldata is not empty.
  153. if (bufferLength < 32) revert ERC7579DecodingError();
  154. // Get the offset of the array (pointer to the array length).
  155. uint256 arrayLengthPointer = uint256(bytes32(executionCalldata[0:32]));
  156. // The array length (at arrayLengthPointer) should be 32 bytes long. We check that this is within the
  157. // buffer bounds. Since we know bufferLength is at least 32, we can subtract with no overflow risk.
  158. if (arrayLengthPointer > bufferLength - 32) revert ERC7579DecodingError();
  159. // Get the array length. arrayLengthPointer + 32 is bounded by bufferLength so it does not overflow.
  160. uint256 arrayLength = uint256(bytes32(executionCalldata[arrayLengthPointer:arrayLengthPointer + 32]));
  161. // Check that the buffer is long enough to store the array elements as "offset pointer":
  162. // - each element of the array is an "offset pointer" to the data.
  163. // - each "offset pointer" (to an array element) takes 32 bytes.
  164. // - validity of the calldata at that location is checked when the array element is accessed, so we only
  165. // need to check that the buffer is large enough to hold the pointers.
  166. //
  167. // Since we know bufferLength is at least arrayLengthPointer + 32, we can subtract with no overflow risk.
  168. // Solidity limits length of such arrays to 2**64-1, this guarantees `arrayLength * 32` does not overflow.
  169. if (arrayLength > type(uint64).max || bufferLength - arrayLengthPointer - 32 < arrayLength * 32)
  170. revert ERC7579DecodingError();
  171. assembly ("memory-safe") {
  172. executionBatch.offset := add(add(executionCalldata.offset, arrayLengthPointer), 32)
  173. executionBatch.length := arrayLength
  174. }
  175. }
  176. }
  177. /// @dev Executes a `call` to the target with the provided {ExecType}.
  178. function _call(
  179. uint256 index,
  180. ExecType execType,
  181. address target,
  182. uint256 value,
  183. bytes calldata data
  184. ) private returns (bytes memory) {
  185. (bool success, bytes memory returndata) = target.call{value: value}(data);
  186. return _validateExecutionMode(index, execType, success, returndata);
  187. }
  188. /// @dev Executes a `delegatecall` to the target with the provided {ExecType}.
  189. function _delegatecall(
  190. uint256 index,
  191. ExecType execType,
  192. address target,
  193. bytes calldata data
  194. ) private returns (bytes memory) {
  195. (bool success, bytes memory returndata) = target.delegatecall(data);
  196. return _validateExecutionMode(index, execType, success, returndata);
  197. }
  198. /// @dev Validates the execution mode and returns the returndata.
  199. function _validateExecutionMode(
  200. uint256 index,
  201. ExecType execType,
  202. bool success,
  203. bytes memory returndata
  204. ) private returns (bytes memory) {
  205. if (execType == ERC7579Utils.EXECTYPE_DEFAULT) {
  206. Address.verifyCallResult(success, returndata);
  207. } else if (execType == ERC7579Utils.EXECTYPE_TRY) {
  208. if (!success) emit ERC7579TryExecuteFail(index, returndata);
  209. } else {
  210. revert ERC7579UnsupportedExecType(execType);
  211. }
  212. return returndata;
  213. }
  214. }
  215. // Operators
  216. using {eqCallType as ==} for CallType global;
  217. using {eqExecType as ==} for ExecType global;
  218. using {eqModeSelector as ==} for ModeSelector global;
  219. using {eqModePayload as ==} for ModePayload global;
  220. /// @dev Compares two `CallType` values for equality.
  221. function eqCallType(CallType a, CallType b) pure returns (bool) {
  222. return CallType.unwrap(a) == CallType.unwrap(b);
  223. }
  224. /// @dev Compares two `ExecType` values for equality.
  225. function eqExecType(ExecType a, ExecType b) pure returns (bool) {
  226. return ExecType.unwrap(a) == ExecType.unwrap(b);
  227. }
  228. /// @dev Compares two `ModeSelector` values for equality.
  229. function eqModeSelector(ModeSelector a, ModeSelector b) pure returns (bool) {
  230. return ModeSelector.unwrap(a) == ModeSelector.unwrap(b);
  231. }
  232. /// @dev Compares two `ModePayload` values for equality.
  233. function eqModePayload(ModePayload a, ModePayload b) pure returns (bool) {
  234. return ModePayload.unwrap(a) == ModePayload.unwrap(b);
  235. }