You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.7 KiB
86 lines
2.7 KiB
// SPDX-License-Identifier: MIT
|
|
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/ERC20Permit.sol)
|
|
|
|
pragma solidity ^0.8.19;
|
|
|
|
import "./IERC20Permit.sol";
|
|
import "../ERC20.sol";
|
|
import "../../../utils/cryptography/ECDSA.sol";
|
|
import "../../../utils/cryptography/EIP712.sol";
|
|
import "../../../utils/Nonces.sol";
|
|
|
|
/**
|
|
* @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
|
|
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
|
|
*
|
|
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
|
|
* presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
|
|
* need to send a transaction, and thus is not required to hold Ether at all.
|
|
*
|
|
* _Available since v3.4._
|
|
*/
|
|
abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces {
|
|
// solhint-disable-next-line var-name-mixedcase
|
|
bytes32 private constant _PERMIT_TYPEHASH =
|
|
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
|
|
|
|
/**
|
|
* @dev Permit deadline has expired.
|
|
*/
|
|
error ERC2612ExpiredSignature(uint256 deadline);
|
|
|
|
/**
|
|
* @dev Mismatched signature.
|
|
*/
|
|
error ERC2612InvalidSigner(address signer, address owner);
|
|
|
|
/**
|
|
* @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
|
|
*
|
|
* It's a good idea to use the same `name` that is defined as the ERC20 token name.
|
|
*/
|
|
constructor(string memory name) EIP712(name, "1") {}
|
|
|
|
/**
|
|
* @dev See {IERC20Permit-permit}.
|
|
*/
|
|
function permit(
|
|
address owner,
|
|
address spender,
|
|
uint256 value,
|
|
uint256 deadline,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) public virtual {
|
|
if (block.timestamp > deadline) {
|
|
revert ERC2612ExpiredSignature(deadline);
|
|
}
|
|
|
|
bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
|
|
|
|
bytes32 hash = _hashTypedDataV4(structHash);
|
|
|
|
address signer = ECDSA.recover(hash, v, r, s);
|
|
if (signer != owner) {
|
|
revert ERC2612InvalidSigner(signer, owner);
|
|
}
|
|
|
|
_approve(owner, spender, value);
|
|
}
|
|
|
|
/**
|
|
* @dev See {IERC20Permit-nonces}.
|
|
*/
|
|
function nonces(address owner) public view virtual override(IERC20Permit, Nonces) returns (uint256) {
|
|
return super.nonces(owner);
|
|
}
|
|
|
|
/**
|
|
* @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
|
|
*/
|
|
// solhint-disable-next-line func-name-mixedcase
|
|
function DOMAIN_SEPARATOR() external view virtual returns (bytes32) {
|
|
return _domainSeparatorV4();
|
|
}
|
|
}
|
|
|