How to use EIP-712 Signature
Introduction to EIP-712
EIP-712, or "Typed structured data hashing and signing," is a standard in Ethereum Improvement Proposals. It provides a standardized way to sign structured data, making the signing process more secure and user-friendly.
Key Components of EIP-712 Signatures
-
EIP712Domain: Every EIP-712 signature must include an EIP712Domain section. This section contains crucial information about the contract and the environment:
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
];This information is displayed during the signing process and ensures that the signature can only be verified by a specific contract on a specific chain.
-
Domain Object: In your signing script, you need to provide the domain information:
const domain = {
name: "EIP712Voting",
version: "1",
chainId: 71, // Conflux eSpace testnet
verifyingContract: "0xDD1184EeC78eD419d948887B8793E64a62f13895",
}; -
Custom Types: You need to define custom types that match your contract's structure:
const types = {
Vote: [
{ name: "voter", type: "address" },
{ name: "proposal", type: "uint256" },
{ name: "nonce", type: "uint256" },
],
}; -
Message: Create a message object with the data to be signed:
const value = {
voter: await signer.getAddress(),
proposal: 1, // Voting for proposal 1
nonce: await contract.nonces(signer.address),
}; -
Signing Process: Use the wallet's
signTypedData()
method to create the signature:const signature = await signer.signTypedData(domain, types, value);