Sign Typed Data (EIP-712)

npm versionLicense: GPL-3.0GitHub stars

EIP-712 is an Ethereum standard for hashing and signing typed structured data. This feature allows you to sign more than just a basic string message. It enables you to sign structured data, which can be useful for validating complex objects in decentralized applications, ensuring data integrity, and preventing signature replay attacks.

In this section, we will explore how to implement EIP-712 message signing using this library.

🧱 1. Framework Usage (ES6/Module Bundlers)

πŸ“ Installation

If you haven’t already:

Or if you use Yarn:

πŸ” A. Sign Typed Data Example

To sign structured data (e.g., user authentication, contract interactions), the EIP-712 standard requires the specification of a domain and message schema. Below is an example of how you can implement EIP-712 signing in your dApp:

πŸ’‘ What’s Happening Here:

  • EIP-712 Domain: This defines the context of the signature, like the contract address and chain ID.

  • Primary Type: The main structure being signed β€” in this case, a Mail object containing from, to, and contents.

  • Message: The actual data being signed. This includes the sender (from), recipient (to), and the message body (contents).

  • Provider & Account: As with other functions, we first establish the connection to the wallet.

For typescript users, declare as a module in your types file:

🌐 2. CommonJS via CDN (HTML/Vanilla JS)

βœ… Embed Script

Ensure this is added before your custom scripts in the HTML body.

πŸ” A. Sign Typed Data Example (CDN)

✨ Key Features of EIP-712:

  • It allows you to type-check data that is being signed, which provides better security than traditional messages.

  • It helps mitigate replay attacks by including a domain separator (chain ID, contract address).

  • Structured data can be signed in a more user-friendly way, which improves the user experience in decentralized applications (dApps).

For production readiness, always wrap async calls with try/catch:

πŸ“š Summary

Feature
Method
Gas Required
Use Case

Sign Typed Data

❌ No

Authentication, contract signatures

Last updated