Created
August 13, 2020 14:15
-
-
Save arefaslani/1dc7f01bf1bd54097316aef2e0f0e60d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.0; | |
contract EIP712Base { | |
struct EIP712Domain { | |
string name; | |
string version; | |
uint256 chainId; | |
address verifyingContract; | |
} | |
bytes32 internal constant EIP712_DOMAIN_TYPEHASH = keccak256( | |
bytes( | |
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" | |
) | |
); | |
bytes32 internal domainSeperator; | |
constructor(string memory name, string memory version) public { | |
domainSeperator = keccak256( | |
abi.encode( | |
EIP712_DOMAIN_TYPEHASH, | |
keccak256(bytes(name)), | |
keccak256(bytes(version)), | |
getChainID(), | |
address(this) | |
) | |
); | |
} | |
function getChainID() internal pure returns (uint256 id) { | |
assembly { | |
id := chainid() | |
} | |
} | |
function getDomainSeperator() private view returns (bytes32) { | |
return domainSeperator; | |
} | |
/** | |
* Accept message hash and returns hash message in EIP712 compatible form | |
* So that it can be used to recover signer from signature signed using EIP712 formatted data | |
* https://eips.ethereum.org/EIPS/eip-712 | |
* "\\x19" makes the encoding deterministic | |
* "\\x01" is the version byte to make it compatible to EIP-191 | |
*/ | |
function toTypedMessageHash(bytes32 messageHash) | |
internal | |
view | |
returns (bytes32) | |
{ | |
return | |
keccak256( | |
abi.encodePacked("\x19\x01", getDomainSeperator(), messageHash) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment