diff --git a/README.md b/README.md index 2770e25..382edf4 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ A system for deploying and verifying proxy contracts with predictable storage la ### 2. TransparentVerifiableProxy - Transparent proxy pattern with verified storage layout -- Fixed storage slots: - - Slot 0: `salt` (uint256) - - Slot 1: `owner` (address) +- Fixed storage slots via [SlotDerivation](https://docs.openzeppelin.com/contracts/5.x/api/utils#SlotDerivation) under `proxy.verifiable` namespace + - `salt` (uint256) + - `owner` (address) - Immutable `creator` field (set in bytecode) - Implements secure upgrade mechanism - Initializable to prevent implementation tampering diff --git a/lib/forge-std b/lib/forge-std index 1714bee..2b59872 160000 --- a/lib/forge-std +++ b/lib/forge-std @@ -1 +1 @@ -Subproject commit 1714bee72e286e73f76e320d110e0eaf5c4e649d +Subproject commit 2b59872eee0b8088ddcade39fe8c041e17bb79c0 diff --git a/lib/openzeppelin-contracts b/lib/openzeppelin-contracts index dbb6104..a277d47 160000 --- a/lib/openzeppelin-contracts +++ b/lib/openzeppelin-contracts @@ -1 +1 @@ -Subproject commit dbb6104ce834628e473d2173bbc9d47f81a9eec3 +Subproject commit a277d472d657dbbcd88a3de5cae0c806130e2df2 diff --git a/src/TransparentVerifiableProxy.sol b/src/TransparentVerifiableProxy.sol index 08174af..787457b 100644 --- a/src/TransparentVerifiableProxy.sol +++ b/src/TransparentVerifiableProxy.sol @@ -8,6 +8,8 @@ pragma solidity ^0.8.20; import {Proxy} from "@openzeppelin/contracts/proxy/Proxy.sol"; import {ERC1967Utils} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol"; import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol"; +import {StorageSlot} from "@openzeppelin/contracts/utils/StorageSlot.sol"; +import {SlotDerivation} from "@openzeppelin/contracts/utils/SlotDerivation.sol"; interface ITransparentVerifiableProxy { /// @dev See {UUPSUpgradeable-upgradeToAndCall} @@ -15,13 +17,17 @@ interface ITransparentVerifiableProxy { } contract TransparentVerifiableProxy is Proxy, Initializable { + using StorageSlot for bytes32; + using SlotDerivation for bytes32; + using SlotDerivation for string; + + string internal constant _VERIFICATION_SLOT = "proxy.verifiable"; + string internal constant _SALT = "salt"; + string internal constant _OWNER = "owner"; + // immutable variable (in bytecode) address public immutable creator; - // storage variables (in storage slots) - uint256 public salt; // Salt, being used creating the proxy (slot 0) - address public owner; // The owner of the proxy contract (slot 1) - // ### EVENTS error ProxyDeniedOwnerAccess(); @@ -52,12 +58,21 @@ contract TransparentVerifiableProxy is Proxy, Initializable { { require(implementation != address(0), "New implementation cannot be the zero address"); - salt = _salt; - owner = _owner; + bytes32 baseSlot = _VERIFICATION_SLOT.erc7201Slot(); + _setSalt(baseSlot, _salt); + _setOwner(baseSlot, _owner); ERC1967Utils.upgradeToAndCall(implementation, data); } + function salt() public view returns (uint256) { + return _getSalt(_VERIFICATION_SLOT.erc7201Slot()); + } + + function owner() public view returns (address) { + return _getOwner(_VERIFICATION_SLOT.erc7201Slot()); + } + /** * @dev Returns the current implementation address. * @@ -96,5 +111,21 @@ contract TransparentVerifiableProxy is Proxy, Initializable { ERC1967Utils.upgradeToAndCall(newImplementation, data); } + function _getSalt(bytes32 baseSlot) internal view returns (uint256) { + return baseSlot.deriveMapping(_SALT).getUint256Slot().value; + } + + function _setSalt(bytes32 baseSlot, uint256 _salt) internal { + baseSlot.deriveMapping(_SALT).getUint256Slot().value = _salt; + } + + function _getOwner(bytes32 baseSlot) internal view returns (address) { + return baseSlot.deriveMapping(_OWNER).getAddressSlot().value; + } + + function _setOwner(bytes32 baseSlot, address _owner) internal { + baseSlot.deriveMapping(_OWNER).getAddressSlot().value = _owner; + } + receive() external payable {} }