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/master/eth_account/_utils/structured_data/hashing.py with minor modifications.
- evmos.utils.eip_712_hash.encode_data(primary_type: str, types: Mapping[str, Sequence[_FieldT]], data: Any) bytes [source]
Encode data defined by
primary_type
.
- evmos.utils.eip_712_hash.encode_field(types: Mapping[str, Sequence[_FieldT]], 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
types
or 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[_FieldT]) 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[_FieldT]]) 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: _FieldT) 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[_FieldT]]) 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.
- evmos.utils.eip_712_hash.hash_domain(structured_data: EIPToSign) bytes [source]
Hash domain part of EIP-712 message.
- evmos.utils.eip_712_hash.hash_message(structured_data: EIPToSign) bytes [source]
Hash message part of EIP-712 message.
Internal utilities
- evmos.utils.polyfill.removeprefix(self, prefix, /)
Return a str with the given prefix string removed if present.
If the string starts with the prefix string, return string[len(prefix):]. Otherwise, return a copy of the original string.
- evmos.utils.polyfill.removesuffix(self, suffix, /)
Return a str with the given suffix string removed if present.
If the string ends with the suffix string and that suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string.