Skip to content

Commit af0e8fc

Browse files
committed
refactoring
1 parent 4752d41 commit af0e8fc

File tree

4 files changed

+99
-94
lines changed

4 files changed

+99
-94
lines changed

src/Kernel.sol

Lines changed: 51 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
8686
}
8787

8888
modifier onlyEntryPointOrSelfOrRoot() {
89-
IValidator validator = ValidatorLib.getValidator(_validationStorage().rootValidator);
9089
if (
9190
msg.sender != address(entrypoint) && msg.sender != address(this) // do rootValidator hook
9291
) {
92+
IValidator validator = ValidatorLib.getValidator(_validationStorage().rootValidator);
9393
if (validator.isModuleType(4)) {
9494
bytes memory ret = IHook(address(validator)).preCheck(msg.sender, msg.value, msg.data);
9595
_;
@@ -140,7 +140,7 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
140140
bytes calldata hookData
141141
) external payable onlyEntryPointOrSelfOrRoot {
142142
ValidationStorage storage vs = _validationStorage();
143-
if (ValidationId.unwrap(_rootValidator) == bytes21(0)) {
143+
if (ValidationId.unwrap(_rootValidator) == bytes21(0) || bytes2(address(this).code) == EIP7702_PREFIX) {
144144
revert InvalidValidator();
145145
}
146146
ValidationType vType = ValidatorLib.getType(_rootValidator);
@@ -196,14 +196,14 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
196196
}
197197
// action installed
198198
bytes memory context;
199-
if (address(config.hook) != HOOK_MODULE_INSTALLED && address(config.hook) != HOOK_ONLY_ENTRYPOINT) {
200-
context = _doPreHook(config.hook, msg.value, msg.data);
201-
} else if (address(config.hook) == HOOK_ONLY_ENTRYPOINT) {
199+
if (address(config.hook) == HOOK_ONLY_ENTRYPOINT) {
202200
// for selector manager, address(0) for the hook will default to type(address).max,
203201
// and this will only allow entrypoints to interact
204202
if (msg.sender != address(entrypoint)) {
205203
revert InvalidCaller();
206204
}
205+
} else if (address(config.hook) != HOOK_MODULE_INSTALLED) {
206+
context = _doPreHook(config.hook, msg.value, msg.data);
207207
}
208208
// execute action
209209
if (config.callType == CALLTYPE_SINGLE) {
@@ -218,7 +218,7 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
218218
revert(add(result, 0x20), mload(result))
219219
}
220220
}
221-
if (address(config.hook) != address(1) && address(config.hook) != HOOK_ONLY_ENTRYPOINT) {
221+
if (address(config.hook) != HOOK_MODULE_INSTALLED && address(config.hook) != HOOK_ONLY_ENTRYPOINT) {
222222
_doPostHook(config.hook, context);
223223
}
224224
assembly {
@@ -245,7 +245,7 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
245245
if (vType == VALIDATION_TYPE_ROOT) {
246246
vId = vs.rootValidator;
247247
}
248-
validationData = _doValidation(vMode, vId, userOp, userOpHash);
248+
validationData = _validateUserOp(vMode, vId, userOp, userOpHash);
249249
ValidationConfig memory vc = vs.validationConfig[vId];
250250
// allow when nonce is not revoked or vType is sudo
251251
if (vType != VALIDATION_TYPE_ROOT && vc.nonce < vs.validNonceFrom) {
@@ -280,6 +280,10 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
280280
}
281281
}
282282

283+
function isValidSignature(bytes32 hash, bytes calldata data) external view returns (bytes4) {
284+
return _verifySignature(hash, data);
285+
}
286+
283287
// --- Execution ---
284288
function executeUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash)
285289
external
@@ -289,15 +293,15 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
289293
{
290294
bytes memory context;
291295
IHook hook = executionHook[userOpHash];
292-
if (address(hook) != address(1)) {
296+
if (address(hook) != HOOK_MODULE_INSTALLED) {
293297
// removed 4bytes selector
294298
context = _doPreHook(hook, msg.value, userOp.callData[4:]);
295299
}
296300
(bool success, bytes memory ret) = ExecLib.executeDelegatecall(address(this), userOp.callData[4:]);
297301
if (!success) {
298302
revert ExecutionReverted();
299303
}
300-
if (address(hook) != address(1)) {
304+
if (address(hook) != HOOK_MODULE_INSTALLED) {
301305
_doPostHook(hook, context);
302306
}
303307
}
@@ -313,11 +317,12 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
313317
revert InvalidExecutor();
314318
}
315319
bytes memory context;
316-
if (address(hook) != HOOK_MODULE_INSTALLED) {
320+
bool callHook = address(hook) != HOOK_MODULE_INSTALLED;
321+
if (callHook) {
317322
context = _doPreHook(hook, msg.value, msg.data);
318323
}
319324
returnData = ExecLib.execute(execMode, executionCalldata);
320-
if (address(hook) != HOOK_MODULE_INSTALLED) {
325+
if (callHook) {
321326
_doPostHook(hook, context);
322327
}
323328
}
@@ -326,37 +331,6 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
326331
ExecLib.execute(execMode, executionCalldata);
327332
}
328333

329-
function isValidSignature(bytes32 hash, bytes calldata signature) external view override returns (bytes4) {
330-
ValidationStorage storage vs = _validationStorage();
331-
(ValidationId vId, bytes calldata sig) = ValidatorLib.decodeSignature(signature);
332-
if (ValidatorLib.getType(vId) == VALIDATION_TYPE_ROOT) {
333-
vId = vs.rootValidator;
334-
}
335-
bool isReplayable = sig.length >= 32 && bytes32(sig[0:32]) == MAGIC_VALUE_SIG_REPLAYABLE;
336-
if (isReplayable) {
337-
sig = sig[32:];
338-
}
339-
ValidationType vType = ValidatorLib.getType(vId);
340-
if (address(vs.validationConfig[vId].hook) == HOOK_MODULE_NOT_INSTALLED && vType != VALIDATION_TYPE_7702) {
341-
revert InvalidValidator();
342-
}
343-
if (vType == VALIDATION_TYPE_VALIDATOR) {
344-
IValidator validator = ValidatorLib.getValidator(vId);
345-
return validator.isValidSignatureWithSender(msg.sender, _toWrappedHash(hash, isReplayable), sig);
346-
} else if (vType == VALIDATION_TYPE_PERMISSION) {
347-
PermissionId pId = ValidatorLib.getPermissionId(vId);
348-
PassFlag permissionFlag = vs.permissionConfig[pId].permissionFlag;
349-
if (PassFlag.unwrap(permissionFlag) & PassFlag.unwrap(SKIP_SIGNATURE) != 0) {
350-
revert PermissionNotAlllowedForSignature();
351-
}
352-
return _checkPermissionSignature(pId, msg.sender, hash, sig, isReplayable);
353-
} else if (vType == VALIDATION_TYPE_7702) {
354-
return _verify7702Signature(_toWrappedHash(hash, isReplayable), sig);
355-
} else {
356-
revert InvalidValidationType();
357-
}
358-
}
359-
360334
function installModule(uint256 moduleType, address module, bytes calldata initData)
361335
external
362336
payable
@@ -381,7 +355,7 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
381355
_installValidation(vId, config, data.validatorData, data.hookData);
382356
if (data.selectorData.length == 4) {
383357
// NOTE: we don't allow configure on selector data on v3.1+, but using bytes instead of bytes4 for selector data to make sure we are future proof
384-
_setSelector(vId, bytes4(data.selectorData[0:4]), true);
358+
_grantAccess(vId, bytes4(data.selectorData[0:4]), true);
385359
}
386360
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
387361
InstallExecutorDataFormat calldata data;
@@ -415,6 +389,10 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
415389
emit ModuleInstalled(moduleType, module);
416390
}
417391

392+
function grantAccess(ValidationId vId, bytes4 selector, bool allow) external payable onlyEntryPointOrSelfOrRoot {
393+
_grantAccess(vId, selector, allow);
394+
}
395+
418396
function installValidations(
419397
ValidationId[] calldata vIds,
420398
ValidationConfig[] memory configs,
@@ -429,7 +407,18 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
429407
payable
430408
onlyEntryPointOrSelfOrRoot
431409
{
432-
IHook hook = _uninstallValidation(vId, deinitData);
410+
IHook hook = _clearValidationData(vId);
411+
ValidationType vType = ValidatorLib.getType(vId);
412+
if (vType == VALIDATION_TYPE_VALIDATOR) {
413+
IValidator validator = ValidatorLib.getValidator(vId);
414+
ModuleLib.uninstallModule(address(validator), deinitData);
415+
emit IERC7579Account.ModuleUninstalled(MODULE_TYPE_VALIDATOR, address(validator));
416+
} else if (vType == VALIDATION_TYPE_PERMISSION) {
417+
PermissionId permission = ValidatorLib.getPermissionId(vId);
418+
_uninstallPermission(permission, deinitData);
419+
} else {
420+
revert InvalidValidationType();
421+
}
433422
_uninstallHook(hook, hookDeinitData);
434423
}
435424

@@ -443,26 +432,31 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
443432
override
444433
onlyEntryPointOrSelfOrRoot
445434
{
446-
if (moduleType == 1) {
435+
if (moduleType == MODULE_TYPE_VALIDATOR) {
447436
ValidationId vId = ValidatorLib.validatorToIdentifier(IValidator(module));
448-
_uninstallValidation(vId, deInitData);
449-
} else if (moduleType == 2) {
450-
_uninstallExecutor(IExecutor(module), deInitData);
451-
} else if (moduleType == 3) {
437+
_clearValidationData(vId);
438+
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
439+
_clearExecutorData(IExecutor(module));
440+
} else if (moduleType == MODULE_TYPE_FALLBACK) {
452441
bytes4 selector = bytes4(deInitData[0:4]);
453-
_uninstallSelector(selector, deInitData[4:]);
454-
} else if (moduleType == 4) {
442+
address target;
443+
_clearSelectorData(selector);
444+
if (target == address(0)) {
445+
return;
446+
}
447+
require(target == module, InvalidSelector());
448+
deInitData = deInitData[4:];
449+
} else if (moduleType == MODULE_TYPE_HOOK) {
455450
ValidationId vId = _validationStorage().rootValidator;
456451
if (_validationStorage().validationConfig[vId].hook == IHook(module)) {
457452
// when root validator hook is being removed
458453
// remove hook on root validator to prevent kernel from being locked
459-
_validationStorage().validationConfig[vId].hook = IHook(address(1));
454+
_validationStorage().validationConfig[vId].hook = IHook(HOOK_MODULE_INSTALLED);
460455
}
461456
// force call onUninstall for hook
462457
// NOTE: for hook, kernel does not support independent hook install,
463458
// hook is expected to be paired with proper validator/executor/selector
464-
ModuleLib.uninstallModule(module, deInitData);
465-
} else if (moduleType == 5) {
459+
} else if (moduleType == MODULE_TYPE_POLICY || moduleType == MODULE_TYPE_SIGNER) {
466460
ValidationId rootValidator = _validationStorage().rootValidator;
467461
bytes32 permissionId = bytes32(deInitData[0:32]);
468462
if (ValidatorLib.getType(rootValidator) == VALIDATION_TYPE_PERMISSION) {
@@ -474,23 +468,13 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
474468
// NOTE: for policy, kernel does not support independent policy install,
475469
// policy is expected to be paired with proper permissionId
476470
// to "REMOVE" permission, use "uninstallValidation()" function
477-
ModuleLib.uninstallModule(module, deInitData);
478-
} else if (moduleType == 6) {
479-
ValidationId rootValidator = _validationStorage().rootValidator;
480-
bytes32 permissionId = bytes32(deInitData[0:32]);
481-
if (ValidatorLib.getType(rootValidator) == VALIDATION_TYPE_PERMISSION) {
482-
if (permissionId == bytes32(PermissionId.unwrap(ValidatorLib.getPermissionId(rootValidator)))) {
483-
revert RootValidatorCannotBeRemoved();
484-
}
485-
}
486-
// force call onUninstall for signer
487471
// NOTE: for signer, kernel does not support independent signer install,
488472
// signer is expected to be paired with proper permissionId
489473
// to "REMOVE" permission, use "uninstallValidation()" function
490-
ModuleLib.uninstallModule(module, deInitData);
491474
} else {
492475
revert InvalidModuleType();
493476
}
477+
ModuleLib.uninstallModule(module, deInitData);
494478
emit ModuleUninstalled(moduleType, module);
495479
}
496480

@@ -510,9 +494,9 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
510494
{
511495
if (moduleType == MODULE_TYPE_VALIDATOR) {
512496
return _validationStorage().validationConfig[ValidatorLib.validatorToIdentifier(IValidator(module))].hook
513-
!= IHook(address(0));
497+
!= IHook(HOOK_MODULE_NOT_INSTALLED);
514498
} else if (moduleType == MODULE_TYPE_EXECUTOR) {
515-
return address(_executorConfig(IExecutor(module)).hook) != address(0);
499+
return address(_executorConfig(IExecutor(module)).hook) != HOOK_MODULE_NOT_INSTALLED;
516500
} else if (moduleType == MODULE_TYPE_FALLBACK) {
517501
return _selectorConfig(bytes4(additionalContext[0:4])).target == module;
518502
} else {

src/core/ExecutorManager.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ abstract contract ExecutorManager {
4747
emit IERC7579Account.ModuleInstalled(MODULE_TYPE_EXECUTOR, address(executor));
4848
}
4949

50-
function _uninstallExecutor(IExecutor executor, bytes calldata executorData) internal returns (IHook hook) {
50+
function _clearExecutorData(IExecutor executor) internal returns (IHook hook) {
5151
ExecutorConfig storage config = _executorConfig(executor);
5252
hook = config.hook;
5353
config.hook = IHook(address(0));
54-
ModuleLib.uninstallModule(address(executor), executorData);
55-
emit IERC7579Account.ModuleUninstalled(MODULE_TYPE_EXECUTOR, address(executor));
5654
}
5755
}

src/core/SelectorManager.sol

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ abstract contract SelectorManager {
6060
ss.callType = callType;
6161
}
6262

63-
function _uninstallSelector(bytes4 selector, bytes calldata selectorDeinitData) internal returns (IHook hook) {
63+
function _clearSelectorData(bytes4 selector) internal returns (IHook hook, address target) {
6464
SelectorConfig storage ss = _selectorConfig(selector);
6565
hook = ss.hook;
6666
ss.hook = IHook(address(0));
6767
if (ss.callType == CALLTYPE_SINGLE) {
68-
ModuleLib.uninstallModule(ss.target, selectorDeinitData);
69-
emit IERC7579Account.ModuleUninstalled(MODULE_TYPE_FALLBACK, ss.target);
68+
target = ss.target; // if callType!=CALLTYPE_SINGLE, don't need to call uninstall
7069
}
7170
ss.target = address(0);
7271
ss.callType = CallType.wrap(bytes1(0x00));

0 commit comments

Comments
 (0)