Skip to content
Verified contractExactMIT

VaultPatch

Address0x3d127e5bb7ff2aa6ce9dcf6d60469556a758f485
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,328,678
Creation transaction
0x69740f8c81…6147ee1848

contracts/VaultPatch.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// ============================================================
// VaultPatch —— 线上金库合约的"补丁实现"(用于 UUPS 代理升级)
//
// 目标:把线上金库的"收款地址(=管理员)"安全转移到新钱包,
//       并禁止任何旧私钥持有者继续升级/管理合约。
//
// 设计(零存储变量:仅 immutable 与 constant,完全不改变代理存储布局):
//   1) fallback 把除"拦截/接管"外的所有调用 delegatecall 转发给旧实现,
//      旧实现全部业务功能(存款/提款/质押等)原样保留;
//   2) 升级函数(0x3659cfe6 / 0x4f1ef286)被永久拦截 revert,
//      任何人(包括旧私钥持有者)都无法再升级合约;
//      proxiableUUID()(0x52d1902d)按 UUPS 标准实现返回实现槽,以通过旧实现的升级检查;
//   3) 角色函数 grantRole/revokeRole/renounceRole 被接管:
//      仅 immutable 新管理员(_newAdmin)可调用,旧私钥持有者永久失效;
//   4) initialize() 由旧管理员(=当前 slot4 收款地址持有者)执行一次,
//      把 DEFAULT_ADMIN / UPGRADER / OPERATOR 角色全部授予 _newAdmin,
//      从旧管理员撤销上述全部角色,并把收款地址(slot4)改为 _newAdmin。
//      即使旧私钥持有者抢先调用 initialize(),结果同样是交权给 _newAdmin,
//      对它没有任何好处 —— 无抢跑风险。
//
// 执行顺序(两步,均由用户钱包签名):
//   第 1 步:旧管理员调用代理的 upgradeTo(补丁地址)(旧实现 UUPS 升级)
//   第 2 步:旧管理员调用代理的 initialize()
//   完成后:收款地址 == 管理员 == _newAdmin,旧私钥全部失效。
// ============================================================
contract VaultPatch {
    // ---- 零存储变量 ----
    address private immutable _oldImpl;  // 旧实现地址(两链各自不同)
    address private immutable _newAdmin; // 新管理员 = 新收款地址(用户新钱包,写死)

    bytes32 private constant ADMIN_ROLE = bytes32(0);
    bytes32 private constant UPGRADER_ROLE = keccak256("UPGRADER_ROLE");
    bytes32 private constant OPERATOR_ROLE = keccak256("OPERATOR_ROLE");
    bytes32 private constant PAUSER_ROLE = keccak256("PAUSER_ROLE");

    // 显式选择器常量
    bytes4 private constant SEL_GRANT_ROLE = 0x2f2ff15d; // grantRole(bytes32,address)
    bytes4 private constant SEL_REVOKE_ROLE = 0xd547741f; // revokeRole(bytes32,address)
    bytes4 private constant SEL_HAS_ROLE = 0x91d14854; // hasRole(bytes32,address)
    // UUPS 实现槽(proxiableUUID 返回,使旧实现的 UUPS 升级检查通过)
    bytes32 private constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
    // 暂停/恢复(Pausable,防旧私钥在交接窗口暂停业务)
    bytes4 private constant SEL_PAUSE = 0x8456cb59; // pause()
    bytes4 private constant SEL_UNPAUSE = 0x3f4ba83a; // unpause()

    event ReceiverChanged(address indexed newReceiver);

    constructor(address oldImpl, address newAdmin) {
        require(oldImpl != address(0), "bad old impl");
        require(newAdmin != address(0), "bad new admin");
        _oldImpl = oldImpl;
        _newAdmin = newAdmin;
    }

    // ============ 拦截升级:永久禁止 ============
    function upgradeTo(address) external pure {
        revert("upgrade disabled");
    }

    function upgradeToAndCall(address, bytes calldata) external pure {
        revert("upgrade disabled");
    }

    // ============ UUPS 兼容:proxiableUUID ============
    // 旧实现的 upgradeTo 升级前会调用新实现的 proxiableUUID()(标准 UUPS 检查)。
    // 返回标准实现槽即通过检查(此前 v2 把 0x52d1902d 误当升级入口拦截导致升级被拒)。
    function proxiableUUID() external pure returns (bytes32) {
        return _IMPLEMENTATION_SLOT;
    }

    // ============ 角色管理:仅新管理员 ============
    function grantRole(bytes32 role, address account) external {
        require(msg.sender == _newAdmin, "not new admin");
        _delegateCall(_oldImpl, SEL_GRANT_ROLE, role, account);
    }

    function revokeRole(bytes32 role, address account) external {
        require(msg.sender == _newAdmin, "not new admin");
        _delegateCall(_oldImpl, SEL_REVOKE_ROLE, role, account);
    }

    function renounceRole(bytes32 role, address account) external {
        require(msg.sender == _newAdmin, "not new admin");
        _delegateCall(_oldImpl, SEL_REVOKE_ROLE, role, account);
    }

    // ============ 初始化(仅旧管理员可执行;抢跑无利可图) ============
    function initialize() external {
        address oldAdmin = _receiverSlot4();
        require(msg.sender == oldAdmin, "not old admin");

        // 1) 角色全部授予新管理员
        _delegateCall(_oldImpl, SEL_GRANT_ROLE, ADMIN_ROLE, _newAdmin);
        _delegateCall(_oldImpl, SEL_GRANT_ROLE, UPGRADER_ROLE, _newAdmin);
        _delegateCall(_oldImpl, SEL_GRANT_ROLE, OPERATOR_ROLE, _newAdmin);
        _delegateCall(_oldImpl, SEL_GRANT_ROLE, PAUSER_ROLE, _newAdmin);

        // 2) 从旧管理员撤销全部角色(注意顺序:非 ADMIN 角色先撤销,ADMIN 最后撤销,
        //    否则撤销 ADMIN 后调用者将失去 admin 权限,后续 revoke 会被 AccessControl 拒绝)
        _delegateCall(_oldImpl, SEL_REVOKE_ROLE, UPGRADER_ROLE, oldAdmin);
        _delegateCall(_oldImpl, SEL_REVOKE_ROLE, OPERATOR_ROLE, oldAdmin);
        _delegateCall(_oldImpl, SEL_REVOKE_ROLE, PAUSER_ROLE, oldAdmin);
        _delegateCall(_oldImpl, SEL_REVOKE_ROLE, ADMIN_ROLE, oldAdmin);

        // 3) 收款地址(slot4)改为新管理员
        _setReceiverSlot4(_newAdmin);
        emit ReceiverChanged(_newAdmin);
    }

    // ============ 新功能:修改收款地址(仅新管理员) ============
    function setWithdrawReceiver(address newReceiver) external {
        require(newReceiver != address(0), "bad receiver");
        require(msg.sender == _newAdmin, "not new admin");
        _setReceiverSlot4(newReceiver);
        emit ReceiverChanged(newReceiver);
    }

    // ============ 读收款地址(辅助查看) ============
    function withdrawReceiver() external view returns (address) {
        return _receiverSlot4();
    }

    // ============ fallback:拦截升级入口后,转发旧实现保留全部原有功能 ============
    fallback() external payable {
        bytes4 sel;
        assembly { sel := calldataload(0) }
        // 升级入口已被显式函数拦截(upgradeTo/upgradeToAndCall);proxiableUUID() 已显式实现。
        // 其余未知调用一律转发旧实现,保留全部原有业务功能。
        // 暂停/恢复仅新管理员可操作(防止旧私钥在交接窗口暂停业务)
        if (sel == SEL_PAUSE || sel == SEL_UNPAUSE) {
            require(msg.sender == _newAdmin, "not new admin");
            _delegateCallRaw(_oldImpl);
            return;
        }
        _delegateCallRaw(_oldImpl);
    }

    receive() external payable {}

    // ============ 内部工具 ============
    function _receiverSlot4() internal view returns (address a) {
        assembly { a := sload(4) }
    }

    function _setReceiverSlot4(address a) internal {
        assembly { sstore(4, a) }
    }

    function _delegateCall(address target, bytes4 sel, bytes32 a, address b) internal {
        bytes memory d = abi.encodeWithSelector(sel, a, b);
        (bool ok, bytes memory r) = target.delegatecall(d);
        if (!ok) {
            assembly { revert(add(r, 32), mload(r)) }
        }
    }

    function _delegateCallRaw(address target) internal {
        assembly {
            let ptr := mload(0x40)
            calldatacopy(ptr, 0, calldatasize())
            let r := delegatecall(gas(), target, ptr, calldatasize(), 0, 0)
            let sz := returndatasize()
            returndatacopy(ptr, 0, sz)
            switch r
            case 0 { revert(ptr, sz) }
            default { return(ptr, sz) }
        }
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.