712 signatures
This commit is contained in:
@ -15,9 +15,13 @@ contract ERC20 is IERC20 {
|
||||
mapping (address => uint256) public balanceOf;
|
||||
mapping (address => mapping (address => uint256)) public allowance;
|
||||
|
||||
// ERC-191 data
|
||||
// ERC-721 data
|
||||
bytes32 public DOMAIN_SEPARATOR;
|
||||
bytes32 public APPROVE_TYPEHASH = keccak256(
|
||||
"Approve(address owner,address spender,uint256 value,uint256 nonce,uint256 expiration)"
|
||||
);
|
||||
uint256 public chainId;
|
||||
mapping (address => uint256) public nonceFor;
|
||||
mapping (address => uint256) public nonces;
|
||||
|
||||
event Transfer(address indexed from, address indexed to, uint256 value);
|
||||
event Approval(address indexed owner, address indexed spender, uint256 value);
|
||||
@ -30,8 +34,15 @@ contract ERC20 is IERC20 {
|
||||
}
|
||||
|
||||
function initialize(uint256 _chainId) internal {
|
||||
require(chainId == 0, "ERC20: ALREADY_INITIALIZED");
|
||||
require(chainId == 0 && DOMAIN_SEPARATOR == bytes32(0), "ERC20: ALREADY_INITIALIZED");
|
||||
chainId = _chainId;
|
||||
DOMAIN_SEPARATOR = keccak256(abi.encode(
|
||||
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
|
||||
keccak256(bytes(name)),
|
||||
keccak256(bytes("1")),
|
||||
chainId,
|
||||
address(this)
|
||||
));
|
||||
}
|
||||
|
||||
function mint(address to, uint256 value) internal {
|
||||
@ -76,8 +87,8 @@ contract ERC20 is IERC20 {
|
||||
)
|
||||
external
|
||||
{
|
||||
require(chainId != 0, "ERC20: UNINITIALIZED");
|
||||
require(nonce == nonceFor[owner]++, "ERC20: INVALID_NONCE");
|
||||
require(chainId != 0 && DOMAIN_SEPARATOR != bytes32(0), "ERC20: UNINITIALIZED");
|
||||
require(nonce == nonces[owner]++, "ERC20: INVALID_NONCE");
|
||||
require(expiration > block.timestamp, "ERC20: EXPIRED_SIGNATURE");
|
||||
|
||||
require(v == 27 || v == 28, "ECDSA: INVALID_V");
|
||||
@ -85,13 +96,12 @@ contract ERC20 is IERC20 {
|
||||
|
||||
bytes32 digest = keccak256(abi.encodePacked(
|
||||
hex'19',
|
||||
hex'00',
|
||||
address(this),
|
||||
keccak256(abi.encodePacked(
|
||||
owner, spender, value, nonce, expiration, chainId
|
||||
hex'01',
|
||||
DOMAIN_SEPARATOR,
|
||||
keccak256(abi.encode(
|
||||
APPROVE_TYPEHASH, owner, spender, value, nonce, expiration
|
||||
))
|
||||
));
|
||||
|
||||
address recoveredAddress = ecrecover(digest, v, r, s);
|
||||
require(recoveredAddress != address(0), "ERC20: INVALID_RECOVERED_ADDRESS");
|
||||
require(owner == recoveredAddress, "ERC20: INVALID_SIGNATURE");
|
||||
|
||||
Reference in New Issue
Block a user