diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/1/token_d0d6d6c5fe4a677d343cc433536bb717bae167dd.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/1/token_d0d6d6c5fe4a677d343cc433536bb717bae167dd.dat new file mode 100644 index 0000000000..ac81886fab Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/1/token_d0d6d6c5fe4a677d343cc433536bb717bae167dd.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/11297108109/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/11297108109/network.dat new file mode 100644 index 0000000000..d447e9d793 Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/11297108109/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/28945486/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/28945486/network.dat new file mode 100644 index 0000000000..a14a9b0446 Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/28945486/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/3125659152/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/3125659152/network.dat new file mode 100644 index 0000000000..29185fc2fa Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/3125659152/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/4/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/4/network.dat new file mode 100644 index 0000000000..5f29959893 Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/4/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/61/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/61/network.dat new file mode 100644 index 0000000000..18493d3049 Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/61/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/888/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/888/network.dat new file mode 100644 index 0000000000..86955f048e Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_chain_id/888/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_slip44/5718350/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_slip44/5718350/network.dat new file mode 100644 index 0000000000..86955f048e Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_slip44/5718350/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_slip44/6060/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_slip44/6060/network.dat new file mode 100644 index 0000000000..6ee543543b Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_slip44/6060/network.dat differ diff --git a/common/tests/fixtures/ethereum/definitions-latest/by_slip44/61/network.dat b/common/tests/fixtures/ethereum/definitions-latest/by_slip44/61/network.dat new file mode 100644 index 0000000000..18493d3049 Binary files /dev/null and b/common/tests/fixtures/ethereum/definitions-latest/by_slip44/61/network.dat differ diff --git a/common/tests/fixtures/ethereum/getaddress.json b/common/tests/fixtures/ethereum/getaddress.json index 43b725ffca..8e76ef8c3a 100644 --- a/common/tests/fixtures/ethereum/getaddress.json +++ b/common/tests/fixtures/ethereum/getaddress.json @@ -39,6 +39,8 @@ { "name": "ETC", "parameters": { + "builtin_network": false, + "slip44": 61, "path": "m/44'/61'/0'/0/0" }, "result": { @@ -48,6 +50,8 @@ { "name": "GoChain", "parameters": { + "builtin_network": false, + "slip44": 6060, "path": "m/44'/6060'/0'/0/0" }, "result": { @@ -57,6 +61,8 @@ { "name": "Wanchain", "parameters": { + "builtin_network": false, + "slip44": 5718350, "path": "m/44'/5718350'/0'/0/0" }, "result": { diff --git a/common/tests/fixtures/ethereum/sign_tx.json b/common/tests/fixtures/ethereum/sign_tx.json index 1d3358a029..fc6334c2b3 100644 --- a/common/tests/fixtures/ethereum/sign_tx.json +++ b/common/tests/fixtures/ethereum/sign_tx.json @@ -16,7 +16,8 @@ "gas_price": "0x14", "gas_limit": "0x14", "tx_type": null, - "value": "0x0" + "value": "0x0", + "builtin_token": false }, "result": { "sig_v": 37, @@ -55,7 +56,8 @@ "gas_price": "0x14", "gas_limit": "0x14", "tx_type": 1, - "value": "0x64" + "value": "0x64", + "builtin_network": false }, "result": { "sig_v": 1811, diff --git a/common/tests/fixtures/ethereum/sign_tx_eip155.json b/common/tests/fixtures/ethereum/sign_tx_eip155.json index b31965bae1..747803f84e 100644 --- a/common/tests/fixtures/ethereum/sign_tx_eip155.json +++ b/common/tests/fixtures/ethereum/sign_tx_eip155.json @@ -53,7 +53,8 @@ "value": "0x2540be400", "to_address": "0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98", "tx_type": null, - "data": "" + "data": "", + "builtin_network": false }, "result": { "sig_v": 43, @@ -72,7 +73,8 @@ "value": "0x2540be400", "to_address": "0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98", "tx_type": null, - "data": "" + "data": "", + "builtin_network": false }, "result": { "sig_v": 158, @@ -91,7 +93,8 @@ "value": "0x2540be400", "to_address": "0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98", "tx_type": null, - "data": "" + "data": "", + "builtin_network": false }, "result": { "sig_v": 57891008, @@ -110,7 +113,8 @@ "value": "0x2540be400", "to_address": "0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98", "tx_type": null, - "data": "" + "data": "", + "builtin_network": false }, "result": { "sig_v": 6251318340, @@ -167,7 +171,8 @@ "value": "0x2540be400", "to_address": "0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98", "tx_type": null, - "data": "" + "data": "", + "builtin_network": false }, "result": { "sig_v": 22594216254, diff --git a/common/tests/fixtures/ethereum/sign_tx_eip1559.json b/common/tests/fixtures/ethereum/sign_tx_eip1559.json index 239c6b07cd..6cf4188dbe 100644 --- a/common/tests/fixtures/ethereum/sign_tx_eip1559.json +++ b/common/tests/fixtures/ethereum/sign_tx_eip1559.json @@ -73,7 +73,8 @@ "gas_limit": "0x14", "max_gas_fee": "0x14", "max_priority_fee": "0x1", - "value": "0x0" + "value": "0x0", + "builtin_token": false }, "result": { "sig_v": 1, @@ -113,7 +114,8 @@ "gas_limit": "0x14", "max_gas_fee": "0x14", "max_priority_fee": "0x1", - "value": "0xa" + "value": "0xa", + "builtin_network": false }, "result": { "sig_v": 0, diff --git a/core/src/apps/ethereum/networks.py b/core/src/apps/ethereum/networks.py index 128f755958..6dce0b2cee 100644 --- a/core/src/apps/ethereum/networks.py +++ b/core/src/apps/ethereum/networks.py @@ -23,6 +23,7 @@ if TYPE_CHECKING: bool # rskip60 ] # fmt: on + UNKNOWN_NETWORK_SHORTCUT = "UNKN" diff --git a/python/src/trezorlib/cli/ethereum.py b/python/src/trezorlib/cli/ethereum.py index 40d6aabc41..1cfc9f3dec 100644 --- a/python/src/trezorlib/cli/ethereum.py +++ b/python/src/trezorlib/cli/ethereum.py @@ -37,6 +37,7 @@ from typing import ( import click from .. import ethereum, tools +from ..tools import UH_ from . import with_client if TYPE_CHECKING: @@ -194,7 +195,7 @@ def _get_ethereum_definitions( if definitions_dir is not None: if chain_id is not None or slip44_hardened is not None: defs.encoded_network = ethereum.network_definition_from_dir( - definitions_dir, chain_id, slip44_hardened + definitions_dir, chain_id, UH_(slip44_hardened) # type: ignore [Argument of type "int | None" cannot be assigned to parameter "x" of type "int" in function "UH_"] ) if chain_id is not None and token_address is not None: defs.encoded_token = ethereum.token_definition_from_dir( @@ -210,7 +211,7 @@ def _get_ethereum_definitions( elif download_definitions: if chain_id is not None or slip44_hardened is not None: defs.encoded_network = ethereum.download_network_definition( - chain_id, slip44_hardened + chain_id, UH_(slip44_hardened) # type: ignore [Argument of type "int | None" cannot be assigned to parameter "x" of type "int" in function "UH_"] ) if chain_id is not None and token_address is not None: defs.encoded_token = ethereum.download_token_definition( diff --git a/python/src/trezorlib/ethereum.py b/python/src/trezorlib/ethereum.py index fe804d7214..bf13aacbfb 100644 --- a/python/src/trezorlib/ethereum.py +++ b/python/src/trezorlib/ethereum.py @@ -21,7 +21,7 @@ from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Tuple import requests from . import exceptions, messages -from .tools import UH_, expect, prepare_message_bytes, session +from .tools import expect, prepare_message_bytes, session if TYPE_CHECKING: from .client import TrezorClient @@ -162,11 +162,11 @@ def download_from_url(url: str, error_msg: str = "") -> bytes: def download_network_definition( - chain_id: Optional[int] = None, slip44_hardened: Optional[int] = None + chain_id: Optional[int] = None, slip44: Optional[int] = None ) -> Optional[bytes]: - if not ((chain_id is None) != (slip44_hardened is None)): # not XOR + if not ((chain_id is None) != (slip44 is None)): # not XOR raise RuntimeError( - "Exactly one of chain_id or slip44_hardened parameters are needed to load network definition from directory." + "Exactly one of chain_id or slip44 parameters are needed to load network definition from directory." ) if chain_id is not None: @@ -178,7 +178,7 @@ def download_network_definition( else: url = DEFS_BASE_URL.format( lookup_type=DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE, - id=UH_(slip44_hardened), # type: ignore [Argument of type "int | None" cannot be assigned to parameter "x" of type "int" in function "UH_"] + id=slip44, name=DEFS_NETWORK_URI_NAME, ) @@ -207,11 +207,11 @@ def download_token_definition( def network_definition_from_dir( path: pathlib.Path, chain_id: Optional[int] = None, - slip44_hardened: Optional[int] = None, + slip44: Optional[int] = None, ) -> Optional[bytes]: - if not ((chain_id is None) != (slip44_hardened is None)): # not XOR + if not ((chain_id is None) != (slip44 is None)): # not XOR raise RuntimeError( - "Exactly one of chain_id or slip44_hardened parameters are needed to load network definition from directory." + "Exactly one of chain_id or slip44 parameters are needed to load network definition from directory." ) def read_definition(path: pathlib.Path) -> Optional[bytes]: @@ -232,7 +232,7 @@ def network_definition_from_dir( return read_definition( path / DEFS_NETWORK_BY_SLIP44_LOOKUP_TYPE - / str(UH_(slip44_hardened)) # type: ignore [Argument of type "int | None" cannot be assigned to parameter "x" of type "int" in function "UH_"] + / str(slip44) / (DEFS_NETWORK_URI_NAME + ".dat") ) @@ -247,6 +247,9 @@ def token_definition_from_dir( "Both chain_id and token_address parameters are needed to load token definition from directory." ) + if token_address is not None and token_address.startswith("0x"): + token_address = token_address[2:] + path = ( path / DEFS_NETWORK_BY_CHAINID_LOOKUP_TYPE diff --git a/tests/device_tests/ethereum/test_getaddress.py b/tests/device_tests/ethereum/test_getaddress.py index b9cd6d8c65..d31813cc6b 100644 --- a/tests/device_tests/ethereum/test_getaddress.py +++ b/tests/device_tests/ethereum/test_getaddress.py @@ -20,12 +20,21 @@ from trezorlib import ethereum from trezorlib.debuglink import TrezorClientDebugLink as Client from trezorlib.tools import parse_path -from ...common import parametrize_using_common_fixtures +from ...common import COMMON_FIXTURES_DIR, parametrize_using_common_fixtures pytestmark = [pytest.mark.altcoin, pytest.mark.ethereum] @parametrize_using_common_fixtures("ethereum/getaddress.json") def test_getaddress(client: Client, parameters, result): + encoded_network = None + if not parameters.get("builtin_network", True): + encoded_network = ethereum.network_definition_from_dir( + path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest", + slip44=parameters["slip44"], + ) address_n = parse_path(parameters["path"]) - assert ethereum.get_address(client, address_n) == result["address"] + assert ( + ethereum.get_address(client, address_n, encoded_network=encoded_network) + == result["address"] + ) diff --git a/tests/device_tests/ethereum/test_signtx.py b/tests/device_tests/ethereum/test_signtx.py index 6ab96677c6..f49fb50ac3 100644 --- a/tests/device_tests/ethereum/test_signtx.py +++ b/tests/device_tests/ethereum/test_signtx.py @@ -14,6 +14,8 @@ # You should have received a copy of the License along with this library. # If not, see . +from typing import Optional + import pytest from trezorlib import ethereum, exceptions, messages @@ -21,7 +23,7 @@ from trezorlib.debuglink import TrezorClientDebugLink as Client, message_filters from trezorlib.exceptions import TrezorFailure from trezorlib.tools import parse_path -from ...common import parametrize_using_common_fixtures +from ...common import COMMON_FIXTURES_DIR, parametrize_using_common_fixtures TO_ADDR = "0x1d1c328764a41bda0492b66baa30c4a339ff85ef" SHOW_ALL = (143, 167) @@ -30,6 +32,31 @@ GO_BACK = (16, 220) pytestmark = [pytest.mark.altcoin, pytest.mark.ethereum] +def get_EthereumEncodedDefinitions( + parameters: dict, +) -> Optional[messages.EthereumEncodedDefinitions]: + encoded_network = None + encoded_token = None + if not parameters.get("builtin_network", True): + encoded_network = ethereum.network_definition_from_dir( + path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest", + chain_id=parameters["chain_id"], + ) + if not parameters.get("builtin_token", True): + encoded_token = ethereum.token_definition_from_dir( + path=COMMON_FIXTURES_DIR / "ethereum" / "definitions-latest", + chain_id=parameters["chain_id"], + token_address=parameters["to_address"], + ) + + if encoded_network is not None or encoded_token is not None: + return messages.EthereumEncodedDefinitions( + encoded_network=encoded_network, encoded_token=encoded_token + ) + + return None + + @parametrize_using_common_fixtures( "ethereum/sign_tx.json", "ethereum/sign_tx_eip155.json", @@ -47,6 +74,7 @@ def test_signtx(client: Client, parameters, result): value=int(parameters["value"], 16), tx_type=parameters["tx_type"], data=bytes.fromhex(parameters["data"]), + definitions=get_EthereumEncodedDefinitions(parameters), ) expected_v = 2 * parameters["chain_id"] + 35 @@ -70,6 +98,7 @@ def test_signtx_eip1559(client: Client, parameters, result): chain_id=parameters["chain_id"], value=int(parameters["value"], 16), data=bytes.fromhex(parameters["data"]), + definitions=get_EthereumEncodedDefinitions(parameters), ) assert sig_r.hex() == result["sig_r"]