Skip to content
Verified contractExactMIT

UniversalRescue

Address0x2d332b60f9786948d9a27ee77381b249d48462a3
Balance

0 ETH

 

Transactions

3

 

Compiler

solc 0.8.34

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.34+commit.80d5c536
Language
solidity
Optimization
Yes, 200 runs
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,385,356
Creation transaction
0x12684ebc64…3cc124c6e1

contracts/UniversalRescue.sol

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

/// @notice One shared implementation for EIP-7702 ERC-20 recoveries.
/// @dev Call dispatchRescue() on THIS deployed contract. Do not call the EOA directly.
///      This is an MVP, not an audited contract.
contract UniversalRescue {
    bytes32 private constant PURPOSE = keccak256("UNIVERSAL_WALLET_RESCUE_V1");
    bytes32 private constant USED_NAMESPACE = keccak256("universal.wallet.rescue.used.v1");
    uint256 private constant HALF_SECP256K1_ORDER =
        0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0;

    // Embedded immutable value lets the same code distinguish the deployment
    // from executions in each delegated EOA's context.
    address public immutable IMPLEMENTATION;

    event TokenRescued(address indexed wallet, address indexed token, address indexed recipient, uint256 amount);

    constructor() {
        IMPLEMENTATION = address(this);
    }

    /// @notice Sponsoring donor sends its EIP-7702 type-4 transaction HERE.
    /// @dev A failed/ignored authorization must NEVER fall through into a call
    ///      to a wallet's existing (possibly malicious) delegation.
    function dispatchRescue(
        address wallet,
        address recipient,
        address[] calldata tokens,
        bytes32 salt,
        uint256 deadline,
        bytes calldata signature
    ) external {
        require(address(this) == IMPLEMENTATION, "Only implementation");
        require(wallet != address(0), "Bad wallet");

        bytes memory expected = abi.encodePacked(hex"ef0100", IMPLEMENTATION);
        require(keccak256(wallet.code) == keccak256(expected), "Wrong delegation");

        (bool ok, bytes memory result) = wallet.call(
            abi.encodeWithSelector(this.rescue.selector, recipient, tokens, salt, deadline, signature)
        );
        if (!ok) {
            assembly { revert(add(result, 32), mload(result)) }
        }
    }

    /// @notice Executes ONLY in the context of a delegated EOA.
    /// @dev The EOA signs a specific recipient/token intent, separate from
    ///      the authorization that sets the EIP-7702 implementation.
    function rescue(
        address recipient,
        address[] calldata tokens,
        bytes32 salt,
        uint256 deadline,
        bytes calldata signature
    ) external {
        require(address(this) != IMPLEMENTATION, "Only delegated wallet");
        require(msg.sender == IMPLEMENTATION, "Only dispatcher");
        require(recipient != address(0) && recipient != address(this), "Bad recipient");
        require(tokens.length > 0 && tokens.length <= 50, "Bad token count");
        require(block.timestamp <= deadline, "Expired");

        bytes32 digest = keccak256(abi.encode(
            PURPOSE,
            block.chainid,
            address(this),
            IMPLEMENTATION,
            recipient,
            keccak256(abi.encode(tokens)),
            salt,
            deadline
        ));
        bytes32 prefixed = keccak256(
            abi.encodePacked("\x19Ethereum Signed Message:\n32", digest)
        );
        require(_recover(prefixed, signature) == address(this), "Invalid signature");

        bytes32 usedSlot = keccak256(abi.encode(digest, USED_NAMESPACE));
        uint256 alreadyUsed;
        assembly { alreadyUsed := sload(usedSlot) }
        require(alreadyUsed == 0, "Intent already used");
        assembly { sstore(usedSlot, 1) }

        // Atomic: one failure reverts every transfer (but note that an EIP-7702
        // authorization in the outer tx may persist even when this call reverts).
        for (uint256 i = 0; i < tokens.length; ++i) {
            address token = tokens[i];
            require(token.code.length > 0, "Not a token contract");
            (bool ok, bytes memory data) = token.staticcall(
                abi.encodeWithSelector(0x70a08231, address(this)) // balanceOf
            );
            require(ok && data.length >= 32, "balanceOf failed");
            uint256 balance = abi.decode(data, (uint256));
            if (balance == 0) continue;

            (ok, data) = token.call(
                abi.encodeWithSelector(0xa9059cbb, recipient, balance) // transfer
            );
            require(ok && (data.length == 0 ||
                (data.length >= 32 && abi.decode(data, (bool)))), "transfer failed");
            emit TokenRescued(address(this), token, recipient, balance);
        }
    }

    /// @notice Destination of a sponsored ZERO-address authorization.
    ///         Reverts when the revocation was not applied.
    function assertRevoked(address wallet) external view {
        require(address(this) == IMPLEMENTATION, "Only implementation");
        require(wallet.code.length == 0, "Wallet is still delegated");
    }

    function _recover(bytes32 hash, bytes calldata signature) private pure returns (address) {
        require(signature.length == 65, "Bad signature length");
        bytes32 r;
        bytes32 s;
        uint8 v;
        assembly {
            r := calldataload(signature.offset)
            s := calldataload(add(signature.offset, 32))
            v := byte(0, calldataload(add(signature.offset, 64)))
        }
        require(v == 27 || v == 28, "Bad v");
        require(uint256(s) > 0 && uint256(s) <= HALF_SECP256K1_ORDER, "Bad s");
        address signer = ecrecover(hash, v, r, s);
        require(signer != address(0), "Bad signer");
        return signer;
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.