import Web3 from "web3";
var sigUtil = require("eth-sig-util")
const typedData = {
types: {
EIP712Domain: [
{name: 'name', type: 'string'},
{name: 'version', type: 'string'},
{name: 'chainId', type: 'uint256' },
{name: 'verifyingContract', type: 'address' },
],
Mail: [
{name: 'from', type: 'address'},
{name: 'to', type: 'address'},
{name: 'value', type: 'uint256'},
],
},
domain: {
name: 'Demo',
version: '1.0',
chainId:'1',
verifyingContract:'0xf8e81D47203A594245E36C48e151709F0C19fBe8'
},
primaryType: 'Mail',
message: {
from: "0xE3a463d743F762D538031BAD3f1E748BB41f96ec",
to: "0x39Ef50bd29Ae125FE10C6a909E42e1C6a94Dde29",
value: 1234234145789,
},
}
var privateKeyHex = Buffer.from(privateKey, 'hex')
var signature = sigUtil.signTypedData_v4(privateKeyHex, {data: typedData})
}
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/ECDSA.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/draft-EIP712.sol";
contract MyContract is EIP712 {
constructor(string memory name, string memory version) EIP712(name, version) {}
function recoverV4(
address from,
address to,
uint256 value,
bytes memory signature
) public view returns (address) {
bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
keccak256("Mail(address from,address to,uint256 value)"),
from,
to,
value
)));
return ECDSA.recover(digest, signature);
}
}