-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathAccount.sol
491 lines (407 loc) · 15 KB
/
Account.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
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "erc6551/interfaces/IERC6551Account.sol";
import "erc6551/lib/ERC6551AccountLib.sol";
import "openzeppelin-contracts/utils/cryptography/ECDSA.sol";
import "openzeppelin-contracts/utils/introspection/IERC165.sol";
import "openzeppelin-contracts/token/ERC721/IERC721.sol";
import "openzeppelin-contracts/token/ERC721/IERC721Receiver.sol";
import "openzeppelin-contracts/token/ERC1155/IERC1155Receiver.sol";
import "openzeppelin-contracts/interfaces/IERC1271.sol";
import "openzeppelin-contracts/utils/cryptography/SignatureChecker.sol";
import "openzeppelin-contracts/proxy/utils/UUPSUpgradeable.sol";
import "openzeppelin-contracts/utils/cryptography/EIP712.sol";
import {BaseAccount as BaseERC4337Account, IEntryPoint, UserOperation} from "account-abstraction/core/BaseAccount.sol";
import "./interfaces/IAccountGuardian.sol";
error NotAuthorized();
error InvalidInput();
error AccountLocked();
error ExceedsMaxLockTime();
error UntrustedImplementation();
error OwnershipCycle();
/**
* @title A smart contract account owned by a single ERC721 token
*/
contract Account is
IERC165,
IERC1271,
IERC6551Account,
IERC721Receiver,
IERC1155Receiver,
UUPSUpgradeable,
BaseERC4337Account,
EIP712
{
using ECDSA for bytes32;
/// @dev EIP-712 name
string public constant NAME = "ERC6551-Account";
/// @dev EIP-712 version
string public constant VERSION = "1";
/// @dev EIP-712 Type for UserOperation
bytes32 public immutable userOperationType;
/// @dev ERC-4337 entry point address
address public immutable _entryPoint;
/// @dev AccountGuardian contract address
address public immutable guardian;
/// @dev timestamp at which this account will be unlocked
uint256 public lockedUntil;
/// @dev mapping from owner => selector => implementation
mapping(address => mapping(bytes4 => address)) public overrides;
/// @dev mapping from owner => caller => has permissions
mapping(address => mapping(address => bool)) public permissions;
event OverrideUpdated(
address owner,
bytes4 selector,
address implementation
);
event PermissionUpdated(address owner, address caller, bool hasPermission);
event LockUpdated(uint256 lockedUntil);
/// @dev reverts if caller is not the owner of the account
modifier onlyOwner() {
if (msg.sender != owner()) revert NotAuthorized();
_;
}
/// @dev reverts if caller is not authorized to execute on this account
modifier onlyAuthorized() {
if (!isAuthorized(msg.sender)) revert NotAuthorized();
_;
}
/// @dev reverts if this account is currently locked
modifier onlyUnlocked() {
if (isLocked()) revert AccountLocked();
_;
}
constructor(address _guardian, address entryPoint_)
EIP712(NAME, VERSION)
{
if (_guardian == address(0) || entryPoint_ == address(0)) {
revert InvalidInput();
}
_entryPoint = entryPoint_;
guardian = _guardian;
userOperationType = keccak256(
"UserOperation(address sender,uint256 nonce,bytes initCode,bytes callData,uint256 callGasLimit,uint256 verificationGasLimit,uint256 preVerificationGas,uint256 maxFeePerGas,uint256 maxPriorityFeePerGas,bytes paymasterAndData,bytes32 userOpHash)"
);
}
/// @dev allows eth transfers by default, but allows account owner to override
receive() external payable {
_handleOverride();
}
/// @dev allows account owner to add additional functions to the account via an override
fallback() external payable {
_handleOverride();
}
/// @dev executes a low-level call against an account if the caller is authorized to make calls
function executeCall(
address to,
uint256 value,
bytes calldata data
) external payable onlyAuthorized onlyUnlocked returns (bytes memory) {
emit TransactionExecuted(to, value, data);
_incrementNonce();
return _call(to, value, data);
}
/// @dev sets the implementation address for a given function call
function setOverrides(
bytes4[] calldata selectors,
address[] calldata implementations
) external onlyUnlocked {
address _owner = owner();
if (msg.sender != _owner) revert NotAuthorized();
uint256 length = selectors.length;
if (implementations.length != length) revert InvalidInput();
for (uint256 i = 0; i < length; i++) {
overrides[_owner][selectors[i]] = implementations[i];
emit OverrideUpdated(_owner, selectors[i], implementations[i]);
}
_incrementNonce();
}
/// @dev grants a given caller execution permissions
function setPermissions(
address[] calldata callers,
bool[] calldata _permissions
) external onlyUnlocked {
address _owner = owner();
if (msg.sender != _owner) revert NotAuthorized();
uint256 length = callers.length;
if (_permissions.length != length) revert InvalidInput();
for (uint256 i = 0; i < length; i++) {
permissions[_owner][callers[i]] = _permissions[i];
emit PermissionUpdated(_owner, callers[i], _permissions[i]);
}
_incrementNonce();
}
/// @dev locks the account until a certain timestamp
function lock(uint256 _lockedUntil) external onlyOwner onlyUnlocked {
if (_lockedUntil > block.timestamp + 365 days)
revert ExceedsMaxLockTime();
lockedUntil = _lockedUntil;
emit LockUpdated(_lockedUntil);
_incrementNonce();
}
/// @dev returns the current lock status of the account as a boolean
function isLocked() public view returns (bool) {
return lockedUntil > block.timestamp;
}
/// @dev EIP-1271 signature validation. By default, only the owner of the account is permissioned to sign.
/// This function can be overriden.
function isValidSignature(bytes32 hash, bytes memory signature)
external
view
returns (bytes4 magicValue)
{
_handleOverrideStatic();
bool isValid = SignatureChecker.isValidSignatureNow(
owner(),
hash,
signature
);
if (isValid) {
return IERC1271.isValidSignature.selector;
}
return "";
}
/// @dev Returns the EIP-155 chain ID, token contract address, and token ID for the token that
/// owns this account.
function token()
external
view
returns (
uint256 chainId,
address tokenContract,
uint256 tokenId
)
{
return ERC6551AccountLib.token();
}
/// @dev Returns the current account nonce
function nonce() public view override returns (uint256) {
return IEntryPoint(_entryPoint).getNonce(address(this), 0);
}
/// @dev Increments the account nonce if the caller is not the ERC-4337 entry point
function _incrementNonce() internal {
if (msg.sender != _entryPoint)
IEntryPoint(_entryPoint).incrementNonce(0);
}
/// @dev Return the ERC-4337 entry point address
function entryPoint() public view override returns (IEntryPoint) {
return IEntryPoint(_entryPoint);
}
/// @dev Returns the owner of the ERC-721 token which owns this account. By default, the owner
/// of the token has full permissions on the account.
function owner() public view returns (address) {
(
uint256 chainId,
address tokenContract,
uint256 tokenId
) = ERC6551AccountLib.token();
if (chainId != block.chainid) return address(0);
return IERC721(tokenContract).ownerOf(tokenId);
}
/// @dev Returns the authorization status for a given caller
function isAuthorized(address caller) public view returns (bool) {
// authorize entrypoint for 4337 transactions
if (caller == _entryPoint) return true;
(
uint256 chainId,
address tokenContract,
uint256 tokenId
) = ERC6551AccountLib.token();
address _owner = IERC721(tokenContract).ownerOf(tokenId);
// authorize token owner
if (caller == _owner) return true;
// authorize caller if owner has granted permissions
if (permissions[_owner][caller]) return true;
// authorize trusted cross-chain executors if not on native chain
if (
chainId != block.chainid &&
IAccountGuardian(guardian).isTrustedExecutor(caller)
) return true;
return false;
}
/// @dev Returns true if a given interfaceId is supported by this account. This method can be
/// extended by an override.
function supportsInterface(bytes4 interfaceId)
public
view
override
returns (bool)
{
bool defaultSupport = interfaceId == type(IERC165).interfaceId ||
interfaceId == type(IERC1155Receiver).interfaceId ||
interfaceId == type(IERC6551Account).interfaceId;
if (defaultSupport) return true;
// if not supported by default, check override
_handleOverrideStatic();
return false;
}
/// @dev Allows ERC-721 tokens to be received so long as they do not cause an ownership cycle.
/// This function can be overriden.
function onERC721Received(
address,
address,
uint256 receivedTokenId,
bytes memory
) public view override returns (bytes4) {
_handleOverrideStatic();
(
uint256 chainId,
address tokenContract,
uint256 tokenId
) = ERC6551AccountLib.token();
if (
chainId == block.chainid &&
tokenContract == msg.sender &&
tokenId == receivedTokenId
) revert OwnershipCycle();
return this.onERC721Received.selector;
}
/// @dev Allows ERC-1155 tokens to be received. This function can be overriden.
function onERC1155Received(
address,
address,
uint256,
uint256,
bytes memory
) public view override returns (bytes4) {
_handleOverrideStatic();
return this.onERC1155Received.selector;
}
/// @dev Allows ERC-1155 token batches to be received. This function can be overriden.
function onERC1155BatchReceived(
address,
address,
uint256[] memory,
uint256[] memory,
bytes memory
) public view override returns (bytes4) {
_handleOverrideStatic();
return this.onERC1155BatchReceived.selector;
}
/// @dev
function buildUserOp712Hash(
UserOperation memory op,
bytes32 opHash
) public view returns (bytes32 op712Hash) {
bytes32 domainSeparator = keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
),
keccak256(bytes(NAME)),
keccak256(bytes(VERSION)),
block.chainid,
op.sender
)
);
bytes32 hashStruct = keccak256(
abi.encode(
keccak256(
"UserOperation(address sender,uint256 nonce,bytes initCode,bytes callData,uint256 callGasLimit,uint256 verificationGasLimit,uint256 preVerificationGas,uint256 maxFeePerGas,uint256 maxPriorityFeePerGas,bytes paymasterAndData,bytes32 userOpHash)"
),
op.sender,
op.nonce,
op.initCode,
op.callData,
op.callGasLimit,
op.verificationGasLimit,
op.preVerificationGas,
op.maxFeePerGas,
op.maxPriorityFeePerGas,
op.paymasterAndData,
opHash
)
);
op712Hash = ECDSA.toTypedDataHash(domainSeparator, hashStruct);
}
/// @dev Contract upgrades can only be performed by the owner and the new implementation must
/// be trusted
function _authorizeUpgrade(address newImplementation)
internal
view
override
onlyOwner
{
bool isTrusted = IAccountGuardian(guardian).isTrustedImplementation(
newImplementation
);
if (!isTrusted) revert UntrustedImplementation();
}
/// @dev Validates a signature for a given ERC-4337 operation
function _validateSignature(
UserOperation calldata userOp,
bytes32 userOpHash
) internal view override returns (uint256 validationData) {
UserOperation memory userOpMemory = userOp;
bytes32 hashStruct = keccak256(
abi.encode(
userOperationType,
userOpMemory.sender,
userOpMemory.nonce,
userOpMemory.initCode,
userOpMemory.callData,
userOpMemory.callGasLimit,
userOpMemory.verificationGasLimit,
userOpMemory.preVerificationGas,
userOpMemory.maxFeePerGas,
userOpMemory.maxPriorityFeePerGas,
userOpMemory.paymasterAndData,
userOpHash
)
);
bool isValid =
this.isValidSignature(_hashTypedDataV4(hashStruct), userOp.signature) == IERC1271.isValidSignature.selector;
if (isValid) {
return 0;
}
return 1;
}
/// @dev Executes a low-level call
function _call(
address to,
uint256 value,
bytes calldata data
) internal returns (bytes memory result) {
bool success;
(success, result) = to.call{value: value}(data);
if (!success) {
assembly {
revert(add(result, 32), mload(result))
}
}
}
/// @dev Executes a low-level call to the implementation if an override is set
function _handleOverride() internal {
address implementation = overrides[owner()][msg.sig];
if (implementation != address(0)) {
bytes memory result = _call(implementation, msg.value, msg.data);
assembly {
return(add(result, 32), mload(result))
}
}
}
/// @dev Executes a low-level static call
function _callStatic(address to, bytes calldata data)
internal
view
returns (bytes memory result)
{
bool success;
(success, result) = to.staticcall(data);
if (!success) {
assembly {
revert(add(result, 32), mload(result))
}
}
}
/// @dev Executes a low-level static call to the implementation if an override is set
function _handleOverrideStatic() internal view {
address implementation = overrides[owner()][msg.sig];
if (implementation != address(0)) {
bytes memory result = _callStatic(implementation, msg.data);
assembly {
return(add(result, 32), mload(result))
}
}
}
}