|
| 1 | +// SPDX-License-Identifier: GPL-3.0 |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol"; |
| 5 | +import {EnumerableMap} from "@openzeppelin/contracts/utils/structs/EnumerableMap.sol"; |
| 6 | +import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 7 | + |
| 8 | +import {IAccountLoupe} from "../interfaces/IAccountLoupe.sol"; |
| 9 | +import {IPluginManager} from "../interfaces/IPluginManager.sol"; |
| 10 | +import {IStandardExecutor} from "../interfaces/IStandardExecutor.sol"; |
| 11 | +import { |
| 12 | + AccountStorage, |
| 13 | + getAccountStorage, |
| 14 | + getPermittedCallKey, |
| 15 | + HookGroup, |
| 16 | + toFunctionReferenceArray |
| 17 | +} from "../libraries/AccountStorage.sol"; |
| 18 | +import {FunctionReference} from "../libraries/FunctionReferenceLib.sol"; |
| 19 | + |
| 20 | +abstract contract AccountLoupe is IAccountLoupe { |
| 21 | + using EnumerableMap for EnumerableMap.Bytes32ToUintMap; |
| 22 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 23 | + |
| 24 | + error ManifestDiscrepancy(address plugin); |
| 25 | + |
| 26 | + /// @inheritdoc IAccountLoupe |
| 27 | + function getExecutionFunctionConfig(bytes4 selector) |
| 28 | + external |
| 29 | + view |
| 30 | + returns (ExecutionFunctionConfig memory config) |
| 31 | + { |
| 32 | + AccountStorage storage _storage = getAccountStorage(); |
| 33 | + |
| 34 | + if ( |
| 35 | + selector == IStandardExecutor.execute.selector || selector == IStandardExecutor.executeBatch.selector |
| 36 | + || selector == UUPSUpgradeable.upgradeTo.selector |
| 37 | + || selector == UUPSUpgradeable.upgradeToAndCall.selector |
| 38 | + || selector == IPluginManager.installPlugin.selector |
| 39 | + || selector == IPluginManager.uninstallPlugin.selector |
| 40 | + ) { |
| 41 | + config.plugin = address(this); |
| 42 | + } else { |
| 43 | + config.plugin = _storage.selectorData[selector].plugin; |
| 44 | + } |
| 45 | + |
| 46 | + config.userOpValidationFunction = _storage.selectorData[selector].userOpValidation; |
| 47 | + |
| 48 | + config.runtimeValidationFunction = _storage.selectorData[selector].runtimeValidation; |
| 49 | + } |
| 50 | + |
| 51 | + /// @inheritdoc IAccountLoupe |
| 52 | + function getExecutionHooks(bytes4 selector) external view returns (ExecutionHooks[] memory execHooks) { |
| 53 | + execHooks = _getHooks(getAccountStorage().selectorData[selector].executionHooks); |
| 54 | + } |
| 55 | + |
| 56 | + /// @inheritdoc IAccountLoupe |
| 57 | + function getPermittedCallHooks(address callingPlugin, bytes4 selector) |
| 58 | + external |
| 59 | + view |
| 60 | + returns (ExecutionHooks[] memory execHooks) |
| 61 | + { |
| 62 | + bytes24 key = getPermittedCallKey(callingPlugin, selector); |
| 63 | + execHooks = _getHooks(getAccountStorage().permittedCalls[key].permittedCallHooks); |
| 64 | + } |
| 65 | + |
| 66 | + /// @inheritdoc IAccountLoupe |
| 67 | + function getPreValidationHooks(bytes4 selector) |
| 68 | + external |
| 69 | + view |
| 70 | + returns ( |
| 71 | + FunctionReference[] memory preUserOpValidationHooks, |
| 72 | + FunctionReference[] memory preRuntimeValidationHooks |
| 73 | + ) |
| 74 | + { |
| 75 | + preUserOpValidationHooks = |
| 76 | + toFunctionReferenceArray(getAccountStorage().selectorData[selector].preUserOpValidationHooks); |
| 77 | + preRuntimeValidationHooks = |
| 78 | + toFunctionReferenceArray(getAccountStorage().selectorData[selector].preRuntimeValidationHooks); |
| 79 | + } |
| 80 | + |
| 81 | + /// @inheritdoc IAccountLoupe |
| 82 | + function getInstalledPlugins() external view returns (address[] memory pluginAddresses) { |
| 83 | + pluginAddresses = getAccountStorage().plugins.values(); |
| 84 | + } |
| 85 | + |
| 86 | + function _getHooks(HookGroup storage hooks) internal view returns (ExecutionHooks[] memory execHooks) { |
| 87 | + uint256 preExecHooksLength = hooks.preHooks.length(); |
| 88 | + uint256 postOnlyExecHooksLength = hooks.postOnlyHooks.length(); |
| 89 | + uint256 maxExecHooksLength = postOnlyExecHooksLength; |
| 90 | + |
| 91 | + // There can only be as many associated post hooks to run as there are pre hooks. |
| 92 | + for (uint256 i = 0; i < preExecHooksLength;) { |
| 93 | + (, uint256 count) = hooks.preHooks.at(i); |
| 94 | + unchecked { |
| 95 | + maxExecHooksLength += (count + 1); |
| 96 | + ++i; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Overallocate on length - not all of this may get filled up. We set the correct length later. |
| 101 | + execHooks = new ExecutionHooks[](maxExecHooksLength); |
| 102 | + uint256 actualExecHooksLength; |
| 103 | + |
| 104 | + for (uint256 i = 0; i < preExecHooksLength;) { |
| 105 | + (bytes32 key,) = hooks.preHooks.at(i); |
| 106 | + FunctionReference preExecHook = FunctionReference.wrap(bytes21(key)); |
| 107 | + |
| 108 | + uint256 associatedPostExecHooksLength = hooks.associatedPostHooks[preExecHook].length(); |
| 109 | + if (associatedPostExecHooksLength > 0) { |
| 110 | + for (uint256 j = 0; j < associatedPostExecHooksLength;) { |
| 111 | + execHooks[actualExecHooksLength].preExecHook = preExecHook; |
| 112 | + (key,) = hooks.associatedPostHooks[preExecHook].at(j); |
| 113 | + execHooks[actualExecHooksLength].postExecHook = FunctionReference.wrap(bytes21(key)); |
| 114 | + |
| 115 | + unchecked { |
| 116 | + ++actualExecHooksLength; |
| 117 | + ++j; |
| 118 | + } |
| 119 | + } |
| 120 | + } else { |
| 121 | + execHooks[actualExecHooksLength].preExecHook = preExecHook; |
| 122 | + |
| 123 | + unchecked { |
| 124 | + ++actualExecHooksLength; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + unchecked { |
| 129 | + ++i; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + for (uint256 i = 0; i < postOnlyExecHooksLength;) { |
| 134 | + (bytes32 key,) = hooks.postOnlyHooks.at(i); |
| 135 | + execHooks[actualExecHooksLength].postExecHook = FunctionReference.wrap(bytes21(key)); |
| 136 | + |
| 137 | + unchecked { |
| 138 | + ++actualExecHooksLength; |
| 139 | + ++i; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Trim the exec hooks array to the actual length, since we may have overallocated. |
| 144 | + assembly ("memory-safe") { |
| 145 | + mstore(execHooks, actualExecHooksLength) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments