eip712

raw JSON →
0.3.3 verified Fri May 01 auth: no python

Message classes for typed structured data hashing and signing in Ethereum, based on EIP-712. Current version 0.3.3, requires Python >=3.10, <4. Maintained actively by ApeWorX.

pip install eip712
error ModuleNotFoundError: No module named 'eip712'
cause Library not installed.
fix
Run 'pip install eip712'
error ImportError: cannot import name 'EIP712Type' from 'eip712'
cause EIP712Type was removed in version 0.3.0.
fix
Use pydantic models instead; or upgrade to a version <0.3.0 if you must use EIP712Type.
error ValidationError: 1 validation error for Mail from_ field required
cause Field name 'from_' is used but pydantic treats it as a field name; likely missing alias.
fix
Define the field with an alias: from_: Person = Field(alias='from')
error AttributeError: 'EIP712Message' object has no attribute 'to_message'
cause Method 'to_message' may not exist in older versions.
fix
Use 'encode_712()' or 'signable_message()' depending on needs. Check docstrings.
breaking v0.3.0 replaced dataclassy with pydantic. If you subclasses EIP712Message directly, you need to adapt to pydantic behavior (e.g., field aliases, validation).
fix Ensure you use pydantic field types and aliases instead of dataclassy patterns.
breaking In v0.3.0, EIP712Type was removed as a separate class. Inner types should be plain pydantic models or Python types.
fix Remove EIP712Type subclass usage; define nested types as pydantic models directly.
gotcha Field names that conflict with Python keywords (e.g., 'from') must use pydantic aliasing with Field(alias=...).
fix Use from eip712 import EIP712Message, Field; then define from_: str = Field(alias='from')
deprecated String type annotations like 'uint256' are deprecated; use pydantic numeric types (e.g., int, float) or custom validators.
fix Replace 'uint256' with int and add pydantic validators if needed.

Define EIP-712 message structures using pydantic-based classes and generate the structured data for signing.

from eip712 import EIP712Message, EIP712Domain

class Person(EIP712Message):
    name: str
    wallet: str

class Mail(EIP712Message):
    from_: Person = Person(name='Alice', wallet='0x...')  # field alias
    to: Person
    contents: str

message = Mail(
    from_=Person(name='Alice', wallet='0x...'),
    to=Person(name='Bob', wallet='0x...'),
    contents='Hello!'
)
print(message.to_message())
print(message.encode_712())
print(message.signable_message())