|
2 | 2 | pragma solidity ^0.8.23; |
3 | 3 |
|
4 | 4 | import {Proxy} from "openzeppelin-contracts/contracts/proxy/Proxy.sol"; |
5 | | -import {ERC1967Utils} from "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Utils.sol"; |
6 | 5 | import {ECDSA} from "openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol"; |
7 | 6 | import {StorageSlot} from "openzeppelin-contracts/contracts/utils/StorageSlot.sol"; |
| 7 | +import {CustomUpgradeable} from "./CustomUpgradeable.sol"; |
| 8 | +import {Address} from "openzeppelin-contracts/contracts/utils/Address.sol"; |
8 | 9 |
|
9 | 10 | /// @title EIP7702Proxy |
10 | 11 | /// @notice Proxy contract designed for EIP-7702 smart accounts |
11 | | -/// @dev Implements ERC-1967 with an initial implementation address and guarded initializer function |
| 12 | +/// @dev Implements a custom upgradeable proxy pattern with a unique storage slot |
| 13 | +/// to avoid collisions with other delegates in an EIP-7702 context. |
12 | 14 | /// @author Coinbase (https://github.com/base-org/eip-7702-proxy) |
13 | | -contract EIP7702Proxy is Proxy { |
14 | | - /// @notice ERC1271 interface constants |
| 15 | +contract EIP7702Proxy is Proxy, CustomUpgradeable { |
| 16 | + /// @notice ERC1271 interface constants for signature validation |
15 | 17 | bytes4 internal constant ERC1271_MAGIC_VALUE = 0x1626ba7e; |
16 | 18 | bytes4 internal constant ERC1271_FAIL_VALUE = 0xffffffff; |
17 | 19 |
|
18 | | - /// @notice Address of this proxy contract delegate |
| 20 | + /// @notice The address of this proxy contract |
19 | 21 | address immutable proxy; |
20 | 22 |
|
21 | | - /// @notice Initial implementation address set during construction |
| 23 | + /// @notice The initial implementation address |
22 | 24 | address immutable initialImplementation; |
23 | 25 |
|
24 | | - /// @notice Function selector on the implementation that is guarded from direct calls |
| 26 | + /// @notice The function selector that is protected from direct calls |
25 | 27 | bytes4 immutable guardedInitializer; |
26 | 28 |
|
27 | | - /// @dev Storage slot with the initialized flag, calculated via ERC-7201 |
| 29 | + /// @dev Storage slot for the initialized flag, calculated via ERC-7201 |
28 | 30 | bytes32 internal constant INITIALIZED_SLOT = |
29 | 31 | keccak256( |
30 | 32 | abi.encode(uint256(keccak256("EIP7702Proxy.initialized")) - 1) |
31 | 33 | ) & ~bytes32(uint256(0xff)); |
32 | 34 |
|
33 | | - /// @notice Emitted when the initialization signature is invalid |
| 35 | + /// @notice Emitted when a signature fails validation during initialization |
34 | 36 | error InvalidSignature(); |
35 | 37 |
|
36 | | - /// @notice Emitted when the `guardedInitializer` is called |
| 38 | + /// @notice Emitted when attempting to call the guarded initializer directly |
37 | 39 | error InvalidInitializer(); |
38 | 40 |
|
39 | | - /// @notice Emitted when trying to delegate before initialization |
| 41 | + /// @notice Emitted when trying to use the proxy before initialization |
40 | 42 | error ProxyNotInitialized(); |
41 | 43 |
|
42 | | - /// @notice Emitted when constructor arguments are zero |
| 44 | + /// @notice Emitted when constructor receives zero address or selector |
43 | 45 | error ZeroValueConstructorArguments(); |
44 | 46 |
|
| 47 | + /// @notice Emitted when an operation is not authorized |
| 48 | + error Unauthorized(); |
| 49 | + |
45 | 50 | /// @notice Initializes the proxy with an initial implementation and guarded initializer |
46 | 51 | /// @param implementation The initial implementation address |
47 | 52 | /// @param initializer The selector of the `guardedInitializer` function |
48 | | - constructor(address implementation, bytes4 initializer) { |
| 53 | + constructor( |
| 54 | + address implementation, |
| 55 | + bytes4 initializer |
| 56 | + ) CustomUpgradeable(implementation, initializer) { |
49 | 57 | if (implementation == address(0)) |
50 | 58 | revert ZeroValueConstructorArguments(); |
51 | 59 | if (initializer == bytes4(0)) revert ZeroValueConstructorArguments(); |
@@ -81,11 +89,19 @@ contract EIP7702Proxy is Proxy { |
81 | 89 | // Set initialized flag before upgrading |
82 | 90 | StorageSlot.getBooleanSlot(INITIALIZED_SLOT).value = true; |
83 | 91 |
|
84 | | - // Set the ERC-1967 implementation slot, emit Upgraded event, call the initializer |
85 | | - ERC1967Utils.upgradeToAndCall( |
86 | | - initialImplementation, |
| 92 | + // Update to use our custom implementation storage instead of ERC1967Utils |
| 93 | + _setImplementation(initialImplementation); |
| 94 | + |
| 95 | + (bool success, ) = initialImplementation.delegatecall( |
87 | 96 | abi.encodePacked(guardedInitializer, args) |
88 | 97 | ); |
| 98 | + if (!success) { |
| 99 | + assembly { |
| 100 | + let ptr := mload(0x40) |
| 101 | + returndatacopy(ptr, 0, returndatasize()) |
| 102 | + revert(ptr, returndatasize()) |
| 103 | + } |
| 104 | + } |
89 | 105 | } |
90 | 106 |
|
91 | 107 | /// @notice Handles ERC-1271 signature validation by enforcing a final `ecrecover` check if signatures fail `isValidSignature` check |
@@ -142,6 +158,30 @@ contract EIP7702Proxy is Proxy { |
142 | 158 | } |
143 | 159 |
|
144 | 160 | function _implementation() internal view override returns (address) { |
145 | | - return ERC1967Utils.getImplementation(); |
| 161 | + return _getImplementation(); |
146 | 162 | } |
| 163 | + |
| 164 | + function upgradeToAndCall( |
| 165 | + address newImplementation, |
| 166 | + bytes memory data, |
| 167 | + bytes calldata signature |
| 168 | + ) public payable override { |
| 169 | + // Create hash of upgrade data |
| 170 | + bytes32 hash = keccak256(abi.encode(proxy, newImplementation, data)); |
| 171 | + |
| 172 | + // Use our existing isValidSignature logic to validate the upgrade |
| 173 | + if (this.isValidSignature(hash, signature) != ERC1271_MAGIC_VALUE) { |
| 174 | + revert Unauthorized(); |
| 175 | + } |
| 176 | + |
| 177 | + // If signature valid, proceed with upgrade |
| 178 | + _setImplementation(newImplementation); |
| 179 | + emit Upgraded(newImplementation); |
| 180 | + |
| 181 | + if (data.length > 0) { |
| 182 | + Address.functionDelegateCall(newImplementation, data); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + receive() external payable {} |
147 | 187 | } |
0 commit comments