@@ -5,6 +5,7 @@ import {Proxy} from "openzeppelin-contracts/contracts/proxy/Proxy.sol";
55import {ERC1967Utils } from "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Utils.sol " ;
66import {ECDSA} from "openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol " ;
77import {Address} from "openzeppelin-contracts/contracts/utils/Address.sol " ;
8+ import {StorageSlot} from "openzeppelin-contracts/contracts/utils/StorageSlot.sol " ;
89
910/// @title EIP7702Proxy
1011/// @notice Proxy contract designed for EIP-7702 smart accounts
@@ -24,6 +25,10 @@ contract EIP7702Proxy is Proxy {
2425 /// @notice Function selector on the implementation that is guarded from direct calls
2526 bytes4 immutable guardedInitializer;
2627
28+ /// @dev Storage slot with the initialized flag
29+ bytes32 internal constant INITIALIZED_SLOT =
30+ bytes32 (uint256 (keccak256 ("EIP7702Proxy.initialized " )) - 1 );
31+
2732 /// @notice Emitted when the implementation is upgraded
2833 event Upgraded (address indexed implementation );
2934
@@ -54,7 +59,7 @@ contract EIP7702Proxy is Proxy {
5459
5560 /// @dev Checks if proxy has been initialized by comparing implementation slot
5661 function _isInitialized () internal view returns (bool ) {
57- return _implementation () == initialImplementation ;
62+ return StorageSlot. getBooleanSlot (INITIALIZED_SLOT).value ;
5863 }
5964
6065 /// @notice Initializes the proxy and implementation with a signed payload
@@ -72,7 +77,10 @@ contract EIP7702Proxy is Proxy {
7277 address recovered = ECDSA.recover (hash, signature);
7378 if (recovered != address (this )) revert InvalidSignature ();
7479
75- // Set the ERC-1967 implementation slot, emit Upgraded event, call the initializer on the initial implementation
80+ // Set initialized flag before upgrading
81+ StorageSlot.getBooleanSlot (INITIALIZED_SLOT).value = true ;
82+
83+ // Set the ERC-1967 implementation slot, emit Upgraded event, call the initializer
7684 ERC1967Utils .upgradeToAndCall (
7785 initialImplementation,
7886 abi.encodePacked (guardedInitializer, args)
@@ -91,6 +99,9 @@ contract EIP7702Proxy is Proxy {
9199 bytes32 hash ,
92100 bytes calldata signature
93101 ) external returns (bytes4 ) {
102+ // Check initialization status first
103+ if (! _isInitialized ()) revert ProxyNotInitialized ();
104+
94105 // First try delegatecall to implementation
95106 (bool success , bytes memory result ) = _implementation ().delegatecall (
96107 msg .data
0 commit comments