Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AA-521 EntryPoint support for eip-7702 #529

Open
wants to merge 34 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
db002af
userOpHash as ERC-712 signature
drortirosh Jan 1, 2025
edb2255
don't encode "domain" by default.
drortirosh Jan 2, 2025
dc43285
working
drortirosh Jan 13, 2025
fc0c4e7
removed initUserOpHashParams (internal helper)
drortirosh Jan 13, 2025
263a232
gas cals
drortirosh Jan 13, 2025
5b052ae
undo gas limit change.
drortirosh Jan 13, 2025
38d4714
pr review (typo hash1)
drortirosh Jan 15, 2025
5f0a6b7
remove unused comment.
drortirosh Jan 20, 2025
68894e4
initial implementation
drortirosh Jan 22, 2025
317d26b
memory-safe
drortirosh Jan 22, 2025
2617f03
optimize overrideInitCode
drortirosh Jan 22, 2025
5052317
addeds: zero-tails, fail if not eip-7702 account.
drortirosh Jan 23, 2025
b8a9d26
gas calcs
drortirosh Jan 23, 2025
d9b3f77
lints
drortirosh Jan 23, 2025
24b5473
Merge branch 'develop' into AA-521-ep-7702
drortirosh Jan 23, 2025
fafeca8
removed extracheck.
drortirosh Jan 23, 2025
f98cd95
gascalc
drortirosh Jan 23, 2025
fb06bc3
tests passes, including 7702-enabled external geth
drortirosh Jan 29, 2025
69499d0
gaschecks, geth docker.
drortirosh Jan 29, 2025
bac3113
geth docker.
drortirosh Jan 29, 2025
e54c2a7
fix geth script, coverage test
drortirosh Jan 29, 2025
146dbbe
separate entrypoint-7702 tests into a separate test file
drortirosh Jan 29, 2025
0860e41
Merge branch 'develop' into AA-521-ep-7702
drortirosh Jan 29, 2025
f25a26b
Merge branch 'develop' into AA-521-ep-7702
drortirosh Jan 30, 2025
ee54899
test timeout
drortirosh Jan 30, 2025
33b294a
lints
drortirosh Feb 4, 2025
ea09602
Merge branch 'develop' into AA-521-ep-7702
drortirosh Feb 6, 2025
8dafd94
account
drortirosh Feb 6, 2025
c026b2e
added wallet tests
drortirosh Feb 9, 2025
1442357
test eip-7702 account
drortirosh Feb 9, 2025
b088f05
test 7702 with paymaster
drortirosh Feb 9, 2025
6459a16
lnts
drortirosh Feb 9, 2025
192be39
coverage
drortirosh Feb 10, 2025
26d6a0b
rename to Simple7702Account
drortirosh Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
skipFiles: [
"test",
"samples/bls/lib",
"utils/Exec.sol"
"utils/Exec.sol",
"samples"
],
configureYulOptimizer: true,
};
65 changes: 65 additions & 0 deletions contracts/core/Eip7702Support.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
pragma solidity ^0.8;

import "../interfaces/PackedUserOperation.sol";
import "../core/UserOperationLib.sol";
// SPDX-License-Identifier: MIT

// EIP-7702 code prefix. Also, we use this prefix as a marker in the initCode. To specify this account is EIP-7702.
uint256 constant EIP7702_PREFIX = 0xef0100;

using UserOperationLib for PackedUserOperation;

//get alternate InitCodeHash (just for UserOp hash) when using EIP-7702
function _getEip7702InitCodeHashOverride(PackedUserOperation calldata userOp) view returns (bytes32) {
bytes calldata initCode = userOp.initCode;
if (!_isEip7702InitCode(initCode)) {
return 0;
}
address delegate = _getEip7702Delegate(userOp.getSender());
if (initCode.length <= 20)
return keccak256(abi.encodePacked(delegate));
else
return keccak256(abi.encodePacked(delegate, initCode[20 :]));
}


