Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
374 lines
8.8 KiB
Markdown
374 lines
8.8 KiB
Markdown
# ERC4337
|
|
|
|
Simple ERC4337 account implementation.
|
|
|
|
|
|
<b>Recommended usage:</b>
|
|
|
|
1. Deploy the ERC4337 as an implementation contract, and verify it on Etherscan.
|
|
2. Create a factory that uses `LibClone.deployERC1967` or
|
|
`LibClone.deployDeterministicERC1967` to clone the implementation. See: [`ERC4337Factory.sol`](accounts/erc4337factory.md).
|
|
|
|
<b>Note:</b>
|
|
|
|
ERC4337 is a very complicated standard with many potential gotchas.
|
|
Also, it is subject to change and has not been finalized
|
|
(so accounts are encouraged to be upgradeable).
|
|
Usually, ERC4337 account implementations are developed by companies with ample funds
|
|
for security reviews. This implementation is intended to serve as a base reference
|
|
for smart account developers working in such companies. If you are using this
|
|
implementation, please do get one or more security reviews before deployment.
|
|
|
|
<b>Inherits:</b>
|
|
|
|
- [`auth/Ownable.sol`](auth/ownable.md)
|
|
- [`utils/UUPSUpgradeable.sol`](utils/uupsupgradeable.md)
|
|
- [`accounts/Receiver.sol`](accounts/receiver.md)
|
|
- [`accounts/ERC1271.sol`](accounts/erc1271.md)
|
|
|
|
|
|
<!-- customintro:start --><!-- customintro:end -->
|
|
|
|
## Structs
|
|
|
|
### PackedUserOperation
|
|
|
|
```solidity
|
|
struct PackedUserOperation {
|
|
address sender;
|
|
uint256 nonce;
|
|
bytes initCode; // Factory address and `factoryData` (or empty).
|
|
bytes callData;
|
|
bytes32 accountGasLimits; // `verificationGas` (16 bytes) and `callGas` (16 bytes).
|
|
uint256 preVerificationGas;
|
|
bytes32 gasFees; // `maxPriorityFee` (16 bytes) and `maxFeePerGas` (16 bytes).
|
|
bytes paymasterAndData; // Paymaster fields (or empty).
|
|
bytes signature;
|
|
}
|
|
```
|
|
|
|
The packed ERC4337 user operation (userOp) struct.
|
|
|
|
### Call
|
|
|
|
```solidity
|
|
struct Call {
|
|
address target;
|
|
uint256 value;
|
|
bytes data;
|
|
}
|
|
```
|
|
|
|
Call struct for the `executeBatch` function.
|
|
|
|
## Constructor
|
|
|
|
### _disableERC4337ImplementationInitializer()
|
|
|
|
```solidity
|
|
function _disableERC4337ImplementationInitializer() internal virtual
|
|
```
|
|
|
|
Automatically initializes the owner for the implementation. This blocks someone
|
|
from initializing the implementation and doing a delegatecall to SELFDESTRUCT.
|
|
Proxies to the implementation will still be able to initialize as per normal.
|
|
|
|
## Initializer
|
|
|
|
### initialize(address)
|
|
|
|
```solidity
|
|
function initialize(address newOwner) public payable virtual
|
|
```
|
|
|
|
Initializes the account with the owner. Can only be called once.
|
|
|
|
## Entry Point
|
|
|
|
### entryPoint()
|
|
|
|
```solidity
|
|
function entryPoint() public view virtual returns (address)
|
|
```
|
|
|
|
Returns the canonical ERC4337 EntryPoint contract (0.7).
|
|
Override this function to return a different EntryPoint.
|
|
|
|
## Validation Operations
|
|
|
|
### validateUserOp(PackedUserOperation,bytes32,uint256)
|
|
|
|
```solidity
|
|
function validateUserOp(
|
|
PackedUserOperation calldata userOp,
|
|
bytes32 userOpHash,
|
|
uint256 missingAccountFunds
|
|
)
|
|
external
|
|
payable
|
|
virtual
|
|
onlyEntryPoint
|
|
payPrefund(missingAccountFunds)
|
|
returns (uint256 validationData)
|
|
```
|
|
|
|
Validates the signature and nonce.
|
|
The EntryPoint will make the call to the recipient only if
|
|
this validation call returns successfully.
|
|
Signature failure should be reported by returning 1 (see: `_validateSignature`).
|
|
This allows making a "simulation call" without a valid signature.
|
|
Other failures (e.g. nonce mismatch, or invalid signature format)
|
|
should still revert to signal failure.
|
|
|
|
### _validateSignature(PackedUserOperation,bytes32)
|
|
|
|
```solidity
|
|
function _validateSignature(
|
|
PackedUserOperation calldata userOp,
|
|
bytes32 userOpHash
|
|
) internal virtual returns (uint256 validationData)
|
|
```
|
|
|
|
Validate `userOp.signature` for the `userOpHash`.
|
|
|
|
### _validateNonce(uint256)
|
|
|
|
```solidity
|
|
function _validateNonce(uint256 nonce) internal virtual
|
|
```
|
|
|
|
Override to validate the nonce of the userOp.
|
|
This method may validate the nonce requirement of this account.
|
|
e.g.
|
|
To limit the nonce to use sequenced userOps only (no "out of order" userOps):
|
|
`require(nonce < type(uint64).max)`
|
|
For a hypothetical account that *requires* the nonce to be out-of-order:
|
|
`require(nonce & type(uint64).max == 0)`
|
|
The actual nonce uniqueness is managed by the EntryPoint, and thus no other
|
|
action is needed by the account itself.
|
|
|
|
### payPrefund(uint256)
|
|
|
|
```solidity
|
|
modifier payPrefund(uint256 missingAccountFunds) virtual
|
|
```
|
|
|
|
Sends to the EntryPoint (i.e. `msg.sender`) the missing funds for this transaction.
|
|
Subclass MAY override this modifier for better funds management.
|
|
(e.g. send to the EntryPoint more than the minimum required, so that in future transactions
|
|
it will not be required to send again)
|
|
`missingAccountFunds` is the minimum value this modifier should send the EntryPoint,
|
|
which MAY be zero, in case there is enough deposit, or the userOp has a paymaster.
|
|
|
|
### onlyEntryPoint()
|
|
|
|
```solidity
|
|
modifier onlyEntryPoint() virtual
|
|
```
|
|
|
|
Requires that the caller is the EntryPoint.
|
|
|
|
## Execution Operations
|
|
|
|
### execute(address,uint256,bytes)
|
|
|
|
```solidity
|
|
function execute(address target, uint256 value, bytes calldata data)
|
|
public
|
|
payable
|
|
virtual
|
|
onlyEntryPointOrOwner
|
|
returns (bytes memory result)
|
|
```
|
|
|
|
Execute a call from this account.
|
|
|
|
### executeBatch(Call[])
|
|
|
|
```solidity
|
|
function executeBatch(Call[] calldata calls)
|
|
public
|
|
payable
|
|
virtual
|
|
onlyEntryPointOrOwner
|
|
returns (bytes[] memory results)
|
|
```
|
|
|
|
Execute a sequence of calls from this account.
|
|
|
|
### delegateExecute(address,bytes)
|
|
|
|
```solidity
|
|
function delegateExecute(address delegate, bytes calldata data)
|
|
public
|
|
payable
|
|
virtual
|
|
onlyEntryPointOrOwner
|
|
delegateExecuteGuard
|
|
returns (bytes memory result)
|
|
```
|
|
|
|
Execute a delegatecall with `delegate` on this account.
|
|
|
|
### delegateExecuteGuard()
|
|
|
|
```solidity
|
|
modifier delegateExecuteGuard() virtual
|
|
```
|
|
|
|
Ensures that the owner and implementation slots' values aren't changed.
|
|
You can override this modifier to ensure the sanctity of other storage slots too.
|
|
|
|
### onlyEntryPointOrOwner()
|
|
|
|
```solidity
|
|
modifier onlyEntryPointOrOwner() virtual
|
|
```
|
|
|
|
Requires that the caller is the EntryPoint, the owner, or the account itself.
|
|
|
|
## Direct Storage Operations
|
|
|
|
### storageLoad(bytes32)
|
|
|
|
```solidity
|
|
function storageLoad(bytes32 storageSlot)
|
|
public
|
|
view
|
|
virtual
|
|
returns (bytes32 result)
|
|
```
|
|
|
|
Returns the raw storage value at `storageSlot`.
|
|
|
|
### storageStore(bytes32,bytes32)
|
|
|
|
```solidity
|
|
function storageStore(bytes32 storageSlot, bytes32 storageValue)
|
|
public
|
|
payable
|
|
virtual
|
|
onlyEntryPointOrOwner
|
|
storageStoreGuard(storageSlot)
|
|
```
|
|
|
|
Writes the raw storage value at `storageSlot`.
|
|
|
|
### storageStoreGuard(bytes32)
|
|
|
|
```solidity
|
|
modifier storageStoreGuard(bytes32 storageSlot) virtual
|
|
```
|
|
|
|
Ensures that the `storageSlot` is prohibited for direct storage writes.
|
|
You can override this modifier to ensure the sanctity of other storage slots too.
|
|
|
|
## Deposit Operations
|
|
|
|
### getDeposit()
|
|
|
|
```solidity
|
|
function getDeposit() public view virtual returns (uint256 result)
|
|
```
|
|
|
|
Returns the account's balance on the EntryPoint.
|
|
|
|
### addDeposit()
|
|
|
|
```solidity
|
|
function addDeposit() public payable virtual
|
|
```
|
|
|
|
Deposit more funds for this account in the EntryPoint.
|
|
|
|
### withdrawDepositTo(address,uint256)
|
|
|
|
```solidity
|
|
function withdrawDepositTo(address to, uint256 amount)
|
|
public
|
|
payable
|
|
virtual
|
|
onlyOwner
|
|
```
|
|
|
|
Withdraw ETH from the account's deposit on the EntryPoint.
|
|
|
|
## Overrides
|
|
|
|
### _checkOwner()
|
|
|
|
```solidity
|
|
function _checkOwner() internal view virtual override(Ownable)
|
|
```
|
|
|
|
Requires that the caller is the owner or the account itself.
|
|
This override affects the `onlyOwner` modifier.
|
|
|
|
### _guardInitializeOwner()
|
|
|
|
```solidity
|
|
function _guardInitializeOwner()
|
|
internal
|
|
pure
|
|
virtual
|
|
override(Ownable)
|
|
returns (bool)
|
|
```
|
|
|
|
To prevent double-initialization (reuses the owner storage slot for efficiency).
|
|
|
|
### _erc1271Signer()
|
|
|
|
```solidity
|
|
function _erc1271Signer()
|
|
internal
|
|
view
|
|
virtual
|
|
override(ERC1271)
|
|
returns (address)
|
|
```
|
|
|
|
Uses the `owner` as the ERC1271 signer.
|
|
|
|
### _erc1271CallerIsSafe()
|
|
|
|
```solidity
|
|
function _erc1271CallerIsSafe()
|
|
internal
|
|
view
|
|
virtual
|
|
override(ERC1271)
|
|
returns (bool)
|
|
```
|
|
|
|
Allow the entry point to skip the ERC7739 nested typed data workflow.
|
|
This is safe as the entry point already includes the smart account in the user op digest.
|
|
|
|
### _authorizeUpgrade(address)
|
|
|
|
```solidity
|
|
function _authorizeUpgrade(address)
|
|
internal
|
|
virtual
|
|
override(UUPSUpgradeable)
|
|
onlyOwner
|
|
```
|
|
|
|
To ensure that only the owner or the account itself can upgrade the implementation.
|
|
|
|
### _useLibZipCdFallback()
|
|
|
|
```solidity
|
|
function _useLibZipCdFallback() internal view virtual returns (bool)
|
|
```
|
|
|
|
If you don't need to use `LibZip.cdFallback`, override this function to return false.
|
|
|
|
### fallback()
|
|
|
|
```solidity
|
|
fallback() external payable virtual override(Receiver) receiverFallback
|
|
```
|
|
|
|
Handle token callbacks. If no token callback is triggered,
|
|
use `LibZip.cdFallback` for generalized calldata decompression. |