I've been revisiting ethers.js
recently to refresh my understanding of the details and to write a simple tutorial called "WTF Ethers" for beginners.
Twitter: @0xAA_Science
Community: Website wtf.academy | WTF Solidity | discord | WeChat Group Application
All the code and tutorials are open-sourced on GitHub: github.com/WTFAcademy/WTF-Ethers
In this tutorial, we will introduce how to use ethers.js
to write an EIP712 signature script. Please refer to WTF Solidity 52: EIP712 for details on EIP712 contract.
EIP712
EIP712 Typed Data Signatures provides a more advanced and secure method for signatures. When a Dapp supporting EIP712 requests a signature, the wallet will display the original data of the signature message, allowing the user to verify the message data before signing.
EIP712 Signature Script
In this section, we will write a script to sign ERP712 signature.
-
Create
provider
andwallet
objects. In this example, we will use the private key of the Remix test wallet. -
Create the EIP712 Domain, which includes the contract's
name
,version
(usually set to "1"),chainId
, andverifyingContract
(the address of the contract that verifies the signature). -
Create the typed data for the signature message, where
types
declares the types, andmessage
contains the data. -
Call the
signTypedData()
signing method of the wallet object with the previously createddomain
,types
, andmessage
variables: -
You can use the
verifyTypedData()
method to recover the signer address from the signature and message, and verify the validity of the signature. Typically, this step is executed in a smart contract.
Summary
In this tutorial, we have introduced how to write an EIP712 signature script using ethers.js
.