function _isEip7702InitCode(bytes calldata initCode) pure returns (bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you write this function without the assembly tricks, how much more gas does it spend? This code is too elaborate for the task of "compare first three bytes".


if (initCode.length < 2) {
return false;
}
uint256 initCodeStart;
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
initCodeStart := calldataload(initCode.offset)
}
// make sure first 20 bytes of initCode are "0xff0100" (padded with zeros)
// initCode can be shorter (e.g. only 3), but then it is already zero-padded.
return (initCodeStart >> (256 - 160)) == ((EIP7702_PREFIX << (160 - 24)));
}

/**
* get the EIP-7702 delegate from contract code.
* requires EXTCODECOPY pr: https://github.com/ethereum/EIPs/pull/9248 (not yet merged or implemented)
**/
function _getEip7702Delegate(address sender) view returns (address) {

uint256 senderCode;

// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
extcodecopy(sender, 0, 0, 32)
senderCode := mload(0)
}
Comment on lines +49 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need any assembly, you can just do sender.code. Also if the code is not exactly 23 bytes long it should revert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. using assembly to avoid creating a dynamic bytes array (even with sender.code, we'd need assembly to grab just the address out of it, as there is no "slice" for memory..)
  2. No need to check it is 23 bytes: since eip-3541, no deployed contract can start with "0xEF", unless it is EOF. The "0xEF0100" prefix of eip-7702 is invalid as EOF, such checking the prefix implies the 23 bytes length...

Copy link
Collaborator

@forshtat forshtat Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with sender.code, we'd need assembly to grab just the address out of it

I think Solidity now supports just converting bytes to bytes32 to uint256 so as far as I can tell there should be no difference. I may be wrong though.

        bytes memory b = sender.code;
        uint256 senderCode = uint256(bytes32(b));

No need to check it is 23 bytes since EIP-3541

I am concerned some up-and-coming L2s may be a little less rigorous in their implementation of EIP-3541 and that would allow some kind of an attack down the line. It would be dirt-cheap to just double-check.

// senderCode is the first 32 bytes of the sender's code
// If it is an EIP-7702 delegate, then top 24 bits are the EIP7702_PREFIX
// next 160 bytes are the delegate address
if (senderCode >> (256 - 24) != EIP7702_PREFIX) {
// instead of just "not an EIP-7702 delegate", if some info.
require(sender.code.length > 0, "sender has no code");
//temp: sanity check for current EIP-7702 implementation.
require(sender.code.length == 23, "EIP-7702 delegate-length");
revert("not an EIP-7702 delegate");
}
return address(uint160(senderCode >> (256 - 160 - 24)));
}
11 changes: 10 additions & 1 deletion contracts/core/EntryPoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "./NonceManager.sol";
import "./SenderCreator.sol";
import "./StakeManager.sol";
import "./UserOperationLib.sol";
import "./Eip7702Support.sol";

import "@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
Expand Down Expand Up @@ -378,8 +379,9 @@ contract EntryPoint is IEntryPoint, StakeManager, NonceManager, ReentrancyGuardT
function getUserOpHash(
PackedUserOperation calldata userOp
) public view returns (bytes32) {
bytes32 overrideInitCodeHash = _getEip7702InitCodeHashOverride(userOp);
return
MessageHashUtils.toTypedDataHash(getDomainSeparatorV4(), userOp.hash());
MessageHashUtils.toTypedDataHash(getDomainSeparatorV4(), userOp.hash(overrideInitCodeHash));
}

