EIP-712 Structs for Python
A Python library providing a robust interface for constructing EIP-712 typed data structures. It simplifies the creation of structured messages that can be signed off-chain and verified on-chain, adhering to the Ethereum EIP-712 standard. The library is currently at version `0.0.1` and aims to streamline secure off-chain data signing.
Warnings
- gotcha The PyPI package is `poly-eip712-structs`, but the Python import name is `eip712_structs`. Ensure you use `from eip712_structs import ...` in your code.
- gotcha Be aware of other packages named `eip712-structs` or `eip712-structs-ng` on PyPI which use the same `eip712_structs` import path. Installing multiple such packages might lead to import conflicts or unexpected behavior.
- gotcha The `make_domain()` helper function for creating the EIP-712 domain separator requires at least one parameter (e.g., `name`, `version`, `chainId`) to be defined. Omitting all parameters will result in an invalid domain struct definition.
- gotcha The GitHub README states support for Python 3.6 and 3.7. However, the official PyPI metadata specifies `requires_python: >=3.9.10`. Users on older Python versions might encounter installation or runtime issues.
- breaking As the library is currently at version `0.0.1`, it is in an early development stage. Future updates, especially before a `1.0.0` release, may introduce breaking API changes without strict adherence to semantic versioning for non-major versions.
Install
-
pip install poly-eip712-structs
Imports
- EIP712Struct
from eip712_structs import EIP712Struct
- make_domain
from eip712_structs import make_domain
- String
from eip712_structs import String
- Uint
from eip712_structs import Uint
- Address
from eip712_structs import Address
- struct
import struct
Quickstart
from eip712_structs import make_domain, EIP712Struct, String, Uint
import os
# Define a domain separator for the EIP-712 message
domain = make_domain(name='My Application', version='1.0.0', chainId=1)
# Define your custom EIP-712 struct type
class MyStruct(EIP712Struct):
some_string = String()
some_number = Uint(256)
# Create an instance of your struct with data
my_data = MyStruct(some_string='hello world', some_number=1234)
# Convert to EIP-712 message dictionary
message_dict = my_data.to_message(domain)
print(f"EIP-712 Message Dict: {message_dict}")
# Get the signable bytes hash (e.g., for use with a private key)
signable_bytes = my_data.signable_bytes(domain)
print(f"Signable Bytes Hash: {signable_bytes.hex()}")
# Example of setting/getting values dictionary-style
my_data['some_number'] = 4567
assert my_data['some_number'] == 4567