draft-AccountBase.sol 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {PackedUserOperation, IAccount, IEntryPoint, IAccountExecute} from "../interfaces/draft-IERC4337.sol";
  4. import {Address} from "../utils/Address.sol";
  5. /**
  6. * @dev A simple ERC4337 account implementation.
  7. *
  8. * This base implementation only includes the minimal logic to process user operations.
  9. * Developers must implement the {_validateUserOp} function to define the account's validation logic.
  10. */
  11. abstract contract AccountBase is IAccount, IAccountExecute {
  12. /**
  13. * @dev Unauthorized call to the account.
  14. */
  15. error AccountUnauthorized(address sender);
  16. /**
  17. * @dev Revert if the caller is not the entry point or the account itself.
  18. */
  19. modifier onlyEntryPointOrSelf() {
  20. _checkEntryPointOrSelf();
  21. _;
  22. }
  23. /**
  24. * @dev Revert if the caller is not the entry point.
  25. */
  26. modifier onlyEntryPoint() {
  27. _checkEntryPoint();
  28. _;
  29. }
  30. /**
  31. * @dev Canonical entry point for the account that forwards and validates user operations.
  32. */
  33. function entryPoint() public view virtual returns (IEntryPoint) {
  34. return IEntryPoint(0x0000000071727De22E5E9d8BAf0edAc6f37da032);
  35. }
  36. /**
  37. * @dev Return the account nonce for the canonical sequence.
  38. */
  39. function getNonce() public view virtual returns (uint256) {
  40. return getNonce(0);
  41. }
  42. /**
  43. * @dev Return the account nonce for a given sequence (key).
  44. */
  45. function getNonce(uint192 key) public view virtual returns (uint256) {
  46. return entryPoint().getNonce(address(this), key);
  47. }
  48. /**
  49. * @dev Returns the digest the offchain signer signed instead of the opaque `userOpHash`.
  50. *
  51. * Given the `userOpHash` calculation is defined by ERC-4337, offchain signers
  52. * may need to sign again this hash by rehashing it with other schemes.
  53. *
  54. * Returns the `userOpHash` by default.
  55. */
  56. function _userOpSignedHash(
  57. PackedUserOperation calldata /* userOp */,
  58. bytes32 userOpHash
  59. ) internal view virtual returns (bytes32) {
  60. return userOpHash;
  61. }
  62. /**
  63. * @inheritdoc IAccount
  64. */
  65. function validateUserOp(
  66. PackedUserOperation calldata userOp,
  67. bytes32 userOpHash,
  68. uint256 missingAccountFunds
  69. ) public virtual onlyEntryPoint returns (uint256) {
  70. uint256 validationData = _validateUserOp(userOp, _userOpSignedHash(userOp, userOpHash));
  71. _payPrefund(missingAccountFunds);
  72. return validationData;
  73. }
  74. /**
  75. * @inheritdoc IAccountExecute
  76. */
  77. function executeUserOp(
  78. PackedUserOperation calldata userOp,
  79. bytes32 /*userOpHash*/
  80. ) public virtual onlyEntryPointOrSelf {
  81. (address target, uint256 value, bytes memory data) = abi.decode(userOp.callData[4:], (address, uint256, bytes));
  82. Address.functionCallWithValue(target, data, value);
  83. }
  84. /**
  85. * @dev Validation logic for {validateUserOp}. The `userOpSignedHash` is the digest from {_userOpSignedHash}.
  86. *
  87. * IMPORTANT: Implementing a mechanism to validate user operations is a security-sensitive operation
  88. * as it may allow an attacker to bypass the account's security measures. Check out {AccountECDSA},
  89. * {AccountP256}, or {AccountRSA} for digital signature validation implementations.
  90. */
  91. function _validateUserOp(
  92. PackedUserOperation calldata userOp,
  93. bytes32 userOpSignedHash
  94. ) internal view virtual returns (uint256 validationData);
  95. /**
  96. * @dev Sends the missing funds for executing the user operation to the {entrypoint}.
  97. * The `missingAccountFunds` must be defined by the entrypoint when calling {validateUserOp}.
  98. */
  99. function _payPrefund(uint256 missingAccountFunds) internal virtual {
  100. if (missingAccountFunds > 0) {
  101. (bool success, ) = payable(msg.sender).call{value: missingAccountFunds}("");
  102. success; // Silence warning. The entrypoint should validate the result.
  103. }
  104. }
  105. /**
  106. * @dev Ensures the caller is the {entrypoint}.
  107. */
  108. function _checkEntryPoint() internal view virtual {
  109. address sender = msg.sender;
  110. if (sender != address(entryPoint())) {
  111. revert AccountUnauthorized(sender);
  112. }
  113. }
  114. /**
  115. * @dev Ensures the caller is the {entrypoint} or the account itself.
  116. */
  117. function _checkEntryPointOrSelf() internal view virtual {
  118. address sender = msg.sender;
  119. if (sender != address(this) && sender != address(entryPoint())) {
  120. revert AccountUnauthorized(sender);
  121. }
  122. }
  123. /**
  124. * @dev Receive Ether.
  125. */
  126. receive() external payable virtual {}
  127. }