|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.11; |
| 3 | + |
| 4 | +/* solhint-disable avoid-low-level-calls */ |
| 5 | +/* solhint-disable no-inline-assembly */ |
| 6 | +/* solhint-disable reason-string */ |
| 7 | + |
| 8 | +// Base |
| 9 | +import "./../utils/BaseAccount.sol"; |
| 10 | + |
| 11 | +// Fixed Extensions |
| 12 | +import "../../extension/Multicall.sol"; |
| 13 | +import "../../dynamic-contracts/extension/Initializable.sol"; |
| 14 | + |
| 15 | +// Utils |
| 16 | +import "../../openzeppelin-presets/utils/cryptography/ECDSA.sol"; |
| 17 | +import "../../dynamic-contracts/extension/PermissionsEnumerable.sol"; |
| 18 | + |
| 19 | +// $$\ $$\ $$\ $$\ $$\ |
| 20 | +// $$ | $$ | \__| $$ | $$ | |
| 21 | +// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ |
| 22 | +// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ |
| 23 | +// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | |
| 24 | +// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | |
| 25 | +// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | |
| 26 | +// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ |
| 27 | + |
| 28 | +/*/////////////////////////////////////////////////////////////// |
| 29 | + Storage layout |
| 30 | +//////////////////////////////////////////////////////////////*/ |
| 31 | + |
| 32 | +library TWAccountStorage { |
| 33 | + bytes32 internal constant TWACCOUNT_STORAGE_POSITION = keccak256("twaccount.storage"); |
| 34 | + |
| 35 | + struct Data { |
| 36 | + uint256 nonce; |
| 37 | + } |
| 38 | + |
| 39 | + function accountStorage() internal pure returns (Data storage twaccountData) { |
| 40 | + bytes32 position = TWACCOUNT_STORAGE_POSITION; |
| 41 | + assembly { |
| 42 | + twaccountData.slot := position |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +contract TWAccountCore is Initializable, Multicall, BaseAccount { |
| 48 | + using ECDSA for bytes32; |
| 49 | + |
| 50 | + /*/////////////////////////////////////////////////////////////// |
| 51 | + State |
| 52 | + //////////////////////////////////////////////////////////////*/ |
| 53 | + |
| 54 | + bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; |
| 55 | + bytes32 public constant SIGNER_ROLE = keccak256("SIGNER_ROLE"); |
| 56 | + |
| 57 | + /// @notice EIP 4337 Entrypoint contract. |
| 58 | + IEntryPoint private immutable entrypointContract; |
| 59 | + |
| 60 | + /*/////////////////////////////////////////////////////////////// |
| 61 | + Constructor, Initializer, Modifiers |
| 62 | + //////////////////////////////////////////////////////////////*/ |
| 63 | + |
| 64 | + // solhint-disable-next-line no-empty-blocks |
| 65 | + receive() external payable virtual {} |
| 66 | + |
| 67 | + constructor(IEntryPoint _entrypoint) { |
| 68 | + entrypointContract = _entrypoint; |
| 69 | + } |
| 70 | + |
| 71 | + /// @notice Initializes the smart contract wallet. |
| 72 | + function initialize(address _defaultAdmin) public virtual initializer { |
| 73 | + _setupRole(DEFAULT_ADMIN_ROLE, _defaultAdmin); |
| 74 | + } |
| 75 | + |
| 76 | + /*/////////////////////////////////////////////////////////////// |
| 77 | + View functions |
| 78 | + //////////////////////////////////////////////////////////////*/ |
| 79 | + |
| 80 | + /// @notice Returns the nonce of the account. |
| 81 | + function nonce() public view virtual override returns (uint256) { |
| 82 | + TWAccountStorage.Data storage twaccountData = TWAccountStorage.accountStorage(); |
| 83 | + return twaccountData.nonce; |
| 84 | + } |
| 85 | + |
| 86 | + /// @notice Returns the EIP 4337 entrypoint contract. |
| 87 | + function entryPoint() public view virtual override returns (IEntryPoint) { |
| 88 | + return entrypointContract; |
| 89 | + } |
| 90 | + |
| 91 | + /// @notice Returns the balance of the account in Entrypoint. |
| 92 | + function getDeposit() public view returns (uint256) { |
| 93 | + return entryPoint().balanceOf(address(this)); |
| 94 | + } |
| 95 | + |
| 96 | + /// @notice Returns whether a signer is authorized to perform transactions using the wallet. |
| 97 | + function isValidSigner(address _signer) public view virtual returns (bool) { |
| 98 | + return _hasRole(SIGNER_ROLE, _signer) || _hasRole(DEFAULT_ADMIN_ROLE, _signer); |
| 99 | + } |
| 100 | + |
| 101 | + /*/////////////////////////////////////////////////////////////// |
| 102 | + External functions |
| 103 | + //////////////////////////////////////////////////////////////*/ |
| 104 | + |
| 105 | + /// @notice Deposit funds for this account in Entrypoint. |
| 106 | + function addDeposit() public payable { |
| 107 | + entryPoint().depositTo{ value: msg.value }(address(this)); |
| 108 | + } |
| 109 | + |
| 110 | + /// @notice Withdraw funds for this account from Entrypoint. |
| 111 | + function withdrawDepositTo(address payable withdrawAddress, uint256 amount) public { |
| 112 | + require(_hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "TWAccount: not admin"); |
| 113 | + entryPoint().withdrawTo(withdrawAddress, amount); |
| 114 | + } |
| 115 | + |
| 116 | + /*/////////////////////////////////////////////////////////////// |
| 117 | + Internal functions |
| 118 | + //////////////////////////////////////////////////////////////*/ |
| 119 | + |
| 120 | + /// @dev Validates the nonce of a user operation and updates account nonce. |
| 121 | + function _validateAndUpdateNonce(UserOperation calldata userOp) internal override { |
| 122 | + TWAccountStorage.Data storage data = TWAccountStorage.accountStorage(); |
| 123 | + require(data.nonce == userOp.nonce, "TWAccount: invalid nonce"); |
| 124 | + |
| 125 | + data.nonce += 1; |
| 126 | + } |
| 127 | + |
| 128 | + /// @notice Validates the signature of a user operation. |
| 129 | + function _validateSignature(UserOperation calldata userOp, bytes32 userOpHash) |
| 130 | + internal |
| 131 | + virtual |
| 132 | + override |
| 133 | + returns (uint256 validationData) |
| 134 | + { |
| 135 | + bytes32 hash = userOpHash.toEthSignedMessageHash(); |
| 136 | + address signer = hash.recover(userOp.signature); |
| 137 | + |
| 138 | + if (!isValidSigner(signer)) return SIG_VALIDATION_FAILED; |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + /// @notice See Permissions-hasRole |
| 143 | + function _hasRole(bytes32 _role, address _account) public view returns (bool) { |
| 144 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 145 | + return data._hasRole[_role][_account]; |
| 146 | + } |
| 147 | + |
| 148 | + /// @notice See Permissions-RoleGranted |
| 149 | + event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender); |
| 150 | + |
| 151 | + /// @notice See Permissions-setupRole |
| 152 | + function _setupRole(bytes32 role, address account) internal virtual { |
| 153 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 154 | + data._hasRole[role][account] = true; |
| 155 | + emit RoleGranted(role, account, msg.sender); |
| 156 | + } |
| 157 | +} |
0 commit comments