|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {IERC7914} from "./interfaces/IERC7914.sol"; |
| 5 | + |
| 6 | +/// @title ERC7914Detector |
| 7 | +/// @notice A utility contract for detecting ERC7914 support in wallet contracts |
| 8 | +/// @dev This contract provides functionality to check if a given address supports |
| 9 | +/// the ERC7914 standard, which enables native token transfers from smart contract |
| 10 | +/// wallets. It handles both regular contracts and EIP-7702 account abstraction wallets. |
| 11 | +/// |
| 12 | +/// The contract works by: |
| 13 | +/// 1. Checking if the address is an EOA (Externally Owned Account) - EOAs cannot support ERC7914 |
| 14 | +/// 2. For EIP-7702 wallets, checking if they delegate to a known ERC7914-compliant contract |
| 15 | +/// 3. For regular contracts, checking if they implement the transferFromNative function |
| 16 | +/// |
| 17 | +/// Intended for offchain view calls. Not gas optimized. |
| 18 | +/// @custom:security-contact security@uniswap.org |
| 19 | +contract ERC7914Detector { |
| 20 | + // EIP-7702 constants from account-abstraction library |
| 21 | + bytes3 internal constant EIP7702_PREFIX = 0xef0100; |
| 22 | + |
| 23 | + // EIP-7702 bytecode structure: 3 bytes prefix + 20 bytes delegate address = 23 bytes total |
| 24 | + uint256 internal constant EIP7702_BYTECODE_SIZE = 23; |
| 25 | + |
| 26 | + address public immutable caliburAddress; |
| 27 | + |
| 28 | + constructor(address _caliburAddress) { |
| 29 | + caliburAddress = _caliburAddress; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @notice Check if a wallet supports ERC7914 by testing for transferFromNative function |
| 34 | + * @param wallet The wallet address to check |
| 35 | + * @return hasERC7914Support true if ERC7914 is supported, false otherwise |
| 36 | + */ |
| 37 | + function hasERC7914Support(address wallet) external view returns (bool) { |
| 38 | + // EOAs cannot support ERC7914 |
| 39 | + uint256 codeSize; |
| 40 | + assembly { |
| 41 | + codeSize := extcodesize(wallet) |
| 42 | + } |
| 43 | + if (codeSize == 0) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Check if this is an EIP-7702 wallet delegating to Calibur |
| 48 | + if (_isEip7702Delegate(wallet)) { |
| 49 | + address delegate = _getEip7702Delegate(wallet); |
| 50 | + // If it delegates to Calibur, it has ERC7914 support |
| 51 | + if (delegate == caliburAddress) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + // If it delegates to another contract, check that contract |
| 55 | + return _checkTransferFromNative(delegate); |
| 56 | + } |
| 57 | + |
| 58 | + // For regular contracts, check if transferFromNative function exists |
| 59 | + return _checkTransferFromNative(wallet); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @notice Get the EIP-7702 bytecode from contract (prefix + delegate address) |
| 64 | + * @param wallet The wallet address to read from |
| 65 | + * @return code The first 23 bytes of bytecode (3 byte prefix + 20 byte delegate) |
| 66 | + */ |
| 67 | + function _getContractCode(address wallet) private view returns (bytes32 code) { |
| 68 | + assembly ("memory-safe") { |
| 69 | + extcodecopy(wallet, 0, 0, EIP7702_BYTECODE_SIZE) |
| 70 | + code := mload(0) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @notice Check if an address is an EIP-7702 wallet |
| 76 | + * @param wallet The wallet address to check |
| 77 | + * @return true if it's an EIP-7702 wallet, false otherwise |
| 78 | + */ |
| 79 | + function _isEip7702Delegate(address wallet) private view returns (bool) { |
| 80 | + if (wallet.code.length < EIP7702_BYTECODE_SIZE) return false; |
| 81 | + return bytes3(_getContractCode(wallet)) == EIP7702_PREFIX; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @notice Get the delegate address from an EIP-7702 wallet |
| 86 | + * @param wallet The EIP-7702 wallet address |
| 87 | + * @return delegate The delegate contract address |
| 88 | + */ |
| 89 | + function _getEip7702Delegate(address wallet) private view returns (address) { |
| 90 | + bytes32 code = _getContractCode(wallet); |
| 91 | + return address(bytes20(code << 24)); |
| 92 | + } |
| 93 | + |
| 94 | + function _checkTransferFromNative(address wallet) private view returns (bool) { |
| 95 | + // Check if the function selector exists by examining the contract bytecode |
| 96 | + // Since transferFromNative requires authorization, we can't call it directly |
| 97 | + bytes4 selector = IERC7914.transferFromNative.selector; |
| 98 | + |
| 99 | + // Use low-level call to check if function exists |
| 100 | + (bool success, bytes memory returnData) = |
| 101 | + wallet.staticcall(abi.encodeWithSelector(selector, address(0), address(0), 0)); |
| 102 | + |
| 103 | + // If the call succeeded and returned a valid boolean, the function exists |
| 104 | + if (success && returnData.length == 32) { |
| 105 | + // Decode and verify it's a valid boolean (0 or 1) |
| 106 | + uint256 returnValue = abi.decode(returnData, (uint256)); |
| 107 | + if (returnValue <= 1) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // If the call succeeded but returned empty data, it hit the fallback |
| 113 | + // This indicates the function doesn't exist |
| 114 | + if (success && returnData.length == 0) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + return false; // Default to function not existing |
| 119 | + } |
| 120 | +} |
0 commit comments