forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFTPermit.sol
103 lines (88 loc) · 3.17 KB
/
NFTPermit.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/Multicall.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFTPermit is ERC712, Multicall {
using Counters for Counters.Counter;
mapping (address => Counters.Counter) private _nonces;
bytes32 private immutable _PERMIT721_TYPEHASH = keccak256("Permit721(address registry,uint256 tokenid,address from, address to,uint256 nonce,uint256 deadline)");
bytes32 private immutable _PERMIT1155_TYPEHASH = keccak256("Permit1155(address registry,uint256 tokenid,address from, address to,uint256 value,uint256 nonce,uint256 deadline,bytes data)");
constructor(string memory name)
EIP712(name, "1")
{}
function DOMAIN_SEPARATOR() external view override returns (bytes32) {
return _domainSeparatorV4();
}
function transfer721WithSign(
IERC721 registry,
uint256 tokenId,
address to,
uint256 deadline,
bytes memory signature
)
external
{
require(block.timestamp <= deadline, "NFTPermit::transfer721WithSign: Expired deadline");
address from = registry.ownerOf(tokenId);
require(
SignatureChecker.isValidSignatureNow(
from,
_hashTypedDataV4(keccak256(abi.encode(
_PERMIT721_TYPEHASH,
registry,
tokenId,
from,
to,
_useNonce(from),
deadline
))),
signature
),
"NFTPermit::transfer721WithSign: Invalid signature"
);
registry.safeTransferFrom(from, to, tokenId);
}
function transfer1155WithSign(
IERC1155 registry,
uint256 tokenId,
address from,
address to,
uint256 value,
uint256 deadline,
bytes memory data,
bytes memory signature
)
external
{
require(block.timestamp <= deadline, "NFTPermit::transfer1155WithSign: Expired deadline");
require(
SignatureChecker.isValidSignatureNow(
from,
_hashTypedDataV4(keccak256(abi.encode(
_PERMIT1155_TYPEHASH,
registry,
tokenId,
from,
to,
value,
_useNonce(from),
deadline,
keccak256(data)
))),
signature
),
"NFTPermit::transfer1155WithSign: Invalid signature"
);
registry.safeTransferFrom(from, to, tokenId, value, data);
}
function nonces(address owner) external view virtual override returns (uint256) {
return _nonces[owner].current();
}
function _useNonce(address owner) internal virtual returns (uint256 current) {
Counters.Counter storage nonce = _nonces[owner];
current = nonce.current();
nonce.increment();
}
}