-
Notifications
You must be signed in to change notification settings - Fork 20
/
MSAAdvanced.sol
390 lines (363 loc) · 14.9 KB
/
MSAAdvanced.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import "./lib/ModeLib.sol";
import { ExecutionLib } from "./lib/ExecutionLib.sol";
import { ExecutionHelper } from "./core/ExecutionHelper.sol";
import { PackedUserOperation } from "account-abstraction/interfaces/PackedUserOperation.sol";
import "./interfaces/IERC7579Module.sol";
import { IERC7579Account } from "./interfaces/IERC7579Account.sol";
import { IMSA } from "./interfaces/IMSA.sol";
import { ModuleManager } from "./core/ModuleManager.sol";
import { HookManager } from "./core/HookManager.sol";
import { RegistryAdapter } from "./core/RegistryAdapter.sol";
import { HashLib } from "./lib/HashLib.sol";
import { ECDSA } from "solady/utils/ECDSA.sol";
import { Initializable } from "./lib/Initializable.sol";
/**
* @author zeroknots.eth | rhinestone.wtf
* Reference implementation of a very simple ERC7579 Account.
* This account implements CallType: SINGLE, BATCH and DELEGATECALL.
* This account implements ExecType: DEFAULT and TRY.
* Hook support is implemented
*/
contract MSAAdvanced is IMSA, ExecutionHelper, ModuleManager, HookManager, RegistryAdapter {
using ExecutionLib for bytes;
using ModeLib for ModeCode;
using ECDSA for bytes32;
/**
* @inheritdoc IERC7579Account
* @dev this function is only callable by the entry point or the account itself
* @dev this function demonstrates how to implement
* CallType SINGLE and BATCH and ExecType DEFAULT and TRY
* @dev this function demonstrates how to implement hook support (modifier)
*/
function execute(
ModeCode mode,
bytes calldata executionCalldata
)
external
payable
onlyEntryPointOrSelf
withHook
{
(CallType callType, ExecType execType,,) = mode.decode();
// check if calltype is batch or single
if (callType == CALLTYPE_BATCH) {
// destructure executionCallData according to batched exec
Execution[] calldata executions = executionCalldata.decodeBatch();
// check if execType is revert or try
if (execType == EXECTYPE_DEFAULT) _execute(executions);
else if (execType == EXECTYPE_TRY) _tryExecute(executions);
else revert UnsupportedExecType(execType);
} else if (callType == CALLTYPE_SINGLE) {
// destructure executionCallData according to single exec
(address target, uint256 value, bytes calldata callData) =
executionCalldata.decodeSingle();
// check if execType is revert or try
if (execType == EXECTYPE_DEFAULT) _execute(target, value, callData);
// TODO: implement event emission for tryExecute singleCall
else if (execType == EXECTYPE_TRY) _tryExecute(target, value, callData);
else revert UnsupportedExecType(execType);
} else if (callType == CALLTYPE_DELEGATECALL) {
// destructure executionCallData according to single exec
address delegate = address(uint160(bytes20(executionCalldata[0:20])));
bytes calldata callData = executionCalldata[20:];
// check if execType is revert or try
if (execType == EXECTYPE_DEFAULT) _executeDelegatecall(delegate, callData);
else if (execType == EXECTYPE_TRY) _tryExecuteDelegatecall(delegate, callData);
else revert UnsupportedExecType(execType);
} else {
revert UnsupportedCallType(callType);
}
}
/**
* @inheritdoc IERC7579Account
* @dev this function is only callable by an installed executor module
* @dev this function demonstrates how to implement
* CallType SINGLE and BATCH and ExecType DEFAULT and TRY
* @dev this function demonstrates how to implement hook support (modifier)
*/
function executeFromExecutor(
ModeCode mode,
bytes calldata executionCalldata
)
external
payable
onlyExecutorModule
withHook
withRegistry(msg.sender, MODULE_TYPE_EXECUTOR)
returns (
bytes[] memory returnData // TODO returnData is not used
)
{
(CallType callType, ExecType execType,,) = mode.decode();
// check if calltype is batch or single
if (callType == CALLTYPE_BATCH) {
// destructure executionCallData according to batched exec
Execution[] calldata executions = executionCalldata.decodeBatch();
// check if execType is revert or try
if (execType == EXECTYPE_DEFAULT) returnData = _execute(executions);
else if (execType == EXECTYPE_TRY) returnData = _tryExecute(executions);
else revert UnsupportedExecType(execType);
} else if (callType == CALLTYPE_SINGLE) {
// destructure executionCallData according to single exec
(address target, uint256 value, bytes calldata callData) =
executionCalldata.decodeSingle();
returnData = new bytes[](1);
bool success;
// check if execType is revert or try
if (execType == EXECTYPE_DEFAULT) {
returnData[0] = _execute(target, value, callData);
}
// TODO: implement event emission for tryExecute singleCall
else if (execType == EXECTYPE_TRY) {
(success, returnData[0]) = _tryExecute(target, value, callData);
if (!success) emit TryExecuteUnsuccessful(0, returnData[0]);
} else {
revert UnsupportedExecType(execType);
}
} else if (callType == CALLTYPE_DELEGATECALL) {
// destructure executionCallData according to single exec
address delegate = address(uint160(bytes20(executionCalldata[0:20])));
bytes calldata callData = executionCalldata[20:];
// check if execType is revert or try
if (execType == EXECTYPE_DEFAULT) _executeDelegatecall(delegate, callData);
else if (execType == EXECTYPE_TRY) _tryExecuteDelegatecall(delegate, callData);
else revert UnsupportedExecType(execType);
} else {
revert UnsupportedCallType(callType);
}
}
/**
* @dev ERC-4337 executeUserOp according to ERC-4337 v0.7
* This function is intended to be called by ERC-4337 EntryPoint.sol
* @dev Ensure adequate authorization control: i.e. onlyEntryPointOrSelf
* The implementation of the function is OPTIONAL
*
* @param userOp PackedUserOperation struct (see ERC-4337 v0.7+)
*/
function executeUserOp(
PackedUserOperation calldata userOp,
bytes32 // userOpHash
)
external
payable
onlyEntryPoint
{
bytes calldata callData = userOp.callData[4:];
(bool success,) = address(this).delegatecall(callData);
if (!success) revert ExecutionFailed();
}
/**
* @inheritdoc IERC7579Account
*/
function installModule(
uint256 moduleTypeId,
address module,
bytes calldata initData
)
external
payable
onlyEntryPointOrSelf
withHook
withRegistry(module, moduleTypeId)
{
if (!IModule(module).isModuleType(moduleTypeId)) revert MismatchModuleTypeId(moduleTypeId);
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 revert UnsupportedModuleType(moduleTypeId);
emit ModuleInstalled(moduleTypeId, module);
}
/**
* @inheritdoc IERC7579Account
*/
function uninstallModule(
uint256 moduleTypeId,
address module,
bytes calldata deInitData
)
external
payable
onlyEntryPointOrSelf
withHook
{
if (moduleTypeId == MODULE_TYPE_VALIDATOR) {
_uninstallValidator(module, deInitData);
} else if (moduleTypeId == MODULE_TYPE_EXECUTOR) {
_uninstallExecutor(module, deInitData);
} else if (moduleTypeId == MODULE_TYPE_FALLBACK) {
_uninstallFallbackHandler(module, deInitData);
} else if (moduleTypeId == MODULE_TYPE_HOOK) {
_uninstallHook(module, deInitData);
} else {
revert UnsupportedModuleType(moduleTypeId);
}
emit ModuleUninstalled(moduleTypeId, module);
}
/**
* @dev ERC-4337 validateUserOp according to ERC-4337 v0.7
* This function is intended to be called by ERC-4337 EntryPoint.sol
* this validation function should decode / sload the validator module to validate the userOp
* and call it.
*
* @dev MSA MUST implement this function signature.
* @param userOp PackedUserOperation struct (see ERC-4337 v0.7+)
*/
function validateUserOp(
PackedUserOperation memory userOp,
bytes32 userOpHash,
uint256 missingAccountFunds
)
external
payable
virtual
onlyEntryPoint
payPrefund(missingAccountFunds)
returns (uint256 validSignature)
{
address validator;
// @notice validator encoding in nonce is just an example!
// @notice this is not part of the standard!
// Account Vendors may choose any other way to implement validator selection
uint256 nonce = userOp.nonce;
assembly {
validator := shr(96, nonce)
}
// check if validator is enabled. If not terminate the validation phase.
if (!_isValidatorInstalled(validator)) {
// if the account is not initialized, then allow initialization with
// 7702 eoa signature
if (!isAlreadyInitialized()) {
(bytes memory initData, bytes memory eoaSignature, bytes memory signature) =
abi.decode(userOp.signature, (bytes, bytes, bytes));
(address bootstrap, bytes memory bootstrapCall) =
abi.decode(initData, (address, bytes));
// Hash the initData and recover the signer
bytes32 hash = HashLib.hash(bootstrap, bootstrapCall);
address signer = ECDSA.recover(hash.toEthSignedMessageHash(), eoaSignature);
// check if the signer is the account
if (signer != address(this)) {
return VALIDATION_FAILED;
}
_initModuleManager();
_initAccount(bootstrap, bootstrapCall);
userOp.signature = signature;
} else {
return VALIDATION_FAILED;
}
}
// bubble up the return value of the validator module
validSignature = IValidator(validator).validateUserOp(userOp, userOpHash);
}
/**
* @dev ERC-1271 isValidSignature
* This function is intended to be used to validate a smart account signature
* and may forward the call to a validator module
*
* @param hash The hash of the data that is signed
* @param data The data that is signed
*/
function isValidSignature(
bytes32 hash,
bytes calldata data
)
external
view
virtual
override
returns (bytes4)
{
address validator = address(bytes20(data[0:20]));
if (!_isValidatorInstalled(validator)) revert InvalidModule(validator);
return IValidator(validator).isValidSignatureWithSender(msg.sender, hash, data[20:]);
}
/**
* @inheritdoc IERC7579Account
*/
function isModuleInstalled(
uint256 moduleTypeId,
address module,
bytes calldata additionalContext
)
external
view
override
returns (bool)
{
if (moduleTypeId == MODULE_TYPE_VALIDATOR) {
return _isValidatorInstalled(module);
} else if (moduleTypeId == MODULE_TYPE_EXECUTOR) {
return _isExecutorInstalled(module);
} else if (moduleTypeId == MODULE_TYPE_FALLBACK) {
return _isFallbackHandlerInstalled(abi.decode(additionalContext, (bytes4)), module);
} else if (moduleTypeId == MODULE_TYPE_HOOK) {
return _isHookInstalled(module);
} else {
return false;
}
}
/**
* @inheritdoc IERC7579Account
*/
function accountId() external view virtual override returns (string memory) {
// vendor.flavour.SemVer
return "uMSA.advanced/withHook.v0.1";
}
/**
* @inheritdoc IERC7579Account
*/
function supportsExecutionMode(ModeCode mode)
external
view
virtual
override
returns (bool isSupported)
{
(CallType callType, ExecType execType,,) = mode.decode();
if (callType == CALLTYPE_BATCH) isSupported = true;
else if (callType == CALLTYPE_SINGLE) isSupported = true;
else if (callType == CALLTYPE_DELEGATECALL) isSupported = true;
// if callType is not single, batch or delegatecall return false
else return false;
if (execType == EXECTYPE_DEFAULT) isSupported = true;
else if (execType == EXECTYPE_TRY) isSupported = true;
// if execType is not default or try, return false
else return false;
}
/**
* @inheritdoc IERC7579Account
*/
function supportsModule(uint256 modulTypeId) external view virtual override returns (bool) {
if (modulTypeId == MODULE_TYPE_VALIDATOR) return true;
else if (modulTypeId == MODULE_TYPE_EXECUTOR) return true;
else if (modulTypeId == MODULE_TYPE_FALLBACK) return true;
else if (modulTypeId == MODULE_TYPE_HOOK) return true;
else return false;
}
/**
* @dev Initializes the account. Function might be called directly, or by a Factory
* @param data. encoded data that can be used during the initialization phase
*/
function initializeAccount(bytes calldata data) public payable virtual {
// protect this function to only be callable when used with the proxy factory
Initializable.checkInitializable();
// checks if already initialized and reverts before setting the state to initialized
_initModuleManager();
// bootstrap the account
(address bootstrap, bytes memory bootstrapCall) = abi.decode(data, (address, bytes));
_initAccount(bootstrap, bootstrapCall);
}
/**
* @dev Bootstrap function to initialize the account
* @param bootstrap. address of the bootstrap contract,
* @param bootstrapCall. encoded data that can be used during the initialization phase
*/
function _initAccount(address bootstrap, bytes memory bootstrapCall) private {
// this is just implemented for demonstration purposes. You can use any other initialization
// logic here.
(bool success,) = bootstrap.delegatecall(bootstrapCall);
if (!success) revert();
}
}