Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Feb 17, 2025
1 parent ff206f1 commit 2aa4bf9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/accounts/EIP7702Proxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract EIP7702Proxy {
}
// Admin workflow.
if eq(caller(), admin) {
let addr := shr(96, shl(96, calldataload(0x04)))
let addr := shr(96, calldataload(0x10))
// `changeAdmin(address)`.
if eq(0x8f283970, fnSel) {
sstore(_ERC1967_ADMIN_SLOT, addr)
Expand Down Expand Up @@ -119,7 +119,7 @@ contract EIP7702Proxy {
// to signal that we should replace it with the actual implementation.
// This also gives the flexibility on whether to let the proxy auto-upgrade,
// or let the authority manually upgrade.
if iszero(xor(sload(_ERC1967_IMPLEMENTATION_SLOT), _ERC1967_IMPLEMENTATION_SLOT)) {
if eq(sload(_ERC1967_IMPLEMENTATION_SLOT), _ERC1967_IMPLEMENTATION_SLOT) {
// The `implementation` is still at `calldatasize()` in memory.
sstore(_ERC1967_IMPLEMENTATION_SLOT, mload(calldatasize()))
}
Expand Down
28 changes: 16 additions & 12 deletions src/accounts/LibEIP7702.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ library LibEIP7702 {
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @dev Failed to perform the proxy query.
error ProxyQueryFailed();
/// @dev Failed to change the proxy admin.
error ChangeProxyAdminFailed();

/// @dev Failed to upgrade the proxy.
error UpgradeProxyFailed();

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
Expand Down Expand Up @@ -41,31 +44,31 @@ library LibEIP7702 {
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @dev Returns the implementation of the proxy.
/// If the `proxy` does not exist, returns `address(0)`.
/// Assumes that the proxy is a proper EIP7702Proxy, if it exists.
function proxyImplementation(address proxy) internal view returns (address result) {
/// @solidity memory-safe-assembly
assembly {
if iszero(mul(returndatasize(), staticcall(gas(), proxy, 0x00, 0x00, 0x00, 0x20))) {
mstore(0x00, 0x26ec9b6a) // `ProxyQueryFailed()`.
revert(0x1c, 0x04)
}
result := mload(0x00)
mstore(0x00, 0x5c60da1b) // `implementation()`.
let t := staticcall(gas(), proxy, 0x1c, 0x04, 0x00, 0x20)
result := mul(mload(0x00), and(gt(returndatasize(), 0x1f), t))
}
}

/// @dev Returns the admin of the proxy.
/// If the `proxy` does not exist, returns `address(0)`.
/// Assumes that the proxy is a proper EIP7702Proxy, if it exists.
function proxyAdmin(address proxy) internal view returns (address result) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0xf851a440) // `admin()`.
if iszero(mul(returndatasize(), staticcall(gas(), proxy, 0x1c, 0x04, 0x00, 0x20))) {
mstore(0x00, 0x26ec9b6a) // `ProxyQueryFailed()`.
revert(0x1c, 0x04)
}
result := mload(0x00)
let t := staticcall(gas(), proxy, 0x1c, 0x04, 0x00, 0x20)
result := mul(mload(0x00), and(gt(returndatasize(), 0x1f), t))
}
}

/// @dev Changes the admin on the proxy. The caller must be the admin.
/// Assumes that the proxy is a proper EIP7702Proxy, if it exists.
function changeProxyAdmin(address proxy, address newAdmin) internal {
/// @solidity memory-safe-assembly
assembly {
Expand All @@ -79,6 +82,7 @@ library LibEIP7702 {
}

/// @dev Changes the implementation on the proxy. The caller must be the admin.
/// Assumes that the proxy is a proper EIP7702Proxy, if it exists.
function upgradeProxy(address proxy, address newImplementation) internal {
/// @solidity memory-safe-assembly
assembly {
Expand Down
9 changes: 5 additions & 4 deletions test/EIP7702Proxy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ contract EIP7702ProxyTest is SoladyTest {

function setValue(uint256 value_) public {
value = value_;
LibEIP7702.requestProxyDelegationInitialization();
}

function revertWithError() public view {
revert CustomError(value);
}

function requestProxyDelegationInitialization() public {
LibEIP7702.requestProxyDelegationInitialization();
function version() external pure returns (uint256) {
return 1;
}

function upgradeToLatestProxyDelegation() public {
Expand All @@ -49,6 +50,8 @@ contract EIP7702ProxyTest is SoladyTest {
function _checkBehavesLikeProxy(address instance) internal {
assertTrue(instance != address(0));

assertEq(EIP7702ProxyTest(instance).version(), 1);

uint256 v = _random();
uint256 thisValue = this.value();
if (thisValue == v) {
Expand Down Expand Up @@ -130,8 +133,6 @@ contract EIP7702ProxyTest is SoladyTest {

_checkBehavesLikeProxy(authority);

EIP7702ProxyTest(authority).requestProxyDelegationInitialization();

// Check that upgrading the proxy won't cause the authority's implementation to change.
if (!f && _randomChance(2)) {
vm.startPrank(admin);
Expand Down

0 comments on commit 2aa4bf9

Please sign in to comment.