draft-IERC4337.sol 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 maxPriorityFee (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(maxPriorityFee, maxFeePerGas)` 16 bytes each
  40. bytes paymasterAndData; // `abi.encodePacked(paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData)`
  41. bytes signature;
  42. }
  43. /**
  44. * @dev Aggregates and validates multiple signatures for a batch of user operations.
  45. */
  46. interface IAggregator {
  47. /**
  48. * @dev Validates the signature for a user operation.
  49. */
  50. function validateUserOpSignature(
  51. PackedUserOperation calldata userOp
  52. ) external view returns (bytes memory sigForUserOp);
  53. /**
  54. * @dev Returns an aggregated signature for a batch of user operation's signatures.
  55. */
  56. function aggregateSignatures(
  57. PackedUserOperation[] calldata userOps
  58. ) external view returns (bytes memory aggregatesSignature);
  59. /**
  60. * @dev Validates that the aggregated signature is valid for the user operations.
  61. *
  62. * Requirements:
  63. *
  64. * - The aggregated signature MUST match the given list of operations.
  65. */
  66. function validateSignatures(PackedUserOperation[] calldata userOps, bytes calldata signature) external view;
  67. }
  68. /**
  69. * @dev Handle nonce management for accounts.
  70. */
  71. interface IEntryPointNonces {
  72. /**
  73. * @dev Returns the nonce for a `sender` account and a `key`.
  74. *
  75. * Nonces for a certain `key` are always increasing.
  76. */
  77. function getNonce(address sender, uint192 key) external view returns (uint256 nonce);
  78. }
  79. /**
  80. * @dev Handle stake management for accounts.
  81. */
  82. interface IEntryPointStake {
  83. /**
  84. * @dev Returns the balance of the account.
  85. */
  86. function balanceOf(address account) external view returns (uint256);
  87. /**
  88. * @dev Deposits `msg.value` to the account.
  89. */
  90. function depositTo(address account) external payable;
  91. /**
  92. * @dev Withdraws `withdrawAmount` from the account to `withdrawAddress`.
  93. */
  94. function withdrawTo(address payable withdrawAddress, uint256 withdrawAmount) external;
  95. /**
  96. * @dev Adds stake to the account with an unstake delay of `unstakeDelaySec`.
  97. */
  98. function addStake(uint32 unstakeDelaySec) external payable;
  99. /**
  100. * @dev Unlocks the stake of the account.
  101. */
  102. function unlockStake() external;
  103. /**
  104. * @dev Withdraws the stake of the account to `withdrawAddress`.
  105. */
  106. function withdrawStake(address payable withdrawAddress) external;
  107. }
  108. /**
  109. * @dev Entry point for user operations.
  110. */
  111. interface IEntryPoint is IEntryPointNonces, IEntryPointStake {
  112. /**
  113. * @dev A user operation at `opIndex` failed with `reason`.
  114. */
  115. error FailedOp(uint256 opIndex, string reason);
  116. /**
  117. * @dev A user operation at `opIndex` failed with `reason` and `inner` returned data.
  118. */
  119. error FailedOpWithRevert(uint256 opIndex, string reason, bytes inner);
  120. /**
  121. * @dev Batch of aggregated user operations per aggregator.
  122. */
  123. struct UserOpsPerAggregator {
  124. PackedUserOperation[] userOps;
  125. IAggregator aggregator;
  126. bytes signature;
  127. }
  128. /**
  129. * @dev Executes a batch of user operations.
  130. */
  131. function handleOps(PackedUserOperation[] calldata ops, address payable beneficiary) external;
  132. /**
  133. * @dev Executes a batch of aggregated user operations per aggregator.
  134. */
  135. function handleAggregatedOps(
  136. UserOpsPerAggregator[] calldata opsPerAggregator,
  137. address payable beneficiary
  138. ) external;
  139. }
  140. /**
  141. * @dev Base interface for an account.
  142. */
  143. interface IAccount {
  144. /**
  145. * @dev Validates a user operation.
  146. */
  147. function validateUserOp(
  148. PackedUserOperation calldata userOp,
  149. bytes32 userOpHash,
  150. uint256 missingAccountFunds
  151. ) external returns (uint256 validationData);
  152. }
  153. /**
  154. * @dev Support for executing user operations by prepending the {executeUserOp} function selector
  155. * to the UserOperation's `callData`.
  156. */
  157. interface IAccountExecute {
  158. /**
  159. * @dev Executes a user operation.
  160. */
  161. function executeUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash) external;
  162. }
  163. /**
  164. * @dev Interface for a paymaster contract that agrees to pay for the gas costs of a user operation.
  165. *
  166. * NOTE: A paymaster must hold a stake to cover the required entrypoint stake and also the gas for the transaction.
  167. */
  168. interface IPaymaster {
  169. enum PostOpMode {
  170. opSucceeded,
  171. opReverted,
  172. postOpReverted
  173. }
  174. /**
  175. * @dev Validates whether the paymaster is willing to pay for the user operation.
  176. *
  177. * NOTE: Bundlers will reject this method if it modifies the state, unless it's whitelisted.
  178. */
  179. function validatePaymasterUserOp(
  180. PackedUserOperation calldata userOp,
  181. bytes32 userOpHash,
  182. uint256 maxCost
  183. ) external returns (bytes memory context, uint256 validationData);
  184. /**
  185. * @dev Verifies the sender is the entrypoint.
  186. */
  187. function postOp(
  188. PostOpMode mode,
  189. bytes calldata context,
  190. uint256 actualGasCost,
  191. uint256 actualUserOpFeePerGas
  192. ) external;
  193. }