const userscore = [{
name: "user",
type: "address"
}, {
name: "score",
type: "uint256"
}, {
name: "id",
type: "uint256"
}, {
name: "expire",
type: "uint256"
}];
// Data
const domainData = {
name: "Simple Dapp",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
};
var message = {
user: "0xe0428E45F41ae8a46284A9B64AB27e0B15A2eAF7",
score: 20,
id: 1,
expire: 20
};
let msgParams = JSON.stringify({
types: {
EIP712Domain: domain,
UserScore: userscore
},
domain: domainData,
primaryType: "UserScore",
message: message
})`
And my contract code, with correct address, r, s, v and another things.
`pragma experimental ABIEncoderV2;
pragma solidity ^0.5.0;
string private constant EIP712_DOMAIN = "EIP712Domain(string name,string version,uint256 chainId)";
string private constant USERSCORE_TYPE = "UserScore(address user,uint256 score,uint256 id,uint256 time)";
bytes32 private constant EIP712_DOMAIN_TYPEHASH = keccak256(abi.encodePacked(EIP712_DOMAIN));
bytes32 private constant USERSCORE_TYPEHASH = keccak256(abi.encodePacked(USERSCORE_TYPE));
bytes32 private constant DOMAIN_SEPARATOR = keccak256(abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256("Simple Dapp"),
keccak256("1"),
chainId
));
struct UserScore {
address user;
uint256 score;
uint256 id;
uint256 time;
}
function hashUserScore(UserScore memory userscore) private pure returns (bytes32){
return keccak256(abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR,
keccak256(abi.encode(
USERSCORE_TYPEHASH,
userscore.user,
userscore.score,
userscore.id,
userscore.time
))
));
}
function verify() public pure returns (bool) {
UserScore memory userscore = UserScore({
user: 0xe0428E45F41ae8a46284A9B64AB27e0B15A2eAF7,
score: 20,
id: 1,
time: 20
});
bytes32 sigR = 0x1443eafac538e7a767cbc1308e34ef088734b7e59dcd25baedace361a4356be1;
bytes32 sigS = 0x2fd582b765b7cf85c3137c06dbfbfb58f02a2a891cc8c78bb4f781888d5a2a7e;
uint8 sigV = 28;
address signer = 0xe0428E45F41ae8a46284A9B64AB27e0B15A2eAF7;
return signer == ecrecover(hashUserScore(userscore), sigV, sigR, sigS);
}