/**
Expand Down Expand Up @@ -441,6 +443,13 @@ contract EntryPoint is IEntryPoint, StakeManager, NonceManager, ReentrancyGuardT
) internal {
if (initCode.length != 0) {
address sender = opInfo.mUserOp.sender;
if ( _isEip7702InitCode(initCode) ) {
if (initCode.length>20 ) {
//already validated it is an EIP-7702 delegate (and hence, already has code)
senderCreator().initEip7702Sender(sender, initCode[20:]);
}
return;
}
if (sender.code.length != 0)
revert FailedOp(opIndex, "AA10 sender already constructed");
address sender1 = senderCreator().createSender{
Expand Down
3 changes: 3 additions & 0 deletions contracts/core/EntryPointSimulations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,7 @@ contract EntryPointSimulations is EntryPoint, IEntryPointSimulations {
return __domainSeparatorV4;
}

function supportsInterface(bytes4) public view virtual override returns (bool) {
return false;
}
}
25 changes: 20 additions & 5 deletions contracts/core/SenderCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pragma solidity ^0.8.23;

import "../interfaces/ISenderCreator.sol";
import "../utils/Exec.sol";
import {IEntryPoint} from "../interfaces/IEntryPoint.sol";

/**
* Helper contract for EntryPoint, to call userOp.initCode from a "neutral" address,
Expand All @@ -23,10 +25,9 @@ contract SenderCreator is ISenderCreator {
function createSender(
bytes calldata initCode
) external returns (address sender) {
if (msg.sender != entryPoint) {
revert("AA97 should call from EntryPoint");
}
require(msg.sender == entryPoint, "AA97 should call from EntryPoint");
address factory = address(bytes20(initCode[0:20]));

bytes memory initCallData = initCode[20:];
bool success;
/* solhint-disable no-inline-assembly */
Expand All @@ -40,10 +41,24 @@ contract SenderCreator is ISenderCreator {
0,
32
)
sender := mload(0)
if success {
sender := mload(0)
}
}
}

