// SPDX-License-Identifier: MIT pragma solidity 0.8.24; /// @title PayGuard Test USD (tUSDC) - GOAT Network Testnet3 only, no value. /// @notice Minimal ERC-20 with EIP-3009 transferWithAuthorization (the method x402 "exact" EVM payments use), /// an EIP-712 domain identical in shape to Circle USDC (name, version "1", chainId, verifyingContract), /// and an open faucet (10 tUSDC per address per 24 h) so anyone can test the x402 route without asking us. contract PayGuardTestUSD { string public constant name = "PayGuard Test USD"; string public constant symbol = "tUSDC"; string public constant version = "1"; uint8 public constant decimals = 6; uint256 public constant FAUCET_AMOUNT = 10_000_000; // 10 tUSDC uint256 public constant FAUCET_COOLDOWN = 1 days; bytes32 public constant TRANSFER_WITH_AUTHORIZATION_TYPEHASH = keccak256("TransferWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)"); bytes32 public constant RECEIVE_WITH_AUTHORIZATION_TYPEHASH = keccak256("ReceiveWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)"); bytes32 private constant EIP712_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); uint256 private constant HALF_N = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; mapping(address => mapping(bytes32 => bool)) public authorizationState; mapping(address => uint256) public lastFaucet; event Transfer(address indexed from, address indexed to, uint256 value); event Approval(address indexed owner, address indexed spender, uint256 value); event AuthorizationUsed(address indexed authorizer, bytes32 indexed nonce); function DOMAIN_SEPARATOR() public view returns (bytes32) { return keccak256(abi.encode(EIP712_TYPEHASH, keccak256(bytes(name)), keccak256(bytes(version)), block.chainid, address(this))); } function eip712Domain() external view returns (bytes1, string memory, string memory, uint256, address, bytes32, uint256[] memory) { return (hex"0f", name, version, block.chainid, address(this), bytes32(0), new uint256[](0)); } function faucet() external { require(block.timestamp >= lastFaucet[msg.sender] + FAUCET_COOLDOWN, "faucet: cooldown"); lastFaucet[msg.sender] = block.timestamp; totalSupply += FAUCET_AMOUNT; balanceOf[msg.sender] += FAUCET_AMOUNT; emit Transfer(address(0), msg.sender, FAUCET_AMOUNT); } /// @notice Faucet to another address (lets a gasless payer be funded by anyone with gas). function faucetTo(address to) external { require(block.timestamp >= lastFaucet[to] + FAUCET_COOLDOWN, "faucet: cooldown"); lastFaucet[to] = block.timestamp; totalSupply += FAUCET_AMOUNT; balanceOf[to] += FAUCET_AMOUNT; emit Transfer(address(0), to, FAUCET_AMOUNT); } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function approve(address spender, uint256 value) external returns (bool) { allowance[msg.sender][spender] = value; emit Approval(msg.sender, spender, value); return true; } function transferFrom(address from, address to, uint256 value) external returns (bool) { uint256 a = allowance[from][msg.sender]; if (a != type(uint256).max) { require(a >= value, "ERC20: allowance"); allowance[from][msg.sender] = a - value; } _transfer(from, to, value); return true; } function transferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s) external { _useAuthorization(TRANSFER_WITH_AUTHORIZATION_TYPEHASH, from, to, value, validAfter, validBefore, nonce, v, r, s); _transfer(from, to, value); } function transferWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, bytes memory signature) external { (uint8 v, bytes32 r, bytes32 s) = _split(signature); _useAuthorization(TRANSFER_WITH_AUTHORIZATION_TYPEHASH, from, to, value, validAfter, validBefore, nonce, v, r, s); _transfer(from, to, value); } function receiveWithAuthorization(address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s) external { require(to == msg.sender, "EIP3009: caller must be payee"); _useAuthorization(RECEIVE_WITH_AUTHORIZATION_TYPEHASH, from, to, value, validAfter, validBefore, nonce, v, r, s); _transfer(from, to, value); } function _useAuthorization(bytes32 typehash, address from, address to, uint256 value, uint256 validAfter, uint256 validBefore, bytes32 nonce, uint8 v, bytes32 r, bytes32 s) internal { require(block.timestamp > validAfter, "EIP3009: not yet valid"); require(block.timestamp < validBefore, "EIP3009: expired"); require(!authorizationState[from][nonce], "EIP3009: authorization used"); bytes32 structHash = keccak256(abi.encode(typehash, from, to, value, validAfter, validBefore, nonce)); bytes32 digest = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), structHash)); require(uint256(s) <= HALF_N, "EIP3009: bad s"); if (v < 27) v += 27; address signer = ecrecover(digest, v, r, s); require(signer != address(0) && signer == from, "EIP3009: invalid signature"); authorizationState[from][nonce] = true; emit AuthorizationUsed(from, nonce); } function _split(bytes memory sig) internal pure returns (uint8 v, bytes32 r, bytes32 s) { require(sig.length == 65, "EIP3009: bad signature length"); assembly { r := mload(add(sig, 32)) s := mload(add(sig, 64)) v := byte(0, mload(add(sig, 96))) } } function _transfer(address from, address to, uint256 value) internal { require(to != address(0), "ERC20: to zero"); uint256 b = balanceOf[from]; require(b >= value, "ERC20: balance"); unchecked { balanceOf[from] = b - value; } balanceOf[to] += value; emit Transfer(from, to, value); } }