-
Notifications
You must be signed in to change notification settings - Fork 25
/
SemiModularAccount.sol
213 lines (172 loc) · 8.52 KB
/
SemiModularAccount.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;
import {ReferenceModularAccount} from "./ReferenceModularAccount.sol";
import {IEntryPoint} from "@eth-infinitism/account-abstraction/interfaces/IEntryPoint.sol";
import {PackedUserOperation} from "@eth-infinitism/account-abstraction/interfaces/PackedUserOperation.sol";
import {ModuleEntityLib} from "../libraries/ModuleEntityLib.sol";
import {IModularAccount, ModuleEntity, ValidationConfig} from "../interfaces/IModularAccount.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import {LibClone} from "solady/utils/LibClone.sol";
contract SemiModularAccount is ReferenceModularAccount {
using MessageHashUtils for bytes32;
using ModuleEntityLib for ModuleEntity;
struct SemiModularAccountStorage {
address fallbackSigner;
bool fallbackSignerDisabled;
}
// keccak256("ERC6900.SemiModularAccount.Storage")
uint256 internal constant _SEMI_MODULAR_ACCOUNT_STORAGE_SLOT =
0x5b9dc9aa943f8fa2653ceceda5e3798f0686455280432166ba472eca0bc17a32;
// keccak256("EIP712Domain(uint256 chainId,address verifyingContract)")
bytes32 private constant _DOMAIN_SEPARATOR_TYPEHASH =
0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218;
// keccak256("ReplaySafeHash(bytes32 hash)")
bytes32 private constant _REPLAY_SAFE_HASH_TYPEHASH =
0x294a8735843d4afb4f017c76faf3b7731def145ed0025fc9b1d5ce30adf113ff;
ModuleEntity internal constant _FALLBACK_VALIDATION = ModuleEntity.wrap(bytes24(type(uint192).max));
uint256 internal constant _SIG_VALIDATION_PASSED = 0;
uint256 internal constant _SIG_VALIDATION_FAILED = 1;
event FallbackSignerSet(address indexed previousFallbackSigner, address indexed newFallbackSigner);
event FallbackSignerDisabledSet(bool prevDisabled, bool newDisabled);
error FallbackSignerMismatch();
error FallbackSignerDisabled();
error InitializerDisabled();
constructor(IEntryPoint anEntryPoint) ReferenceModularAccount(anEntryPoint) {}
/// @notice Updates the fallback signer address in storage.
/// @dev This function causes the fallback signer getter to ignore the bytecode signer if it is nonzero. It can
/// also be used to revert back to the bytecode signer by setting to zero.
/// @param fallbackSigner The new signer to set.
function updateFallbackSigner(address fallbackSigner) external wrapNativeFunction {
SemiModularAccountStorage storage _storage = _getSemiModularAccountStorage();
emit FallbackSignerSet(_storage.fallbackSigner, fallbackSigner);
_storage.fallbackSigner = fallbackSigner;
}
/// @notice Sets whether the fallback signer validation should be enabled or disabled.
/// @dev Due to being initially zero, we need to store "disabled" rather than "enabled" in storage.
/// @param isDisabled True to disable fallback signer validation, false to enable it.
function setFallbackSignerDisabled(bool isDisabled) external wrapNativeFunction {
SemiModularAccountStorage storage _storage = _getSemiModularAccountStorage();
emit FallbackSignerDisabledSet(_storage.fallbackSignerDisabled, isDisabled);
_storage.fallbackSignerDisabled = isDisabled;
}
/// @notice Returns whether the fallback signer validation is disabled.
/// @return True if the fallback signer validation is disabled, false if it is enabled.
function isFallbackSignerDisabled() external view returns (bool) {
return _getSemiModularAccountStorage().fallbackSignerDisabled;
}
/// @notice Returns the fallback signer associated with this account, regardless if the fallback signer
/// validation is enabled or not.
/// @return The fallback signer address, either overriden in storage, or read from bytecode.
function getFallbackSigner() external view returns (address) {
return _retrieveFallbackSignerUnchecked(_getSemiModularAccountStorage());
}
/// Override reverts on initialization, effectively disabling the initializer.
function initializeWithValidation(ValidationConfig, bytes4[] calldata, bytes calldata, bytes[] calldata)
external
pure
override
{
revert InitializerDisabled();
}
/// @inheritdoc IModularAccount
function accountId() external pure override returns (string memory) {
return "erc6900.reference-semi-modular-account.0.8.0";
}
function replaySafeHash(bytes32 hash) public view virtual returns (bytes32) {
return
MessageHashUtils.toTypedDataHash({domainSeparator: _domainSeparator(), structHash: _hashStruct(hash)});
}
function _execUserOpValidation(
ModuleEntity userOpValidationFunction,
PackedUserOperation memory userOp,
bytes32 userOpHash
) internal override returns (uint256) {
if (userOpValidationFunction.eq(_FALLBACK_VALIDATION)) {
address fallbackSigner = _getFallbackSigner();
if (
SignatureChecker.isValidSignatureNow(
fallbackSigner, userOpHash.toEthSignedMessageHash(), userOp.signature
)
) {
return _SIG_VALIDATION_PASSED;
}
return _SIG_VALIDATION_FAILED;
}
return super._execUserOpValidation(userOpValidationFunction, userOp, userOpHash);
}
function _execRuntimeValidation(
ModuleEntity runtimeValidationFunction,
bytes calldata callData,
bytes calldata authorization
) internal override {
if (runtimeValidationFunction.eq(_FALLBACK_VALIDATION)) {
address fallbackSigner = _getFallbackSigner();
if (msg.sender != fallbackSigner) {
revert FallbackSignerMismatch();
}
} else {
super._execRuntimeValidation(runtimeValidationFunction, callData, authorization);
}
}
function _exec1271Validation(ModuleEntity sigValidation, bytes32 hash, bytes calldata signature)
internal
view
override
returns (bytes4)
{
if (sigValidation.eq(_FALLBACK_VALIDATION)) {
address fallbackSigner = _getFallbackSigner();
if (SignatureChecker.isValidSignatureNow(fallbackSigner, replaySafeHash(hash), signature)) {
return _1271_MAGIC_VALUE;
}
return _1271_INVALID;
}
return super._exec1271Validation(sigValidation, hash, signature);
}
function _globalValidationAllowed(bytes4 selector) internal view override returns (bool) {
return selector == this.setFallbackSignerDisabled.selector
|| selector == this.updateFallbackSigner.selector || super._globalValidationAllowed(selector);
}
function _isValidationGlobal(ModuleEntity validationFunction) internal view override returns (bool) {
return validationFunction.eq(_FALLBACK_VALIDATION) || super._isValidationGlobal(validationFunction);
}
function _getFallbackSigner() internal view returns (address) {
SemiModularAccountStorage storage _storage = _getSemiModularAccountStorage();
if (_storage.fallbackSignerDisabled) {
revert FallbackSignerDisabled();
}
return _retrieveFallbackSignerUnchecked(_storage);
}
function _retrieveFallbackSignerUnchecked(SemiModularAccountStorage storage _storage)
internal
view
returns (address)
{
address storageFallbackSigner = _storage.fallbackSigner;
if (storageFallbackSigner != address(0)) {
return storageFallbackSigner;
}
bytes memory appendedData = LibClone.argsOnERC1967(address(this), 0, 20);
return address(uint160(bytes20(appendedData)));
}
function _domainSeparator() internal view returns (bytes32) {
return keccak256(abi.encode(_DOMAIN_SEPARATOR_TYPEHASH, block.chainid, address(this)));
}
function _getSemiModularAccountStorage() internal pure returns (SemiModularAccountStorage storage) {
SemiModularAccountStorage storage _storage;
assembly ("memory-safe") {
_storage.slot := _SEMI_MODULAR_ACCOUNT_STORAGE_SLOT
}
return _storage;
}
function _hashStruct(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;
}
}