|
2 | 2 | pragma solidity ^0.8.20; |
3 | 3 |
|
4 | 4 | import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; |
| 5 | +import {IERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol"; |
| 6 | +import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; |
| 7 | +import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol"; |
| 8 | +import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol"; |
5 | 9 |
|
6 | | -/** |
7 | | - * @title MockUSDC |
8 | | - * @dev Simple ERC20 for POM Demo. 6 decimals to match real USDC. |
9 | | - * Public minting allowed for sandbox testing. |
10 | | - */ |
11 | | -contract MockUSDC is ERC20 { |
12 | | - constructor() ERC20("Mock USDC", "mUSDC") {} |
| 10 | +abstract contract ERC20PermitV2 is ERC20, IERC20Permit, EIP712, Nonces { |
| 11 | + bytes32 private constant PERMIT_TYPEHASH = |
| 12 | + keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); |
| 13 | + |
| 14 | + constructor(string memory name) EIP712(name, "2") {} |
| 15 | + |
| 16 | + function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) |
| 17 | + public |
| 18 | + virtual |
| 19 | + override |
| 20 | + { |
| 21 | + require(block.timestamp <= deadline, "ERC20Permit: expired deadline"); |
| 22 | + |
| 23 | + bytes32 structHash = keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline)); |
| 24 | + bytes32 digest = _hashTypedDataV4(structHash); |
| 25 | + |
| 26 | + address recoveredAddress = ECDSA.recover(digest, v, r, s); |
| 27 | + require(recoveredAddress == owner, "ERC20Permit: invalid signature"); |
| 28 | + |
| 29 | + _approve(owner, spender, value); |
| 30 | + } |
| 31 | + |
| 32 | + function nonces(address owner) public view virtual override(IERC20Permit, Nonces) returns (uint256) { |
| 33 | + return super.nonces(owner); |
| 34 | + } |
| 35 | + |
| 36 | + function DOMAIN_SEPARATOR() external view returns (bytes32) { |
| 37 | + return _domainSeparatorV4(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +contract MockUSDC is ERC20PermitV2 { |
| 42 | + mapping(address => mapping(bytes32 => bool)) private _authorizationStates; |
| 43 | + |
| 44 | + bytes32 public constant TRANSFER_WITH_AUTHORIZATION_TYPEHASH = keccak256( |
| 45 | + "TransferWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)" |
| 46 | + ); |
| 47 | + |
| 48 | + bytes32 public constant RECEIVE_WITH_AUTHORIZATION_TYPEHASH = keccak256( |
| 49 | + "ReceiveWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)" |
| 50 | + ); |
| 51 | + |
| 52 | + bytes32 public constant CANCEL_AUTHORIZATION_TYPEHASH = |
| 53 | + keccak256("CancelAuthorization(address authorizer,bytes32 nonce)"); |
| 54 | + |
| 55 | + event AuthorizationUsed(address indexed authorizer, bytes32 indexed nonce); |
| 56 | + event AuthorizationCanceled(address indexed authorizer, bytes32 indexed nonce); |
| 57 | + |
| 58 | + constructor() ERC20("USD Coin", "USDC") ERC20PermitV2("USD Coin") { |
| 59 | + _mint(msg.sender, 1_000_000 * 10 ** decimals()); |
| 60 | + } |
13 | 61 |
|
14 | | - // 6 decimals to match USDC on Base |
15 | 62 | function decimals() public view virtual override returns (uint8) { |
16 | 63 | return 6; |
17 | 64 | } |
18 | 65 |
|
19 | | - /** |
20 | | - * @dev Mint tokens for testing. Publicly available for sandbox faucets. |
21 | | - */ |
22 | 66 | function mint(address to, uint256 amount) external { |
23 | 67 | _mint(to, amount); |
24 | 68 | } |
| 69 | + |
| 70 | + function transferWithAuthorization( |
| 71 | + address from, |
| 72 | + address to, |
| 73 | + uint256 value, |
| 74 | + uint256 validAfter, |
| 75 | + uint256 validBefore, |
| 76 | + bytes32 nonce, |
| 77 | + uint8 v, |
| 78 | + bytes32 r, |
| 79 | + bytes32 s |
| 80 | + ) external { |
| 81 | + _transferWithAuthorization(from, to, value, validAfter, validBefore, nonce, v, r, s); |
| 82 | + } |
| 83 | + |
| 84 | + function receiveWithAuthorization( |
| 85 | + address from, |
| 86 | + address to, |
| 87 | + uint256 value, |
| 88 | + uint256 validAfter, |
| 89 | + uint256 validBefore, |
| 90 | + bytes32 nonce, |
| 91 | + uint8 v, |
| 92 | + bytes32 r, |
| 93 | + bytes32 s |
| 94 | + ) external { |
| 95 | + require(msg.sender == to, "caller must be the recipient"); |
| 96 | + _transferWithAuthorization(from, to, value, validAfter, validBefore, nonce, v, r, s); |
| 97 | + } |
| 98 | + |
| 99 | + function cancelAuthorization(address authorizer, bytes32 nonce, uint8 v, bytes32 r, bytes32 s) external { |
| 100 | + bytes32 structHash = keccak256(abi.encode(CANCEL_AUTHORIZATION_TYPEHASH, authorizer, nonce)); |
| 101 | + bytes32 digest = _hashTypedDataV4(structHash); |
| 102 | + address signer = ECDSA.recover(digest, v, r, s); |
| 103 | + require(signer == authorizer, "invalid signature"); |
| 104 | + |
| 105 | + require(!_authorizationStates[authorizer][nonce], "authorization already used"); |
| 106 | + _authorizationStates[authorizer][nonce] = true; |
| 107 | + |
| 108 | + emit AuthorizationCanceled(authorizer, nonce); |
| 109 | + } |
| 110 | + |
| 111 | + function _transferWithAuthorization( |
| 112 | + address from, |
| 113 | + address to, |
| 114 | + uint256 value, |
| 115 | + uint256 validAfter, |
| 116 | + uint256 validBefore, |
| 117 | + bytes32 nonce, |
| 118 | + uint8 v, |
| 119 | + bytes32 r, |
| 120 | + bytes32 s |
| 121 | + ) internal { |
| 122 | + require(block.timestamp > validAfter, "authorization is not yet valid"); |
| 123 | + require(block.timestamp < validBefore, "authorization is expired"); |
| 124 | + require(!_authorizationStates[from][nonce], "authorization already used"); |
| 125 | + |
| 126 | + bytes32 structHash = keccak256( |
| 127 | + abi.encode(TRANSFER_WITH_AUTHORIZATION_TYPEHASH, from, to, value, validAfter, validBefore, nonce) |
| 128 | + ); |
| 129 | + bytes32 digest = _hashTypedDataV4(structHash); |
| 130 | + address signer = ECDSA.recover(digest, v, r, s); |
| 131 | + require(signer == from, "invalid signature"); |
| 132 | + |
| 133 | + _authorizationStates[from][nonce] = true; |
| 134 | + _transfer(from, to, value); |
| 135 | + |
| 136 | + emit AuthorizationUsed(from, nonce); |
| 137 | + } |
25 | 138 | } |
0 commit comments