EIP-712 Structs for Python

0.0.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to define an EIP-712 domain, create a custom struct, instantiate it with data, and convert it into a signable EIP-712 message dictionary and its corresponding byte hash.

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

view raw JSON →