mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-24 15:38:22 +00:00
fixup! feat(core, legacy, python, protobuf): refactoring
This commit is contained in:
parent
8780d56cba
commit
9f36248520
@ -198,12 +198,12 @@ def _get_ethereum_definitions(
|
|||||||
defs = ethereum.messages.EthereumDefinitions()
|
defs = ethereum.messages.EthereumDefinitions()
|
||||||
if definitions_dir is not None:
|
if definitions_dir is not None:
|
||||||
if chain_id is not None or slip44 is not None:
|
if chain_id is not None or slip44 is not None:
|
||||||
defs.encoded_network = ethereum.network_definition_from_dir(
|
defs.encoded_network = ethereum.get_definition_from_path(
|
||||||
definitions_dir, chain_id, slip44
|
ethereum.get_network_definition_path(definitions_dir, chain_id, slip44)
|
||||||
)
|
)
|
||||||
if chain_id is not None and token_address is not None:
|
if chain_id is not None and token_address is not None:
|
||||||
defs.encoded_token = ethereum.token_definition_from_dir(
|
defs.encoded_token = ethereum.get_definition_from_path(
|
||||||
definitions_dir, chain_id, token_address
|
ethereum.get_token_definition_path(definitions_dir, chain_id, token_address)
|
||||||
)
|
)
|
||||||
elif network_def_file is not None or token_def_file is not None:
|
elif network_def_file is not None or token_def_file is not None:
|
||||||
if network_def_file is not None:
|
if network_def_file is not None:
|
||||||
@ -214,12 +214,12 @@ def _get_ethereum_definitions(
|
|||||||
defs.encoded_token = token_def_file.read()
|
defs.encoded_token = token_def_file.read()
|
||||||
elif download_definitions:
|
elif download_definitions:
|
||||||
if chain_id is not None or slip44 is not None:
|
if chain_id is not None or slip44 is not None:
|
||||||
defs.encoded_network = ethereum.download_network_definition(
|
defs.encoded_network = ethereum.download_from_url(
|
||||||
chain_id, slip44
|
ethereum.get_network_definition_url(chain_id, slip44)
|
||||||
)
|
)
|
||||||
if chain_id is not None and token_address is not None:
|
if chain_id is not None and token_address is not None:
|
||||||
defs.encoded_token = ethereum.download_token_definition(
|
defs.encoded_network = ethereum.download_from_url(
|
||||||
chain_id, token_address
|
ethereum.get_token_definition_url(chain_id, token_address)
|
||||||
)
|
)
|
||||||
|
|
||||||
return defs
|
return defs
|
||||||
|
@ -30,11 +30,11 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
|
|
||||||
# TODO: change once we know the urls
|
# TODO: change once we know the urls
|
||||||
DEFS_BASE_URL = "https://data.trezor.io/eth_definitions/{lookup_type}/{id}/{name}.dat"
|
DEFS_BASE_URL = "https://data.trezor.io/eth_definitions/{lookup_type}/{id}/{name}"
|
||||||
DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE = "by_chain_id"
|
DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE = "by_chain_id"
|
||||||
DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE = "by_slip44"
|
DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE = "by_slip44"
|
||||||
DEFS_NETWORK_URI_NAME = "network"
|
DEFS_NETWORK_URI_NAME = "network.dat"
|
||||||
DEFS_TOKEN_URI_NAME = "token_{hex_address}"
|
DEFS_TOKEN_URI_NAME = "token_{hex_address}.dat"
|
||||||
|
|
||||||
|
|
||||||
def int_to_big_endian(value: int) -> bytes:
|
def int_to_big_endian(value: int) -> bytes:
|
||||||
@ -152,110 +152,92 @@ def encode_data(value: Any, type_name: str) -> bytes:
|
|||||||
raise ValueError(f"Unsupported data type for direct field encoding: {type_name}")
|
raise ValueError(f"Unsupported data type for direct field encoding: {type_name}")
|
||||||
|
|
||||||
|
|
||||||
def download_from_url(url: str, error_msg: str = "") -> bytes:
|
def download_from_url(url: str) -> bytes:
|
||||||
try:
|
try:
|
||||||
r = requests.get(url)
|
r = requests.get(url)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r.content
|
return r.content
|
||||||
except requests.exceptions.HTTPError as err:
|
except requests.exceptions.HTTPError as err:
|
||||||
raise RuntimeError(f"{error_msg}{err}")
|
raise RuntimeError(err.response)
|
||||||
|
|
||||||
|
|
||||||
def download_network_definition(
|
def get_network_definition_url(
|
||||||
chain_id: Optional[int] = None, slip44: Optional[int] = None
|
chain_id: Optional[int] = None, slip44: Optional[int] = None
|
||||||
) -> Optional[bytes]:
|
) -> str:
|
||||||
if not ((chain_id is None) != (slip44 is None)): # not XOR
|
if not ((chain_id is None) != (slip44 is None)): # not XOR
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Exactly one of chain_id or slip44 parameters are needed to load network definition from directory."
|
"Exactly one of chain_id or slip44 parameters are needed to construct network definition url."
|
||||||
)
|
)
|
||||||
|
|
||||||
if chain_id is not None:
|
if chain_id is not None:
|
||||||
url = DEFS_BASE_URL.format(
|
return DEFS_BASE_URL.format(
|
||||||
lookup_type=DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE,
|
lookup_type=DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE,
|
||||||
id=chain_id,
|
id=chain_id,
|
||||||
name=DEFS_NETWORK_URI_NAME,
|
name=DEFS_NETWORK_URI_NAME,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
url = DEFS_BASE_URL.format(
|
return DEFS_BASE_URL.format(
|
||||||
lookup_type=DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE,
|
lookup_type=DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE,
|
||||||
id=slip44,
|
id=slip44,
|
||||||
name=DEFS_NETWORK_URI_NAME,
|
name=DEFS_NETWORK_URI_NAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
error_msg = f'While downloading network definition from "{url}" following HTTP error occured: '
|
|
||||||
return download_from_url(url, error_msg)
|
|
||||||
|
|
||||||
|
def get_token_definition_url(
|
||||||
def download_token_definition(
|
|
||||||
chain_id: int = None, token_address: str = None
|
chain_id: int = None, token_address: str = None
|
||||||
) -> Optional[bytes]:
|
) -> str:
|
||||||
if chain_id is None or token_address is None:
|
if chain_id is None or token_address is None:
|
||||||
raise RuntimeError(
|
raise ValueError(
|
||||||
"Both chain_id and token_address parameters are needed to download token definition."
|
"Both chain_id and token_address parameters are needed to construct token definition url."
|
||||||
)
|
)
|
||||||
|
|
||||||
url = DEFS_BASE_URL.format(
|
addr = token_address.lower()
|
||||||
|
if addr.startswith("0x"):
|
||||||
|
addr = addr[2:]
|
||||||
|
|
||||||
|
return DEFS_BASE_URL.format(
|
||||||
lookup_type=DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE,
|
lookup_type=DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE,
|
||||||
id=chain_id,
|
id=chain_id,
|
||||||
name=DEFS_TOKEN_URI_NAME.format(hex_address=token_address),
|
name=DEFS_TOKEN_URI_NAME.format(hex_address=addr),
|
||||||
)
|
)
|
||||||
|
|
||||||
error_msg = f'While downloading token definition from "{url}" following HTTP error occured: '
|
|
||||||
return download_from_url(url, error_msg)
|
|
||||||
|
|
||||||
|
def get_network_definition_path(
|
||||||
def network_definition_from_dir(
|
base_path: pathlib.Path,
|
||||||
path: pathlib.Path,
|
|
||||||
chain_id: Optional[int] = None,
|
chain_id: Optional[int] = None,
|
||||||
slip44: Optional[int] = None,
|
slip44: Optional[int] = None,
|
||||||
) -> Optional[bytes]:
|
) -> pathlib.Path:
|
||||||
if not ((chain_id is None) != (slip44 is None)): # not XOR
|
if not ((chain_id is None) != (slip44 is None)): # not XOR
|
||||||
raise RuntimeError(
|
raise ValueError(
|
||||||
"Exactly one of chain_id or slip44 parameters are needed to load network definition from directory."
|
"Exactly one of chain_id or slip44 parameters are needed to construct network definition path."
|
||||||
)
|
)
|
||||||
|
|
||||||
def read_definition(path: pathlib.Path) -> Optional[bytes]:
|
|
||||||
if not path.exists() or not path.is_file():
|
|
||||||
return None
|
|
||||||
|
|
||||||
with open(path, mode="rb") as f:
|
|
||||||
return f.read()
|
|
||||||
|
|
||||||
if chain_id is not None:
|
if chain_id is not None:
|
||||||
return read_definition(
|
return base_path / DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE / str(chain_id) / DEFS_NETWORK_URI_NAME
|
||||||
path
|
|
||||||
/ DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE
|
|
||||||
/ str(chain_id)
|
|
||||||
/ (DEFS_NETWORK_URI_NAME + ".dat")
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
return read_definition(
|
return base_path / DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE / str(slip44) / DEFS_NETWORK_URI_NAME
|
||||||
path
|
|
||||||
/ DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE
|
|
||||||
/ str(slip44)
|
|
||||||
/ (DEFS_NETWORK_URI_NAME + ".dat")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def token_definition_from_dir(
|
def get_token_definition_path(
|
||||||
path: pathlib.Path,
|
base_path: pathlib.Path,
|
||||||
chain_id: int = None,
|
chain_id: int = None,
|
||||||
token_address: str = None,
|
token_address: str = None,
|
||||||
) -> Optional[bytes]:
|
) -> pathlib.Path:
|
||||||
if chain_id is None or token_address is None:
|
if chain_id is None or token_address is None:
|
||||||
raise RuntimeError(
|
raise ValueError(
|
||||||
"Both chain_id and token_address parameters are needed to load token definition from directory."
|
"Both chain_id and token_address parameters are needed to construct token definition path."
|
||||||
)
|
)
|
||||||
|
|
||||||
if token_address.startswith("0x"):
|
addr = token_address.lower()
|
||||||
token_address = token_address[2:]
|
if addr.startswith("0x"):
|
||||||
|
addr = addr[2:]
|
||||||
|
|
||||||
path = (
|
return base_path / DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE / str(chain_id) / DEFS_TOKEN_URI_NAME.format(hex_address=addr)
|
||||||
path
|
|
||||||
/ DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE
|
|
||||||
/ str(chain_id)
|
def get_definition_from_path(
|
||||||
/ (DEFS_TOKEN_URI_NAME.format(hex_address=token_address.lower()) + ".dat")
|
path: pathlib.Path,
|
||||||
)
|
) -> Optional[bytes]:
|
||||||
if not path.exists() or not path.is_file():
|
if not path.exists() or not path.is_file():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -35,9 +35,11 @@ def test_getaddress(client: Client, parameters, result):
|
|||||||
"slip44", encoded_network_slip44
|
"slip44", encoded_network_slip44
|
||||||
)
|
)
|
||||||
|
|
||||||
encoded_network = ethereum.network_definition_from_dir(
|
encoded_network = ethereum.get_definition_from_path(
|
||||||
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
ethereum.get_network_definition_path(
|
||||||
slip44=encoded_network_slip44,
|
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
||||||
|
slip44=encoded_network_slip44,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
ethereum.get_address(client, address_n, encoded_network=encoded_network)
|
ethereum.get_address(client, address_n, encoded_network=encoded_network)
|
||||||
|
@ -39,9 +39,11 @@ def test_ethereum_sign_typed_data(client: Client, parameters, result):
|
|||||||
"slip44", encoded_network_slip44
|
"slip44", encoded_network_slip44
|
||||||
)
|
)
|
||||||
|
|
||||||
encoded_network = ethereum.network_definition_from_dir(
|
encoded_network = ethereum.get_definition_from_path(
|
||||||
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
ethereum.get_network_definition_path(
|
||||||
slip44=encoded_network_slip44,
|
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
||||||
|
slip44=encoded_network_slip44,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
ret = ethereum.sign_typed_data(
|
ret = ethereum.sign_typed_data(
|
||||||
client,
|
client,
|
||||||
@ -84,9 +86,11 @@ def test_ethereum_sign_typed_data_blind(client: Client, parameters, result):
|
|||||||
"slip44", encoded_network_slip44
|
"slip44", encoded_network_slip44
|
||||||
)
|
)
|
||||||
|
|
||||||
encoded_network = ethereum.network_definition_from_dir(
|
encoded_network = ethereum.get_definition_from_path(
|
||||||
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
ethereum.get_network_definition_path(
|
||||||
slip44=encoded_network_slip44,
|
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
||||||
|
slip44=encoded_network_slip44,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
ret = ethereum.sign_typed_data_hash(
|
ret = ethereum.sign_typed_data_hash(
|
||||||
client,
|
client,
|
||||||
|
@ -35,9 +35,11 @@ def test_signmessage(client: Client, parameters, result):
|
|||||||
"slip44", encoded_network_slip44
|
"slip44", encoded_network_slip44
|
||||||
)
|
)
|
||||||
|
|
||||||
encoded_network = ethereum.network_definition_from_dir(
|
encoded_network = ethereum.get_definition_from_path(
|
||||||
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
ethereum.get_network_definition_path(
|
||||||
slip44=encoded_network_slip44,
|
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
||||||
|
slip44=encoded_network_slip44,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
res = ethereum.sign_message(client, address_n, parameters["msg"], encoded_network)
|
res = ethereum.sign_message(client, address_n, parameters["msg"], encoded_network)
|
||||||
assert res.address == result["address"]
|
assert res.address == result["address"]
|
||||||
|
@ -44,14 +44,18 @@ def get_definitions(
|
|||||||
token_chain_id = parameters["definitions"].get("token_chain_id", token_chain_id)
|
token_chain_id = parameters["definitions"].get("token_chain_id", token_chain_id)
|
||||||
token_address = parameters["definitions"].get("to_address", token_address)
|
token_address = parameters["definitions"].get("to_address", token_address)
|
||||||
|
|
||||||
encoded_network = ethereum.network_definition_from_dir(
|
encoded_network = ethereum.get_definition_from_path(
|
||||||
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
ethereum.get_network_definition_path(
|
||||||
chain_id=chain_id,
|
COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
||||||
|
chain_id,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
encoded_token = ethereum.token_definition_from_dir(
|
encoded_token = ethereum.get_definition_from_path(
|
||||||
path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
ethereum.get_token_definition_path(
|
||||||
chain_id=token_chain_id,
|
COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest",
|
||||||
token_address=token_address,
|
token_chain_id,
|
||||||
|
token_address,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return messages.EthereumDefinitions(
|
return messages.EthereumDefinitions(
|
||||||
|
Loading…
Reference in New Issue
Block a user