From 4678b4dd14cbec4726703c5cc62879aba8b4a63b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Tanr=C4=B1kulu?= Date: Tue, 12 Nov 2024 15:15:44 +0100 Subject: [PATCH] implement diamond storage pattern --- README.md | 6 ++-- src/TransparentVerifiableProxy.sol | 45 ++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2770e25..5618f66 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 Diamond Storage pattern: + - Slot 'proxy.verifiable.salt': `salt` (uint256) + - Slot 'proxy.verifiable.owner': `owner` (address) - Immutable `creator` field (set in bytecode) - Implements secure upgrade mechanism - Initializable to prevent implementation tampering diff --git a/src/TransparentVerifiableProxy.sol b/src/TransparentVerifiableProxy.sol index 08174af..d19de15 100644 --- a/src/TransparentVerifiableProxy.sol +++ b/src/TransparentVerifiableProxy.sol @@ -9,6 +9,21 @@ 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"; +// EIP-2535 Diamond Storage pattern +// ref: https://eips.ethereum.org/EIPS/eip-2535#storage +library StorageSlot { + bytes32 constant SLOT_SALT = keccak256("proxy.verifiable.salt"); + bytes32 constant SLOT_OWNER = keccak256("proxy.verifiable.owner"); + + function getSaltSlot() internal pure returns (bytes32) { + return SLOT_SALT; + } + + function getOwnerSlot() internal pure returns (bytes32) { + return SLOT_OWNER; + } +} + interface ITransparentVerifiableProxy { /// @dev See {UUPSUpgradeable-upgradeToAndCall} function upgradeToAndCall(address newImplementation, bytes calldata data) external payable; @@ -18,10 +33,6 @@ contract TransparentVerifiableProxy is Proxy, Initializable { // 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 +63,34 @@ contract TransparentVerifiableProxy is Proxy, Initializable { { require(implementation != address(0), "New implementation cannot be the zero address"); - salt = _salt; - owner = _owner; + bytes32 saltSlot = StorageSlot.getSaltSlot(); + bytes32 ownerSlot = StorageSlot.getOwnerSlot(); + assembly { + sstore(saltSlot, _salt) + sstore(ownerSlot, _owner) + } ERC1967Utils.upgradeToAndCall(implementation, data); } + function salt() public view returns (uint256) { + bytes32 slot = StorageSlot.getSaltSlot(); + uint256 value; + assembly { + value := sload(slot) + } + return value; + } + + function owner() public view returns (address) { + bytes32 slot = StorageSlot.getOwnerSlot(); + address value; + assembly { + value := sload(slot) + } + return value; + } + /** * @dev Returns the current implementation address. *