// use initCode to initialize an EIP-7702 account
// caller (EntryPoint) already verified it is an EIP-7702 account.
function initEip7702Sender(
address sender,
bytes calldata initCallData
) external {
require(msg.sender == entryPoint, "AA97 should call from EntryPoint");
// solhint-disable-next-line avoid-low-level-calls
bool success = Exec.call(sender, 0, initCallData, gasleft());
if (!success) {
sender = address(0);
bytes memory result = Exec.getReturnData(2048);
revert IEntryPoint.FailedOpWithRevert(0,"AA13 EIP7702 sender init failed", result);
}
}
}
12 changes: 8 additions & 4 deletions contracts/core/UserOperationLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ library UserOperationLib {
/**
* Pack the user operation data into bytes for hashing.
* @param userOp - The user operation data.
* @param overrideInitCodeHash - If set, encode this instead of the initCode field in the userOp.
*/
function encode(
PackedUserOperation calldata userOp
PackedUserOperation calldata userOp,
bytes32 overrideInitCodeHash
) internal pure returns (bytes memory ret) {
address sender = getSender(userOp);
uint256 nonce = userOp.nonce;
bytes32 hashInitCode = calldataKeccak(userOp.initCode);
bytes32 hashInitCode = overrideInitCodeHash != 0 ? overrideInitCodeHash : calldataKeccak(userOp.initCode);
bytes32 hashCallData = calldataKeccak(userOp.callData);
bytes32 accountGasLimits = userOp.accountGasLimits;
uint256 preVerificationGas = userOp.preVerificationGas;
Expand Down Expand Up @@ -136,10 +138,12 @@ library UserOperationLib {
/**
* Hash the user operation data.
* @param userOp - The user operation data.
* @param overrideInitCodeHash - If set, the initCode hash will be replaced with this value just for UserOp hashing.
*/
function hash(
PackedUserOperation calldata userOp
PackedUserOperation calldata userOp,
bytes32 overrideInitCodeHash
) internal pure returns (bytes32) {
return keccak256(encode(userOp));
return keccak256(encode(userOp, overrideInitCodeHash));
}
}
3 changes: 3 additions & 0 deletions contracts/interfaces/ISenderCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ interface ISenderCreator {
* @return sender Address of the newly created sender contract.
*/
function createSender(bytes calldata initCode) external returns (address sender);

// call initCode to initialize an EIP-7702 account
function initEip7702Sender(address sender, bytes calldata initCode) external;
}
84 changes: 84 additions & 0 deletions contracts/samples/Simple7702Account.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: MIT
// based on: https://gist.github.com/frangio/e40305b9f99de290b73750dff5ebe50a
pragma solidity ^0.8;

import "../interfaces/PackedUserOperation.sol";
import "../core/Helpers.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "../core/BaseAccount.sol";

/**
* Simple7702Account.sol
* A minimal account to be used with EIP-7702 (for batching) and ERC-4337 (for gas sponsoring)
*/
contract Simple7702Account is BaseAccount, IERC165, IERC1271, ERC1155Holder, ERC721Holder {

// temporary address of entryPoint v0.8
function entryPoint() public pure override returns (IEntryPoint) {
return IEntryPoint(0x6F4F5099a64044D69EB7419d66760fD4106fcE3C);
}

/**
* Make this account callable through ERC-4337 EntryPoint.
* The UserOperation should be signed by this account's private key.
*/
function _validateSignature(
PackedUserOperation calldata userOp,
bytes32 userOpHash
) internal virtual override returns (uint256 validationData) {

if (address(this) != ECDSA.recover(userOpHash, userOp.signature)) {
return SIG_VALIDATION_FAILED;
}
return 0;
}

function _requireFromSelfOrEntryPoint() internal view virtual {
require(
msg.sender == address(this) ||
msg.sender == address(entryPoint()),
"not from self or EntryPoint"
);
}

struct Call {
address target;
uint256 value;
bytes data;
}

function execute(Call[] calldata calls) external {
_requireFromSelfOrEntryPoint();

for (uint256 i = 0; i < calls.length; i++) {
Call calldata call = calls[i];
(bool ok, bytes memory ret) = call.target.call{value: call.value}(call.data);
if (!ok) {
// solhint-disable-next-line no-inline-assembly
assembly { revert(add(ret, 32), mload(ret)) }
}
}
}

function supportsInterface(bytes4 id) public override(ERC1155Holder, IERC165) pure returns (bool) {
return
id == type(IERC165).interfaceId ||
id == type(IAccount).interfaceId ||
id == type(IERC1271).interfaceId ||
id == type(IERC1155Receiver).interfaceId ||
id == type(IERC721Receiver).interfaceId;
}

//deliberately return the same signature as returned by the EOA itself: This way,
// ERC-1271 can be used regardless if the account currently has this code or not.
function isValidSignature(bytes32 hash, bytes memory signature) public view returns (bytes4 magicValue) {
return ECDSA.recover(hash, signature) == address(this) ? this.isValidSignature.selector : bytes4(0);
}

receive() external payable {
}
}
53 changes: 53 additions & 0 deletions contracts/test/TestEip7702DelegateAccount.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pragma solidity ^0.8.23;
// SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "../core/BaseAccount.sol";
import "../core/Eip7702Support.sol";

contract TestEip7702DelegateAccount is BaseAccount {

IEntryPoint private immutable _entryPoint;
bool public testInitCalled;

constructor(IEntryPoint anEntryPoint) {
_entryPoint = anEntryPoint;
}

function testInit() public {
testInitCalled = true;
}

function entryPoint() public view override virtual returns (IEntryPoint) {
return _entryPoint;
}

// Require the function call went through EntryPoint or owner
function _requireFromEntryPointOrOwner() internal view {
require(msg.sender == address(this) || msg.sender == address(entryPoint()), "account: not Owner or EntryPoint");
}

/**
* execute a transaction (called directly from owner, or by entryPoint)
* @param dest destination address to call
* @param value the value to pass in this call
* @param func the calldata to pass in this call
*/
function execute(address dest, uint256 value, bytes calldata func) external {
_requireFromEntryPointOrOwner();
(bool success,) = dest.call{value: value}(func);
require(success, "call failed");
}

function _validateSignature(
PackedUserOperation calldata userOp,
bytes32 userOpHash
) internal virtual override returns (uint256 validationData) {
if (userOp.initCode.length > 20) {
require(testInitCalled, "testInit not called");
}
if (ECDSA.recover(userOpHash, userOp.signature) == address(this)) {
return 0;
}
return 1;
}
}
7 changes: 5 additions & 2 deletions contracts/test/TestUtil.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
pragma solidity ^0.8.23;

import "../interfaces/PackedUserOperation.sol";
import "../core/UserOperationLib.sol";
import "../core/Eip7702Support.sol";

contract TestUtil {
using UserOperationLib for PackedUserOperation;

function encodeUserOp(PackedUserOperation calldata op) external pure returns (bytes memory){
return op.encode();
return op.encode(0);
}

function isEip7702InitCode(bytes calldata initCode) external pure returns (bool) {
return _isEip7702InitCode(initCode);
}
}
Loading
Loading