Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement SlotDerivation for fixed storage slot #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
43 changes: 37 additions & 6 deletions src/TransparentVerifiableProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ 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}
function upgradeToAndCall(address newImplementation, bytes calldata data) external payable;
}

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();

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 {}
}