draft-IERC4337.sol 9.9 KB

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