-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathSemiModularAccountBase.sol
More file actions
296 lines (255 loc) · 13 KB
/
Copy pathSemiModularAccountBase.sol
File metadata and controls
296 lines (255 loc) · 13 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
// This file is part of Modular Account.
//
// Copyright 2024 Alchemy Insights, Inc.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
// Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see
// <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.28;
import {ModuleEntity, ValidationConfig} from "@erc6900/reference-implementation/interfaces/IModularAccount.sol";
import {ModuleEntityLib} from "@erc6900/reference-implementation/libraries/ModuleEntityLib.sol";
import {ValidationConfigLib} from "@erc6900/reference-implementation/libraries/ValidationConfigLib.sol";
import {IEntryPoint} from "@eth-infinitism/account-abstraction/interfaces/IEntryPoint.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import {FALLBACK_VALIDATION_ID, FALLBACK_VALIDATION_LOOKUP_KEY} from "../helpers/Constants.sol";
import {ExecutionInstallDelegate} from "../helpers/ExecutionInstallDelegate.sol";
import {SignatureType} from "../helpers/SignatureType.sol";
import {RTCallBuffer, SigCallBuffer, UOCallBuffer} from "../libraries/ExecutionLib.sol";
import {ValidationLocatorLib, ValidationLookupKey} from "../libraries/ValidationLocatorLib.sol";
import {ModularAccountBase} from "./ModularAccountBase.sol";
/// @title Semi-Modular Account Base
/// @author Alchemy
/// @notice Abstract base contract for the Alchemy Semi-Modular Account variants. Includes fallback signer
/// functionality.
/// @dev Inherits ModularAccountBase. Overrides certain functionality from ModularAccountBase, and exposes an
/// internal virtual getter for the fallback signer.
abstract contract SemiModularAccountBase is ModularAccountBase {
using MessageHashUtils for bytes32;
using ModuleEntityLib for ModuleEntity;
using ValidationConfigLib for ValidationConfig;
struct SemiModularAccountStorage {
address fallbackSigner;
bool fallbackSignerDisabled;
}
// keccak256("ReplaySafeHash(bytes32 hash)")
bytes32 private constant _REPLAY_SAFE_HASH_TYPEHASH =
0x294a8735843d4afb4f017c76faf3b7731def145ed0025fc9b1d5ce30adf113ff;
// keccak256("ERC6900.SemiModularAccount.Storage")
uint256 internal constant _SEMI_MODULAR_ACCOUNT_STORAGE_SLOT =
0x5b9dc9aa943f8fa2653ceceda5e3798f0686455280432166ba472eca0bc17a32;
uint256 internal constant _SIG_VALIDATION_PASSED = 0;
uint256 internal constant _SIG_VALIDATION_FAILED = 1;
event FallbackSignerUpdated(address indexed newFallbackSigner, bool isDisabled);
error FallbackSignerMismatch();
error FallbackValidationInstallationNotAllowed();
error FallbackSignerDisabled();
error InvalidSignatureType();
constructor(IEntryPoint entryPoint, ExecutionInstallDelegate executionInstallDelegate)
ModularAccountBase(entryPoint, executionInstallDelegate)
{}
/// @notice Updates the fallback signer data in storage.
/// @param fallbackSigner The new signer to set.
/// @param isDisabled Whether to disable fallback signing entirely.
function updateFallbackSignerData(address fallbackSigner, bool isDisabled) external wrapNativeFunction {
SemiModularAccountStorage storage _storage = _getSemiModularAccountStorage();
_storage.fallbackSigner = fallbackSigner;
_storage.fallbackSignerDisabled = isDisabled;
emit FallbackSignerUpdated(fallbackSigner, isDisabled);
}
function installValidation(
ValidationConfig validationConfig,
bytes4[] calldata selectors,
bytes calldata installData,
bytes[] calldata hooks
) external override wrapNativeFunction {
// Previously, it was possible to "alias" the fallback validation by installing a module at the reserved
// validation entity id 0. Not failing here could cause unexpected behavior, so this is checked to
// explicitly revert and warn the caller that this operation would not do what is requested.
//
// Note that this state can still be reached by upgrading from MA to SMA, but should be handled with
// initialization and de-init steps.
if (validationConfig.entityId() == FALLBACK_VALIDATION_ID && validationConfig.module() != address(0)) {
revert FallbackValidationInstallationNotAllowed();
}
_installValidation(validationConfig, selectors, installData, hooks);
}
/// @notice Returns the fallback signer data in storage.
/// @return The fallback signer and a boolean, true if the fallback signer validation is disabled, false if it
/// is enabled.
function getFallbackSignerData() external view returns (address, bool) {
SemiModularAccountStorage storage _storage = _getSemiModularAccountStorage();
return (_retrieveFallbackSignerUnchecked(_storage), _storage.fallbackSignerDisabled);
}
function _execUserOpValidation(
ValidationLookupKey validationLookupKey,
bytes32 userOpHash,
bytes calldata signatureSegment,
UOCallBuffer callBuffer
) internal override returns (uint256) {
if (validationLookupKey.eq(FALLBACK_VALIDATION_LOOKUP_KEY)) {
address fallbackSigner = _getFallbackSigner();
if (_checkSignature(fallbackSigner, userOpHash, signatureSegment)) {
return _SIG_VALIDATION_PASSED;
}
return _SIG_VALIDATION_FAILED;
}
return super._execUserOpValidation(validationLookupKey, userOpHash, signatureSegment, callBuffer);
}
function _execRuntimeValidation(
ValidationLookupKey validationLookupKey,
RTCallBuffer callBuffer,
bytes calldata authorization
) internal override {
if (validationLookupKey.eq(FALLBACK_VALIDATION_LOOKUP_KEY)) {
address fallbackSigner = _getFallbackSigner();
if (msg.sender != fallbackSigner) {
revert FallbackSignerMismatch();
}
} else {
super._execRuntimeValidation(validationLookupKey, callBuffer, authorization);
}
}
function _exec1271Validation(
SigCallBuffer buffer,
bytes32 hash,
ValidationLookupKey validationLookupKey,
bytes calldata signature
) internal view override returns (bytes4) {
if (validationLookupKey.eq(FALLBACK_VALIDATION_LOOKUP_KEY)) {
address fallbackSigner = _getFallbackSigner();
// If called during validateUserOp, this implies that we're doing a deferred validation installation.
// In this case, as the hash is already replay-safe, we don't need to wrap it.
if (msg.sig != this.validateUserOp.selector) {
hash = _replaySafeHash(hash);
}
if (_checkSignature(fallbackSigner, hash, signature)) {
return _1271_MAGIC_VALUE;
}
return _1271_INVALID;
}
return super._exec1271Validation(buffer, hash, validationLookupKey, signature);
}
function _checkSignature(address owner, bytes32 digest, bytes calldata sig) internal view returns (bool) {
if (sig.length < 1) {
revert InvalidSignatureType();
}
SignatureType sigType = SignatureType(uint8(bytes1(sig)));
sig = sig[1:];
if (sigType == SignatureType.EOA) {
(address recovered, ECDSA.RecoverError err,) = ECDSA.tryRecover(digest, sig);
if (err == ECDSA.RecoverError.NoError && recovered == owner) {
return true;
}
return false;
} else if (sigType == SignatureType.CONTRACT_OWNER) {
return SignatureChecker.isValidERC1271SignatureNow(owner, digest, sig);
}
revert InvalidSignatureType();
}
function _isValidationGlobal(ValidationLookupKey validationFunction) internal view override returns (bool) {
if (validationFunction.eq(FALLBACK_VALIDATION_LOOKUP_KEY) || super._isValidationGlobal(validationFunction))
{
return true;
}
// At this point, the validation is not the fallback, and it's not an installed global validation.
SemiModularAccountStorage storage smaStorage = _getSemiModularAccountStorage();
// Before checking direct-call validation, we return false if fallback validation is disabled.
if (smaStorage.fallbackSignerDisabled) {
return false;
}
// Retrieve the fallback signer.
address fallbackSigner = _retrieveFallbackSignerUnchecked(smaStorage);
// Compute the direct call validation key.
ValidationLookupKey fallbackDirectCallValidation = ValidationLocatorLib.directCallLookupKey(fallbackSigner);
// Return true if the validation function passed is the fallback direct call validation key, and the sender
// is the fallback signer. This enforces that context is a
return validationFunction.eq(fallbackDirectCallValidation) && msg.sender == fallbackSigner;
}
function _getFallbackSigner() internal view returns (address) {
SemiModularAccountStorage storage _storage = _getSemiModularAccountStorage();
if (_storage.fallbackSignerDisabled) {
revert FallbackSignerDisabled();
}
// This can return zero.
return _retrieveFallbackSignerUnchecked(_storage);
}
/// @dev SMA implementations must implement their own fallback signer getter.
///
/// NOTE: The passed storage pointer may point to a struct with a zero address signer. It's up
/// to inheritors to determine what to do with that information. No assumptions about storage
/// state are safe to make besides layout.
function _retrieveFallbackSignerUnchecked(SemiModularAccountStorage storage _storage)
internal
view
virtual
returns (address)
{
return _storage.fallbackSigner;
}
/// @notice Returns the replay-safe hash generated from the passed typed data hash for 1271 validation.
/// @param hash The typed data hash to wrap in a replay-safe hash.
/// @return The replay-safe hash, to be used for 1271 signature generation.
///
/// @dev Generates a replay-safe hash to wrap a standard typed data hash. This prevents replay attacks by
/// enforcing the domain separator, which includes this contract's address and the chainId. This is only
/// relevant for 1271 validation because UserOp validation relies on the UO hash and the Entrypoint has
/// safeguards.
///
/// NOTE: Like in signature-based validation modules, the returned hash should be used to generate signatures,
/// but the original hash should be passed to the external-facing function for 1271 validation.
function _replaySafeHash(bytes32 hash) internal view returns (bytes32) {
return MessageHashUtils.toTypedDataHash({
domainSeparator: _domainSeparator(), structHash: _hashStructReplaySafeHash(hash)
});
}
function _getSemiModularAccountStorage() internal pure returns (SemiModularAccountStorage storage) {
SemiModularAccountStorage storage _storage;
assembly ("memory-safe") {
_storage.slot := _SEMI_MODULAR_ACCOUNT_STORAGE_SLOT
}
return _storage;
}
// Conditionally skip allocation of call buffers.
function _validationIsNative(ValidationLookupKey validationLookupKey)
internal
pure
virtual
override
returns (bool)
{
return validationLookupKey.eq(FALLBACK_VALIDATION_LOOKUP_KEY);
}
/// @notice Adds a EIP-712 replay safe hash wrapper to the digest
/// @param hash The hash to wrap in a replay-safe hash
/// @return The replay-safe hash
function _hashStructReplaySafeHash(bytes32 hash) internal pure virtual returns (bytes32) {
bytes32 res;
assembly ("memory-safe") {
mstore(0x00, _REPLAY_SAFE_HASH_TYPEHASH)
mstore(0x20, hash)
res := keccak256(0, 0x40)
}
return res;
}
/// @dev Overrides ModularAccountView.
function _isNativeFunction(uint32 selector) internal pure virtual override returns (bool) {
return super._isNativeFunction(selector) || selector == uint32(this.updateFallbackSignerData.selector)
|| selector == uint32(this.getFallbackSignerData.selector);
}
/// @dev Overrides ModularAccountView.
function _isWrappedNativeFunction(uint32 selector) internal pure virtual override returns (bool) {
return
super._isWrappedNativeFunction(selector) || selector == uint32(this.updateFallbackSignerData.selector);
}
}