1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-13 18:18:08 +00:00

tests(core): built-in Ethereum definitions

This commit is contained in:
Martin Novak 2022-09-19 14:29:56 +02:00
parent c1f27e0a11
commit 7fbbfaa665
19 changed files with 81 additions and 23 deletions

View File

@ -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": {

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -23,6 +23,7 @@ if TYPE_CHECKING:
bool # rskip60
]
# fmt: on
UNKNOWN_NETWORK_SHORTCUT = "UNKN"

View File

@ -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(

View File

@ -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

View File

@ -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"]
)

View File

@ -14,6 +14,8 @@
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
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"]