Skip to content

Commit

Permalink
simplify permitted call mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
adamegyed committed May 29, 2024
1 parent 46a0f31 commit 30ce3cf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 20 deletions.
7 changes: 1 addition & 6 deletions src/account/AccountStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ struct AccountStorage {
mapping(address => PluginData) pluginData;
// Execution functions and their associated functions
mapping(bytes4 => SelectorData) selectorData;
// bytes24 key = address(calling plugin) || bytes4(selector of execution function)
mapping(bytes24 => bool) callPermitted;
mapping(address caller => mapping(bytes4 selector => bool)) callPermitted;
// key = address(calling plugin) || target address
mapping(IPlugin => mapping(address => PermittedExternalCallData)) permittedExternalCalls;
// For ERC165 introspection
Expand All @@ -77,10 +76,6 @@ function getAccountStorage() pure returns (AccountStorage storage _storage) {
}
}

function getPermittedCallKey(address addr, bytes4 selector) pure returns (bytes24) {
return bytes24(bytes20(addr)) | (bytes24(selector) >> 160);
}

using EnumerableSet for EnumerableSet.Bytes32Set;

function toSetValue(FunctionReference functionReference) pure returns (bytes32) {
Expand Down
5 changes: 2 additions & 3 deletions src/account/PluginManagerInternals.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
getAccountStorage,
SelectorData,
toSetValue,
getPermittedCallKey,
PermittedExternalCallData
} from "./AccountStorage.sol";

Expand Down Expand Up @@ -220,7 +219,7 @@ abstract contract PluginManagerInternals is IPluginManager {
for (uint256 i = 0; i < length; ++i) {
// If there are duplicates, this will just enable the flag again. This is not a problem, since the
// boolean will be set to false twice during uninstall, which is fine.
_storage.callPermitted[getPermittedCallKey(plugin, manifest.permittedExecutionSelectors[i])] = true;
_storage.callPermitted[plugin][manifest.permittedExecutionSelectors[i]] = true;
}

// Add the permitted external calls to the account.
Expand Down Expand Up @@ -417,7 +416,7 @@ abstract contract PluginManagerInternals is IPluginManager {

length = manifest.permittedExecutionSelectors.length;
for (uint256 i = 0; i < length; ++i) {
_storage.callPermitted[getPermittedCallKey(plugin, manifest.permittedExecutionSelectors[i])] = false;
_storage.callPermitted[plugin][manifest.permittedExecutionSelectors[i]] = false;
}

length = manifest.executionFunctions.length;
Expand Down
5 changes: 1 addition & 4 deletions src/account/UpgradeableModularAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {AccountLoupe} from "./AccountLoupe.sol";
import {
AccountStorage,
getAccountStorage,
getPermittedCallKey,
SelectorData,
toSetValue,
toFunctionReference,
Expand Down Expand Up @@ -187,11 +186,9 @@ contract UpgradeableModularAccount is
bytes4 selector = bytes4(data[:4]);
address callingPlugin = msg.sender;

bytes24 execFromPluginKey = getPermittedCallKey(callingPlugin, selector);

AccountStorage storage _storage = getAccountStorage();

if (!_storage.callPermitted[execFromPluginKey]) {
if (!_storage.callPermitted[callingPlugin][selector]) {
revert ExecFromPluginNotPermitted(callingPlugin, selector);
}

Expand Down
8 changes: 1 addition & 7 deletions test/libraries/AccountStorage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.19;

import {Test} from "forge-std/Test.sol";
import {_ACCOUNT_STORAGE_SLOT, getPermittedCallKey} from "../../src/account/AccountStorage.sol";
import {_ACCOUNT_STORAGE_SLOT} from "../../src/account/AccountStorage.sol";
import {AccountStorageInitializable} from "../../src/account/AccountStorageInitializable.sol";
import {MockDiamondStorageContract} from "../mocks/MockDiamondStorageContract.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
Expand Down Expand Up @@ -34,10 +34,4 @@ contract AccountStorageTest is Test {
// post init slot should contains: packed(uint8 initialized = 1, bool initializing = 0)
assertEq(uint256(vm.load(proxy, _ACCOUNT_STORAGE_SLOT)), uint256(1));
}

function testFuzz_permittedCallKey(address addr, bytes4 selector) public {
bytes24 key = getPermittedCallKey(addr, selector);
assertEq(bytes20(addr), bytes20(key));
assertEq(bytes4(selector), bytes4(key << 160));
}
}

0 comments on commit 30ce3cf

Please sign in to comment.