From 2263e6bb90701feca166645caa8a4cd93e0aefbd Mon Sep 17 00:00:00 2001 From: Andrei Kozlov Date: Fri, 26 Jul 2024 11:18:13 +0300 Subject: [PATCH 1/2] More abstraction on the Proxy Factory --- .../TransparentProxyFactoryZkSync.sol | 50 +++---------------- .../ITransparentProxyFactoryZkSync.sol | 12 ----- .../TransparentProxyFactory.sol | 32 ++---------- .../TransparentProxyFactoryBase.sol | 26 +++++++++- 4 files changed, 35 insertions(+), 85 deletions(-) diff --git a/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol b/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol index 7b54ba1..93de2f8 100644 --- a/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol +++ b/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol @@ -18,59 +18,21 @@ contract TransparentProxyFactoryZkSync is TransparentProxyFactoryBase, ITransparentProxyFactoryZkSync { - /// @inheritdoc ITransparentProxyFactoryZkSync - bytes32 public immutable TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH; - - /// @inheritdoc ITransparentProxyFactoryZkSync - bytes32 public immutable PROXY_ADMIN_INIT_CODE_HASH; - /// @inheritdoc ITransparentProxyFactoryZkSync bytes32 public constant ZKSYNC_CREATE2_PREFIX = keccak256('zksyncCreate2'); - constructor() { - // to get the bytecode-hash in zkSync, we sanatize the bytes returned from the creationCode - TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH = bytes32(_sliceBytes(type(TransparentUpgradeableProxy).creationCode, 36, 32)); - PROXY_ADMIN_INIT_CODE_HASH = bytes32(_sliceBytes(type(ProxyAdmin).creationCode, 36, 32)); - } - - /// @inheritdoc ITransparentProxyFactory - function predictCreateDeterministic( - address logic, - address admin, - bytes calldata data, - bytes32 salt - ) public view override returns (address) { - return - _predictCreate2Address( - address(this), - salt, - TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH, - abi.encode(logic, admin, data) - ); - } - - /// @inheritdoc ITransparentProxyFactory - function predictCreateDeterministicProxyAdmin(bytes32 salt) - public - view - override - returns (address) - { - return _predictCreate2Address(address(this), salt, PROXY_ADMIN_INIT_CODE_HASH, abi.encode()); - } - function _predictCreate2Address( address sender, bytes32 salt, - bytes32 creationCodeHash, + bytes memory creationCode, bytes memory constructorInput - ) internal pure returns (address) { + ) internal pure override returns (address) { bytes32 addressHash = keccak256( bytes.concat( ZKSYNC_CREATE2_PREFIX, bytes32(uint256(uint160(sender))), salt, - creationCodeHash, + bytes32(_sliceBytes(creationCode, 36, 32)), keccak256(constructorInput) ) ); @@ -78,7 +40,11 @@ contract TransparentProxyFactoryZkSync is return address(uint160(uint256(addressHash))); } - function _sliceBytes(bytes memory data, uint256 start, uint256 length) internal pure returns (bytes memory) { + function _sliceBytes( + bytes memory data, + uint256 start, + uint256 length + ) internal pure returns (bytes memory) { require(start + length <= data.length, 'Slice out of bounds'); bytes memory result = new bytes(length); diff --git a/src-zksync/contracts/transparent-proxy/interfaces/ITransparentProxyFactoryZkSync.sol b/src-zksync/contracts/transparent-proxy/interfaces/ITransparentProxyFactoryZkSync.sol index 72e61e4..89865f2 100644 --- a/src-zksync/contracts/transparent-proxy/interfaces/ITransparentProxyFactoryZkSync.sol +++ b/src-zksync/contracts/transparent-proxy/interfaces/ITransparentProxyFactoryZkSync.sol @@ -2,18 +2,6 @@ pragma solidity >=0.8.0; interface ITransparentProxyFactoryZkSync { - /** - * @notice method to get the hash of creation bytecode of the TransparentUpgradableProxy contract - * @return hashed of creation bytecode of the TransparentUpgradableProxy contract - */ - function TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH() external returns (bytes32); - - /** - * @notice method to get the hash of creation bytecode of the ProxyAdmin contract - * @return hashed of creation bytecode of the ProxyAdmin contract - */ - function PROXY_ADMIN_INIT_CODE_HASH() external returns (bytes32); - /** * @notice method to get the zksync create2 prefix used for create2 address derivation in zksync * @return create2 prefix used for create2 address derivation diff --git a/src/contracts/transparent-proxy/TransparentProxyFactory.sol b/src/contracts/transparent-proxy/TransparentProxyFactory.sol index 3524ea8..a6446b6 100644 --- a/src/contracts/transparent-proxy/TransparentProxyFactory.sol +++ b/src/contracts/transparent-proxy/TransparentProxyFactory.sol @@ -12,44 +12,18 @@ import {TransparentProxyFactoryBase, ITransparentProxyFactory, ProxyAdmin, Trans * @dev Highly recommended to pass as `admin` on creation an OZ ProxyAdmin instance **/ contract TransparentProxyFactory is TransparentProxyFactoryBase { - /// @inheritdoc ITransparentProxyFactory - function predictCreateDeterministic( - address logic, - address admin, - bytes calldata data, - bytes32 salt - ) public view override returns (address) { - return - _predictCreate2Address( - address(this), - salt, - type(TransparentUpgradeableProxy).creationCode, - abi.encode(logic, admin, data) - ); - } - - /// @inheritdoc ITransparentProxyFactory - function predictCreateDeterministicProxyAdmin(bytes32 salt) - public - view - override - returns (address) - { - return _predictCreate2Address(address(this), salt, type(ProxyAdmin).creationCode, abi.encode()); - } - function _predictCreate2Address( address creator, bytes32 salt, bytes memory creationCode, - bytes memory contructorArgs - ) internal pure returns (address) { + bytes memory constructorArgs + ) internal pure override returns (address) { bytes32 hash = keccak256( abi.encodePacked( bytes1(0xff), creator, salt, - keccak256(abi.encodePacked(creationCode, contructorArgs)) + keccak256(abi.encodePacked(creationCode, constructorArgs)) ) ); diff --git a/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol b/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol index 0d25a5f..b9c3dad 100644 --- a/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol +++ b/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol @@ -67,8 +67,30 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory { address admin, bytes calldata data, bytes32 salt - ) public view virtual returns (address); + ) public view override returns (address) { + return + _predictCreate2Address( + address(this), + salt, + type(TransparentUpgradeableProxy).creationCode, + abi.encode(logic, admin, data) + ); + } /// @inheritdoc ITransparentProxyFactory - function predictCreateDeterministicProxyAdmin(bytes32 salt) public view virtual returns (address); + function predictCreateDeterministicProxyAdmin(bytes32 salt) + public + view + override + returns (address) + { + return _predictCreate2Address(address(this), salt, type(ProxyAdmin).creationCode, abi.encode()); + } + + function _predictCreate2Address( + address creator, + bytes32 salt, + bytes memory creationCode, + bytes memory constructorArgs + ) internal pure virtual returns (address); } From 7a329f15f6c51b78abc5ccdc6f4df695c60d04f2 Mon Sep 17 00:00:00 2001 From: Andrei Kozlov Date: Fri, 26 Jul 2024 11:22:01 +0300 Subject: [PATCH 2/2] Remove unnececcery imports --- .../transparent-proxy/TransparentProxyFactoryZkSync.sol | 4 +--- .../transparent-proxy/TransparentProxyFactory.sol | 2 +- .../transparent-proxy/TransparentProxyFactoryBase.sol | 9 ++------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol b/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol index 93de2f8..268d4ff 100644 --- a/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol +++ b/src-zksync/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol @@ -1,9 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.24; -import {TransparentProxyFactoryBase, ITransparentProxyFactory} from '../../../src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol'; -import {TransparentUpgradeableProxy} from '../../../src/contracts/transparent-proxy/TransparentUpgradeableProxy.sol'; -import {ProxyAdmin} from '../../../src/contracts/transparent-proxy/ProxyAdmin.sol'; +import {TransparentProxyFactoryBase} from '../../../src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol'; import {ITransparentProxyFactoryZkSync} from './interfaces/ITransparentProxyFactoryZkSync.sol'; /** diff --git a/src/contracts/transparent-proxy/TransparentProxyFactory.sol b/src/contracts/transparent-proxy/TransparentProxyFactory.sol index a6446b6..099d2ef 100644 --- a/src/contracts/transparent-proxy/TransparentProxyFactory.sol +++ b/src/contracts/transparent-proxy/TransparentProxyFactory.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; -import {TransparentProxyFactoryBase, ITransparentProxyFactory, ProxyAdmin, TransparentUpgradeableProxy} from './TransparentProxyFactoryBase.sol'; +import {TransparentProxyFactoryBase} from './TransparentProxyFactoryBase.sol'; /** * @title TransparentProxyFactory diff --git a/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol b/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol index b9c3dad..41c8b06 100644 --- a/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol +++ b/src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol @@ -67,7 +67,7 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory { address admin, bytes calldata data, bytes32 salt - ) public view override returns (address) { + ) public view returns (address) { return _predictCreate2Address( address(this), @@ -78,12 +78,7 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory { } /// @inheritdoc ITransparentProxyFactory - function predictCreateDeterministicProxyAdmin(bytes32 salt) - public - view - override - returns (address) - { + function predictCreateDeterministicProxyAdmin(bytes32 salt) public view returns (address) { return _predictCreate2Address(address(this), salt, type(ProxyAdmin).creationCode, abi.encode()); }