Skip to content
Verified contractExactMIT

UGTPAccount

Address0xa4b2befee38792b931777df37feae679ed8cfa17
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.29

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.29+commit.ab55807c
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,665,394
Creation transaction
0x94ed6b7819…a7bd2fbe4f

src/UGTPAccount.sol

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

/// @title UGTPAccount — EIP-7702 Delegate Contract
/// @notice Deployed at the same deterministic CREATE2 address on all 6 target chains.
///         When an EOA signs an EIP-7702 authorization designating this contract,
///         the EOA gains smart-account capabilities: batched execution, session keys,
///         sponsored transactions (via ERC-4337 EntryPoint v0.8), and EIP-1271 signatures.
///
/// @dev 7702 design: no initializer, no stored owner. address(this) IS the EOA.
///      All storage under ERC-7201 namespaced slots. Reentrancy-guarded execute paths.
/// @custom:security-contact [email protected]

import {IERC1271} from "./interfaces/IERC1271.sol";
import {IPolicyHook} from "./interfaces/IPolicyHook.sol";
import {IERC721Receiver} from "./interfaces/IERC721Receiver.sol";
import {IERC1155Receiver} from "./interfaces/IERC1155Receiver.sol";

contract UGTPAccount is IERC1271, IERC721Receiver, IERC1155Receiver {
    // -----------------------------------------------------------------------
    // Types
    // -----------------------------------------------------------------------

    struct Call {
        address target;
        uint256 value;
        bytes data;
    }

    enum ModuleType {
        SessionKeys,
        Privacy,
        Recovery
    }

    // -----------------------------------------------------------------------
    // ERC-7201 storage namespace
    // -----------------------------------------------------------------------
    // keccak256(abi.encode(uint256(keccak256("ugtp.account.storage.v1")) - 1)) & ~bytes32(uint256(0xff))
    bytes32 private constant STORAGE_SLOT = 0x0e23e67f40c06be44770c0cbbe0259a246240e3e9411783a15a9467a07d59200;

    /// @custom:storage-location erc7201:ugtp.account.storage.v1
    struct AccountStorage {
        uint256 executionNonce;
        bool reentrancyLock;
        address policyHook;
        mapping(address => SessionKey) sessionKeys;
        mapping(ModuleType => bool) enabledModules;
    }

    struct SessionKey {
        bool active;
        uint48 expiry;
        uint128 maxValuePerTx;
        uint128 dailyValueCap;
        uint128 dailyValueUsed;
        uint48 lastResetDay;
        mapping(address => bool) allowedTargets;
    }

    // -----------------------------------------------------------------------
    // Constants
    // -----------------------------------------------------------------------
    address constant ENTRYPOINT_V08 = 0x4337084D9E255Ff0702461CF8895CE9E3b5Ff108;

    bytes32 public constant EXECUTE_TYPEHASH =
        keccak256("Execute(address target,uint256 value,bytes data,uint256 nonce,uint48 deadline)");

    bytes32 public constant EXECUTE_BATCH_TYPEHASH =
        keccak256("ExecuteBatch(Call[] calls,uint256 nonce,uint48 deadline)Call(address target,uint256 value,bytes data)");

    bytes32 public constant CALL_TYPEHASH = keccak256("Call(address target,uint256 value,bytes data)");

    // EIP-712 domain separator is computed per-call (includes chainId for cross-chain safety)
    bytes32 public constant DOMAIN_TYPEHASH =
        keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");

    bytes32 public constant NAME_HASH = keccak256("UGTPAccount");
    bytes32 public constant VERSION_HASH = keccak256("1");

    // -----------------------------------------------------------------------
    // Events
    // -----------------------------------------------------------------------
    event Executed(address indexed target, uint256 value, bytes data);
    event BatchExecuted(uint256 count);
    event RelayedExecution(address indexed signer, uint256 nonce);
    event SessionKeyAdded(address indexed key, uint48 expiry);
    event SessionKeyRevoked(address indexed key);
    event PolicyHookSet(address indexed hook);
    event ModuleEnabled(ModuleType indexed moduleType);
    event ModuleDisabled(ModuleType indexed moduleType);

    // -----------------------------------------------------------------------
    // Errors
    // -----------------------------------------------------------------------
    error Unauthorized();
    error Reentrancy();
    error ExecutionFailed(uint256 index, bytes reason);
    error InvalidSignature();
    error DeadlineExpired();
    error InvalidNonce();
    error SessionKeyExpired();
    error SessionKeyInactive();
    error SessionKeyValueExceeded();
    error SessionKeyDailyCapExceeded();
    error SessionKeyTargetNotAllowed();

    // -----------------------------------------------------------------------
    // Modifiers
    // -----------------------------------------------------------------------
    modifier onlySelf() {
        if (msg.sender != address(this)) revert Unauthorized();
        _;
    }

    modifier onlyAuthorized() {
        if (msg.sender != address(this) && msg.sender != ENTRYPOINT_V08) {
            revert Unauthorized();
        }
        _;
    }

    modifier nonReentrant() {
        AccountStorage storage s = _getStorage();
        if (s.reentrancyLock) revert Reentrancy();
        s.reentrancyLock = true;
        _;
        s.reentrancyLock = false;
    }

    // -----------------------------------------------------------------------
    // Execute — self / EntryPoint
    // -----------------------------------------------------------------------

    function execute(address target, uint256 value, bytes calldata data) external onlyAuthorized nonReentrant {
        _call(target, value, data, 0);
        _notifyPolicyHook(target, value, data);
        emit Executed(target, value, data);
    }

    function executeBatch(Call[] calldata calls) external onlyAuthorized nonReentrant {
        for (uint256 i = 0; i < calls.length; i++) {
            _call(calls[i].target, calls[i].value, calls[i].data, i);
            _notifyPolicyHook(calls[i].target, calls[i].value, calls[i].data);
        }
        emit BatchExecuted(calls.length);
    }

    // -----------------------------------------------------------------------
    // Execute — session keys
    // -----------------------------------------------------------------------

    function executeAsSessionKey(address target, uint256 value, bytes calldata data) external nonReentrant {
        _validateSessionKey(msg.sender, target, value);
        _call(target, value, data, 0);
        _notifyPolicyHook(target, value, data);
        emit Executed(target, value, data);
    }

    function executeBatchAsSessionKey(Call[] calldata calls) external nonReentrant {
        uint256 totalValue = 0;
        for (uint256 i = 0; i < calls.length; i++) {
            _validateSessionKeyTarget(msg.sender, calls[i].target);
            totalValue += calls[i].value;
        }
        _validateSessionKeyValue(msg.sender, totalValue);
        for (uint256 i = 0; i < calls.length; i++) {
            _call(calls[i].target, calls[i].value, calls[i].data, i);
            _notifyPolicyHook(calls[i].target, calls[i].value, calls[i].data);
        }
        emit BatchExecuted(calls.length);
    }

    // -----------------------------------------------------------------------
    // Execute — EIP-712 relayed (sponsored non-4337 path)
    // -----------------------------------------------------------------------

    function executeWithSignature(
        address target,
        uint256 value,
        bytes calldata data,
        uint256 nonce,
        uint48 deadline,
        bytes calldata signature
    ) external nonReentrant {
        if (block.timestamp > deadline) revert DeadlineExpired();

        AccountStorage storage s = _getStorage();
        if (nonce != s.executionNonce) revert InvalidNonce();
        s.executionNonce++;

        bytes32 structHash = keccak256(abi.encode(EXECUTE_TYPEHASH, target, value, keccak256(data), nonce, deadline));
        address signer = _recoverEIP712Signer(structHash, signature);
        if (signer != address(this)) revert InvalidSignature();

        _call(target, value, data, 0);
        _notifyPolicyHook(target, value, data);
        emit RelayedExecution(signer, nonce);
        emit Executed(target, value, data);
    }

    function executeBatchWithSignature(
        Call[] calldata calls,
        uint256 nonce,
        uint48 deadline,
        bytes calldata signature
    ) external nonReentrant {
        if (block.timestamp > deadline) revert DeadlineExpired();

        AccountStorage storage s = _getStorage();
        if (nonce != s.executionNonce) revert InvalidNonce();
        s.executionNonce++;

        bytes32 structHash = _hashExecuteBatch(calls, nonce, deadline);
        address signer = _recoverEIP712Signer(structHash, signature);
        if (signer != address(this)) revert InvalidSignature();

        for (uint256 i = 0; i < calls.length; i++) {
            _call(calls[i].target, calls[i].value, calls[i].data, i);
            _notifyPolicyHook(calls[i].target, calls[i].value, calls[i].data);
        }
        emit RelayedExecution(signer, nonce);
        emit BatchExecuted(calls.length);
    }

    // -----------------------------------------------------------------------
    // EIP-1271 signature validation
    // -----------------------------------------------------------------------

    function isValidSignature(bytes32 hash, bytes calldata signature) external view override returns (bytes4) {
        if (signature.length == 65) {
            (address recovered, bool valid) = _tryRecover(hash, signature);
            if (valid && recovered == address(this)) {
                return IERC1271.isValidSignature.selector;
            }
        }
        return 0xffffffff;
    }

    // -----------------------------------------------------------------------
    // Session key management (self-only)
    // -----------------------------------------------------------------------

    function addSessionKey(
        address key,
        uint48 expiry,
        uint128 maxValuePerTx,
        uint128 dailyValueCap,
        address[] calldata allowedTargets
    ) external onlySelf {
        AccountStorage storage store = _getStorage();
        SessionKey storage sk = store.sessionKeys[key];
        sk.active = true;
        sk.expiry = expiry;
        sk.maxValuePerTx = maxValuePerTx;
        sk.dailyValueCap = dailyValueCap;
        sk.dailyValueUsed = 0;
        sk.lastResetDay = uint48(block.timestamp / 1 days);
        for (uint256 i = 0; i < allowedTargets.length; i++) {
            sk.allowedTargets[allowedTargets[i]] = true;
        }
        emit SessionKeyAdded(key, expiry);
    }

    function revokeSessionKey(address key) external onlySelf {
        _getStorage().sessionKeys[key].active = false;
        emit SessionKeyRevoked(key);
    }

    // -----------------------------------------------------------------------
    // Policy hook management (self-only)
    // -----------------------------------------------------------------------

    function setPolicyHook(address hook) external onlySelf {
        _getStorage().policyHook = hook;
        emit PolicyHookSet(hook);
    }

    function getPolicyHook() external view returns (address) {
        return _getStorage().policyHook;
    }

    // -----------------------------------------------------------------------
    // Module registry (self-only, minimal ERC-7579 inspired)
    // -----------------------------------------------------------------------

    function enableModule(ModuleType moduleType) external onlySelf {
        _getStorage().enabledModules[moduleType] = true;
        emit ModuleEnabled(moduleType);
    }

    function disableModule(ModuleType moduleType) external onlySelf {
        _getStorage().enabledModules[moduleType] = false;
        emit ModuleDisabled(moduleType);
    }

    function isModuleEnabled(ModuleType moduleType) external view returns (bool) {
        return _getStorage().enabledModules[moduleType];
    }

    // -----------------------------------------------------------------------
    // Nonce
    // -----------------------------------------------------------------------

    function getExecutionNonce() external view returns (uint256) {
        return _getStorage().executionNonce;
    }

    // -----------------------------------------------------------------------
    // Session key queries
    // -----------------------------------------------------------------------

    function isSessionKeyActive(address key) external view returns (bool) {
        SessionKey storage sk = _getStorage().sessionKeys[key];
        return sk.active && block.timestamp <= sk.expiry;
    }

    function getSessionKeyExpiry(address key) external view returns (uint48) {
        return _getStorage().sessionKeys[key].expiry;
    }

    // -----------------------------------------------------------------------
    // EIP-712 domain separator
    // -----------------------------------------------------------------------

    function domainSeparator() public view returns (bytes32) {
        return keccak256(abi.encode(DOMAIN_TYPEHASH, NAME_HASH, VERSION_HASH, block.chainid, address(this)));
    }

    // -----------------------------------------------------------------------
    // ERC-721 / ERC-1155 receiver callbacks
    // -----------------------------------------------------------------------

    function onERC721Received(address, address, uint256, bytes calldata) external pure override returns (bytes4) {
        return IERC721Receiver.onERC721Received.selector;
    }

    function onERC1155Received(address, address, uint256, uint256, bytes calldata)
        external
        pure
        override
        returns (bytes4)
    {
        return IERC1155Receiver.onERC1155Received.selector;
    }

    function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
        external
        pure
        override
        returns (bytes4)
    {
        return IERC1155Receiver.onERC1155BatchReceived.selector;
    }

    // -----------------------------------------------------------------------
    // Receive / Fallback
    // -----------------------------------------------------------------------

    receive() external payable {}

    fallback() external payable {}

    // -----------------------------------------------------------------------
    // Internal — calls
    // -----------------------------------------------------------------------

    function _call(address target, uint256 value, bytes calldata data, uint256 index) internal {
        (bool success, bytes memory result) = target.call{value: value}(data);
        if (!success) {
            revert ExecutionFailed(index, result);
        }
    }

    // -----------------------------------------------------------------------
    // Internal — policy hook
    // -----------------------------------------------------------------------

    function _notifyPolicyHook(address target, uint256 value, bytes calldata data) internal {
        address hook = _getStorage().policyHook;
        if (hook != address(0)) {
            try IPolicyHook(hook).afterExecution(address(this), target, value, data) {} catch {}
        }
    }

    // -----------------------------------------------------------------------
    // Internal — session key validation
    // -----------------------------------------------------------------------

    function _validateSessionKey(address key, address target, uint256 value) internal {
        _validateSessionKeyTarget(key, target);
        _validateSessionKeyValue(key, value);
    }

    function _validateSessionKeyTarget(address key, address target) internal view {
        SessionKey storage sk = _getStorage().sessionKeys[key];
        if (!sk.active) revert SessionKeyInactive();
        if (block.timestamp > sk.expiry) revert SessionKeyExpired();
        if (!sk.allowedTargets[target]) revert SessionKeyTargetNotAllowed();
    }

    function _validateSessionKeyValue(address key, uint256 value) internal {
        SessionKey storage sk = _getStorage().sessionKeys[key];
        if (value > sk.maxValuePerTx) revert SessionKeyValueExceeded();

        uint48 today = uint48(block.timestamp / 1 days);
        if (today > sk.lastResetDay) {
            sk.dailyValueUsed = 0;
            sk.lastResetDay = today;
        }
        if (sk.dailyValueUsed + uint128(value) > sk.dailyValueCap) revert SessionKeyDailyCapExceeded();
        sk.dailyValueUsed += uint128(value);
    }

    // -----------------------------------------------------------------------
    // Internal — EIP-712 signature
    // -----------------------------------------------------------------------

    function _recoverEIP712Signer(bytes32 structHash, bytes calldata signature) internal view returns (address) {
        bytes32 digest = keccak256(abi.encodePacked("\x19\x01", domainSeparator(), structHash));
        (address recovered, bool valid) = _tryRecover(digest, signature);
        if (!valid) revert InvalidSignature();
        return recovered;
    }

    function _tryRecover(bytes32 hash, bytes calldata signature) internal pure returns (address, bool) {
        if (signature.length != 65) return (address(0), false);

        bytes32 r;
        bytes32 s;
        uint8 v;
        assembly {
            r := calldataload(signature.offset)
            s := calldataload(add(signature.offset, 0x20))
            v := byte(0, calldataload(add(signature.offset, 0x40)))
        }

        if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
            return (address(0), false);
        }
        if (v != 27 && v != 28) return (address(0), false);

        address recovered = ecrecover(hash, v, r, s);
        return (recovered, recovered != address(0));
    }

    function _hashExecuteBatch(Call[] calldata calls, uint256 nonce, uint48 deadline)
        internal
        pure
        returns (bytes32)
    {
        bytes32[] memory callHashes = new bytes32[](calls.length);
        for (uint256 i = 0; i < calls.length; i++) {
            callHashes[i] = keccak256(
                abi.encode(CALL_TYPEHASH, calls[i].target, calls[i].value, keccak256(calls[i].data))
            );
        }
        return keccak256(abi.encode(EXECUTE_BATCH_TYPEHASH, keccak256(abi.encodePacked(callHashes)), nonce, deadline));
    }

    // -----------------------------------------------------------------------
    // Internal — storage
    // -----------------------------------------------------------------------

    function _getStorage() internal pure returns (AccountStorage storage s) {
        bytes32 slot = STORAGE_SLOT;
        assembly {
            s.slot := slot
        }
    }
}

src/interfaces/IERC1155Receiver.sol

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

interface IERC1155Receiver {
    function onERC1155Received(address operator, address from, uint256 id, uint256 value, bytes calldata data)
        external
        returns (bytes4);

    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external returns (bytes4);
}

src/interfaces/IERC1271.sol

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

interface IERC1271 {
    function isValidSignature(bytes32 hash, bytes calldata signature) external view returns (bytes4 magicValue);
}

src/interfaces/IERC721Receiver.sol

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

interface IERC721Receiver {
    function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data)
        external
        returns (bytes4);
}

src/interfaces/IPolicyHook.sol

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

interface IPolicyHook {
    function afterExecution(address account, address target, uint256 value, bytes calldata data) external;
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.