"""
Encoding of calldata descriptor instructions to TLV format.
See https://github.com/LedgerHQ/generic_parser for specifications of these payloads.
"""
from datetime import datetime
from enum import IntEnum
from typing import assert_never
from erc7730.common.binary import from_hex, tlv
from erc7730.common.pydantic import pydantic_enum_by_name
from erc7730.model.calldata.types import TrustedNameSource, TrustedNameType
from erc7730.model.calldata.v1.eip712 import (
CalldataDescriptorEIP712FieldV1,
CalldataDescriptorEIP712StructV1,
CalldataDescriptorInstructionEIP712SchemaV1,
)
from erc7730.model.calldata.v1.instruction import (
CalldataDescriptorFieldVisibilityV1,
CalldataDescriptorInstructionEIP712MessageInfoV1,
CalldataDescriptorInstructionEnumValueV1,
CalldataDescriptorInstructionFieldV1,
CalldataDescriptorInstructionMapEntryV1,
CalldataDescriptorInstructionTransactionInfoBaseV1,
CalldataDescriptorInstructionTransactionInfoV1,
)
from erc7730.model.calldata.v1.param import (
CalldataDescriptorParamAmountV1,
CalldataDescriptorParamCalldataV1,
CalldataDescriptorParamDatetimeV1,
CalldataDescriptorParamDurationV1,
CalldataDescriptorParamEnumV1,
CalldataDescriptorParamNetworkV1,
CalldataDescriptorParamNFTV1,
CalldataDescriptorParamRawV1,
CalldataDescriptorParamTokenAmountV1,
CalldataDescriptorParamTokenV1,
CalldataDescriptorParamTrustedNameV1,
CalldataDescriptorParamType,
CalldataDescriptorParamUnitV1,
)
from erc7730.model.calldata.v1.value import (
CalldataDescriptorContainerPathV1,
CalldataDescriptorDataPathV1,
CalldataDescriptorEIP712PathElementArraySliceV1,
CalldataDescriptorEIP712PathElementLeafSliceV1,
CalldataDescriptorEIP712PathElementStructFieldV1,
CalldataDescriptorEIP712PathElementV1,
CalldataDescriptorEIP712PathV1,
CalldataDescriptorMapRefV1,
CalldataDescriptorPathElementArrayV1,
CalldataDescriptorPathElementLeafV1,
CalldataDescriptorPathElementRefV1,
CalldataDescriptorPathElementSliceV1,
CalldataDescriptorPathElementTupleV1,
CalldataDescriptorPathElementV1,
CalldataDescriptorValueConstantV1,
CalldataDescriptorValueMapRefV1,
CalldataDescriptorValuePathV1,
CalldataDescriptorValueV1,
)
[docs]
@pydantic_enum_by_name
class CalldataDescriptorTransactionInfoTag(IntEnum):
# OPERATION_INFO is TRANSACTION_INFO or EIP712_MESSAGE_INFO depending on its context: CHAIN_ID, CONTRACT_ADDR and
# SELECTOR are for a transaction only, DOMAIN_SEPARATOR and PRIMARY_TYPE_HASH for an EIP-712 message only.
VERSION = 0x00
CHAIN_ID = 0x01
CONTRACT_ADDR = 0x02
SELECTOR = 0x03
FIELDS_HASH = 0x04
OPERATION_TYPE = 0x05
CREATOR_NAME = 0x06
CREATOR_LEGAL_NAME = 0x07
CREATOR_URL = 0x08
CONTRACT_NAME = 0x09
DEPLOY_DATE = 0x0A
DOMAIN_SEPARATOR = 0x0B
PRIMARY_TYPE_HASH = 0x0C
SIGNATURE = 0xFF
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEIP712SchemaTag(IntEnum):
VERSION = 0x00
EIP712_STRUCT = 0x01
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEIP712StructTag(IntEnum):
VERSION = 0x00
NAME = 0x01
EIP712_FIELD = 0x02
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEIP712FieldTag(IntEnum):
VERSION = 0x00
NAME = 0x01
TYPE = 0x02
TYPE_SIZE = 0x03
ARRAY_DIM = 0x04
STRUCT_NAME = 0x05
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEIP712PathTag(IntEnum):
VERSION = 0x00
EIP712_STRUCT_FIELD = 0x01
EIP712_ARRAY_SLICE = 0x02
EIP712_LEAF_SLICE = 0x03
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEIP712ArraySliceTag(IntEnum):
START = 0x01
END = 0x02
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEIP712LeafSliceTag(IntEnum):
START = 0x01
END = 0x02
[docs]
@pydantic_enum_by_name
class CalldataDescriptorEnumValueTag(IntEnum):
VERSION = 0x00
CHAIN_ID = 0x01
CONTRACT_ADDR = 0x02
SELECTOR = 0x03
ID = 0x04
VALUE = 0x05
NAME = 0x06
DOMAIN_SEPARATOR = 0x07
PRIMARY_TYPE_HASH = 0x08
[docs]
@pydantic_enum_by_name
class CalldataDescriptorMapEntryTag(IntEnum):
VERSION = 0x00
CHAIN_ID = 0x01
CONTRACT_ADDR = 0x02
SELECTOR = 0x03
ID = 0x04
KEY = 0x05
VALUE = 0x06
DOMAIN_SEPARATOR = 0x07
PRIMARY_TYPE_HASH = 0x08
SIGNATURE = 0xFF
[docs]
@pydantic_enum_by_name
class CalldataDescriptorFieldTag(IntEnum):
VERSION = 0x00
NAME = 0x01
PARAM_TYPE = 0x02
PARAM = 0x03
VISIBLE = 0x04
CONSTRAINT = 0x05
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamRawTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamAmountTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamTokenAmountTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
TOKEN = 0x02
NATIVE_CURRENCY = 0x03
THRESHOLD = 0x04
ABOVE_THRESHOLD_MSG = 0x05
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamNFTTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
COLLECTION = 0x02
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamDurationTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamUnitTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
BASE = 0x02
DECIMALS = 0x03
PREFIX = 0x04
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamCalldataTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
CALLEE = 0x02
CHAIN_ID = 0x03
SELECTOR = 0x04
AMOUNT = 0x05
SPENDER = 0x06
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamTokenTag(IntEnum):
VERSION = 0x00
ADDRESS = 0x01
NATIVE_CURRENCY = 0x02
[docs]
@pydantic_enum_by_name
class CalldataDescriptorParamNetworkTag(IntEnum):
VERSION = 0x00
VALUE = 0x01
[docs]
@pydantic_enum_by_name
class CalldataDescriptorValueTag(IntEnum):
VERSION = 0x00
TYPE_FAMILY = 0x01
TYPE_SIZE = 0x02
DATA_PATH = 0x03
CONTAINER_PATH = 0x04
CONSTANT = 0x05
MAP_REF = 0x06
[docs]
@pydantic_enum_by_name
class CalldataDescriptorMapRefTag(IntEnum):
VERSION = 0x00
ID = 0x01
KEY = 0x02
[docs]
@pydantic_enum_by_name
class CalldataDescriptorPathElementTag(IntEnum):
VERSION = 0x00
TUPLE = 0x01
ARRAY = 0x02
REF = 0x03
LEAF = 0x04
SLICE = 0x05
[docs]
@pydantic_enum_by_name
class CalldataDescriptorPathArrayElementTag(IntEnum):
WEIGHT = 0x01
START = 0x02
END = 0x03
[docs]
@pydantic_enum_by_name
class CalldataDescriptorPathSliceElementTag(IntEnum):
START = 0x01
END = 0x02
# the Ethereum app parses an OPERATION_INFO in its 16 KB heap
MAX_OPERATION_INFO_SIZE = 16384
def _check_operation_info_size(out: bytes | bytearray) -> bytes | bytearray:
if len(out) >= MAX_OPERATION_INFO_SIZE:
raise OverflowError(
f"operation info of {len(out)} bytes does not fit the {MAX_OPERATION_INFO_SIZE} bytes limit"
)
return out
def _tlv_operation_info_body(obj: CalldataDescriptorInstructionTransactionInfoBaseV1) -> bytes:
"""
Encode the tags TRANSACTION_INFO and EIP712_MESSAGE_INFO have in common, which are all but the VERSION and the tags
identifying the operation.
@param obj: object representation of struct
@return: encoded tags TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorTransactionInfoTag.FIELDS_HASH, from_hex(obj.hash))
out += tlv(CalldataDescriptorTransactionInfoTag.OPERATION_TYPE, obj.operation_type)
if (creator_name := obj.creator_name) is not None:
out += tlv(CalldataDescriptorTransactionInfoTag.CREATOR_NAME, creator_name)
if (creator_legal_name := obj.creator_legal_name) is not None:
out += tlv(CalldataDescriptorTransactionInfoTag.CREATOR_LEGAL_NAME, creator_legal_name)
if (creator_url := obj.creator_url) is not None:
out += tlv(CalldataDescriptorTransactionInfoTag.CREATOR_URL, creator_url)
if (contract_name := obj.contract_name) is not None:
out += tlv(CalldataDescriptorTransactionInfoTag.CONTRACT_NAME, contract_name)
if (deploy_date := obj.deploy_date) is not None:
tstamp = int(datetime.fromisoformat(deploy_date).timestamp())
out += tlv(CalldataDescriptorTransactionInfoTag.DEPLOY_DATE, tstamp.to_bytes(4))
return out
[docs]
def tlv_transaction_info(obj: CalldataDescriptorInstructionTransactionInfoV1) -> bytes:
"""
Encode a struct of type TRANSACTION_INFO.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorTransactionInfoTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorTransactionInfoTag.CHAIN_ID, obj.chain_id.to_bytes(8))
out += tlv(CalldataDescriptorTransactionInfoTag.CONTRACT_ADDR, from_hex(obj.address))
out += tlv(CalldataDescriptorTransactionInfoTag.SELECTOR, from_hex(obj.selector))
out += _tlv_operation_info_body(obj)
return _check_operation_info_size(out)
[docs]
def tlv_eip712_message_info(obj: CalldataDescriptorInstructionEIP712MessageInfoV1) -> bytes:
"""
Encode a struct of type EIP712_MESSAGE_INFO (without its signature, which is computed by CAL).
The device refuses CHAIN_ID, CONTRACT_ADDR and SELECTOR in a message info: it is bound to the message by the
domain separator and the primary type hash.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorTransactionInfoTag.VERSION, obj.version.to_bytes(1))
out += _tlv_operation_info_body(obj)
out += tlv(CalldataDescriptorTransactionInfoTag.DOMAIN_SEPARATOR, from_hex(obj.domain_separator))
out += tlv(CalldataDescriptorTransactionInfoTag.PRIMARY_TYPE_HASH, from_hex(obj.primary_type_hash))
return _check_operation_info_size(out)
[docs]
def tlv_eip712_schema(obj: CalldataDescriptorInstructionEIP712SchemaV1) -> bytes:
"""
Encode a struct of type EIP712_SCHEMA (all EIP712_STRUCT entries nested).
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorEIP712SchemaTag.VERSION, obj.version.to_bytes(1))
for struct in obj.structs:
out += tlv(CalldataDescriptorEIP712SchemaTag.EIP712_STRUCT, tlv_eip712_struct(struct))
return out
[docs]
def tlv_eip712_struct(obj: CalldataDescriptorEIP712StructV1) -> bytes:
"""
Encode a struct of type EIP712_STRUCT.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorEIP712StructTag.VERSION, (1).to_bytes(1))
out += tlv(CalldataDescriptorEIP712StructTag.NAME, obj.name)
for field in obj.fields:
out += tlv(CalldataDescriptorEIP712StructTag.EIP712_FIELD, tlv_eip712_field(field))
return out
[docs]
def tlv_eip712_field(obj: CalldataDescriptorEIP712FieldV1) -> bytes:
"""
Encode a struct of type EIP712_FIELD.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorEIP712FieldTag.VERSION, (1).to_bytes(1))
out += tlv(CalldataDescriptorEIP712FieldTag.NAME, obj.name)
out += tlv(CalldataDescriptorEIP712FieldTag.TYPE, obj.sol_type.value.to_bytes(1))
if (type_size := obj.type_size) is not None:
out += tlv(CalldataDescriptorEIP712FieldTag.TYPE_SIZE, type_size.to_bytes(1))
for dimension in obj.array_dimensions:
if dimension is None:
# empty payload marks a dynamic dimension
out += tlv(CalldataDescriptorEIP712FieldTag.ARRAY_DIM)
else:
# a fixed dimension is minimal big-endian on at least one byte (T[0] -> 04 01 00, T[2] -> 04 01 02), as an
# empty payload is a dynamic dimension
length = max(1, (dimension.bit_length() + 7) // 8)
out += tlv(CalldataDescriptorEIP712FieldTag.ARRAY_DIM, dimension.to_bytes(length))
if (struct_name := obj.struct_name) is not None:
out += tlv(CalldataDescriptorEIP712FieldTag.STRUCT_NAME, struct_name)
return out
def _tlv_operation_context(
tags: type[CalldataDescriptorEnumValueTag] | type[CalldataDescriptorMapEntryTag],
obj: CalldataDescriptorInstructionEnumValueV1 | CalldataDescriptorInstructionMapEntryV1,
) -> bytes:
"""
Encode the tags binding an ENUM_VALUE or MAP_ENTRY to an operation: chain, contract and selector for a
transaction, domain separator and primary type hash for an EIP-712 message, and none of the other.
"""
out = bytearray()
if obj.selector is not None:
if obj.chain_id is None or obj.address is None:
raise ValueError("A chain id and a contract address are required with a selector.")
out += tlv(tags["CHAIN_ID"], obj.chain_id.to_bytes(8))
out += tlv(tags["CONTRACT_ADDR"], from_hex(obj.address))
out += tlv(tags["SELECTOR"], from_hex(obj.selector))
elif obj.domain_separator is not None and obj.primary_type_hash is not None:
out += tlv(tags["DOMAIN_SEPARATOR"], from_hex(obj.domain_separator))
out += tlv(tags["PRIMARY_TYPE_HASH"], from_hex(obj.primary_type_hash))
return out
[docs]
def tlv_enum_value(obj: CalldataDescriptorInstructionEnumValueV1) -> bytes:
"""
Encode a struct of type ENUM_VALUE.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorEnumValueTag.VERSION, obj.version.to_bytes(1))
out += _tlv_operation_context(CalldataDescriptorEnumValueTag, obj)
out += tlv(CalldataDescriptorEnumValueTag.ID, obj.id.to_bytes(1))
out += tlv(CalldataDescriptorEnumValueTag.VALUE, obj.value.to_bytes(1))
out += tlv(CalldataDescriptorEnumValueTag.NAME, obj.name)
return out
[docs]
def tlv_map_entry(obj: CalldataDescriptorInstructionMapEntryV1) -> bytes:
"""
Encode a struct of type MAP_ENTRY (without its signature, which is computed by CAL).
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorMapEntryTag.VERSION, obj.version.to_bytes(1))
out += _tlv_operation_context(CalldataDescriptorMapEntryTag, obj)
out += tlv(CalldataDescriptorMapEntryTag.ID, obj.id.to_bytes(1))
out += tlv(CalldataDescriptorMapEntryTag.KEY, from_hex(obj.key))
out += tlv(CalldataDescriptorMapEntryTag.VALUE, from_hex(obj.value))
return out
[docs]
def tlv_field(obj: CalldataDescriptorInstructionFieldV1) -> bytes:
"""
Encode a struct of type FIELD.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorFieldTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorFieldTag.NAME, obj.name)
param_type: CalldataDescriptorParamType
param_value: bytes
match obj.param:
case CalldataDescriptorParamRawV1():
param_type = CalldataDescriptorParamType.RAW
param_value = tlv_param_raw(obj.param)
case CalldataDescriptorParamAmountV1():
param_type = CalldataDescriptorParamType.AMOUNT
param_value = tlv_param_amount(obj.param)
case CalldataDescriptorParamTokenAmountV1():
param_type = CalldataDescriptorParamType.TOKEN_AMOUNT
param_value = tlv_param_token_amount(obj.param)
case CalldataDescriptorParamNFTV1():
param_type = CalldataDescriptorParamType.NFT
param_value = tlv_param_nft(obj.param)
case CalldataDescriptorParamDatetimeV1():
param_type = CalldataDescriptorParamType.DATETIME
param_value = tlv_param_datetime(obj.param)
case CalldataDescriptorParamDurationV1():
param_type = CalldataDescriptorParamType.DURATION
param_value = tlv_param_duration(obj.param)
case CalldataDescriptorParamUnitV1():
param_type = CalldataDescriptorParamType.UNIT
param_value = tlv_param_unit(obj.param)
case CalldataDescriptorParamEnumV1():
param_type = CalldataDescriptorParamType.ENUM
param_value = tlv_param_enum(obj.param)
case CalldataDescriptorParamTrustedNameV1():
param_type = CalldataDescriptorParamType.TRUSTED_NAME
param_value = tlv_param_trusted_name(obj.param)
case CalldataDescriptorParamCalldataV1():
param_type = CalldataDescriptorParamType.CALLDATA
param_value = tlv_param_calldata(obj.param)
case CalldataDescriptorParamTokenV1():
param_type = CalldataDescriptorParamType.TOKEN
param_value = tlv_param_token(obj.param)
case CalldataDescriptorParamNetworkV1():
param_type = CalldataDescriptorParamType.NETWORK
param_value = tlv_param_network(obj.param)
case _:
assert_never(obj.param)
out += tlv(CalldataDescriptorFieldTag.PARAM_TYPE, param_type.value.to_bytes(1))
out += tlv(CalldataDescriptorFieldTag.PARAM, param_value)
# VISIBLE defaults to ALWAYS when absent, so it is omitted in that case to keep payloads stable.
# It must be serialized before any CONSTRAINT, which the device rejects otherwise.
if obj.visibility is not CalldataDescriptorFieldVisibilityV1.ALWAYS:
out += tlv(CalldataDescriptorFieldTag.VISIBLE, obj.visibility.value.to_bytes(1))
for constraint in obj.constraints or []:
out += tlv(CalldataDescriptorFieldTag.CONSTRAINT, from_hex(constraint))
return out
[docs]
def tlv_param_raw(obj: CalldataDescriptorParamRawV1) -> bytes:
"""
Encode a struct of type PARAM_RAW.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamRawTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamRawTag.VALUE, tlv_value(obj.value))
return out
[docs]
def tlv_param_amount(obj: CalldataDescriptorParamAmountV1) -> bytes:
"""
Encode a struct of type PARAM_AMOUNT.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamAmountTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamAmountTag.VALUE, tlv_value(obj.value))
return out
[docs]
def tlv_param_token_amount(obj: CalldataDescriptorParamTokenAmountV1) -> bytes:
"""
Encode a struct of type PARAM_TOKEN_AMOUNT.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamTokenAmountTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamTokenAmountTag.VALUE, tlv_value(obj.value))
if (token := obj.token) is not None:
out += tlv(CalldataDescriptorParamTokenAmountTag.TOKEN, tlv_value(token))
if (native_currencies := obj.native_currencies) is not None:
for currency in native_currencies:
out += tlv(CalldataDescriptorParamTokenAmountTag.NATIVE_CURRENCY, from_hex(currency))
if (threshold := obj.threshold) is not None:
out += tlv(CalldataDescriptorParamTokenAmountTag.THRESHOLD, from_hex(threshold))
if (above_threshold_message := obj.above_threshold_message) is not None:
out += tlv(CalldataDescriptorParamTokenAmountTag.ABOVE_THRESHOLD_MSG, above_threshold_message)
return out
[docs]
def tlv_param_nft(obj: CalldataDescriptorParamNFTV1) -> bytes:
"""
Encode a struct of type PARAM_NFT.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamNFTTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamNFTTag.VALUE, tlv_value(obj.value))
out += tlv(CalldataDescriptorParamNFTTag.COLLECTION, tlv_value(obj.collection))
return out
[docs]
def tlv_param_datetime(obj: CalldataDescriptorParamDatetimeV1) -> bytes:
"""
Encode a struct of type PARAM_DATETIME.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamDateTimeTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamDateTimeTag.VALUE, tlv_value(obj.value))
out += tlv(CalldataDescriptorParamDateTimeTag.TYPE, obj.date_type.to_bytes(1))
return out
[docs]
def tlv_param_duration(obj: CalldataDescriptorParamDurationV1) -> bytes:
"""
Encode a struct of type PARAM_DURATION.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamDurationTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamDurationTag.VALUE, tlv_value(obj.value))
return out
[docs]
def tlv_param_unit(obj: CalldataDescriptorParamUnitV1) -> bytes:
"""
Encode a struct of type PARAM_UNIT.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamUnitTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamUnitTag.VALUE, tlv_value(obj.value))
out += tlv(CalldataDescriptorParamUnitTag.BASE, obj.base)
# always emit DECIMALS (defaulting to 0): app-ethereum >= 1.22.4 rejects PARAM_UNIT structs without it
out += tlv(CalldataDescriptorParamUnitTag.DECIMALS, (obj.decimals or 0).to_bytes(1))
if (prefix := obj.prefix) is not None:
out += tlv(CalldataDescriptorParamUnitTag.PREFIX, prefix.to_bytes(1))
return out
[docs]
def tlv_param_enum(obj: CalldataDescriptorParamEnumV1) -> bytes:
"""
Encode a struct of type PARAM_ENUM.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamEnumTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamEnumTag.VALUE, tlv_value(obj.value))
out += tlv(CalldataDescriptorParamEnumTag.ID, obj.id.to_bytes(1))
return out
[docs]
def tlv_param_trusted_name(obj: CalldataDescriptorParamTrustedNameV1) -> bytes:
"""
Encode a struct of type PARAM_TRUSTED_NAME.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamTrustedNameTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamTrustedNameTag.VALUE, tlv_value(obj.value))
out_types = bytearray()
for name_type in obj.types:
out_types += TrustedNameType(name_type).int_value.to_bytes(1)
out += tlv(CalldataDescriptorParamTrustedNameTag.TYPES, out_types)
out_sources = bytearray()
for name_source in obj.sources:
out_sources += TrustedNameSource(name_source).int_value.to_bytes(1)
out += tlv(CalldataDescriptorParamTrustedNameTag.SOURCES, out_sources)
if (sender_addresses := obj.sender_addresses) is not None:
for address in sender_addresses:
out += tlv(CalldataDescriptorParamTrustedNameTag.SENDER_ADDRESS, from_hex(address))
return out
[docs]
def tlv_param_calldata(obj: CalldataDescriptorParamCalldataV1) -> bytes:
"""
Encode a struct of type PARAM_CALL_DATA.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamCalldataTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamCalldataTag.VALUE, tlv_value(obj.value))
out += tlv(CalldataDescriptorParamCalldataTag.CALLEE, tlv_value(obj.callee))
if (chain_id := obj.chain_id) is not None:
out += tlv(CalldataDescriptorParamCalldataTag.CHAIN_ID, tlv_value(chain_id))
if (selector := obj.selector) is not None:
out += tlv(CalldataDescriptorParamCalldataTag.SELECTOR, tlv_value(selector))
if (amount := obj.amount) is not None:
out += tlv(CalldataDescriptorParamCalldataTag.AMOUNT, tlv_value(amount))
if (spender := obj.spender) is not None:
out += tlv(CalldataDescriptorParamCalldataTag.SPENDER, tlv_value(spender))
return out
[docs]
def tlv_param_token(obj: CalldataDescriptorParamTokenV1) -> bytes:
"""
Encode a struct of type PARAM_TOKEN.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamTokenTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamTokenTag.ADDRESS, tlv_value(obj.value))
if (native_currencies := obj.native_currencies) is not None:
for currency in native_currencies:
out += tlv(CalldataDescriptorParamTokenTag.NATIVE_CURRENCY, from_hex(currency))
return out
[docs]
def tlv_param_network(obj: CalldataDescriptorParamNetworkV1) -> bytes:
"""
Encode a struct of type PARAM_NETWORK.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorParamNetworkTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorParamNetworkTag.VALUE, tlv_value(obj.value))
return out
[docs]
def tlv_value(obj: CalldataDescriptorValueV1) -> bytes:
"""
Encode a struct of type VALUE.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorValueTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorValueTag.TYPE_FAMILY, obj.type_family.value.to_bytes(1))
if (type_size := obj.type_size) is not None:
try:
out += tlv(CalldataDescriptorValueTag.TYPE_SIZE, type_size.to_bytes(1))
except OverflowError:
raise OverflowError(f"Type {obj.type_family} with size {type_size} bits is too big to convert") from None
match obj:
case CalldataDescriptorValuePathV1() as path:
match path.binary_path:
case CalldataDescriptorContainerPathV1() as container_path:
out += tlv(CalldataDescriptorValueTag.CONTAINER_PATH, container_path.value.to_bytes(1))
case CalldataDescriptorDataPathV1() as data_path:
out_path = bytearray()
out_path += tlv(CalldataDescriptorPathElementTag.VERSION, path.version.to_bytes(1))
for element in data_path.elements:
out_path += tlv_data_path_element(element)
out += tlv(CalldataDescriptorValueTag.DATA_PATH, out_path)
case CalldataDescriptorEIP712PathV1() as eip712_path:
# tag 0x03 is context-dependent (DATA_PATH for calldata, EIP712_PATH for EIP-712 messages)
out_path = bytearray()
out_path += tlv(CalldataDescriptorEIP712PathTag.VERSION, path.version.to_bytes(1))
for eip712_element in eip712_path.elements:
out_path += tlv_eip712_path_element(eip712_element)
out += tlv(CalldataDescriptorValueTag.DATA_PATH, out_path)
case _:
assert_never(path.binary_path)
case CalldataDescriptorValueConstantV1() as constant:
out += tlv(CalldataDescriptorValueTag.CONSTANT, from_hex(constant.raw))
case CalldataDescriptorValueMapRefV1() as map_value:
out += tlv(CalldataDescriptorValueTag.MAP_REF, tlv_map_ref(map_value.map_ref))
case _:
assert_never(obj)
return out
[docs]
def tlv_map_ref(obj: CalldataDescriptorMapRefV1) -> bytes:
"""
Encode a struct of type MAP_REF.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
out += tlv(CalldataDescriptorMapRefTag.VERSION, obj.version.to_bytes(1))
out += tlv(CalldataDescriptorMapRefTag.ID, obj.id.to_bytes(1))
out += tlv(CalldataDescriptorMapRefTag.KEY, tlv_value(obj.key))
return out
[docs]
def tlv_data_path_element(obj: CalldataDescriptorPathElementV1) -> bytes:
"""
Encode a struct of type PATH_ELEMENT.
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
match obj:
case CalldataDescriptorPathElementTupleV1() as tup:
out += tlv(CalldataDescriptorPathElementTag.TUPLE, tup.offset.to_bytes(2))
case CalldataDescriptorPathElementArrayV1() as arr:
array_out = bytearray()
array_out += tlv(CalldataDescriptorPathArrayElementTag.WEIGHT, arr.weight.to_bytes(1))
if (start := arr.start) is not None:
array_out += tlv(CalldataDescriptorPathArrayElementTag.START, start.to_bytes(2, signed=True))
if (end := arr.end) is not None:
array_out += tlv(CalldataDescriptorPathArrayElementTag.END, end.to_bytes(2, signed=True))
out += tlv(CalldataDescriptorPathElementTag.ARRAY, bytes(array_out))
case CalldataDescriptorPathElementRefV1():
out += tlv(CalldataDescriptorPathElementTag.REF)
case CalldataDescriptorPathElementLeafV1() as leaf:
out += tlv(CalldataDescriptorPathElementTag.LEAF, leaf.leaf_type.value.to_bytes(1))
case CalldataDescriptorPathElementSliceV1() as slice:
slice_out = bytearray()
if (start := slice.start) is not None:
slice_out += tlv(CalldataDescriptorPathSliceElementTag.START, start.to_bytes(2, signed=True))
if (end := slice.end) is not None:
slice_out += tlv(CalldataDescriptorPathSliceElementTag.END, end.to_bytes(2, signed=True))
out += tlv(CalldataDescriptorPathElementTag.SLICE, bytes(slice_out))
case _:
assert_never(obj)
return out
[docs]
def tlv_eip712_path_element(obj: CalldataDescriptorEIP712PathElementV1) -> bytes:
"""
Encode an element of an EIP712_PATH (a struct field index, an array slice or a leaf slice).
@param obj: object representation of struct
@return: encoded struct TLV
"""
out = bytearray()
match obj:
case CalldataDescriptorEIP712PathElementStructFieldV1() as struct_field:
out += tlv(CalldataDescriptorEIP712PathTag.EIP712_STRUCT_FIELD, struct_field.index.to_bytes(1))
case CalldataDescriptorEIP712PathElementArraySliceV1() as array_slice:
slice_out = bytearray()
if (start := array_slice.start) is not None:
slice_out += tlv(CalldataDescriptorEIP712ArraySliceTag.START, start.to_bytes(2, signed=True))
if (end := array_slice.end) is not None:
slice_out += tlv(CalldataDescriptorEIP712ArraySliceTag.END, end.to_bytes(2, signed=True))
out += tlv(CalldataDescriptorEIP712PathTag.EIP712_ARRAY_SLICE, bytes(slice_out))
case CalldataDescriptorEIP712PathElementLeafSliceV1() as leaf_slice:
slice_out = bytearray()
if (start := leaf_slice.start) is not None:
slice_out += tlv(CalldataDescriptorEIP712LeafSliceTag.START, start.to_bytes(2, signed=True))
if (end := leaf_slice.end) is not None:
slice_out += tlv(CalldataDescriptorEIP712LeafSliceTag.END, end.to_bytes(2, signed=True))
out += tlv(CalldataDescriptorEIP712PathTag.EIP712_LEAF_SLICE, bytes(slice_out))
case _:
assert_never(obj)
return out