-
Notifications
You must be signed in to change notification settings - Fork 682
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
base: develop
Are you sure you want to change the base?
Changes from 17 commits
db002af
edb2255
dc43285
fc0c4e7
263a232
5b052ae
38d4714
5f0a6b7
68894e4
317d26b
2617f03
5052317
b8a9d26
d9b3f77
24b5473
fafeca8
f98cd95
fb06bc3
69499d0
bac3113
e54c2a7
146dbbe
0860e41
f25a26b
ee54899
33b294a
ea09602
8dafd94
c026b2e
1442357
b088f05
6459a16
192be39
26d6a0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 InitCode (just for hashing) when using EIP-7702 | ||
function _getEip7702InitCodeOverride(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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 < 3) { | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need any assembly, you can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think Solidity now supports just converting
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 | ||
require(senderCode >> (256 - 24) == EIP7702_PREFIX, "not an EIP-7702 delegate"); | ||
return address(uint160(senderCode >> (256 - 160 - 24))); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -378,8 +379,9 @@ contract EntryPoint is IEntryPoint, StakeManager, NonceManager, ReentrancyGuardT | |
function getUserOpHash( | ||
PackedUserOperation calldata userOp | ||
) public view returns (bytes32) { | ||
bytes32 overrideInitCode = _getEip7702InitCodeOverride(userOp); | ||
return | ||
MessageHashUtils.toTypedDataHash(getDomainSeparatorV4(), userOp.hash()); | ||
MessageHashUtils.toTypedDataHash(getDomainSeparatorV4(), userOp.hash(overrideInitCode)); | ||
} | ||
|
||
/** | ||
|
@@ -441,6 +443,11 @@ contract EntryPoint is IEntryPoint, StakeManager, NonceManager, ReentrancyGuardT | |
) internal { | ||
if (initCode.length != 0) { | ||
address sender = opInfo.mUserOp.sender; | ||
if ( _isEip7702InitCode(initCode) ) { | ||
//already validated it is an EIP-7702 delegate (and hence, already has code) | ||
senderCreator().initEip7702Sender(sender, initCode[20:]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was under impression that you wanted to call |
||
return; | ||
} | ||
if (sender.code.length != 0) | ||
revert FailedOp(opIndex, "AA10 sender already constructed"); | ||
address sender1 = senderCreator().createSender{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,7 +192,7 @@ contract EntryPointSimulations is EntryPoint, IEntryPointSimulations { | |
|
||
//slightly stricter gas limit than the real EntryPoint | ||
function _getVerificationGasLimit(uint256 verificationGasLimit) internal pure virtual override returns (uint256) { | ||
return verificationGasLimit - 300; | ||
return verificationGasLimit - 350; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Magic number. As you need to change it, please extract a constant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unrelated. we're going to remove "Simulation" from the critical path, and thus all this code will be removed. |
||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,10 +58,22 @@ library UserOperationLib { | |
*/ | ||
function encode( | ||
PackedUserOperation calldata userOp | ||
) internal pure returns (bytes memory ret) { | ||
return encode(userOp, ""); | ||
} | ||
|
||
/** | ||
* Pack the user operation data into bytes for hashing. | ||
* @param userOp - The user operation data. | ||
* @param overrideInitCode - If set, encode this instead of the initCode field in the userOp. | ||
*/ | ||
function encode( | ||
PackedUserOperation calldata userOp, | ||
bytes32 overrideInitCode | ||
) internal pure returns (bytes memory ret) { | ||
address sender = getSender(userOp); | ||
uint256 nonce = userOp.nonce; | ||
bytes32 hashInitCode = calldataKeccak(userOp.initCode); | ||
bytes32 hashInitCode = overrideInitCode==0 ? calldataKeccak(userOp.initCode) : overrideInitCode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just make a function |
||
bytes32 hashCallData = calldataKeccak(userOp.callData); | ||
bytes32 accountGasLimits = userOp.accountGasLimits; | ||
uint256 preVerificationGas = userOp.preVerificationGas; | ||
|
@@ -136,10 +148,12 @@ library UserOperationLib { | |
/** | ||
* Hash the user operation data. | ||
* @param userOp - The user operation data. | ||
* @param overrideInitCode - If set, the initCode will be replaced with this value just for hashing. | ||
*/ | ||
function hash( | ||
PackedUserOperation calldata userOp | ||
PackedUserOperation calldata userOp, | ||
bytes32 overrideInitCode | ||
) internal pure returns (bytes32) { | ||
return keccak256(encode(userOp)); | ||
return keccak256(encode(userOp, overrideInitCode)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function returns a hash, this is not a correct name.