|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +import {PackedUserOperation, IAccount, IEntryPoint, IAccountExecute} from "@openzeppelin/contracts/interfaces/draft-IERC4337.sol"; |
| 6 | +import {ERC4337Utils} from "@openzeppelin/contracts/account/utils/draft-ERC4337Utils.sol"; |
| 7 | +import {Address} from "@openzeppelin/contracts/utils/Address.sol"; |
| 8 | +import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; |
| 9 | +import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol"; |
| 10 | +import {AbstractSigner} from "../utils/cryptography/AbstractSigner.sol"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @dev A simple ERC4337 account implementation. This base implementation only includes the minimal logic to process |
| 14 | + * user operations. |
| 15 | + * |
| 16 | + * Developers must implement the {AbstractSigner-_rawSignatureValidation} function to define the account's validation logic. |
| 17 | + * |
| 18 | + * IMPORTANT: Implementing a mechanism to validate signatures is a security-sensitive operation as it may allow an |
| 19 | + * attacker to bypass the account's security measures. Check out {SignerECDSA}, {SignerP256}, or {SignerRSA} for |
| 20 | + * digital signature validation implementations. |
| 21 | + */ |
| 22 | +abstract contract AccountCore is AbstractSigner, EIP712, IAccount, IAccountExecute { |
| 23 | + using MessageHashUtils for bytes32; |
| 24 | + |
| 25 | + bytes32 internal constant _PACKED_USER_OPERATION = |
| 26 | + keccak256( |
| 27 | + "PackedUserOperation(address sender,uint256 nonce,bytes initCode,bytes callData,bytes32 accountGasLimits,uint256 preVerificationGas,bytes32 gasFees,bytes paymasterAndData,address entrypoint)" |
| 28 | + ); |
| 29 | + |
| 30 | + /** |
| 31 | + * @dev Unauthorized call to the account. |
| 32 | + */ |
| 33 | + error AccountUnauthorized(address sender); |
| 34 | + |
| 35 | + /** |
| 36 | + * @dev Revert if the caller is not the entry point or the account itself. |
| 37 | + */ |
| 38 | + modifier onlyEntryPointOrSelf() { |
| 39 | + _checkEntryPointOrSelf(); |
| 40 | + _; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @dev Revert if the caller is not the entry point. |
| 45 | + */ |
| 46 | + modifier onlyEntryPoint() { |
| 47 | + _checkEntryPoint(); |
| 48 | + _; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dev Canonical entry point for the account that forwards and validates user operations. |
| 53 | + */ |
| 54 | + function entryPoint() public view virtual returns (IEntryPoint) { |
| 55 | + return IEntryPoint(0x0000000071727De22E5E9d8BAf0edAc6f37da032); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @dev Return the account nonce for the canonical sequence. |
| 60 | + */ |
| 61 | + function getNonce() public view virtual returns (uint256) { |
| 62 | + return getNonce(0); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dev Return the account nonce for a given sequence (key). |
| 67 | + */ |
| 68 | + function getNonce(uint192 key) public view virtual returns (uint256) { |
| 69 | + return entryPoint().getNonce(address(this), key); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritdoc IAccount |
| 74 | + */ |
| 75 | + function validateUserOp( |
| 76 | + PackedUserOperation calldata userOp, |
| 77 | + bytes32 userOpHash, |
| 78 | + uint256 missingAccountFunds |
| 79 | + ) public virtual onlyEntryPoint returns (uint256) { |
| 80 | + uint256 validationData = _rawSignatureValidation(_signableUserOpHash(userOp, userOpHash), userOp.signature) |
| 81 | + ? ERC4337Utils.SIG_VALIDATION_SUCCESS |
| 82 | + : ERC4337Utils.SIG_VALIDATION_FAILED; |
| 83 | + _payPrefund(missingAccountFunds); |
| 84 | + return validationData; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @inheritdoc IAccountExecute |
| 89 | + */ |
| 90 | + function executeUserOp( |
| 91 | + PackedUserOperation calldata userOp, |
| 92 | + bytes32 /*userOpHash*/ |
| 93 | + ) public virtual onlyEntryPointOrSelf { |
| 94 | + (address target, uint256 value, bytes memory data) = abi.decode(userOp.callData[4:], (address, uint256, bytes)); |
| 95 | + Address.functionCallWithValue(target, data, value); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @dev Returns the digest used by an offchain signer instead of the opaque `userOpHash`. |
| 100 | + * |
| 101 | + * Given the `userOpHash` calculation is defined by ERC-4337, offchain signers |
| 102 | + * may need to sign again this hash by rehashing it with other schemes (e.g. ERC-191). |
| 103 | + * |
| 104 | + * Returns a typehash following EIP-712 typed data hashing for readability. |
| 105 | + */ |
| 106 | + function _signableUserOpHash( |
| 107 | + PackedUserOperation calldata userOp, |
| 108 | + bytes32 /* userOpHash */ |
| 109 | + ) internal view virtual returns (bytes32) { |
| 110 | + return |
| 111 | + _hashTypedDataV4( |
| 112 | + keccak256( |
| 113 | + abi.encode( |
| 114 | + _PACKED_USER_OPERATION, |
| 115 | + userOp.sender, |
| 116 | + userOp.nonce, |
| 117 | + keccak256(userOp.initCode), |
| 118 | + keccak256(userOp.callData), |
| 119 | + userOp.accountGasLimits, |
| 120 | + userOp.preVerificationGas, |
| 121 | + userOp.gasFees, |
| 122 | + keccak256(userOp.paymasterAndData), |
| 123 | + entryPoint() |
| 124 | + ) |
| 125 | + ) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @dev Sends the missing funds for executing the user operation to the {entrypoint}. |
| 131 | + * The `missingAccountFunds` must be defined by the entrypoint when calling {validateUserOp}. |
| 132 | + */ |
| 133 | + function _payPrefund(uint256 missingAccountFunds) internal virtual { |
| 134 | + if (missingAccountFunds > 0) { |
| 135 | + (bool success, ) = payable(msg.sender).call{value: missingAccountFunds}(""); |
| 136 | + success; // Silence warning. The entrypoint should validate the result. |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @dev Ensures the caller is the {entrypoint}. |
| 142 | + */ |
| 143 | + function _checkEntryPoint() internal view virtual { |
| 144 | + address sender = msg.sender; |
| 145 | + if (sender != address(entryPoint())) { |
| 146 | + revert AccountUnauthorized(sender); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @dev Ensures the caller is the {entrypoint} or the account itself. |
| 152 | + */ |
| 153 | + function _checkEntryPointOrSelf() internal view virtual { |
| 154 | + address sender = msg.sender; |
| 155 | + if (sender != address(this) && sender != address(entryPoint())) { |
| 156 | + revert AccountUnauthorized(sender); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @dev Receive Ether. |
| 162 | + */ |
| 163 | + receive() external payable virtual {} |
| 164 | +} |
0 commit comments