|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import {IERC7579Module, MODULE_TYPE_EXECUTOR} from "@openzeppelin/contracts/interfaces/draft-IERC7579.sol"; |
| 5 | +import {ERC7579Executor} from "./ERC7579Executor.sol"; |
| 6 | +import {ERC7579Utils} from "@openzeppelin/contracts/account/utils/draft-ERC7579Utils.sol"; |
| 7 | +import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @dev Implementation of an {ERC7579Executor} that allows authorizing specific function selectors |
| 11 | + * that can be executed on the account. |
| 12 | + * |
| 13 | + * This module provides a way to restrict which functions can be executed on the account by |
| 14 | + * maintaining a set of allowed function selectors. Only calls to functions with selectors |
| 15 | + * in the set will be allowed to execute. |
| 16 | + */ |
| 17 | +abstract contract ERC7579SelectorExecutor is ERC7579Executor { |
| 18 | + using EnumerableSet for EnumerableSet.Bytes32Set; |
| 19 | + |
| 20 | + /// @dev Emitted when a selector is added to the set |
| 21 | + event ERC7579ExecutorSelectorAuthorized(address indexed account, bytes4 selector); |
| 22 | + |
| 23 | + /// @dev Emitted when a selector is removed from the set |
| 24 | + event ERC7579ExecutorSelectorRemoved(address indexed account, bytes4 selector); |
| 25 | + |
| 26 | + /// @dev Error thrown when attempting to execute a non-authorized selector |
| 27 | + error ERC7579ExecutorSelectorNotAuthorized(bytes4 selector); |
| 28 | + |
| 29 | + /// @dev Mapping from account to set of authorized selectors |
| 30 | + mapping(address account => EnumerableSet.Bytes32Set) private _authorizedSelectors; |
| 31 | + |
| 32 | + /// @dev Returns whether a selector is authorized for the specified account |
| 33 | + function isAuthorized(address account, bytes4 selector) public view virtual returns (bool) { |
| 34 | + return _authorizedSelectors[account].contains(selector); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @dev Returns the set of authorized selectors for the specified account. |
| 39 | + * |
| 40 | + * WARNING: This operation copies the entire selectors set to memory, which |
| 41 | + * can be expensive or may result in unbounded computation. |
| 42 | + */ |
| 43 | + function selectors(address account) public view virtual returns (bytes4[] memory) { |
| 44 | + bytes32[] memory bytes32Selectors = _authorizedSelectors[account].values(); |
| 45 | + bytes4[] memory selectors_ = new bytes4[](bytes32Selectors.length); |
| 46 | + for (uint256 i = 0; i < bytes32Selectors.length; i++) { |
| 47 | + selectors_[i] = bytes4(bytes32Selectors[i]); |
| 48 | + } |
| 49 | + return selectors_; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dev Sets up the module's initial configuration when installed by an account. |
| 54 | + * The initData should be encoded as: `abi.encode(bytes4[] selectors)` |
| 55 | + */ |
| 56 | + function onInstall(bytes calldata initData) public virtual override { |
| 57 | + if (initData.length > 0) { |
| 58 | + bytes4[] memory selectors_ = abi.decode(initData, (bytes4[])); |
| 59 | + _addSelectors(msg.sender, selectors_); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dev Cleans up module's configuration when uninstalled from an account. |
| 65 | + * Clears all selectors. |
| 66 | + * |
| 67 | + * WARNING: This function has unbounded gas costs and may become uncallable if the set grows too large. |
| 68 | + * See {EnumerableSetExtended-clear}. |
| 69 | + */ |
| 70 | + function onUninstall(bytes calldata /* data */) public virtual override { |
| 71 | + _authorizedSelectors[msg.sender].clear(); |
| 72 | + } |
| 73 | + |
| 74 | + /// @dev Adds `selectors` to the set for the calling account |
| 75 | + function addSelectors(bytes4[] memory newSelectors) public virtual { |
| 76 | + _addSelectors(msg.sender, newSelectors); |
| 77 | + } |
| 78 | + |
| 79 | + /// @dev Removes a selector from the set for the calling account |
| 80 | + function removeSelectors(bytes4[] memory oldSelectors) public virtual { |
| 81 | + _removeSelectors(msg.sender, oldSelectors); |
| 82 | + } |
| 83 | + |
| 84 | + /// @dev Internal version of {addSelectors} that takes an `account` as argument |
| 85 | + function _addSelectors(address account, bytes4[] memory newSelectors) internal virtual { |
| 86 | + uint256 newSelectorsLength = newSelectors.length; |
| 87 | + for (uint256 i = 0; i < newSelectorsLength; i++) { |
| 88 | + if (_authorizedSelectors[account].add(newSelectors[i])) { |
| 89 | + emit ERC7579ExecutorSelectorAuthorized(account, newSelectors[i]); |
| 90 | + } // no-op if the selector is already in the set |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// @dev Internal version of {removeSelectors} that takes an `account` as argument |
| 95 | + function _removeSelectors(address account, bytes4[] memory oldSelectors) internal virtual { |
| 96 | + uint256 oldSelectorsLength = oldSelectors.length; |
| 97 | + for (uint256 i = 0; i < oldSelectorsLength; i++) { |
| 98 | + if (_authorizedSelectors[account].remove(oldSelectors[i])) { |
| 99 | + emit ERC7579ExecutorSelectorRemoved(account, oldSelectors[i]); |
| 100 | + } // no-op if the selector is not in the set |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @dev See {ERC7579Executor-_validateExecution}. |
| 106 | + * Validates that the selector (first 4 bytes of the actual callData) is authorized before execution. |
| 107 | + */ |
| 108 | + function _validateExecution( |
| 109 | + address account, |
| 110 | + bytes32 /* salt */, |
| 111 | + bytes32 /* mode */, |
| 112 | + bytes calldata data |
| 113 | + ) internal virtual override returns (bytes calldata) { |
| 114 | + // Decode ERC7579 single execution calldata to extract the actual function callData |
| 115 | + (, , bytes calldata callData) = ERC7579Utils.decodeSingle(data); |
| 116 | + |
| 117 | + bytes4 selector = bytes4(callData[0:4]); |
| 118 | + require(isAuthorized(account, selector), ERC7579ExecutorSelectorNotAuthorized(selector)); |
| 119 | + |
| 120 | + return data; |
| 121 | + } |
| 122 | +} |
0 commit comments