// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol"; import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol"; /// Paid calls on Fiber, wallet to wallet. /// /// A caller lets this contract spend their USDC (a signed permit is enough, so the caller needs no gas), then signs /// one Payment for each call. Anyone may submit a signed Payment, and the money can only go where the payer signed: /// `amount` to `payee` and `cut` to `till`. Each Payment works once and only until its deadline. /// /// Nothing is held here. There is no owner and nothing can be changed. contract FiberPay is EIP712 { using SafeERC20 for IERC20; struct Payment { address payer; address payee; uint256 amount; address till; uint256 cut; bytes32 nonce; uint256 deadline; } bytes32 public constant PAYMENT_TYPEHASH = keccak256( "Payment(address payer,address payee,uint256 amount,address till,uint256 cut,bytes32 nonce,uint256 deadline)" ); IERC20 public immutable token; /// payer => nonce => spent or cancelled mapping(address => mapping(bytes32 => bool)) public used; event Paid(address indexed payer, address indexed payee, uint256 amount, address till, uint256 cut, bytes32 nonce); event Cancelled(address indexed payer, bytes32 nonce); error Expired(); error AlreadyUsed(); error BadSignature(); constructor(IERC20 token_) EIP712("FiberPay", "1") { token = token_; } /// Collect a signed payment. Reverts unless every transfer in it goes through. function settle(Payment calldata p, bytes calldata signature) external { if (block.timestamp > p.deadline) revert Expired(); if (used[p.payer][p.nonce]) revert AlreadyUsed(); if (!_signedBy(p.payer, digest(p), signature)) revert BadSignature(); used[p.payer][p.nonce] = true; token.safeTransferFrom(p.payer, p.payee, p.amount); if (p.cut > 0) token.safeTransferFrom(p.payer, p.till, p.cut); emit Paid(p.payer, p.payee, p.amount, p.till, p.cut, p.nonce); } /// A payer voids a payment they signed and no longer want collected. function cancel(bytes32 nonce) external { used[msg.sender][nonce] = true; emit Cancelled(msg.sender, nonce); } /// The payer's own key first, whether or not the payer has code: a wallet with an EIP-7702 delegation has code /// and still signs with its key. Then ERC-1271, for contract wallets. (The ERC-8004 registry checks the same way.) function _signedBy(address payer, bytes32 hash, bytes calldata signature) private view returns (bool) { (address recovered, ECDSA.RecoverError err,) = ECDSA.tryRecover(hash, signature); if (err == ECDSA.RecoverError.NoError && recovered == payer) return true; return SignatureChecker.isValidERC1271SignatureNow(payer, hash, signature); } /// The EIP-712 hash a payer signs for `p`. function digest(Payment calldata p) public view returns (bytes32) { return _hashTypedDataV4( keccak256(abi.encode(PAYMENT_TYPEHASH, p.payer, p.payee, p.amount, p.till, p.cut, p.nonce, p.deadline)) ); } }