Skip to content

Commit

Permalink
feat: add selector and calltype to fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
kopy-kat committed Jun 7, 2024
1 parent 95fba09 commit a71cad1
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 116 deletions.
1 change: 1 addition & 0 deletions src/Mocks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { MockTarget } from "module-bases/mocks/MockTarget.sol";
import { MockValidator } from "module-bases/mocks/MockValidator.sol";
import { MockExecutor } from "module-bases/mocks/MockExecutor.sol";
import { MockHook } from "module-bases/mocks/MockHook.sol";
import { MockFallback } from "module-bases/mocks/MockFallback.sol";

/*//////////////////////////////////////////////////////////////
Tokens
Expand Down
3 changes: 2 additions & 1 deletion src/external/ERC7579.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
CALLTYPE_BATCH,
CALLTYPE_DELEGATECALL,
EXECTYPE_DEFAULT,
MODE_DEFAULT
MODE_DEFAULT,
CALLTYPE_STATIC
} from "erc7579/lib/ModeLib.sol";
import { Execution, ExecutionLib as ERC7579ExecutionLib } from "erc7579/lib/ExecutionLib.sol";

Expand Down
38 changes: 38 additions & 0 deletions src/test/helpers/ERC7579Helpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AccountInstance } from "../RhinestoneModuleKit.sol";
import { HelperBase } from "./HelperBase.sol";
import { IAccountModulesPaginated } from "./interfaces/IAccountModulesPaginated.sol";
import { IERC1271, EIP1271_MAGIC_VALUE } from "src/Interfaces.sol";
import { CallType } from "src/external/ERC7579.sol";

contract ERC7579Helpers is HelperBase {
/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -76,6 +77,43 @@ contract ERC7579Helpers is HelperBase {
data = abi.encode(previous, initData);
}

/**
* get callData to install fallback on ERC7579 Account
*/
function getInstallFallbackData(
address, /* account */
address fallbackHandler,
bytes memory initData
)
public
pure
virtual
override
returns (bytes memory data)
{
(bytes4 selector, CallType callType, bytes memory _initData) =
abi.decode(initData, (bytes4, CallType, bytes));
data = abi.encodePacked(selector, callType, _initData);
}

/**
* get callData to uninstall fallback on ERC7579 Account
*/
function getUninstallFallbackData(
address, /* account */
address fallbackHandler,
bytes memory initData
)
public
pure
virtual
override
returns (bytes memory data)
{
(bytes4 selector,, bytes memory _initData) = abi.decode(initData, (bytes4, CallType, bytes));
data = abi.encodePacked(selector, _initData);
}

