draft-IERC4337.sol 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.2.0-rc.0) (interfaces/draft-IERC4337.sol)
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev A https://github.com/ethereum/ercs/blob/master/ERCS/erc-4337.md#useroperation[user operation] is composed of the following elements:
  6. * - `sender` (`address`): The account making the operation
  7. * - `nonce` (`uint256`): Anti-replay parameter (see “Semi-abstracted Nonce Support” )
  8. * - `factory` (`address`): account factory, only for new accounts
  9. * - `factoryData` (`bytes`): data for account factory (only if account factory exists)
  10. * - `callData` (`bytes`): The data to pass to the sender during the main execution call
  11. * - `callGasLimit` (`uint256`): The amount of gas to allocate the main execution call
  12. * - `verificationGasLimit` (`uint256`): The amount of gas to allocate for the verification step
  13. * - `preVerificationGas` (`uint256`): Extra gas to pay the bundler
  14. * - `maxFeePerGas` (`uint256`): Maximum fee per gas (similar to EIP-1559 max_fee_per_gas)
  15. * - `maxPriorityFeePerGas` (`uint256`): Maximum priority fee per gas (similar to EIP-1559 max_priority_fee_per_gas)
  16. * - `paymaster` (`address`): Address of paymaster contract, (or empty, if account pays for itself)
  17. * - `paymasterVerificationGasLimit` (`uint256`): The amount of gas to allocate for the paymaster validation code
  18. * - `paymasterPostOpGasLimit` (`uint256`): The amount of gas to allocate for the paymaster post-operation code
  19. * - `paymasterData` (`bytes`): Data for paymaster (only if paymaster exists)
  20. * - `signature` (`bytes`): Data passed into the account to verify authorization
  21. *
  22. * When passed to on-chain contacts, the following packed version is used.
  23. * - `sender` (`address`)
  24. * - `nonce` (`uint256`)
  25. * - `initCode` (`bytes`): concatenation of factory address and factoryData (or empty)
  26. * - `callData` (`bytes`)
  27. * - `accountGasLimits` (`bytes32`): concatenation of verificationGas (16 bytes) and callGas (16 bytes)
  28. * - `preVerificationGas` (`uint256`)
  29. * - `gasFees` (`bytes32`): concatenation of maxPriorityFeePerGas (16 bytes) and maxFeePerGas (16 bytes)
  30. * - `paymasterAndData` (`bytes`): concatenation of paymaster fields (or empty)
  31. * - `signature` (`bytes`)
  32. */
  33. struct PackedUserOperation {
  34. address sender;
  35. uint256 nonce;
  36. bytes initCode; // `abi.encodePacked(factory, factoryData)`
  37. bytes callData;
  38. bytes32 accountGasLimits; // `abi.encodePacked(verificationGasLimit, callGasLimit)` 16 bytes each
  39. uint256 preVerificationGas;
  40. bytes32 gasFees; // `abi.encodePacked(maxPriorityFeePerGas, maxFeePerGas)` 16 bytes each
  41. bytes paymasterAndData; // `abi.encodePacked(paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData)` (20 bytes, 16 bytes, 16 bytes, dynamic)
  42. bytes signature;
  43. }
  44. /**
  45. * @dev Aggregates and validates multiple signatures for a batch of user operations.
  46. *
  47. * A contract could implement this interface with custom validation schemes that allow signature aggregation,
  48. * enabling significant optimizations and gas savings for execution and transaction data cost.
  49. *
  50. * Bundlers and clients whitelist supported aggregators.
  51. *
  52. * See https://eips.ethereum.org/EIPS/eip-7766[ERC-7766]
  53. */
  54. interface IAggregator {
  55. /**
  56. * @dev Validates the signature for a user operation.
  57. * Returns an alternative signature that should be used during bundling.
  58. */
  59. function validateUserOpSignature(
  60. PackedUserOperation calldata userOp
  61. ) external view returns (bytes memory sigForUserOp);
  62. /**
  63. * @dev Returns an aggregated signature for a batch of user operation's signatures.
  64. */
  65. function aggregateSignatures(
  66. PackedUserOperation[] calldata userOps
  67. ) external view returns (bytes memory aggregatesSignature);
  68. /**
  69. * @dev Validates that the aggregated signature is valid for the user operations.
  70. *
  71. * Requirements:
  72. *
  73. * - The aggregated signature MUST match the given list of operations.
  74. */
  75. function validateSignatures(PackedUserOperation[] calldata userOps, bytes calldata signature) external view;
  76. }
  77. /**
  78. * @dev Handle nonce management for accounts.
  79. *
  80. * Nonces are used in accounts as a replay protection mechanism and to ensure the order of user operations.
  81. * To avoid limiting the number of operations an account can perform, the interface allows using parallel
  82. * nonces by using a `key` parameter.
  83. *
  84. * See https://eips.ethereum.org/EIPS/eip-4337#semi-abstracted-nonce-support[ERC-4337 semi-abstracted nonce support].
  85. */
  86. interface IEntryPointNonces {
  87. /**
  88. * @dev Returns the nonce for a `sender` account and a `key`.
  89. *
  90. * Nonces for a certain `key` are always increasing.
  91. */
  92. function getNonce(address sender, uint192 key) external view returns (uint256 nonce);
  93. }
  94. /**
  95. * @dev Handle stake management for entities (i.e. accounts, paymasters, factories).
  96. *
  97. * The EntryPoint must implement the following API to let entities like paymasters have a stake,
  98. * and thus have more flexibility in their storage access
  99. * (see https://eips.ethereum.org/EIPS/eip-4337#reputation-scoring-and-throttlingbanning-for-global-entities[reputation, throttling and banning.])
  100. */
  101. interface IEntryPointStake {
  102. /**
  103. * @dev Returns the balance of the account.
  104. */
  105. function balanceOf(address account) external view returns (uint256);
  106. /**
  107. * @dev Deposits `msg.value` to the account.
  108. */
  109. function depositTo(address account) external payable;
  110. /**
  111. * @dev Withdraws `withdrawAmount` from the account to `withdrawAddress`.
  112. */
  113. function withdrawTo(address payable withdrawAddress, uint256 withdrawAmount) external;
  114. /**
  115. * @dev Adds stake to the account with an unstake delay of `unstakeDelaySec`.
  116. */
  117. function addStake(uint32 unstakeDelaySec) external payable;
  118. /**
  119. * @dev Unlocks the stake of the account.
  120. */
  121. function unlockStake() external;
  122. /**
  123. * @dev Withdraws the stake of the account to `withdrawAddress`.
  124. */
  125. function withdrawStake(address payable withdrawAddress) external;
  126. }
  127. /**
  128. * @dev Entry point for user operations.
  129. *
  130. * User operations are validated and executed by this contract.
  131. */
  132. interface IEntryPoint is IEntryPointNonces, IEntryPointStake {
  133. /**
  134. * @dev A user operation at `opIndex` failed with `reason`.
  135. */
  136. error FailedOp(uint256 opIndex, string reason);
  137. /**
  138. * @dev A user operation at `opIndex` failed with `reason` and `inner` returned data.
  139. */
  140. error FailedOpWithRevert(uint256 opIndex, string reason, bytes inner);
  141. /**
  142. * @dev Batch of aggregated user operations per aggregator.
  143. */
  144. struct UserOpsPerAggregator {
  145. PackedUserOperation[] userOps;
  146. IAggregator aggregator;
  147. bytes signature;
  148. }
  149. /**
  150. * @dev Executes a batch of user operations.
  151. * @param beneficiary Address to which gas is refunded up completing the execution.
  152. */
  153. function handleOps(PackedUserOperation[] calldata ops, address payable beneficiary) external;
  154. /**
  155. * @dev Executes a batch of aggregated user operations per aggregator.
  156. * @param beneficiary Address to which gas is refunded up completing the execution.
  157. */
  158. function handleAggregatedOps(
  159. UserOpsPerAggregator[] calldata opsPerAggregator,
  160. address payable beneficiary
  161. ) external;
  162. }
  163. /**
  164. * @dev Base interface for an ERC-4337 account.
  165. */
  166. interface IAccount {
  167. /**
  168. * @dev Validates a user operation.
  169. *
  170. * * MUST validate the caller is a trusted EntryPoint
  171. * * MUST validate that the signature is a valid signature of the userOpHash, and SHOULD
  172. * return SIG_VALIDATION_FAILED (and not revert) on signature mismatch. Any other error MUST revert.
  173. * * MUST pay the entryPoint (caller) at least the “missingAccountFunds” (which might
  174. * be zero, in case the current account’s deposit is high enough)
  175. *
  176. * Returns an encoded packed validation data that is composed of the following elements:
  177. *
  178. * - `authorizer` (`address`): 0 for success, 1 for failure, otherwise the address of an authorizer contract
  179. * - `validUntil` (`uint48`): The UserOp is valid only up to this time. Zero for “infinite”.
  180. * - `validAfter` (`uint48`): The UserOp is valid only after this time.
  181. */
  182. function validateUserOp(
  183. PackedUserOperation calldata userOp,
  184. bytes32 userOpHash,
  185. uint256 missingAccountFunds
  186. ) external returns (uint256 validationData);
  187. }
  188. /**
  189. * @dev Support for executing user operations by prepending the {executeUserOp} function selector
  190. * to the UserOperation's `callData`.
  191. */
  192. interface IAccountExecute {
  193. /**
  194. * @dev Executes a user operation.
  195. */
  196. function executeUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash) external;
  197. }
  198. /**
  199. * @dev Interface for a paymaster contract that agrees to pay for the gas costs of a user operation.
  200. *
  201. * NOTE: A paymaster must hold a stake to cover the required entrypoint stake and also the gas for the transaction.
  202. */
  203. interface IPaymaster {
  204. enum PostOpMode {
  205. opSucceeded,
  206. opReverted,
  207. postOpReverted
  208. }
  209. /**
  210. * @dev Validates whether the paymaster is willing to pay for the user operation. See
  211. * {IAccount-validateUserOp} for additional information on the return value.
  212. *
  213. * NOTE: Bundlers will reject this method if it modifies the state, unless it's whitelisted.
  214. */
  215. function validatePaymasterUserOp(
  216. PackedUserOperation calldata userOp,
  217. bytes32 userOpHash,
  218. uint256 maxCost
  219. ) external returns (bytes memory context, uint256 validationData);
  220. /**
  221. * @dev Verifies the sender is the entrypoint.
  222. * @param actualGasCost the actual amount paid (by account or paymaster) for this UserOperation
  223. * @param actualUserOpFeePerGas total gas used by this UserOperation (including preVerification, creation, validation and execution)
  224. */
  225. function postOp(
  226. PostOpMode mode,
  227. bytes calldata context,
  228. uint256 actualGasCost,
  229. uint256 actualUserOpFeePerGas
  230. ) external;
  231. }