From 55dd96aca26abb7018acd1941c30e70c22d969fb Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Wed, 5 Jun 2024 11:59:56 +0100 Subject: [PATCH 01/11] fix: kernel validation selector --- src/test/ModuleKitUserOp.sol | 3 --- src/test/utils/ERC7579Helpers.sol | 32 +++++++++------------------ src/test/utils/KernelHelpers.sol | 36 ++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/test/ModuleKitUserOp.sol b/src/test/ModuleKitUserOp.sol index 8d31d81f..3e8d37f6 100644 --- a/src/test/ModuleKitUserOp.sol +++ b/src/test/ModuleKitUserOp.sol @@ -56,7 +56,6 @@ library ModuleKitUserOp { address txValidator ) internal - view returns (UserOpData memory userOpData) { bytes memory erc7579ExecCall = ERC7579Helpers.encode(target, value, callData); @@ -73,7 +72,6 @@ library ModuleKitUserOp { address txValidator ) internal - view returns (UserOpData memory userOpData) { bytes memory erc7579ExecCall = ERC7579Helpers.encode(executions); @@ -92,7 +90,6 @@ library ModuleKitUserOp { address txValidator ) internal - view returns (UserOpData memory userOpData) { Execution[] memory executions = ERC7579Helpers.toExecutions(targets, values, callDatas); diff --git a/src/test/utils/ERC7579Helpers.sol b/src/test/utils/ERC7579Helpers.sol index 89dfd51a..417f00f2 100644 --- a/src/test/utils/ERC7579Helpers.sol +++ b/src/test/utils/ERC7579Helpers.sol @@ -102,12 +102,7 @@ library ERC7579Helpers { userOp = PackedUserOperation({ sender: instance.account, - nonce: getNonce( - instance.account, - instance.aux.entrypoint, - txValidator, - address(instance.defaultValidator) - ), + nonce: getNonce(instance, callData, txValidator), initCode: initCode, callData: callData, accountGasLimits: bytes32(abi.encodePacked(uint128(2e6), uint128(2e6))), @@ -126,7 +121,6 @@ library ERC7579Helpers { address txValidator ) internal - view returns (PackedUserOperation memory userOp, bytes32 userOpHash) { bytes memory initCode; @@ -145,12 +139,7 @@ library ERC7579Helpers { userOp = PackedUserOperation({ sender: instance.account, - nonce: getNonce( - instance.account, - instance.aux.entrypoint, - txValidator, - address(instance.defaultValidator) - ), + nonce: getNonce(instance, callData, txValidator), initCode: initCode, callData: callData, accountGasLimits: bytes32(abi.encodePacked(uint128(2e6), uint128(2e6))), @@ -461,27 +450,26 @@ library ERC7579Helpers { } function getNonce( - address account, - IEntryPoint entrypoint, - address validator, - address defaultValidator + AccountInstance memory instance, + bytes memory callData, + address txValidator ) internal - view returns (uint256 nonce) { AccountType env = getAccountType(); if (env == AccountType.KERNEL) { ValidationType vType; - if (validator == defaultValidator) { + if (txValidator == address(instance.defaultValidator)) { vType = VALIDATION_TYPE_ROOT; } else { + KernelHelpers.enableValidator(instance, callData, txValidator); vType = VALIDATION_TYPE_VALIDATOR; } - nonce = KernelHelpers.encodeNonce(vType, false, account, defaultValidator); + nonce = KernelHelpers.encodeNonce(vType, false, instance.account, txValidator); } else { - uint192 key = uint192(bytes24(bytes20(address(validator)))); - nonce = entrypoint.getNonce(address(account), key); + uint192 key = uint192(bytes24(bytes20(address(txValidator)))); + nonce = instance.aux.entrypoint.getNonce(address(instance.account), key); } } } diff --git a/src/test/utils/KernelHelpers.sol b/src/test/utils/KernelHelpers.sol index 61cda673..e1954498 100644 --- a/src/test/utils/KernelHelpers.sol +++ b/src/test/utils/KernelHelpers.sol @@ -1,12 +1,24 @@ pragma solidity ^0.8.23; import { ValidatorLib } from "kernel/utils/ValidationTypeLib.sol"; -import { ValidationType, ValidationMode } from "kernel/types/Types.sol"; +import { ValidationType, ValidationMode, ValidationId } from "kernel/types/Types.sol"; import "kernel/types/Constants.sol"; import { ENTRYPOINT_ADDR } from "../predeploy/EntryPoint.sol"; import { IEntryPoint } from "kernel/interfaces/IEntryPoint.sol"; import { IERC7579Account } from "erc7579/interfaces/IERC7579Account.sol"; import { MockFallback } from "kernel/mock/MockFallback.sol"; +import { Kernel } from "kernel/Kernel.sol"; +import { AccountInstance } from "../RhinestoneModuleKit.sol"; +import { IValidator } from "kernel/interfaces/IERC7579Modules.sol"; +import { etch } from "./Vm.sol"; + +contract SetSelector is Kernel { + constructor(IEntryPoint _entrypoint) Kernel(_entrypoint) { } + + function setSelector(ValidationId vId, bytes4 selector, bool allowed) external { + _setSelector(vId, selector, allowed); + } +} library KernelHelpers { function encodeNonce( @@ -39,6 +51,28 @@ library KernelHelpers { return IEntryPoint(ENTRYPOINT_ADDR).getNonce(account, nonceKey); } + function enableValidator( + AccountInstance memory instance, + bytes memory callData, + address txValidator + ) + internal + { + ValidationId vId = ValidatorLib.validatorToIdentifier(IValidator(txValidator)); + bytes4 selector; + assembly { + selector := mload(add(callData, 32)) + } + bool isAllowedSelector = Kernel(payable(instance.account)).isAllowedSelector(vId, selector); + if (!isAllowedSelector) { + bytes memory accountCode = instance.account.code; + address _setSelector = address(new SetSelector(IEntryPoint(ENTRYPOINT_ADDR))); + etch(instance.account, _setSelector.code); + SetSelector(payable(instance.account)).setSelector(vId, selector, true); + etch(instance.account, accountCode); + } + } + /** * @dev * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L311-L321 From 4419be91174b148adc55828c53c38eeaa2604d08 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Wed, 5 Jun 2024 14:56:04 +0100 Subject: [PATCH 02/11] fix: safe7579 uninstall hook --- src/test/utils/ERC7579Helpers.sol | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/utils/ERC7579Helpers.sol b/src/test/utils/ERC7579Helpers.sol index 417f00f2..e749ccf5 100644 --- a/src/test/utils/ERC7579Helpers.sol +++ b/src/test/utils/ERC7579Helpers.sol @@ -348,8 +348,16 @@ library ERC7579Helpers { pure returns (bytes memory callData) { - callData = - abi.encodeCall(IERC7579Account.uninstallModule, (MODULE_TYPE_HOOK, hook, initData)); + AccountType env = getAccountType(); + if (env == AccountType.SAFE) { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, + (MODULE_TYPE_HOOK, hook, abi.encode(HookType.GLOBAL, bytes4(0x0), initData)) + ); + } else { + callData = + abi.encodeCall(IERC7579Account.uninstallModule, (MODULE_TYPE_HOOK, hook, initData)); + } } /** From bfb2c1be9b9f16a5c6ea43b9dbf2b2038b0c2316 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Wed, 5 Jun 2024 14:57:19 +0100 Subject: [PATCH 03/11] fix: change func to view --- src/test/utils/ERC7579Helpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/utils/ERC7579Helpers.sol b/src/test/utils/ERC7579Helpers.sol index e749ccf5..a4ecc49b 100644 --- a/src/test/utils/ERC7579Helpers.sol +++ b/src/test/utils/ERC7579Helpers.sol @@ -345,7 +345,7 @@ library ERC7579Helpers { bytes memory initData ) internal - pure + view returns (bytes memory callData) { AccountType env = getAccountType(); From 3308800be5d0907954957b79a289df10fbec1a76 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 11:18:17 +0100 Subject: [PATCH 04/11] fix: fallback --- src/test/utils/ERC7579Helpers.sol | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/test/utils/ERC7579Helpers.sol b/src/test/utils/ERC7579Helpers.sol index a4ecc49b..41f53a64 100644 --- a/src/test/utils/ERC7579Helpers.sol +++ b/src/test/utils/ERC7579Helpers.sol @@ -18,6 +18,7 @@ import { KernelHelpers } from "./KernelHelpers.sol"; import { getAccountType, AccountType } from "src/accounts/MultiAccountHelpers.sol"; import { HookType } from "safe7579/DataTypes.sol"; import { SafeHelpers } from "./SafeHelpers.sol"; +import { CALLTYPE_STATIC } from "safe7579/lib/ModeLib.sol"; interface IAccountModulesPaginated { function getValidatorPaginated( @@ -372,6 +373,21 @@ library ERC7579Helpers { pure returns (bytes memory callData) { + AccountType env = getAccountType(); + if (env == AccountType.SAFE) { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, + ( + MODULE_TYPE_FALLBACK, + fallbackHandler, + abi.encode(bytes4(0x0), CALLTYPE_STATIC, initData) + ) + ); + } else { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) + ); + } callData = abi.encodeCall( IERC7579Account.installModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) ); @@ -389,10 +405,17 @@ library ERC7579Helpers { pure returns (bytes memory callData) { - fallbackHandler = fallbackHandler; //avoid solhint-no-unused-vars - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) - ); + AccountType env = getAccountType(); + if (env == AccountType.SAFE) { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, + (MODULE_TYPE_FALLBACK, fallbackHandler, abi.encode(bytes4(0x0), initData)) + ); + } else { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) + ); + } } /** From 85d5a2176c77040c4006d74a5668d7de6fa8be9d Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 16:41:47 +0100 Subject: [PATCH 05/11] feat: merge from main --- src/test/helpers/HelperBase.sol | 43 +++----------------- src/test/helpers/KernelHelpers.sol | 40 ++++++++++++++++-- src/test/helpers/SafeHelpers.sol | 65 +++++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 41 deletions(-) diff --git a/src/test/helpers/HelperBase.sol b/src/test/helpers/HelperBase.sol index e935949e..1c9e09d3 100644 --- a/src/test/helpers/HelperBase.sol +++ b/src/test/helpers/HelperBase.sol @@ -243,16 +243,8 @@ abstract contract HelperBase { virtual returns (bytes memory callData) { - AccountType env = getAccountType(); - if (env == AccountType.SAFE) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, - (MODULE_TYPE_HOOK, hook, abi.encode(HookType.GLOBAL, bytes4(0x0), initData)) - ); - } else { - callData = - abi.encodeCall(IERC7579Account.uninstallModule, (MODULE_TYPE_HOOK, hook, initData)); - } + callData = + abi.encodeCall(IERC7579Account.uninstallModule, (MODULE_TYPE_HOOK, hook, initData)); } /** @@ -268,21 +260,6 @@ abstract contract HelperBase { virtual returns (bytes memory callData) { - AccountType env = getAccountType(); - if (env == AccountType.SAFE) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, - ( - MODULE_TYPE_FALLBACK, - fallbackHandler, - abi.encode(bytes4(0x0), CALLTYPE_STATIC, initData) - ) - ); - } else { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) - ); - } callData = abi.encodeCall( IERC7579Account.installModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) ); @@ -301,17 +278,10 @@ abstract contract HelperBase { virtual returns (bytes memory callData) { - AccountType env = getAccountType(); - if (env == AccountType.SAFE) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, - (MODULE_TYPE_FALLBACK, fallbackHandler, abi.encode(bytes4(0x0), initData)) - ); - } else { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) - ); - } + fallbackHandler = fallbackHandler; //avoid solhint-no-unused-vars + callData = abi.encodeCall( + IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) + ); } /** @@ -389,7 +359,6 @@ abstract contract HelperBase { address txValidator ) public - view virtual returns (uint256 nonce) { diff --git a/src/test/helpers/KernelHelpers.sol b/src/test/helpers/KernelHelpers.sol index 47680172..b8789f7f 100644 --- a/src/test/helpers/KernelHelpers.sol +++ b/src/test/helpers/KernelHelpers.sol @@ -1,23 +1,34 @@ +// SPDX-License-Identifier: MIT pragma solidity ^0.8.23; import { AccountInstance } from "../RhinestoneModuleKit.sol"; import { ValidatorLib } from "kernel/utils/ValidationTypeLib.sol"; -import { ValidationType, ValidationMode } from "kernel/types/Types.sol"; +import { ValidationType, ValidationMode, ValidationId } from "kernel/types/Types.sol"; import "kernel/types/Constants.sol"; import { ENTRYPOINT_ADDR } from "../predeploy/EntryPoint.sol"; import { IEntryPoint } from "kernel/interfaces/IEntryPoint.sol"; import { IERC7579Account } from "erc7579/interfaces/IERC7579Account.sol"; import { MockFallback } from "kernel/mock/MockFallback.sol"; import { HelperBase } from "./HelperBase.sol"; +import { Kernel } from "kernel/Kernel.sol"; +import { etch } from "../utils/Vm.sol"; +import { IValidator } from "kernel/interfaces/IERC7579Modules.sol"; + +contract SetSelector is Kernel { + constructor(IEntryPoint _entrypoint) Kernel(_entrypoint) { } + + function setSelector(ValidationId vId, bytes4 selector, bool allowed) external { + _setSelector(vId, selector, allowed); + } +} contract KernelHelpers is HelperBase { function getNonce( AccountInstance memory instance, - bytes memory, + bytes memory callData, address txValidator ) public - view virtual override returns (uint256 nonce) @@ -26,6 +37,7 @@ contract KernelHelpers is HelperBase { if (txValidator == address(instance.defaultValidator)) { vType = VALIDATION_TYPE_ROOT; } else { + enableValidator(instance, callData, txValidator); vType = VALIDATION_TYPE_VALIDATOR; } nonce = encodeNonce(vType, false, instance.account, txValidator); @@ -96,6 +108,28 @@ contract KernelHelpers is HelperBase { return IEntryPoint(ENTRYPOINT_ADDR).getNonce(account, nonceKey); } + function enableValidator( + AccountInstance memory instance, + bytes memory callData, + address txValidator + ) + internal + { + ValidationId vId = ValidatorLib.validatorToIdentifier(IValidator(txValidator)); + bytes4 selector; + assembly { + selector := mload(add(callData, 32)) + } + bool isAllowedSelector = Kernel(payable(instance.account)).isAllowedSelector(vId, selector); + if (!isAllowedSelector) { + bytes memory accountCode = instance.account.code; + address _setSelector = address(new SetSelector(IEntryPoint(ENTRYPOINT_ADDR))); + etch(instance.account, _setSelector.code); + SetSelector(payable(instance.account)).setSelector(vId, selector, true); + etch(instance.account, accountCode); + } + } + /** * @dev * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L311-L321 diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index ca1a10a7..b7c7c3a2 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -10,11 +10,13 @@ import { IERC7579Account, MODULE_TYPE_HOOK, MODULE_TYPE_VALIDATOR, - MODULE_TYPE_EXECUTOR + MODULE_TYPE_EXECUTOR, + MODULE_TYPE_FALLBACK } from "../../external/ERC7579.sol"; import { HookType } from "safe7579/DataTypes.sol"; import { IAccountFactory } from "src/accounts/interface/IAccountFactory.sol"; import { IAccountModulesPaginated } from "./interfaces/IAccountModulesPaginated.sol"; +import { CALLTYPE_STATIC } from "safe7579/lib/ModeLib.sol"; contract SafeHelpers is HelperBase { /** @@ -109,6 +111,67 @@ contract SafeHelpers is HelperBase { ); } + /** + * get callData to uninstall hook on ERC7579 Account + */ + function uninstallHook( + address, /* account */ + address hook, + bytes memory initData + ) + public + pure + virtual + override + returns (bytes memory callData) + { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, + (MODULE_TYPE_HOOK, hook, abi.encode(HookType.GLOBAL, bytes4(0x0), initData)) + ); + } + + function installFallback( + address, /* account */ + address fallbackHandler, + bytes memory initData + ) + public + pure + virtual + override + returns (bytes memory callData) + { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, + ( + MODULE_TYPE_FALLBACK, + fallbackHandler, + abi.encode(bytes4(0x0), CALLTYPE_STATIC, initData) + ) + ); + } + + /** + * get callData to uninstall fallback on ERC7579 Account + */ + function uninstallFallback( + address, /* account */ + address fallbackHandler, + bytes memory initData + ) + public + pure + virtual + override + returns (bytes memory callData) + { + callData = abi.encodeCall( + IERC7579Account.uninstallModule, + (MODULE_TYPE_FALLBACK, fallbackHandler, abi.encode(bytes4(0x0), initData)) + ); + } + function execUserOp( AccountInstance memory instance, bytes memory callData, From 66710e3b14c63275e881a93ea4a42034618bb258 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 16:42:04 +0100 Subject: [PATCH 06/11] chore: remove unused code --- src/test/helpers/HelperBase.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/helpers/HelperBase.sol b/src/test/helpers/HelperBase.sol index 1c9e09d3..b8a5cb25 100644 --- a/src/test/helpers/HelperBase.sol +++ b/src/test/helpers/HelperBase.sol @@ -278,7 +278,6 @@ abstract contract HelperBase { virtual returns (bytes memory callData) { - fallbackHandler = fallbackHandler; //avoid solhint-no-unused-vars callData = abi.encodeCall( IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) ); From 30e944fd65820dfb417432709598e7713e60e114 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 17:53:00 +0100 Subject: [PATCH 07/11] fix: fallback install --- src/test/helpers/SafeHelpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index b7c7c3a2..fa6b3ee1 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -143,7 +143,7 @@ contract SafeHelpers is HelperBase { returns (bytes memory callData) { callData = abi.encodeCall( - IERC7579Account.uninstallModule, + IERC7579Account.installModule, ( MODULE_TYPE_FALLBACK, fallbackHandler, From 92e35eb53e16cac9dd5d2e77ba250a7f2e22056d Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 18:03:40 +0100 Subject: [PATCH 08/11] fix: getInstallModuleData --- src/test/helpers/HelperBase.sol | 36 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/test/helpers/HelperBase.sol b/src/test/helpers/HelperBase.sol index b8a5cb25..578083af 100644 --- a/src/test/helpers/HelperBase.sol +++ b/src/test/helpers/HelperBase.sol @@ -395,9 +395,9 @@ abstract contract HelperBase { } function getInstallModuleData( - AccountInstance memory, - uint256, - address, + AccountInstance memory instance, + uint256 moduleType, + address module, bytes memory data ) public @@ -405,13 +405,23 @@ abstract contract HelperBase { virtual returns (bytes memory) { - return data; + if (moduleType == MODULE_TYPE_VALIDATOR) { + return installValidator(instance.account, module, data); + } else if (moduleType == MODULE_TYPE_EXECUTOR) { + return installExecutor(instance.account, module, data); + } else if (moduleType == MODULE_TYPE_HOOK) { + return installHook(instance.account, module, data); + } else if (moduleType == MODULE_TYPE_FALLBACK) { + return installFallback(instance.account, module, data); + } else { + revert("Invalid module type"); + } } function getUninstallModuleData( - AccountInstance memory, - uint256, - address, + AccountInstance memory instance, + uint256 moduleType, + address module, bytes memory data ) public @@ -419,6 +429,16 @@ abstract contract HelperBase { virtual returns (bytes memory) { - return data; + if (moduleType == MODULE_TYPE_VALIDATOR) { + return uninstallValidator(instance.account, module, data); + } else if (moduleType == MODULE_TYPE_EXECUTOR) { + return uninstallExecutor(instance.account, module, data); + } else if (moduleType == MODULE_TYPE_HOOK) { + return uninstallHook(instance.account, module, data); + } else if (moduleType == MODULE_TYPE_FALLBACK) { + return uninstallFallback(instance.account, module, data); + } else { + revert("Invalid module type"); + } } } From 15ba587f3564f2af6c76148eb74b123743410b17 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 18:28:36 +0100 Subject: [PATCH 09/11] fix: get module data --- src/test/helpers/ERC7579Helpers.sol | 20 ++---- src/test/helpers/HelperBase.sol | 95 +++++++++++++---------------- src/test/helpers/KernelHelpers.sol | 35 ----------- src/test/helpers/SafeHelpers.sol | 60 ++++++------------ 4 files changed, 66 insertions(+), 144 deletions(-) diff --git a/src/test/helpers/ERC7579Helpers.sol b/src/test/helpers/ERC7579Helpers.sol index ed9ae5d0..3062ff27 100644 --- a/src/test/helpers/ERC7579Helpers.sol +++ b/src/test/helpers/ERC7579Helpers.sol @@ -14,7 +14,7 @@ contract ERC7579Helpers is HelperBase { /** * get callData to uninstall validator on ERC7579 Account */ - function uninstallValidator( + function getUninstallValidatorData( address account, address validator, bytes memory initData @@ -23,7 +23,7 @@ contract ERC7579Helpers is HelperBase { view virtual override - returns (bytes memory callData) + returns (bytes memory data) { // get previous validator in sentinel list address previous; @@ -40,17 +40,13 @@ contract ERC7579Helpers is HelperBase { if (array[i] == validator) previous = array[i - 1]; } } - initData = abi.encode(previous, initData); - - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_VALIDATOR, validator, initData) - ); + data = abi.encode(previous, initData); } /** * get callData to uninstall executor on ERC7579 Account */ - function uninstallExecutor( + function getUninstallExecutorData( address account, address executor, bytes memory initData @@ -59,7 +55,7 @@ contract ERC7579Helpers is HelperBase { view virtual override - returns (bytes memory callData) + returns (bytes memory data) { // get previous executor in sentinel list address previous; @@ -76,10 +72,6 @@ contract ERC7579Helpers is HelperBase { if (array[i] == executor) previous = array[i - 1]; } } - initData = abi.encode(previous, initData); - - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_EXECUTOR, executor, initData) - ); + data = abi.encode(previous, initData); } } diff --git a/src/test/helpers/HelperBase.sol b/src/test/helpers/HelperBase.sol index 578083af..46ff8a94 100644 --- a/src/test/helpers/HelperBase.sol +++ b/src/test/helpers/HelperBase.sol @@ -103,16 +103,17 @@ abstract contract HelperBase { returns (bytes memory callData) { if (moduleType == MODULE_TYPE_VALIDATOR) { - return installValidator(account, module, initData); + initData = getInstallValidatorData(account, module, initData); } else if (moduleType == MODULE_TYPE_EXECUTOR) { - return installExecutor(account, module, initData); + initData = getInstallExecutorData(account, module, initData); } else if (moduleType == MODULE_TYPE_HOOK) { - return installHook(account, module, initData); + initData = getInstallHookData(account, module, initData); } else if (moduleType == MODULE_TYPE_FALLBACK) { - return installFallback(account, module, initData); + initData = getInstallFallbackData(account, module, initData); } else { revert("Invalid module type"); } + callData = abi.encodeCall(IERC7579Account.installModule, (moduleType, module, initData)); } /** @@ -130,22 +131,23 @@ abstract contract HelperBase { returns (bytes memory callData) { if (moduleType == MODULE_TYPE_VALIDATOR) { - return uninstallValidator(account, module, initData); + initData = getUninstallValidatorData(account, module, initData); } else if (moduleType == MODULE_TYPE_EXECUTOR) { - return uninstallExecutor(account, module, initData); + initData = getUninstallExecutorData(account, module, initData); } else if (moduleType == MODULE_TYPE_HOOK) { - return uninstallHook(account, module, initData); + initData = getUninstallHookData(account, module, initData); } else if (moduleType == MODULE_TYPE_FALLBACK) { - return uninstallFallback(account, module, initData); + initData = getUninstallFallbackData(account, module, initData); } else { revert("Invalid module type"); } + callData = abi.encodeCall(IERC7579Account.uninstallModule, (moduleType, module, initData)); } /** * get callData to install validator on ERC7579 Account */ - function installValidator( + function getInstallValidatorData( address, /* account */ address validator, bytes memory initData @@ -153,17 +155,15 @@ abstract contract HelperBase { public pure virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.installModule, (MODULE_TYPE_VALIDATOR, validator, initData) - ); + data = initData; } /** * get callData to uninstall validator on ERC7579 Account */ - function uninstallValidator( + function getUninstallValidatorData( address account, address validator, bytes memory initData @@ -171,17 +171,15 @@ abstract contract HelperBase { public view virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_VALIDATOR, validator, initData) - ); + data = initData; } /** * get callData to install executor on ERC7579 Account */ - function installExecutor( + function getInstallExecutorData( address, /* account */ address executor, bytes memory initData @@ -189,17 +187,15 @@ abstract contract HelperBase { public pure virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.installModule, (MODULE_TYPE_EXECUTOR, executor, initData) - ); + data = initData; } /** * get callData to uninstall executor on ERC7579 Account */ - function uninstallExecutor( + function getUninstallExecutorData( address account, address executor, bytes memory initData @@ -207,17 +203,15 @@ abstract contract HelperBase { public view virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_EXECUTOR, executor, initData) - ); + data = initData; } /** * get callData to install hook on ERC7579 Account */ - function installHook( + function getInstallHookData( address, /* account */ address hook, bytes memory initData @@ -225,15 +219,15 @@ abstract contract HelperBase { public view virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall(IERC7579Account.installModule, (MODULE_TYPE_HOOK, hook, initData)); + data = initData; } /** * get callData to uninstall hook on ERC7579 Account */ - function uninstallHook( + function getUninstallHookData( address, /* account */ address hook, bytes memory initData @@ -241,16 +235,15 @@ abstract contract HelperBase { public pure virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = - abi.encodeCall(IERC7579Account.uninstallModule, (MODULE_TYPE_HOOK, hook, initData)); + data = initData; } /** * get callData to install fallback on ERC7579 Account */ - function installFallback( + function getInstallFallbackData( address, /* account */ address fallbackHandler, bytes memory initData @@ -258,17 +251,15 @@ abstract contract HelperBase { public pure virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.installModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) - ); + data = initData; } /** * get callData to uninstall fallback on ERC7579 Account */ - function uninstallFallback( + function getUninstallFallbackData( address, /* account */ address fallbackHandler, bytes memory initData @@ -276,11 +267,9 @@ abstract contract HelperBase { public pure virtual - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_FALLBACK, fallbackHandler, initData) - ); + data = initData; } /** @@ -406,13 +395,13 @@ abstract contract HelperBase { returns (bytes memory) { if (moduleType == MODULE_TYPE_VALIDATOR) { - return installValidator(instance.account, module, data); + return getInstallValidatorData(instance.account, module, data); } else if (moduleType == MODULE_TYPE_EXECUTOR) { - return installExecutor(instance.account, module, data); + return getInstallExecutorData(instance.account, module, data); } else if (moduleType == MODULE_TYPE_HOOK) { - return installHook(instance.account, module, data); + return getInstallHookData(instance.account, module, data); } else if (moduleType == MODULE_TYPE_FALLBACK) { - return installFallback(instance.account, module, data); + return getInstallFallbackData(instance.account, module, data); } else { revert("Invalid module type"); } @@ -430,13 +419,13 @@ abstract contract HelperBase { returns (bytes memory) { if (moduleType == MODULE_TYPE_VALIDATOR) { - return uninstallValidator(instance.account, module, data); + return getUninstallValidatorData(instance.account, module, data); } else if (moduleType == MODULE_TYPE_EXECUTOR) { - return uninstallExecutor(instance.account, module, data); + return getUninstallExecutorData(instance.account, module, data); } else if (moduleType == MODULE_TYPE_HOOK) { - return uninstallHook(instance.account, module, data); + return getUninstallHookData(instance.account, module, data); } else if (moduleType == MODULE_TYPE_FALLBACK) { - return uninstallFallback(instance.account, module, data); + return getUninstallFallbackData(instance.account, module, data); } else { revert("Invalid module type"); } diff --git a/src/test/helpers/KernelHelpers.sol b/src/test/helpers/KernelHelpers.sol index b8789f7f..6922e703 100644 --- a/src/test/helpers/KernelHelpers.sol +++ b/src/test/helpers/KernelHelpers.sol @@ -43,41 +43,6 @@ contract KernelHelpers is HelperBase { nonce = encodeNonce(vType, false, instance.account, txValidator); } - /** - * get callData to uninstall executor on ERC7579 Account - */ - function uninstallExecutor( - address, - address executor, - bytes memory initData - ) - public - view - virtual - override - returns (bytes memory callData) - { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_EXECUTOR, executor, initData) - ); - } - - function uninstallValidator( - address, - address validator, - bytes memory initData - ) - public - view - virtual - override - returns (bytes memory callData) - { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_VALIDATOR, validator, initData) - ); - } - function encodeNonce( ValidationType vType, bool enable, diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index fa6b3ee1..d4dda9cf 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -22,7 +22,7 @@ contract SafeHelpers is HelperBase { /** * get callData to uninstall validator on ERC7579 Account */ - function uninstallValidator( + function getUninstallValidatorData( address account, address validator, bytes memory initData @@ -31,7 +31,7 @@ contract SafeHelpers is HelperBase { view virtual override - returns (bytes memory callData) + returns (bytes memory data) { // get previous validator in sentinel list address previous; @@ -48,17 +48,13 @@ contract SafeHelpers is HelperBase { if (array[i] == validator) previous = array[i - 1]; } } - initData = abi.encode(previous, initData); - - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_VALIDATOR, validator, initData) - ); + data = abi.encode(previous, initData); } /** * get callData to uninstall executor on ERC7579 Account */ - function uninstallExecutor( + function getUninstallExecutorData( address account, address executor, bytes memory initData @@ -67,7 +63,7 @@ contract SafeHelpers is HelperBase { view virtual override - returns (bytes memory callData) + returns (bytes memory data) { // get previous executor in sentinel list address previous; @@ -84,17 +80,13 @@ contract SafeHelpers is HelperBase { if (array[i] == executor) previous = array[i - 1]; } } - initData = abi.encode(previous, initData); - - callData = abi.encodeCall( - IERC7579Account.uninstallModule, (MODULE_TYPE_EXECUTOR, executor, initData) - ); + data = abi.encode(previous, initData); } /** * get callData to install hook on ERC7579 Account */ - function installHook( + function getInstallHookData( address, /* account */ address hook, bytes memory initData @@ -103,18 +95,15 @@ contract SafeHelpers is HelperBase { view virtual override - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.installModule, - (MODULE_TYPE_HOOK, hook, abi.encode(HookType.GLOBAL, bytes4(0x0), initData)) - ); + data = abi.encode(HookType.GLOBAL, bytes4(0x0), initData); } /** * get callData to uninstall hook on ERC7579 Account */ - function uninstallHook( + function getUninstallHookData( address, /* account */ address hook, bytes memory initData @@ -123,15 +112,12 @@ contract SafeHelpers is HelperBase { pure virtual override - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, - (MODULE_TYPE_HOOK, hook, abi.encode(HookType.GLOBAL, bytes4(0x0), initData)) - ); + data = abi.encode(HookType.GLOBAL, bytes4(0x0), initData); } - function installFallback( + function getInstallFallbackData( address, /* account */ address fallbackHandler, bytes memory initData @@ -140,22 +126,15 @@ contract SafeHelpers is HelperBase { pure virtual override - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.installModule, - ( - MODULE_TYPE_FALLBACK, - fallbackHandler, - abi.encode(bytes4(0x0), CALLTYPE_STATIC, initData) - ) - ); + data = abi.encode(bytes4(0x0), CALLTYPE_STATIC, initData); } /** * get callData to uninstall fallback on ERC7579 Account */ - function uninstallFallback( + function getUninstallFallbackData( address, /* account */ address fallbackHandler, bytes memory initData @@ -164,12 +143,9 @@ contract SafeHelpers is HelperBase { pure virtual override - returns (bytes memory callData) + returns (bytes memory data) { - callData = abi.encodeCall( - IERC7579Account.uninstallModule, - (MODULE_TYPE_FALLBACK, fallbackHandler, abi.encode(bytes4(0x0), initData)) - ); + data = abi.encode(bytes4(0x0), initData); } function execUserOp( From 3b5f9d7a086d8b05bc08ff0a08f88365c6ed2683 Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 18:32:57 +0100 Subject: [PATCH 10/11] fix: double call to get module data --- src/test/ModuleKitHelpers.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/ModuleKitHelpers.sol b/src/test/ModuleKitHelpers.sol index 84b2b49d..0f36f073 100644 --- a/src/test/ModuleKitHelpers.sol +++ b/src/test/ModuleKitHelpers.sol @@ -33,7 +33,6 @@ library ModuleKitHelpers { internal returns (UserOpData memory userOpData) { - data = getInstallModuleData(instance, moduleTypeId, module, data); userOpData = instance.getInstallModuleOps( moduleTypeId, module, data, address(instance.defaultValidator) ); @@ -53,7 +52,6 @@ library ModuleKitHelpers { internal returns (UserOpData memory userOpData) { - data = getUninstallModuleData(instance, moduleTypeId, module, data); userOpData = instance.getUninstallModuleOps( moduleTypeId, module, data, address(instance.defaultValidator) ); From e22b1efffb828cf0cf20779eb5c2e1137f2f61af Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Thu, 6 Jun 2024 18:40:53 +0100 Subject: [PATCH 11/11] fix: kernel module data --- src/test/helpers/KernelHelpers.sol | 119 +++++------------------------ 1 file changed, 20 insertions(+), 99 deletions(-) diff --git a/src/test/helpers/KernelHelpers.sol b/src/test/helpers/KernelHelpers.sol index 6922e703..9fb28768 100644 --- a/src/test/helpers/KernelHelpers.sol +++ b/src/test/helpers/KernelHelpers.sol @@ -99,12 +99,15 @@ contract KernelHelpers is HelperBase { * @dev * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L311-L321 */ - function getDefaultInstallValidatorData( - address, + function getInstallValidatorData( + address, /* account */ + address, /* module */ bytes memory initData ) public pure + virtual + override returns (bytes memory data) { data = abi.encodePacked(address(0), abi.encode(initData, abi.encodePacked(""))); @@ -114,12 +117,15 @@ contract KernelHelpers is HelperBase { * @dev * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L324-L334 */ - function getDefaultInstallExecutorData( - address, + function getInstallExecutorData( + address, /* account */ + address, /* module */ bytes memory initData ) public pure + virtual + override returns (bytes memory data) { data = abi.encodePacked(address(0), abi.encode(initData, abi.encodePacked(""))); @@ -129,12 +135,15 @@ contract KernelHelpers is HelperBase { * @dev * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L336-L345 */ - function getDefaultInstallFallbackData( - address, + function getInstallFallbackData( + address, /* account */ + address, /* module */ bytes memory initData ) public pure + virtual + override returns (bytes memory data) { data = abi.encodePacked( @@ -144,110 +153,22 @@ contract KernelHelpers is HelperBase { ); } - /** - * @dev - * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L311-L321 - */ - function getDefaultInstallHookData( - address, - bytes memory initData - ) - public - pure - returns (bytes memory data) - { - data = initData; - } - - /** - * @dev - * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L397-L398 - */ - function getDefaultUninstallValidatorData( - address module, - bytes memory deinitData - ) - public - pure - returns (bytes memory data) - { } - - /** - * @dev - * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L400 - */ - function getDefaultUninstallExecutorData( - address module, - bytes memory deinitData - ) - public - pure - returns (bytes memory data) - { } - /** * @dev * https://github.com/zerodevapp/kernel/blob/a807c8ec354a77ebb7cdb73c5be9dd315cda0df2/src/Kernel.sol#L402-L403 */ - function getDefaultUninstallFallbackData( - address, + function getUninstallFallbackData( + address, /* account */ + address, /* module */ bytes memory deinitData ) public pure - returns (bytes memory data) - { - data = abi.encodePacked(MockFallback.fallbackFunction.selector, deinitData); - } - - function getInstallModuleData( - AccountInstance memory, - uint256 moduleTypeId, - address module, - bytes memory data - ) - public - view virtual override - returns (bytes memory) - { - if (moduleTypeId == MODULE_TYPE_EXECUTOR) { - data = KernelHelpers.getDefaultInstallExecutorData(module, data); - } else if (moduleTypeId == MODULE_TYPE_VALIDATOR) { - data = KernelHelpers.getDefaultInstallValidatorData(module, data); - } else if (moduleTypeId == MODULE_TYPE_FALLBACK) { - data = KernelHelpers.getDefaultInstallFallbackData(module, data); - } else { - //TODO fix hook encoding impl in kernel helpers lib - data = KernelHelpers.getDefaultInstallHookData(module, data); - } - - return data; - } - - function getUninstallModuleData( - AccountInstance memory, - uint256 moduleTypeId, - address module, - bytes memory data - ) - public - view - virtual - override - returns (bytes memory) + returns (bytes memory data) { - if (moduleTypeId == MODULE_TYPE_EXECUTOR) { - data = KernelHelpers.getDefaultUninstallExecutorData(module, data); - } else if (moduleTypeId == MODULE_TYPE_VALIDATOR) { - data = KernelHelpers.getDefaultUninstallValidatorData(module, data); - } else if (moduleTypeId == MODULE_TYPE_FALLBACK) { - data = KernelHelpers.getDefaultUninstallFallbackData(module, data); - } else { - //TODO handle for hook - } - return data; + data = abi.encodePacked(MockFallback.fallbackFunction.selector, deinitData); } function isModuleInstalled(