/*//////////////////////////////////////////////////////////////////////////
SIGNATURE UTILS
//////////////////////////////////////////////////////////////////////////*/
Expand Down
88 changes: 17 additions & 71 deletions src/test/helpers/HelperBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ abstract contract HelperBase {
if (instance.account.code.length == 0) {
initCode = instance.initCode;
}

bytes memory callData;
if (isInstall) {
callData = installModule({
account: instance.account,
initData = getInstallModuleData({
instance: instance,
moduleType: moduleType,
module: module,
initData: initData
});
callData = abi.encodeCall(IERC7579Account.installModule, (moduleType, module, initData));
} else {
callData = uninstallModule({
account: instance.account,
initData = getUninstallModuleData({
instance: instance,
moduleType: moduleType,
module: module,
initData: initData
});
callData =
abi.encodeCall(IERC7579Account.uninstallModule, (moduleType, module, initData));
}

userOp = PackedUserOperation({
Expand All @@ -97,62 +99,6 @@ abstract contract HelperBase {
userOpHash = instance.aux.entrypoint.getUserOpHash(userOp);
}

/**
* Router function to install a module on an ERC7579 account
*/
function installModule(
address account,
uint256 moduleType,
address module,
bytes memory initData
)
public
view
virtual
returns (bytes memory callData)
{
if (moduleType == MODULE_TYPE_VALIDATOR) {
initData = getInstallValidatorData(account, module, initData);
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
initData = getInstallExecutorData(account, module, initData);
} else if (moduleType == MODULE_TYPE_HOOK) {
initData = getInstallHookData(account, module, initData);
} else if (moduleType == MODULE_TYPE_FALLBACK) {
initData = getInstallFallbackData(account, module, initData);
} else {
revert("Invalid module type");
}
callData = abi.encodeCall(IERC7579Account.installModule, (moduleType, module, initData));
}

/**
* Router function to uninstall a module on an ERC7579 account
*/
function uninstallModule(
address account,
uint256 moduleType,
address module,
bytes memory initData
)
public
view
virtual
returns (bytes memory callData)
{
if (moduleType == MODULE_TYPE_VALIDATOR) {
initData = getUninstallValidatorData(account, module, initData);
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
initData = getUninstallExecutorData(account, module, initData);
} else if (moduleType == MODULE_TYPE_HOOK) {
initData = getUninstallHookData(account, module, initData);
} else if (moduleType == MODULE_TYPE_FALLBACK) {
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
*/
Expand Down Expand Up @@ -314,21 +260,21 @@ abstract contract HelperBase {
AccountInstance memory instance,
uint256 moduleType,
address module,
bytes memory data
bytes memory initData
)
public
view
virtual
returns (bytes memory)
{
if (moduleType == MODULE_TYPE_VALIDATOR) {
return getInstallValidatorData(instance.account, module, data);
return getInstallValidatorData(instance.account, module, initData);
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
return getInstallExecutorData(instance.account, module, data);
return getInstallExecutorData(instance.account, module, initData);
} else if (moduleType == MODULE_TYPE_HOOK) {
return getInstallHookData(instance.account, module, data);
return getInstallHookData(instance.account, module, initData);
} else if (moduleType == MODULE_TYPE_FALLBACK) {
return getInstallFallbackData(instance.account, module, data);
return getInstallFallbackData(instance.account, module, initData);
} else {
revert("Invalid module type");
}
Expand All @@ -338,21 +284,21 @@ abstract contract HelperBase {
AccountInstance memory instance,
uint256 moduleType,
address module,
bytes memory data
bytes memory initData
)
public
view
virtual
returns (bytes memory)
{
if (moduleType == MODULE_TYPE_VALIDATOR) {
return getUninstallValidatorData(instance.account, module, data);
return getUninstallValidatorData(instance.account, module, initData);
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
return getUninstallExecutorData(instance.account, module, data);
return getUninstallExecutorData(instance.account, module, initData);
} else if (moduleType == MODULE_TYPE_HOOK) {
return getUninstallHookData(instance.account, module, data);
return getUninstallHookData(instance.account, module, initData);
} else if (moduleType == MODULE_TYPE_FALLBACK) {
return getUninstallFallbackData(instance.account, module, data);
return getUninstallFallbackData(instance.account, module, initData);
} else {
revert("Invalid module type");
}
Expand Down
12 changes: 8 additions & 4 deletions src/test/helpers/KernelHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Kernel } from "kernel/Kernel.sol";
import { etch } from "../utils/Vm.sol";
import { IValidator } from "kernel/interfaces/IERC7579Modules.sol";
import { IERC1271, EIP1271_MAGIC_VALUE } from "src/Interfaces.sol";
import { CallType } from "src/external/ERC7579.sol";

contract SetSelector is Kernel {
constructor(IEntryPoint _entrypoint) Kernel(_entrypoint) { }
Expand Down Expand Up @@ -164,10 +165,12 @@ contract KernelHelpers is HelperBase {
override
returns (bytes memory data)
{
(bytes4 selector, CallType callType, bytes memory _initData) =
abi.decode(initData, (bytes4, CallType, bytes));
data = abi.encodePacked(
MockFallback.fallbackFunction.selector,
selector,
address(0),
abi.encode(initData, abi.encodePacked(""))
abi.encode(abi.encodePacked(callType, _initData), abi.encodePacked(""))
);
}

Expand All @@ -178,15 +181,16 @@ contract KernelHelpers is HelperBase {
function getUninstallFallbackData(
address, /* account */
address, /* module */
bytes memory deinitData
bytes memory initData
)
public
pure
virtual
override
returns (bytes memory data)
{
data = abi.encodePacked(MockFallback.fallbackFunction.selector, deinitData);
(bytes4 selector,, bytes memory _initData) = abi.decode(initData, (bytes4, CallType, bytes));
data = abi.encodePacked(selector, _initData);
}

function isModuleInstalled(
Expand Down
29 changes: 10 additions & 19 deletions src/test/helpers/SafeHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { IAccountModulesPaginated } from "./interfaces/IAccountModulesPaginated.
import { CALLTYPE_STATIC } from "safe7579/lib/ModeLib.sol";
import { IERC1271, EIP1271_MAGIC_VALUE } from "src/Interfaces.sol";
import { startPrank, stopPrank } from "../utils/Vm.sol";
import { CallType } from "src/external/ERC7579.sol";

contract SafeHelpers is HelperBase {
/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -162,20 +163,6 @@ contract SafeHelpers is HelperBase {
data = abi.encode(HookType.GLOBAL, bytes4(0x0), initData);
}

function getInstallFallbackData(
address, /* account */
address fallbackHandler,
bytes memory initData
)
public
pure
virtual
override
returns (bytes memory data)
{
data = abi.encode(bytes4(0x0), CALLTYPE_STATIC, initData);
}

/**
* get callData to uninstall fallback on ERC7579 Account
*/
Expand All @@ -190,7 +177,8 @@ contract SafeHelpers is HelperBase {
override
returns (bytes memory data)
{
data = abi.encode(bytes4(0x0), initData);
(bytes4 selector,, bytes memory _initData) = abi.decode(initData, (bytes4, CallType, bytes));
data = abi.encode(selector, _initData);
}

function configModuleUserOp(
Expand All @@ -213,19 +201,22 @@ contract SafeHelpers is HelperBase {

bytes memory callData;
if (isInstall) {
callData = installModule({
account: instance.account,
initData = getInstallModuleData({
instance: instance,
moduleType: moduleType,
module: module,
initData: initData
});
callData = abi.encodeCall(IERC7579Account.installModule, (moduleType, module, initData));
} else {
callData = uninstallModule({
account: instance.account,
initData = getUninstallModuleData({
instance: instance,
moduleType: moduleType,
module: module,
initData: initData
});
callData =
abi.encodeCall(IERC7579Account.uninstallModule, (moduleType, module, initData));
}

if (initCode.length != 0) {
Expand Down
Loading

0 comments on commit a71cad1

Please sign in to comment.