Helper functions
EIP-712 hashing helper
Hashing utilities for EIP-712 messages.
Copied under the MIT license from https://github.com/ethereum/eth-account/blob/main/eth_account/_utils/encode_typed_data/encoding_and_hashing.py with minor modifications.
- evmos.utils.eip_712_hash.encode_data(primary_type: str, types: Mapping[str, Sequence[_FieldDef]], data: Any) bytes[source]
Encode data defined by
primary_type.
- evmos.utils.eip_712_hash.encode_field(types: Mapping[str, Sequence[_FieldDef]], name: str, field_type: str, value: Any) tuple[str, bytes][source]
Encode field according to given type.
- Parameters:
types – mapping with all known types
name – field name
field_type – type of field (must be present in
typesor be basic type)value – value to encode
- Returns:
tuple of form (result_type, encoded_bytes)
- evmos.utils.eip_712_hash.encode_struct(struct_name: str, struct_field_types: Iterable[_FieldDef]) str[source]
Stringify a single struct in
'NAME(type1 name1,type2 name2,...)'format.
- evmos.utils.eip_712_hash.encode_type(primary_type: str, types: Mapping[str, Sequence[_FieldDef]]) str[source]
Encode type as concatenation of itself and all dependencies (alphabetical order).
The type of a struct is encoded as
name ‖ "(" ‖ member₁ ‖ "," ‖ member₂ ‖ "," ‖ … ‖ memberₙ ")"where each member is written as
type ‖ " " ‖ name.
- evmos.utils.eip_712_hash.field_identifier(field: _FieldDef) str[source]
Stringify a field in
'TYPE NAME'format.
- evmos.utils.eip_712_hash.get_array_dimensions(data: Any) tuple[int | str, ...][source]
Given an data item, check that it is an array and return the dimensions.
Examples
>>> get_array_dimensions([[1, 2, 3], [4, 5, 6]]) (3, 2)
- evmos.utils.eip_712_hash.get_dependencies(primary_type: str, types: Mapping[str, Sequence[_FieldDef]]) tuple[str, ...][source]
Perform DFS to get all the dependencies of the primary_type.
- evmos.utils.eip_712_hash.get_depths_and_dimensions(data: Any, depth: int) Iterator[tuple[int, int]][source]
Generate tuples of depth and dimension of each element at that depth.