@@ -25,7 +25,7 @@ struct Message {
2525
2626Python representation:
2727``` python
28- from eip712_structs import EIP712Struct, Address, String, make_domain, struct_to_dict
28+ from eip712_structs import EIP712Struct, Address, String, make_domain, struct_to_message
2929
3030class Message (EIP712Struct ):
3131 to = Address()
@@ -37,7 +37,16 @@ msg = Message(to='0xdead...beef', contents='hello world')
3737msg.encode_data() # The struct's data in encoded form
3838
3939domain = make_domain(name = ' example' )
40- msg_body, msg_hash = struct_to_dict(msg, domain)
40+ msg_body, signable_bytes = struct_to_message(msg, domain)
41+
42+ # msg_body is a standardized dict with keys primaryType, types, domain, and message
43+ # suitable for converting to JSON for making requests
44+
45+ # `signable bytes` is a deterministic bytes representation of the struct
46+ # Suitable for hashing or signing
47+ # bytes 0-1: b'\x19\x01'
48+ # bytes 2-33: domain separator (hash of domain type and data)
49+ # bytes 34-65: hash of struct type and data
4150```
4251
4352#### Dynamic construction
@@ -52,22 +61,29 @@ Message.to = Address()
5261setattr (Message, ' from' , Address())
5362```
5463
55- #### Creating Messages and Hashing
64+ #### The domain separator
5665Messages also require a domain struct. A helper method exists for this purpose.
66+ All values to the ` make_domain() `
67+ function are optional - but at least one must be defined. If omitted, the resulting
68+ domain struct's definition leaves out the parameter entirely.
69+
70+ The full signature: <br />
71+ ` make_domain(name: string, version: string, chainId: uint256, verifyingContract: address, salt: bytes32) `
5772
5873``` python
59- from eip712_structs import EIP712Struct, String, make_domain, struct_to_dict
74+ from eip712_structs import EIP712Struct, String, make_domain, struct_to_message
6075
61- domain = make_domain(name = ' my_domain' ) # Also accepts kwargs: version, chainId, verifyingContract, salt
76+ domain = make_domain(name = ' my_domain' )
6277
6378class Foo (EIP712Struct ):
6479 bar = String()
6580
6681foo = Foo(bar = ' baz' )
6782
68- message_dict, message_hash = struct_to_dict (foo, domain)
83+ message_dict, message_hash = struct_to_message (foo, domain)
6984```
7085
86+
7187## Member Types
7288
7389### Basic types
@@ -111,6 +127,23 @@ Dog.encode_type() # Dog(string name,string breed)
111127Person.encode_type() # Person(string name,Dog dog)Dog(string name,string breed)
112128```
113129
130+ Instantiating the structs with nested values may be done like:
131+
132+ ``` python
133+ # Method one: set it to a struct
134+ dog = Dog(name = ' Mochi' , breed = ' Corgi' )
135+ person = Person(name = ' Ed' , dog = dog)
136+
137+ # Method two: set it to a dict - the underlying struct is built for you
138+ person = Person(
139+ name = ' Ed' ,
140+ dog = {
141+ ' name' : ' Mochi' ,
142+ ' breed' : ' Corgi' ,
143+ }
144+ )
145+ ```
146+
114147### Arrays
115148Arrays are also supported for the standard.
116149
@@ -123,8 +156,9 @@ array_member = Array(<item_type>[, <optional_length>])
123156
124157For example:
125158``` python
126- dynamic_array = Array(X()) # X[] dynamic_array
127- static_array = Array(X(), 10 ) # X[10] static_array
159+ dynamic_array = Array(String()) # String[] dynamic_array
160+ static_array = Array(String(), 10 ) # String[10] static_array
161+ struct_array = Array(MyStruct, 10 ) # MyStruct[10] - again, don't instantiate structs like the basic types
128162```
129163
130164## Development
0 commit comments