-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleManager.sol
More file actions
816 lines (741 loc) · 37.6 KB
/
Copy pathModuleManager.sol
File metadata and controls
816 lines (741 loc) · 37.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
// ──────────────────────────────────────────────────────────────────────────────
// _ __ _ __
// / | / /__ | |/ /_ _______
// / |/ / _ \| / / / / ___/
// / /| / __/ / /_/ (__ )
// /_/ |_/\___/_/|_\__,_/____/
//
// ──────────────────────────────────────────────────────────────────────────────
// Nexus: A suite of contracts for Modular Smart Accounts compliant with ERC-7579 and ERC-4337, developed by Biconomy.
// Learn more at https://biconomy.io. To report security issues, please contact us at: security@biconomy.io
import { SentinelListLib } from "sentinellist/SentinelList.sol";
import { Storage } from "./Storage.sol";
import {
IModule,
IValidator,
IExecutor,
IHook,
IPreValidationHookERC1271,
IPreValidationHookERC4337,
IFallback
} from "erc7579/interfaces/IERC7579Module.sol";
import { CallType, CALLTYPE_SINGLE, CALLTYPE_STATIC } from "../../lib/erc-7579/ModeLib.sol";
import { ExecLib } from "../../lib/erc-7579/ExecLib.sol";
import { LocalCallDataParserLib } from "../../lib/nexus/local/LocalCallDataParserLib.sol";
import { IModuleManager } from "../../interfaces/nexus/base/IModuleManager.sol";
// solhint-disable no-unused-import
import {
MODULE_TYPE_VALIDATOR,
MODULE_TYPE_EXECUTOR,
MODULE_TYPE_FALLBACK,
MODULE_TYPE_HOOK,
MODULE_TYPE_PREVALIDATION_HOOK_ERC1271,
MODULE_TYPE_PREVALIDATION_HOOK_ERC4337,
MODULE_TYPE_MULTI,
MODULE_ENABLE_MODE_TYPE_HASH,
EMERGENCY_UNINSTALL_TYPE_HASH,
ERC1271_SUCCESS
} from "../../types/Constants.sol";
// solhint-enable no-unused-import
import { EIP712 } from "solady/utils/EIP712.sol";
import { ExcessivelySafeCall } from "excessively-safe-call/ExcessivelySafeCall.sol";
import { PackedUserOperation } from "account-abstraction/interfaces/PackedUserOperation.sol";
import { EmergencyUninstall } from "../../types/DataTypes.sol";
/// @title Nexus - ModuleManager
/// @notice Manages Validator, Executor, Hook, and Fallback modules within the Nexus suite, supporting
/// @dev Implements SentinelList for managing modules via a linked list structure, adhering to ERC-7579.
/// @author @livingrockrises | Biconomy | chirag@biconomy.io
/// @author @aboudjem | Biconomy | adam.boudjemaa@biconomy.io
/// @author @filmakarov | Biconomy | filipp.makarov@biconomy.io
/// @author @zeroknots | Rhinestone.wtf | zeroknots.eth
/// Special thanks to the Solady team for foundational contributions: https://github.com/Vectorized/solady
abstract contract ModuleManager is Storage, EIP712, IModuleManager {
using SentinelListLib for SentinelListLib.SentinelList;
using LocalCallDataParserLib for bytes;
using ExecLib for address;
using ExcessivelySafeCall for address;
/// @dev The default validator address.
/// @notice To explicitly initialize the default validator, Nexus.execute(_DEFAULT_VALIDATOR.onInstall(...)) should
/// be
/// called.
address internal immutable _DEFAULT_VALIDATOR;
/// @notice Ensures the message sender is a registered executor module.
modifier onlyExecutorModule() virtual {
require(_getAccountStorage().executors.contains(msg.sender), InvalidModule(msg.sender));
_;
}
/// @notice Does pre-checks and post-checks using an installed hook on the account.
/// @dev sender, msg.data and msg.value is passed to the hook to implement custom flows.
modifier withHook() {
address hook = _getHook();
if (hook == address(0)) {
_;
} else {
bytes memory hookData = IHook(hook).preCheck(msg.sender, msg.value, msg.data);
_;
IHook(hook).postCheck(hookData);
}
}
/// @dev initData should block the implementation from being used as a Smart Account
constructor(address defaultValidator_, bytes memory initData_) {
if (!IValidator(defaultValidator_).isModuleType(MODULE_TYPE_VALIDATOR)) {
revert MismatchModuleTypeId();
}
IValidator(defaultValidator_).onInstall(initData_);
_DEFAULT_VALIDATOR = defaultValidator_;
}
// receive function
receive() external payable { }
/// @dev Fallback function to manage incoming calls using designated handlers based on the call type.
/// Hooked manually in the _fallback function
fallback() external payable {
_fallback(msg.data);
}
/// @dev Retrieves the default validator address.
/// @return The address of the default validator.
function getDefaultValidator() external view returns (address) {
return _DEFAULT_VALIDATOR;
}
/// @dev Retrieves a paginated list of validator addresses from the linked list.
/// This utility function is not defined by the ERC-7579 standard and is implemented to facilitate
/// easier management and retrieval of large sets of validator modules.
/// @param cursor The address to start pagination from, or zero to start from the first entry.
/// @param size The number of validator addresses to return.
/// @return array An array of validator addresses.
/// @return next The address to use as a cursor for the next page of results.
function getValidatorsPaginated(
address cursor,
uint256 size
)
external
view
returns (address[] memory array, address next)
{
(array, next) = _paginate(_getAccountStorage().validators, cursor, size);
}
/// @dev Retrieves a paginated list of executor addresses from the linked list.
/// This utility function is not defined by the ERC-7579 standard and is implemented to facilitate
/// easier management and retrieval of large sets of executor modules.
/// @param cursor The address to start pagination from, or zero to start from the first entry.
/// @param size The number of executor addresses to return.
/// @return array An array of executor addresses.
/// @return next The address to use as a cursor for the next page of results.
function getExecutorsPaginated(
address cursor,
uint256 size
)
external
view
returns (address[] memory array, address next)
{
(array, next) = _paginate(_getAccountStorage().executors, cursor, size);
}
/// @notice Retrieves the currently active hook address.
/// @return hook The address of the active hook module.
function getActiveHook() external view returns (address hook) {
return _getHook();
}
/// @notice Fetches the fallback handler for a specific selector.
/// @param selector The function selector to query.
/// @return calltype The type of call that the handler manages.
/// @return handler The address of the fallback handler.
function getFallbackHandlerBySelector(bytes4 selector) external view returns (CallType, address) {
FallbackHandler memory handler = _getAccountStorage().fallbacks[selector];
return (handler.calltype, handler.handler);
}
/// @dev Initializes the module manager by setting up default states for validators and executors.
function _initSentinelLists() internal virtual {
// account module storage
AccountStorage storage ams = _getAccountStorage();
ams.executors.init();
ams.validators.init();
}
/// @dev Implements Module Enable Mode flow.
/// @param packedData Data source to parse data required to perform Module Enable mode from.
/// @return userOpSignature the clean signature which can be further used for userOp validation
function _enableMode(
bytes32 userOpHash,
bytes calldata packedData
)
internal
returns (bytes calldata userOpSignature)
{
address module;
uint256 moduleType;
bytes calldata moduleInitData;
bytes calldata enableModeSignature;
(module, moduleType, moduleInitData, enableModeSignature, userOpSignature) = packedData.parseEnableModeData();
address enableModeSigValidator = _handleValidator(address(bytes20(enableModeSignature[0:20])));
enableModeSignature = enableModeSignature[20:];
bytes32 structHash = _getEnableModeDataHash(module, moduleType, userOpHash, moduleInitData);
bool isValid = _checkEnableModeSignature(structHash, enableModeSignature, enableModeSigValidator);
assembly {
if iszero(isValid) {
// revert EnableModeSigError()
mstore(0x00, 0x46fdc333)
revert(0x1c, 0x04)
}
}
this.installModule(moduleType, module, moduleInitData);
}
/// @notice Installs a new module to the smart account.
/// @param moduleTypeId The type identifier of the module being installed, which determines its role:
/// - 0 for MultiType
/// - 1 for Validator
/// - 2 for Executor
/// - 3 for Fallback
/// - 4 for Hook
/// - 8 for PreValidationHookERC1271
/// - 9 for PreValidationHookERC4337
/// @param module The address of the module to install.
/// @param initData Initialization data for the module.
/// @dev This function goes through hook checks via withHook modifier.
/// @dev No need to check that the module is already installed, as this check is done
/// when trying to sstore the module in an appropriate SentinelList
function _installModule(uint256 moduleTypeId, address module, bytes calldata initData) internal {
if (!_areSentinelListsInitialized()) {
_initSentinelLists();
}
if (module == address(0)) revert ModuleAddressCanNotBeZero();
if (moduleTypeId == MODULE_TYPE_VALIDATOR) {
_installValidator(module, initData);
} else if (moduleTypeId == MODULE_TYPE_EXECUTOR) {
_installExecutor(module, initData);
} else if (moduleTypeId == MODULE_TYPE_FALLBACK) {
_installFallbackHandler(module, initData);
} else if (moduleTypeId == MODULE_TYPE_HOOK) {
_installHook(module, initData);
} else if (_isPrevalidationHookType(moduleTypeId)) {
_installPreValidationHook(moduleTypeId, module, initData);
} else if (moduleTypeId == MODULE_TYPE_MULTI) {
_multiTypeInstall(module, initData);
} else {
revert InvalidModuleTypeId(moduleTypeId);
}
}
/// @dev Installs a new validator module after checking if it matches the required module type.
/// @param validator The address of the validator module to be installed.
/// @param data Initialization data to configure the validator upon installation.
function _installValidator(address validator, bytes calldata data) internal virtual withHook {
if (!IValidator(validator).isModuleType(MODULE_TYPE_VALIDATOR)) revert MismatchModuleTypeId();
if (validator == _DEFAULT_VALIDATOR) {
revert DefaultValidatorAlreadyInstalled();
}
_getAccountStorage().validators.push(validator);
IValidator(validator).onInstall(data);
}
/// @dev Uninstalls a validator module.
/// @param validator The address of the validator to be uninstalled.
/// @param data De-initialization data to configure the validator upon uninstallation.
function _uninstallValidator(address validator, bytes calldata data) internal virtual {
SentinelListLib.SentinelList storage validators = _getAccountStorage().validators;
(address prev, bytes memory disableModuleData) = abi.decode(data, (address, bytes));
// Perform the removal first
validators.pop(prev, validator);
validator.excessivelySafeCall(
gasleft(), 0, 0, abi.encodeWithSelector(IModule.onUninstall.selector, disableModuleData)
);
}
/// @dev Installs a new executor module after checking if it matches the required module type.
/// @param executor The address of the executor module to be installed.
/// @param data Initialization data to configure the executor upon installation.
function _installExecutor(address executor, bytes calldata data) internal virtual withHook {
if (!IExecutor(executor).isModuleType(MODULE_TYPE_EXECUTOR)) revert MismatchModuleTypeId();
_getAccountStorage().executors.push(executor);
IExecutor(executor).onInstall(data);
}
/// @dev Uninstalls an executor module by removing it from the executors list.
/// @param executor The address of the executor to be uninstalled.
/// @param data De-initialization data to configure the executor upon uninstallation.
function _uninstallExecutor(address executor, bytes calldata data) internal virtual {
(address prev, bytes memory disableModuleData) = abi.decode(data, (address, bytes));
_getAccountStorage().executors.pop(prev, executor);
executor.excessivelySafeCall(
gasleft(), 0, 0, abi.encodeWithSelector(IModule.onUninstall.selector, disableModuleData)
);
}
/// @dev Installs a hook module, ensuring no other hooks are installed before proceeding.
/// @param hook The address of the hook to be installed.
/// @param data Initialization data to configure the hook upon installation.
function _installHook(address hook, bytes calldata data) internal virtual withHook {
if (!IHook(hook).isModuleType(MODULE_TYPE_HOOK)) revert MismatchModuleTypeId();
address currentHook = _getHook();
require(currentHook == address(0), HookAlreadyInstalled(currentHook));
_setHook(hook);
IHook(hook).onInstall(data);
}
/// @dev Uninstalls a hook module, ensuring the current hook matches the one intended for uninstallation.
/// @param hook The address of the hook to be uninstalled.
/// @param hookType The type of the hook to be uninstalled.
/// @param data De-initialization data to configure the hook upon uninstallation.
function _uninstallHook(address hook, uint256 hookType, bytes calldata data) internal virtual {
if (hookType == MODULE_TYPE_HOOK) {
_setHook(address(0));
} else if (_isPrevalidationHookType(hookType)) {
_uninstallPreValidationHook(hookType);
}
hook.excessivelySafeCall(gasleft(), 0, 0, abi.encodeWithSelector(IModule.onUninstall.selector, data));
}
/// @dev Sets the current hook in the storage to the specified address.
/// @param hook The new hook address.
function _setHook(address hook) internal virtual {
_getAccountStorage().hook = IHook(hook);
}
/// @dev Installs a fallback handler for a given selector with initialization data.
/// @param handler The address of the fallback handler to install.
/// @param params The initialization parameters including the selector and call type.
function _installFallbackHandler(address handler, bytes calldata params) internal virtual withHook {
if (!IFallback(handler).isModuleType(MODULE_TYPE_FALLBACK)) revert MismatchModuleTypeId();
// Extract the function selector from the provided parameters.
bytes4 selector = bytes4(params[0:4]);
// Extract the call type from the provided parameters.
CallType calltype = CallType.wrap(bytes1(params[4]));
require(calltype == CALLTYPE_SINGLE || calltype == CALLTYPE_STATIC, FallbackCallTypeInvalid());
// Extract the initialization data from the provided parameters.
bytes memory initData = params[5:];
// Revert if the selector is either `onInstall(bytes)` (0x6d61fe70) or `onUninstall(bytes)` (0x8a91b0e3) or
// explicit bytes(0).
// These selectors are explicitly forbidden to prevent security vulnerabilities.
// Allowing these selectors would enable unauthorized users to uninstall and reinstall critical modules.
// If a validator module is uninstalled and reinstalled without proper authorization, it can compromise
// the account's security and integrity. By restricting these selectors, we ensure that the fallback handler
// cannot be manipulated to disrupt the expected behavior and security of the account.
require(
!(selector == bytes4(0x6d61fe70) || selector == bytes4(0x8a91b0e3) || selector == bytes4(0)),
FallbackSelectorForbidden()
);
// Revert if a fallback handler is already installed for the given selector.
// This check ensures that we do not overwrite an existing fallback handler, which could lead to unexpected
// behavior.
require(!_isFallbackHandlerInstalled(selector), FallbackAlreadyInstalledForSelector(selector));
// Store the fallback handler and its call type in the account storage.
// This maps the function selector to the specified fallback handler and call type.
_getAccountStorage().fallbacks[selector] = FallbackHandler(handler, calltype);
// Invoke the `onInstall` function of the fallback handler with the provided initialization data.
// This step allows the fallback handler to perform any necessary setup or initialization.
IFallback(handler).onInstall(initData);
}
/// @dev Uninstalls a fallback handler for a given selector.
/// @param fallbackHandler The address of the fallback handler to uninstall.
/// @param data The de-initialization data containing the selector.
function _uninstallFallbackHandler(address fallbackHandler, bytes calldata data) internal virtual {
_getAccountStorage().fallbacks[bytes4(data[0:4])] = FallbackHandler(address(0), CallType.wrap(0x00));
fallbackHandler.excessivelySafeCall(
gasleft(), 0, 0, abi.encodeWithSelector(IModule.onUninstall.selector, data[4:])
);
}
/// @dev Installs a pre-validation hook module, ensuring no other pre-validation hooks are installed before
/// proceeding.
/// @param preValidationHookType The type of the pre-validation hook.
/// @param preValidationHook The address of the pre-validation hook to be installed.
/// @param data Initialization data to configure the hook upon installation.
function _installPreValidationHook(
uint256 preValidationHookType,
address preValidationHook,
bytes calldata data
)
internal
virtual
withHook
{
if (!IModule(preValidationHook).isModuleType(preValidationHookType)) revert MismatchModuleTypeId();
address currentPreValidationHook = _getPreValidationHook(preValidationHookType);
require(currentPreValidationHook == address(0), PrevalidationHookAlreadyInstalled(currentPreValidationHook));
_setPreValidationHook(preValidationHookType, preValidationHook);
IModule(preValidationHook).onInstall(data);
}
/// @dev Uninstalls a pre-validation hook module
/// @param hookType The type of the pre-validation hook.
function _uninstallPreValidationHook(uint256 hookType) internal virtual {
_setPreValidationHook(hookType, address(0));
}
/// @dev Sets the current pre-validation hook in the storage to the specified address, based on the hook type.
/// @param hookType The type of the pre-validation hook.
/// @param hook The new hook address.
function _setPreValidationHook(uint256 hookType, address hook) internal virtual {
if (hookType == MODULE_TYPE_PREVALIDATION_HOOK_ERC1271) {
_getAccountStorage().preValidationHookERC1271 = IPreValidationHookERC1271(hook);
} else if (hookType == MODULE_TYPE_PREVALIDATION_HOOK_ERC4337) {
_getAccountStorage().preValidationHookERC4337 = IPreValidationHookERC4337(hook);
}
}
/// @notice Installs a module with multiple types in a single operation.
/// @dev This function handles installing a multi-type module by iterating through each type and initializing it.
/// The initData should include an ABI-encoded tuple of (uint[] types, bytes[] initDatas).
/// @param module The address of the multi-type module.
/// @param initData Initialization data for each type within the module.
function _multiTypeInstall(address module, bytes calldata initData) internal virtual {
(uint256[] calldata types, bytes[] calldata initDatas) = initData.parseMultiTypeInitData();
uint256 length = types.length;
if (initDatas.length != length) revert InvalidInput();
// iterate over all module types and install the module as a type accordingly
for (uint256 i; i < length; i++) {
uint256 theType = types[i];
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INSTALL VALIDATORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
if (theType == MODULE_TYPE_VALIDATOR) {
_installValidator(module, initDatas[i]);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INSTALL EXECUTORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
else if (theType == MODULE_TYPE_EXECUTOR) {
_installExecutor(module, initDatas[i]);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INSTALL FALLBACK */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
else if (theType == MODULE_TYPE_FALLBACK) {
_installFallbackHandler(module, initDatas[i]);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INSTALL HOOK (global only, not sig-specific) */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
else if (theType == MODULE_TYPE_HOOK) {
_installHook(module, initDatas[i]);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* INSTALL PRE-VALIDATION HOOK */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
else if (_isPrevalidationHookType(theType)) {
_installPreValidationHook(theType, module, initDatas[i]);
}
}
}
/// @notice Checks if an emergency uninstall signature is valid.
/// @param data The emergency uninstall data.
/// @param signature The signature to validate.
function _checkEmergencyUninstallSignature(EmergencyUninstall calldata data, bytes calldata signature) internal {
address validator = _handleValidator(address(bytes20(signature[0:20])));
// Hash the data
bytes32 hash = _hashTypedData(
keccak256(
abi.encode(
EMERGENCY_UNINSTALL_TYPE_HASH, data.hook, data.hookType, keccak256(data.deInitData), data.nonce
)
)
);
// Check if nonce is valid
require(!_getAccountStorage().nonces[data.nonce], InvalidNonce());
// Mark nonce as used
_getAccountStorage().nonces[data.nonce] = true;
// Check if the signature is valid
require(
(IValidator(validator).isValidSignatureWithSender(address(this), hash, signature[20:]) == ERC1271_SUCCESS),
EmergencyUninstallSigError()
);
}
/// @dev Calls the pre-validation hook for ERC-4337.
/// @param hash The hash of the user operation.
/// @param userOp The user operation data.
/// @param missingAccountFunds The amount of missing account funds.
/// @return postHash The updated hash after the pre-validation hook.
/// @return postSig The updated signature after the pre-validation hook.
function _withPreValidationHook(
bytes32 hash,
PackedUserOperation memory userOp,
uint256 missingAccountFunds
)
internal
virtual
returns (bytes32 postHash, bytes memory postSig)
{
// Get the pre-validation hook for ERC-4337
address preValidationHook = _getPreValidationHook(MODULE_TYPE_PREVALIDATION_HOOK_ERC4337);
// If no pre-validation hook is installed, return the original hash and signature
if (preValidationHook == address(0)) {
return (hash, userOp.signature);
}
// Otherwise, call the pre-validation hook and return the updated hash and signature
else {
return
IPreValidationHookERC4337(preValidationHook).preValidationHookERC4337(userOp, missingAccountFunds, hash);
}
}
/// @dev Retrieves the pre-validation hook from the storage based on the hook type.
/// @param preValidationHookType The type of the pre-validation hook.
/// @return preValidationHook The address of the pre-validation hook.
function _getPreValidationHook(uint256 preValidationHookType) internal view returns (address preValidationHook) {
preValidationHook = preValidationHookType == MODULE_TYPE_PREVALIDATION_HOOK_ERC1271
? address(_getAccountStorage().preValidationHookERC1271)
: address(_getAccountStorage().preValidationHookERC4337);
}
/// @dev Calls the pre-validation hook for ERC-1271.
/// @param hash The hash of the user operation.
/// @param signature The signature to validate.
/// @return postHash The updated hash after the pre-validation hook.
/// @return postSig The updated signature after the pre-validation hook.
function _withPreValidationHook(
bytes32 hash,
bytes calldata signature
)
internal
view
virtual
returns (bytes32 postHash, bytes memory postSig)
{
// Get the pre-validation hook for ERC-1271
address preValidationHook = _getPreValidationHook(MODULE_TYPE_PREVALIDATION_HOOK_ERC1271);
// If no pre-validation hook is installed, return the original hash and signature
if (preValidationHook == address(0)) {
return (hash, signature);
}
// Otherwise, call the pre-validation hook and return the updated hash and signature
else {
return IPreValidationHookERC1271(preValidationHook).preValidationHookERC1271(msg.sender, hash, signature);
}
}
/// @notice Checks if an enable mode signature is valid.
/// @param structHash data hash.
/// @param sig Signature.
/// @param validator Validator address.
function _checkEnableModeSignature(
bytes32 structHash,
bytes calldata sig,
address validator
)
internal
view
returns (bool)
{
bytes32 eip712Digest = _hashTypedData(structHash);
// Use standard IERC-1271/ERC-7739 interface.
// Even if the validator doesn't support 7739 under the hood, it is still secure,
// as eip712digest is already built based on 712Domain of this Smart Account
// This interface should always be exposed by validators as per ERC-7579
try IValidator(validator).isValidSignatureWithSender(address(this), eip712Digest, sig) returns (bytes4 res) {
return res == ERC1271_SUCCESS;
} catch {
return false;
}
}
/// @notice Checks if a module is installed on the smart account.
/// @param moduleTypeId The module type ID.
/// @param module The module address.
/// @param additionalContext Additional context for checking installation.
/// @return True if the module is installed, false otherwise.
function _isModuleInstalled(
uint256 moduleTypeId,
address module,
bytes calldata additionalContext
)
internal
view
returns (bool)
{
additionalContext;
if (moduleTypeId == MODULE_TYPE_VALIDATOR) {
return _isValidatorInstalled(module);
} else if (moduleTypeId == MODULE_TYPE_EXECUTOR) {
return _isExecutorInstalled(module);
} else if (moduleTypeId == MODULE_TYPE_FALLBACK) {
bytes4 selector;
if (additionalContext.length >= 4) {
selector = bytes4(additionalContext[0:4]);
} else {
selector = bytes4(0x00000000);
}
return _isFallbackHandlerInstalled(selector, module);
} else if (moduleTypeId == MODULE_TYPE_HOOK) {
return _isHookInstalled(module);
} else if (_isPrevalidationHookType(moduleTypeId)) {
return _getPreValidationHook(moduleTypeId) == module;
} else {
return false;
}
}
/// @dev Does bytecode optimized check of the module type id being a pre-validation hook type.
/// @param moduleTypeId The module type id to check.
/// return True if the module type id is a pre-validation hook type, otherwise false.
/// If theType == 8: 8 - 8 = 0, and 0 < 2 ✓
/// If theType == 9: 9 - 8 = 1, and 1 < 2 ✓
/// If theType < 8 (e.g., 7): 7 - 8 = type(uint256).max (wraps), and type(uint256).max < 2 is false ✓
/// If theType >= 10: result is >= 2, and fails the check ✓
function _isPrevalidationHookType(uint256 moduleTypeId) internal pure returns (bool res) {
assembly {
res := lt(sub(moduleTypeId, 8), 2)
}
}
/// @dev Checks if the validator list is already initialized.
/// In theory it doesn't 100% mean there is a validator or executor installed.
/// Use below functions to check for validators and executors.
function _areSentinelListsInitialized() internal view virtual returns (bool) {
// account module storage
AccountStorage storage ams = _getAccountStorage();
return ams.validators.alreadyInitialized() && ams.executors.alreadyInitialized();
}
/// @dev Checks if a fallback handler is set for a given selector.
/// @param selector The function selector to check.
/// @return True if a fallback handler is set, otherwise false.
function _isFallbackHandlerInstalled(bytes4 selector) internal view virtual returns (bool) {
FallbackHandler storage handler = _getAccountStorage().fallbacks[selector];
return handler.handler != address(0);
}
/// @dev Checks if the expected fallback handler is installed for a given selector.
/// @param selector The function selector to check.
/// @param expectedHandler The address of the handler expected to be installed.
/// @return True if the installed handler matches the expected handler, otherwise false.
function _isFallbackHandlerInstalled(bytes4 selector, address expectedHandler) internal view returns (bool) {
FallbackHandler storage handler = _getAccountStorage().fallbacks[selector];
return handler.handler == expectedHandler;
}
/// @dev Checks if a validator is currently installed.
/// @param validator The address of the validator to check.
/// @return True if the validator is installed, otherwise false.
function _isValidatorInstalled(address validator) internal view virtual returns (bool) {
return _getAccountStorage().validators.contains(validator);
}
/// @dev Checks if an executor is currently installed.
/// @param executor The address of the executor to check.
/// @return True if the executor is installed, otherwise false.
function _isExecutorInstalled(address executor) internal view virtual returns (bool) {
return _getAccountStorage().executors.contains(executor);
}
/// @dev Checks if a hook is currently installed.
/// @param hook The address of the hook to check.
/// @return True if the hook is installed, otherwise false.
function _isHookInstalled(address hook) internal view returns (bool) {
return _getHook() == hook;
}
/// @dev Retrieves the current hook from the storage.
/// @return hook The address of the current hook.
function _getHook() internal view returns (address hook) {
hook = address(_getAccountStorage().hook);
}
/// @dev Checks if the account is an ERC7702 account
function _amIERC7702() internal view returns (bool res) {
assembly {
// use extcodesize as the first cheapest check
if eq(extcodesize(address()), 23) {
// use extcodecopy to copy first 3 bytes of this contract and compare with 0xef0100
extcodecopy(address(), 0, 0, 3)
res := eq(0xef0100, shr(232, mload(0x00)))
}
// if it is not 23, we do not even check the first 3 bytes
}
}
/// @dev Returns the validator address to use
function _handleValidator(address _validator) internal view returns (address validator) {
if (_validator == address(0)) {
validator = _DEFAULT_VALIDATOR;
} else {
if (!_isValidatorInstalled(_validator)) {
assembly {
// revert ValidatorNotInstalled(address)
mstore(0x00, 0x6859e01e)
mstore(0x20, _validator)
revert(0x1c, 0x24)
}
}
validator = _validator;
}
}
/// @notice Builds the enable mode data hash as per eip712
/// @param module Module being enabled
/// @param moduleType Type of the module as per EIP-7579
/// @param userOpHash Hash of the User Operation
/// @param initData Module init data.
/// @return structHash data hash
function _getEnableModeDataHash(
address module,
uint256 moduleType,
bytes32 userOpHash,
bytes calldata initData
)
internal
pure
returns (bytes32)
{
bytes32 structHash;
assembly {
let ptr := mload(0x40)
mstore(ptr, MODULE_ENABLE_MODE_TYPE_HASH)
mstore(add(ptr, 0x20), module)
mstore(add(ptr, 0x40), moduleType)
mstore(add(ptr, 0x60), userOpHash)
// Hash initData and store at ptr + 0x80
// calldatacopy to copy initData to memory, then hash it
let initDataPtr := add(ptr, 0xa0)
calldatacopy(initDataPtr, initData.offset, initData.length)
let initDataHash := keccak256(initDataPtr, initData.length)
mstore(add(ptr, 0x80), initDataHash)
// Hash the entire struct
structHash := keccak256(ptr, 0xa0)
// restore free memory ptr
mstore(0x40, add(add(ptr, 0xa0), initData.length))
}
return structHash;
}
function _fallback(bytes calldata callData) private {
bool success;
bytes memory result;
FallbackHandler storage $fallbackHandler = _getAccountStorage().fallbacks[msg.sig];
address handler = $fallbackHandler.handler;
CallType calltype = $fallbackHandler.calltype;
if (handler != address(0)) {
// hook manually
address hook = _getHook();
bytes memory hookData;
if (hook != address(0)) {
hookData = IHook(hook).preCheck(msg.sender, msg.value, msg.data);
}
//if there's a fallback handler, call it
if (calltype == CALLTYPE_STATIC) {
(success, result) = handler.staticcall(ExecLib.get2771CallData(callData));
} else if (calltype == CALLTYPE_SINGLE) {
(success, result) = handler.call{ value: msg.value }(ExecLib.get2771CallData(callData));
} else {
revert UnsupportedCallType(calltype);
}
// Use revert message from fallback handler if the call was not successful
assembly {
if iszero(success) { revert(add(result, 0x20), mload(result)) }
}
// hook post check
if (hook != address(0)) {
IHook(hook).postCheck(hookData);
}
// return the result
assembly {
return(add(result, 0x20), mload(result))
}
}
// If there's no handler, the call can be one of onERCXXXReceived()
// No need to hook this as no execution is done here
bytes32 s;
/// @solidity memory-safe-assembly
assembly {
s := shr(224, calldataload(0))
// 0x150b7a02: `onERC721Received(address,address,uint256,bytes)`.
// 0xf23a6e61: `onERC1155Received(address,address,uint256,uint256,bytes)`.
// 0xbc197c81: `onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)`.
if or(eq(s, 0x150b7a02), or(eq(s, 0xf23a6e61), eq(s, 0xbc197c81))) {
mstore(0x20, s) // Store `msg.sig`.
return(0x3c, 0x20) // Return `msg.sig`.
}
mstore(0x00, 0x08c63e27)
mstore(0x20, shl(224, s)) // Left-align bytes4 for ABI encoding
revert(0x1c, 0x24) // revert MissingFallbackHandler(msg.sig)
}
}
/// @dev Helper function to paginate entries in a SentinelList.
/// @param list The SentinelList to paginate.
/// @param cursor The cursor to start paginating from.
/// @param size The number of entries to return.
/// @return array The array of addresses in the list.
/// @return nextCursor The cursor for the next page of entries.
function _paginate(
SentinelListLib.SentinelList storage list,
address cursor,
uint256 size
)
private
view
returns (address[] memory array, address nextCursor)
{
(array, nextCursor) = list.getEntriesPaginated(cursor, size);
}
}