Skip to content
Merged
3 changes: 2 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
shamefully-hoist=true
shamefully-hoist=true
package-manager-strict=false
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"solady": "github:vectorized/solady"
},
"devDependencies": {
"solhint": "^5.0.1"
"solhint": "^5.0.3"
},
"files": [
"src",
Expand Down
999 changes: 481 additions & 518 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

32 changes: 30 additions & 2 deletions src/MSAAdvanced.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { RegistryAdapter } from "./core/RegistryAdapter.sol";
import { HashLib } from "./lib/HashLib.sol";
import { ECDSA } from "solady/utils/ECDSA.sol";
import { Initializable } from "./lib/Initializable.sol";
import { ERC7779Adapter } from "./core/ERC7779Adapter.sol";
import { SentinelListLib } from "sentinellist/SentinelList.sol";

/**
* @author zeroknots.eth | rhinestone.wtf
Expand All @@ -22,10 +24,18 @@ import { Initializable } from "./lib/Initializable.sol";
* This account implements ExecType: DEFAULT and TRY.
* Hook support is implemented
*/
contract MSAAdvanced is IMSA, ExecutionHelper, ModuleManager, HookManager, RegistryAdapter {
contract MSAAdvanced is
IMSA,
ExecutionHelper,
ModuleManager,
HookManager,
RegistryAdapter,
ERC7779Adapter
{
using ExecutionLib for bytes;
using ModeLib for ModeCode;
using ECDSA for bytes32;
using SentinelListLib for SentinelListLib.SentinelList;

/**
* @inheritdoc IERC7579Account
Expand Down Expand Up @@ -246,7 +256,6 @@ contract MSAAdvanced is IMSA, ExecutionHelper, ModuleManager, HookManager, Regis
if (signer != address(this)) {
return VALIDATION_FAILED;
}

return VALIDATION_SUCCESS;
} else {
return VALIDATION_FAILED;
Expand Down Expand Up @@ -361,6 +370,18 @@ contract MSAAdvanced is IMSA, ExecutionHelper, ModuleManager, HookManager, Regis

// checks if already initialized and reverts before setting the state to initialized
_initModuleManager();
bool isERC7702;
assembly {
isERC7702 :=
eq(
extcodehash(address()),
0xeadcdba66a79ab5dce91622d1d75c8cff5cff0b96944c3bf1072cd08ce018329 // (keccak256(0xef01))
)
}
if (isERC7702) {
_addStorageBase(MODULEMANAGER_STORAGE_LOCATION);
_addStorageBase(HOOKMANAGER_STORAGE_LOCATION);
}

// bootstrap the account
(address bootstrap, bytes memory bootstrapCall) = abi.decode(data, (address, bytes));
Expand All @@ -378,4 +399,11 @@ contract MSAAdvanced is IMSA, ExecutionHelper, ModuleManager, HookManager, Regis
(bool success,) = bootstrap.delegatecall(bootstrapCall);
if (!success) revert();
}

function _onRedelegation() internal override {
_tryUninstallValidators();
_tryUninstallExecutors();
_tryUninstallHook(_getHook());
_initModuleManager();
}
}
67 changes: 67 additions & 0 deletions src/core/ERC7779Adapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import { IERC7779 } from "../interfaces/IERC7779.sol";

abstract contract ERC7779Adapter is IERC7779 {
error NonAuthorizedOnRedelegationCaller();

// keccak256(abi.encode(uint256(keccak256(bytes("InteroperableDelegatedAccount.ERC.Storage"))) -
// 1)) & ~bytes32(uint256(0xff));
bytes32 internal constant ERC7779_STORAGE_BASE =
0xc473de86d0138e06e4d4918a106463a7cc005258d2e21915272bcb4594c18900;

struct ERC7779Storage {
bytes32[] storageBases;
}
/*
* @dev Externally shares the storage bases that has been used throughout the account.
* Majority of 7702 accounts will have their distinctive storage base to reduce the
chance of storage collision.
* This allows the external entities to know what the storage base is of the account.
* Wallets willing to redelegate already-delegated accounts should call
accountStorageBase() to check if it confirms with the account it plans to redelegate.
*
* The bytes32 array should be stored at the storage slot:
keccak(keccak('InteroperableDelegatedAccount.ERC.Storage')-1) & ~0xff
* This is an append-only array so newly redelegated accounts should not overwrite the
storage at this slot, but just append their base to the array.
* This append operation should be done during the initialization of the account.
*/

function accountStorageBases() external view returns (bytes32[] memory) {
ERC7779Storage storage $;
assembly {

Check warning on line 34 in src/core/ERC7779Adapter.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

Avoid to use inline assembly. It is acceptable only in rare cases
$.slot := ERC7779_STORAGE_BASE
}
return $.storageBases;
}

function _addStorageBase(bytes32 storageBase) internal {
ERC7779Storage storage $;
assembly {

Check warning on line 42 in src/core/ERC7779Adapter.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

Avoid to use inline assembly. It is acceptable only in rare cases
$.slot := ERC7779_STORAGE_BASE
}
$.storageBases.push(storageBase);
}

/*
* @dev Function called before redelegation.
* This function should prepare the account for a delegation to a different
implementation.
* This function could be triggered by the new wallet that wants to redelegate an already
delegated EOA.
* It should uninitialize storages if needed and execute wallet-specific logic to prepare
for redelegation.
* msg.sender should be the owner of the account.
*/
function onRedelegation() external returns (bool) {
require(msg.sender == address(this), NonAuthorizedOnRedelegationCaller());

Check warning on line 59 in src/core/ERC7779Adapter.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

GC: Use Custom Errors instead of require statements
_onRedelegation();
return true;
}

/// @dev This function is called before redelegation.
/// @dev Account should override this function to implement the specific logic.
function _onRedelegation() internal virtual;
}
12 changes: 12 additions & 0 deletions src/core/HookManager.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import "./ModuleManager.sol";

Check warning on line 4 in src/core/HookManager.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

global import of path ./ModuleManager.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "../interfaces/IERC7579Account.sol";

Check warning on line 5 in src/core/HookManager.sol

View workflow job for this annotation

GitHub Actions / lint / forge-lint

global import of path ../interfaces/IERC7579Account.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "../interfaces/IERC7579Module.sol";

/**
Expand All @@ -10,6 +10,8 @@
* @author zeroknots.eth | rhinestone.wtf
*/
abstract contract HookManager {
event HookUninstallFailed(address hook, bytes data);

/// @custom:storage-location erc7201:hookmanager.storage.msa
struct HookManagerStorage {
IHook _hook;
Expand Down Expand Up @@ -55,6 +57,16 @@
IHook(hook).onUninstall(data);
}

function _tryUninstallHook(address hook) internal virtual {
if (hook != address(0)) {
try IHook(hook).onUninstall("") { }
catch {
emit HookUninstallFailed(hook, "");
}
_setHook(address(0));
}
}

function _getHook() internal view returns (address _hook) {
bytes32 slot = HOOKMANAGER_STORAGE_LOCATION;
assembly {
Expand Down
71 changes: 71 additions & 0 deletions src/core/ModuleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ abstract contract ModuleManager is AccountBase, Receiver {
error NoFallbackHandler(bytes4 selector);
error CannotRemoveLastValidator();

event ValidatorUninstallFailed(address validator, bytes data);
event ExecutorUninstallFailed(address executor, bytes data);

// forgefmt: disable-next-line
// keccak256(abi.encode(uint256(keccak256("modulemanager.storage.msa")) - 1)) & ~bytes32(uint256(0xff));
bytes32 internal constant MODULEMANAGER_STORAGE_LOCATION =
Expand Down Expand Up @@ -92,6 +95,40 @@ abstract contract ModuleManager is AccountBase, Receiver {
IValidator(validator).onUninstall(disableModuleData);
}

/*
function _tryUninstallValidators(bytes[] calldata data) internal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this left commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, we dont clear storage so it can persist multiple delegations to this impl

SentinelListLib.SentinelList storage $valdiators = $moduleManager().$valdiators;
uint256 length = data.length;
uint256 index;
address validator = $valdiators.getNext(SENTINEL);
while (validator != SENTINEL) {
bytes memory uninstallData;
if (index < length) {
uninstallData = data[index];
}
try IValidator(validator).onUninstall(uninstallData) {} catch {
emit ValidatorUninstallFailed(validator, uninstallData);
}
validator = $valdiators.getNext(validator);
index++;
}
$valdiators.popAll();
}
*/

function _tryUninstallValidators() internal {
SentinelListLib.SentinelList storage $valdiators = $moduleManager().$valdiators;
address validator = $valdiators.getNext(SENTINEL);
while (validator != SENTINEL) {
try IValidator(validator).onUninstall("") { }
catch {
emit ValidatorUninstallFailed(validator, "");
}
validator = $valdiators.getNext(validator);
}
$valdiators.popAll();
}

function _isValidatorInstalled(address validator) internal view virtual returns (bool) {
SentinelListLib.SentinelList storage $valdiators = $moduleManager().$valdiators;
return $valdiators.contains(validator);
Expand Down Expand Up @@ -131,6 +168,40 @@ abstract contract ModuleManager is AccountBase, Receiver {
IExecutor(executor).onUninstall(disableModuleData);
}

/*
function _tryUninstallExecutors(bytes[] calldata data) internal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this left commented out?

SentinelListLib.SentinelList storage $executors = $moduleManager().$executors;
uint256 length = data.length;
uint256 index;
address executor = $executors.getNext(SENTINEL);
while (executor != SENTINEL) {
bytes memory uninstallData;
if (index < length) {
uninstallData = data[index];
}
try IExecutor(executor).onUninstall(uninstallData) {} catch {
emit ExecutorUninstallFailed(executor, uninstallData);
}
executor = $executors.getNext(executor);
index++;
}
$executors.popAll();
}
*/

function _tryUninstallExecutors() internal {
SentinelListLib.SentinelList storage $executors = $moduleManager().$executors;
address executor = $executors.getNext(SENTINEL);
while (executor != SENTINEL) {
try IExecutor(executor).onUninstall("") { }
catch {
emit ExecutorUninstallFailed(executor, "");
}
executor = $executors.getNext(executor);
}
$executors.popAll();
}

function _isExecutorInstalled(address executor) internal view virtual returns (bool) {
SentinelListLib.SentinelList storage $executors = $moduleManager().$executors;
return $executors.contains(executor);
Expand Down
32 changes: 32 additions & 0 deletions src/interfaces/IERC7779.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

interface IERC7779 {
/*
* @dev Externally shares the storage bases that has been used throughout the account.
* Majority of 7702 accounts will have their distinctive storage base to reduce the
chance of storage collision.
* This allows the external entities to know what the storage base is of the account.
* Wallets willing to redelegate already-delegated accounts should call
accountStorageBase() to check if it confirms with the account it plans to redelegate.
*
* The bytes32 array should be stored at the storage slot:
keccak(keccak('InteroperableDelegatedAccount.ERC.Storage')-1) & ~0xff
* This is an append-only array so newly redelegated accounts should not overwrite the
storage at this slot, but just append their base to the array.
* This append operation should be done during the initialization of the account.
*/
function accountStorageBases() external view returns (bytes32[] memory);

/*
* @dev Function called before redelegation.
* This function should prepare the account for a delegation to a different
implementation.
* This function could be triggered by the new wallet that wants to redelegate an already
delegated EOA.
* It should uninitialize storages if needed and execute wallet-specific logic to prepare
for redelegation.
* msg.sender should be the owner of the account.
*/
function onRedelegation() external returns (bool);
}
3 changes: 2 additions & 1 deletion src/interfaces/IMSA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ pragma solidity ^0.8.21;

import { IERC7579Account } from "./IERC7579Account.sol";
import { IERC4337Account } from "./IERC4337Account.sol";
import { IERC7779 } from "./IERC7779.sol";

import { CallType, ExecType, ModeCode } from "../lib/ModeLib.sol";

interface IMSA is IERC7579Account, IERC4337Account {
interface IMSA is IERC7579Account, IERC4337Account, IERC7779 {
// Error thrown when an unsupported ModuleType is requested
error UnsupportedModuleType(uint256 moduleTypeId);
// Error thrown when an execution with an unsupported CallType was made
Expand Down
48 changes: 48 additions & 0 deletions test/advanced/EIP7702.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import {
import "./TestBaseUtilAdvanced.t.sol";
import { HashLib } from "src/lib/HashLib.sol";
import { ECDSA } from "solady/utils/ECDSA.sol";
import {
MODULE_TYPE_VALIDATOR,
MODULE_TYPE_EXECUTOR,
MODULE_TYPE_HOOK
} from "src/core/ModuleManager.sol";
import { MockHook } from "../mocks/MockHook.sol";

contract EIP7702 is TestBaseUtilAdvanced {
using ECDSA for bytes32;
Expand Down Expand Up @@ -284,4 +290,46 @@ contract EIP7702 is TestBaseUtilAdvanced {
// Assert that the value was set ie that execution was successful
assertTrue(valueTarget.balance == value);
}

function test_onRedelegation() public {
address account = test_initializeAndExecSingle();

MockHook hook = new MockHook();

vm.prank(address(entrypoint));
IMSA(account).installModule(MODULE_TYPE_HOOK, address(hook), "");

assertTrue(
IMSA(account).isModuleInstalled(MODULE_TYPE_VALIDATOR, address(defaultValidator), "")
);
assertTrue(
IMSA(account).isModuleInstalled(MODULE_TYPE_EXECUTOR, address(defaultExecutor), "")
);
assertTrue(IMSA(account).isModuleInstalled(MODULE_TYPE_HOOK, address(hook), ""));
// storage is cleared
vm.prank(address(account));
IMSA(account).onRedelegation();
assertFalse(
IMSA(account).isModuleInstalled(MODULE_TYPE_VALIDATOR, address(defaultValidator), "")
);
assertFalse(
IMSA(account).isModuleInstalled(MODULE_TYPE_EXECUTOR, address(defaultExecutor), "")
);
assertFalse(IMSA(account).isModuleInstalled(MODULE_TYPE_HOOK, address(hook), ""));

// account is properly initialized to install modules again
vm.startPrank(address(entrypoint));
IMSA(account).installModule(MODULE_TYPE_VALIDATOR, address(defaultValidator), "");
IMSA(account).installModule(MODULE_TYPE_EXECUTOR, address(defaultExecutor), "");
IMSA(account).installModule(MODULE_TYPE_HOOK, address(hook), "");

vm.stopPrank();
assertTrue(
IMSA(account).isModuleInstalled(MODULE_TYPE_VALIDATOR, address(defaultValidator), "")
);
assertTrue(
IMSA(account).isModuleInstalled(MODULE_TYPE_EXECUTOR, address(defaultExecutor), "")
);
assertTrue(IMSA(account).isModuleInstalled(MODULE_TYPE_HOOK, address(hook), ""));
}
}
Loading
Loading