|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; |
| 5 | +import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol"; |
| 6 | +import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; |
| 7 | +import "@openzeppelin/contracts/interfaces/IERC1271.sol"; |
| 8 | +import "@openzeppelin/contracts/utils/Counters.sol"; |
| 9 | + |
| 10 | +import "./interfaces/IERC721Permit.sol"; |
| 11 | + |
| 12 | +contract ERC721Permit is IERC721Permit, ERC721, EIP712 { |
| 13 | + using Counters for Counters.Counter; |
| 14 | + |
| 15 | + mapping(uint256 => Counters.Counter) private _nonces; |
| 16 | + |
| 17 | + // solhint-disable-next-line var-name-mixedcase |
| 18 | + bytes32 private immutable _PERMIT_TYPEHASH = |
| 19 | + keccak256( |
| 20 | + "Permit(address spender,uint256 tokenId,uint256 nonce,uint256 deadline)" |
| 21 | + ); |
| 22 | + |
| 23 | + constructor(string memory name, string memory symbol) |
| 24 | + ERC721(name, symbol) |
| 25 | + EIP712(name, "1") |
| 26 | + { |
| 27 | + this; |
| 28 | + } |
| 29 | + |
| 30 | + function nonces(uint256 tokenId) |
| 31 | + external |
| 32 | + view |
| 33 | + virtual |
| 34 | + override |
| 35 | + returns (uint256) |
| 36 | + { |
| 37 | + return _nonces[tokenId].current(); |
| 38 | + } |
| 39 | + |
| 40 | + // solhint-disable-next-line func-name-mixedcase |
| 41 | + function DOMAIN_SEPARATOR() external view override returns (bytes32) { |
| 42 | + return _domainSeparatorV4(); |
| 43 | + } |
| 44 | + |
| 45 | + function supportsInterface(bytes4 interfaceId) |
| 46 | + public |
| 47 | + view |
| 48 | + virtual |
| 49 | + override(IERC165, ERC721) |
| 50 | + returns (bool) |
| 51 | + { |
| 52 | + return |
| 53 | + interfaceId == type(IERC721Permit).interfaceId || // 0x5604e225 |
| 54 | + super.supportsInterface(interfaceId); |
| 55 | + } |
| 56 | + |
| 57 | + function permit( |
| 58 | + address spender, |
| 59 | + uint256 tokenId, |
| 60 | + uint256 deadline, |
| 61 | + bytes memory signature |
| 62 | + ) external override { |
| 63 | + _permit(spender, tokenId, deadline, signature); |
| 64 | + } |
| 65 | + |
| 66 | + function _transfer( |
| 67 | + address from, |
| 68 | + address to, |
| 69 | + uint256 tokenId |
| 70 | + ) internal virtual override { |
| 71 | + _nonces[tokenId].increment(); |
| 72 | + super._transfer(from, to, tokenId); |
| 73 | + } |
| 74 | + |
| 75 | + function _permit( |
| 76 | + address spender, |
| 77 | + uint256 tokenId, |
| 78 | + uint256 deadline, |
| 79 | + bytes memory signature |
| 80 | + ) internal virtual { |
| 81 | + // solhint-disable-next-line not-rely-on-time |
| 82 | + require(block.timestamp <= deadline, "ERC721Permit: expired deadline"); |
| 83 | + |
| 84 | + bytes32 structHash = keccak256( |
| 85 | + abi.encode( |
| 86 | + _PERMIT_TYPEHASH, |
| 87 | + spender, |
| 88 | + tokenId, |
| 89 | + _nonces[tokenId].current(), |
| 90 | + deadline |
| 91 | + ) |
| 92 | + ); |
| 93 | + bytes32 hash = _hashTypedDataV4(structHash); |
| 94 | + |
| 95 | + (address signer, ) = ECDSA.tryRecover(hash, signature); |
| 96 | + bool isValidEOASignature = signer != address(0) && |
| 97 | + _isApprovedOrOwner(signer, tokenId); |
| 98 | + |
| 99 | + require( |
| 100 | + isValidEOASignature || |
| 101 | + _isValidContractERC1271Signature(ownerOf(tokenId), hash, signature) || |
| 102 | + _isValidContractERC1271Signature(getApproved(tokenId), hash, signature), |
| 103 | + "ERC721Permit: invalid signature" |
| 104 | + ); |
| 105 | + |
| 106 | + _approve(spender, tokenId); |
| 107 | + } |
| 108 | + |
| 109 | + function _isValidContractERC1271Signature( |
| 110 | + address signer, |
| 111 | + bytes32 hash, |
| 112 | + bytes memory signature |
| 113 | + ) private view returns (bool) { |
| 114 | + (bool success, bytes memory result) = signer.staticcall( |
| 115 | + abi.encodeWithSelector( |
| 116 | + IERC1271.isValidSignature.selector, |
| 117 | + hash, |
| 118 | + signature |
| 119 | + ) |
| 120 | + ); |
| 121 | + return (success && |
| 122 | + result.length == 32 && |
| 123 | + abi.decode(result, (bytes4)) == IERC1271.isValidSignature.selector); |
| 124 | + } |
| 125 | +} |
0 commit comments