I'm recently re-learning solidity, consolidating the details, and writing a "WTF Solidity Minimalist Introduction" for novices (programming experts can find another tutorial), updating 1-3 lectures every week.
Twitter: @0xAA_Science
Community: Discord|WeChat Group |Official website wtf.academy
All codes and tutorials are open source on github: github.com/AmazingAng/WTF-Solidity
In this lecture, we introduce a more advanced and secure signature method, EIP712 typed data signature.
EIP712
Previously we introduced EIP191 signature standard (personal sign), which can sign a message. But it is too simple. When the signature data is complex, the user can only see a string of hexadecimal strings (the hash of the data) and cannot verify whether the signature content is as expected.

EIP712 Typed Data Signature is a more advanced and more secure signature method. When an EIP712-enabled Dapp requests a signature, the wallet displays the original data of the signed message and the user can sign after verifying that the data meets expectations.

How to use EIP712
The application of EIP712 generally includes two parts: off-chain signature (front-end or script) and on-chain verification (contract). Below we use a simple example EIP712Storage
to introduce the use of EIP712. The EIP712Storage
contract has a state variable number
, which needs to be verified by the EIP712 signature before it can be changed.
Off-chain signature
-
The EIP712 signature must contain an
EIP712Domain
part, which contains the name of the contract, version (generally agreed to be "1"), chainId, and verifyingContract (the contract address to verify the signature).This information is displayed when the user signs and ensures that only specific contracts for a specific chain can verify the signature. You need to pass in the corresponding parameters in the script.
-
You need to customize a signature data type according to the usage scenario, and it must match the contract. In the
EIP712Storage
example, we define aStorage
type, which has two members:spender
of typeaddress
, which specifies the caller who can modify the variable;number
of typeuint256
, which specifies The modified value of the variable. -
Create a
message
variable and pass in the typed data to be signed. -
Call the
signTypedData()
method of the wallet object, passing in thedomain
,types
, andmessage
variables from the previous step for signature (ethersjs v6
is used here).
On-chain verification
Next is the EIP712Storage
contract part, which needs to verify the signature and, if passed, modify the number
state variable. It has 5
state variables.
EIP712DOMAIN_TYPEHASH
: The type hash ofEIP712Domain
, which is a constant.STORAGE_TYPEHASH
: The type hash ofStorage
, which is a constant.DOMAIN_SEPARATOR
: This is the unique value of each domain (Dapp) mixed in the signature, consisting ofEIP712DOMAIN_TYPEHASH
andEIP712Domain
(name, version, chainId, verifyingContract), initialized inconstructor()
.number
: The state variable that stores the value in the contract can be modified by thepermitStore()
method.owner
: Contract owner, initialized inconstructor()
, and verify the validity of the signature in thepermitStore()
method.
In addition, the EIP712Storage
contract has 3
functions.
- Constructor: Initialize
DOMAIN_SEPARATOR
andowner
. retrieve()
: Read the value ofnumber
.permitStore
: Verify the EIP712 signature and modify the value ofnumber
. First, it breaks the signature intor
,s
, andv
. The signed message textdigest
is then spelt out usingDOMAIN_SEPARATOR
,STORAGE_TYPEHASH
, the caller address, and the_num
parameter entered. Finally, use therecover()
method ofECDSA
to recover the signer's address. If the signature is valid, update the value ofnumber
.
Remix Reappearance
-
Deploy the
EIP712Storage
contract. -
Run
eip712storage.html
, change theContract Address
to the deployedEIP712Storage
contract address, and then click theConnect Metamask
andSign Permit
buttons to sign. To sign, use the wallet that deploys the contract, such as the Remix test wallet: -
Call the
permitStore()
method of the contract, enter the corresponding_num
and signature, and modify the value ofnumber
. -
Call the
retrieve()
method of the contract and see that the value ofnumber
has changed.
Summary
In this lecture, we introduce EIP712 typed data signature, a more advanced and secure signature standard. When requesting a signature, the wallet displays the original data of the signed message and the user can sign after verifying the data. This standard is widely used and is used in Metamask, Uniswap token pairs, DAI stable currency and other scenarios. I hope everyone can master it.