ERC20Permit.sol 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/extensions/ERC20Permit.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC20Permit} from "./IERC20Permit.sol";
  5. import {ERC20} from "../ERC20.sol";
  6. import {ECDSA} from "../../../utils/cryptography/ECDSA.sol";
  7. import {EIP712} from "../../../utils/cryptography/EIP712.sol";
  8. import {Nonces} from "../../../utils/Nonces.sol";
  9. /**
  10. * @dev Implementation of the ERC-20 Permit extension allowing approvals to be made via signatures, as defined in
  11. * https://eips.ethereum.org/EIPS/eip-2612[ERC-2612].
  12. *
  13. * Adds the {permit} method, which can be used to change an account's ERC-20 allowance (see {IERC20-allowance}) by
  14. * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
  15. * need to send a transaction, and thus is not required to hold Ether at all.
  16. */
  17. abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces {
  18. bytes32 private constant PERMIT_TYPEHASH =
  19. keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
  20. /**
  21. * @dev Permit deadline has expired.
  22. */
  23. error ERC2612ExpiredSignature(uint256 deadline);
  24. /**
  25. * @dev Mismatched signature.
  26. */
  27. error ERC2612InvalidSigner(address signer, address owner);
  28. /**
  29. * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
  30. *
  31. * It's a good idea to use the same `name` that is defined as the ERC-20 token name.
  32. */
  33. constructor(string memory name) EIP712(name, "1") {}
  34. /**
  35. * @inheritdoc IERC20Permit
  36. */
  37. function permit(
  38. address owner,
  39. address spender,
  40. uint256 value,
  41. uint256 deadline,
  42. uint8 v,
  43. bytes32 r,
  44. bytes32 s
  45. ) public virtual {
  46. if (block.timestamp > deadline) {
  47. revert ERC2612ExpiredSignature(deadline);
  48. }
  49. bytes32 structHash = keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
  50. bytes32 hash = _hashTypedDataV4(structHash);
  51. address signer = ECDSA.recover(hash, v, r, s);
  52. if (signer != owner) {
  53. revert ERC2612InvalidSigner(signer, owner);
  54. }
  55. _approve(owner, spender, value);
  56. }
  57. /**
  58. * @inheritdoc IERC20Permit
  59. */
  60. function nonces(address owner) public view virtual override(IERC20Permit, Nonces) returns (uint256) {
  61. return super.nonces(owner);
  62. }
  63. /**
  64. * @inheritdoc IERC20Permit
  65. */
  66. // solhint-disable-next-line func-name-mixedcase
  67. function DOMAIN_SEPARATOR() external view virtual returns (bytes32) {
  68. return _domainSeparatorV4();
  69. }
  70. }