Refactor of 'ragger' test 'eip712'
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Optional
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import ledger_app_clients.ethereum.response_parser as ResponseParser
|
import ledger_app_clients.ethereum.response_parser as ResponseParser
|
||||||
|
from ledger_app_clients.ethereum.utils import recover_message
|
||||||
from ledger_app_clients.ethereum.client import EthAppClient
|
from ledger_app_clients.ethereum.client import EthAppClient
|
||||||
from ledger_app_clients.ethereum.eip712 import InputData
|
from ledger_app_clients.ethereum.eip712 import InputData
|
||||||
from ledger_app_clients.ethereum.settings import SettingID, settings_toggle
|
from ledger_app_clients.ethereum.settings import SettingID, settings_toggle
|
||||||
@@ -16,6 +17,7 @@ from ledger_app_clients.ethereum.settings import SettingID, settings_toggle
|
|||||||
from ragger.backend import BackendInterface
|
from ragger.backend import BackendInterface
|
||||||
from ragger.firmware import Firmware
|
from ragger.firmware import Firmware
|
||||||
from ragger.navigator import Navigator, NavInsID
|
from ragger.navigator import Navigator, NavInsID
|
||||||
|
|
||||||
from constants import ROOT_SNAPSHOT_PATH
|
from constants import ROOT_SNAPSHOT_PATH
|
||||||
|
|
||||||
|
|
||||||
@@ -96,11 +98,11 @@ def autonext(firmware: Firmware, navigator: Navigator):
|
|||||||
moves = [NavInsID.USE_CASE_REVIEW_TAP]
|
moves = [NavInsID.USE_CASE_REVIEW_TAP]
|
||||||
if snaps_config is not None:
|
if snaps_config is not None:
|
||||||
navigator.navigate_and_compare(ROOT_SNAPSHOT_PATH,
|
navigator.navigate_and_compare(ROOT_SNAPSHOT_PATH,
|
||||||
snaps_config.test_name,
|
snaps_config.test_name,
|
||||||
moves,
|
moves,
|
||||||
screen_change_before_first_instruction=False,
|
screen_change_before_first_instruction=False,
|
||||||
screen_change_after_last_instruction=False,
|
screen_change_after_last_instruction=False,
|
||||||
snap_start_idx=snaps_config.idx)
|
snap_start_idx=snaps_config.idx)
|
||||||
snaps_config.idx += 1
|
snaps_config.idx += 1
|
||||||
else:
|
else:
|
||||||
navigator.navigate(moves,
|
navigator.navigate(moves,
|
||||||
@@ -151,37 +153,38 @@ def test_eip712_new(firmware: Firmware,
|
|||||||
app_client = EthAppClient(backend)
|
app_client = EthAppClient(backend)
|
||||||
if firmware.device == "nanos":
|
if firmware.device == "nanos":
|
||||||
pytest.skip("Not supported on LNS")
|
pytest.skip("Not supported on LNS")
|
||||||
else:
|
|
||||||
test_path = f"{input_file.parent}/{'-'.join(input_file.stem.split('-')[:-1])}"
|
|
||||||
conf_file = f"{test_path}.ini"
|
|
||||||
|
|
||||||
filters = None
|
test_path = f"{input_file.parent}/{'-'.join(input_file.stem.split('-')[:-1])}"
|
||||||
if filtering:
|
conf_file = f"{test_path}.ini"
|
||||||
try:
|
|
||||||
with open(f"{test_path}-filter.json", encoding="utf-8") as f:
|
|
||||||
filters = json.load(f)
|
|
||||||
except (IOError, json.decoder.JSONDecodeError) as e:
|
|
||||||
pytest.skip(f"Filter file error: {e.strerror}")
|
|
||||||
|
|
||||||
config = ConfigParser()
|
filters = None
|
||||||
config.read(conf_file)
|
if filtering:
|
||||||
|
try:
|
||||||
|
filterfile = Path(f"{test_path}-filter.json")
|
||||||
|
with open(filterfile, encoding="utf-8") as f:
|
||||||
|
filters = json.load(f)
|
||||||
|
except (IOError, json.decoder.JSONDecodeError) as e:
|
||||||
|
pytest.skip(f"{filterfile.name}: {e.strerror}")
|
||||||
|
|
||||||
# sanity check
|
config = ConfigParser()
|
||||||
assert "signature" in config.sections()
|
config.read(conf_file)
|
||||||
assert "v" in config["signature"]
|
|
||||||
assert "r" in config["signature"]
|
|
||||||
assert "s" in config["signature"]
|
|
||||||
|
|
||||||
if verbose:
|
# sanity check
|
||||||
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])
|
assert "signature" in config.sections()
|
||||||
|
assert "v" in config["signature"]
|
||||||
|
assert "r" in config["signature"]
|
||||||
|
assert "s" in config["signature"]
|
||||||
|
|
||||||
with open(input_file, encoding="utf-8") as file:
|
if verbose:
|
||||||
v, r, s = eip712_new_common(firmware,
|
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])
|
||||||
navigator,
|
|
||||||
app_client,
|
with open(input_file, encoding="utf-8") as file:
|
||||||
json.load(file),
|
v, r, s = eip712_new_common(firmware,
|
||||||
filters,
|
navigator,
|
||||||
verbose)
|
app_client,
|
||||||
|
json.load(file),
|
||||||
|
filters,
|
||||||
|
verbose)
|
||||||
|
|
||||||
assert v == bytes.fromhex(config["signature"]["v"])
|
assert v == bytes.fromhex(config["signature"]["v"])
|
||||||
assert r == bytes.fromhex(config["signature"]["r"])
|
assert r == bytes.fromhex(config["signature"]["r"])
|
||||||
@@ -197,44 +200,48 @@ def test_eip712_address_substitution(firmware: Firmware,
|
|||||||
app_client = EthAppClient(backend)
|
app_client = EthAppClient(backend)
|
||||||
if firmware.device == "nanos":
|
if firmware.device == "nanos":
|
||||||
pytest.skip("Not supported on LNS")
|
pytest.skip("Not supported on LNS")
|
||||||
|
|
||||||
|
with app_client.get_public_addr(display=False):
|
||||||
|
pass
|
||||||
|
_, DEVICE_ADDR, _ = ResponseParser.pk_addr(app_client.response().data)
|
||||||
|
|
||||||
|
test_name = "eip712_address_substitution"
|
||||||
|
if verbose:
|
||||||
|
test_name += "_verbose"
|
||||||
|
snaps_config = SnapshotsConfig(test_name)
|
||||||
|
with open(f"{eip712_json_path()}/address_substitution.json", encoding="utf-8") as file:
|
||||||
|
data = json.load(file)
|
||||||
|
|
||||||
|
app_client.provide_token_metadata("DAI",
|
||||||
|
bytes.fromhex(data["message"]["token"][2:]),
|
||||||
|
18,
|
||||||
|
1)
|
||||||
|
|
||||||
|
challenge = ResponseParser.challenge(app_client.get_challenge().data)
|
||||||
|
app_client.provide_domain_name(challenge,
|
||||||
|
"vitalik.eth",
|
||||||
|
bytes.fromhex(data["message"]["to"][2:]))
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])
|
||||||
|
filters = None
|
||||||
else:
|
else:
|
||||||
test_name = "eip712_address_substitution"
|
filters = {
|
||||||
if verbose:
|
"name": "Token test",
|
||||||
test_name += "_verbose"
|
"fields": {
|
||||||
snaps_config = SnapshotsConfig(test_name)
|
"amount": "Amount",
|
||||||
with open(f"{eip712_json_path()}/address_substitution.json", encoding="utf-8") as file:
|
"token": "Token",
|
||||||
data = json.load(file)
|
"to": "To",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
app_client.provide_token_metadata("DAI",
|
vrs = eip712_new_common(firmware,
|
||||||
bytes.fromhex(data["message"]["token"][2:]),
|
navigator,
|
||||||
18,
|
app_client,
|
||||||
1)
|
data,
|
||||||
|
filters,
|
||||||
|
verbose)
|
||||||
|
|
||||||
challenge = ResponseParser.challenge(app_client.get_challenge().data)
|
# verify signature
|
||||||
app_client.provide_domain_name(challenge,
|
addr = recover_message(data, vrs)
|
||||||
"vitalik.eth",
|
assert addr == DEVICE_ADDR
|
||||||
bytes.fromhex(data["message"]["to"][2:]))
|
|
||||||
|
|
||||||
if verbose:
|
|
||||||
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])
|
|
||||||
filters = None
|
|
||||||
else:
|
|
||||||
filters = {
|
|
||||||
"name": "Token test",
|
|
||||||
"fields": {
|
|
||||||
"amount": "Amount",
|
|
||||||
"token": "Token",
|
|
||||||
"to": "To",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
v, r, s = eip712_new_common(firmware,
|
|
||||||
navigator,
|
|
||||||
app_client,
|
|
||||||
data,
|
|
||||||
filters,
|
|
||||||
verbose)
|
|
||||||
|
|
||||||
assert v == bytes.fromhex("1b")
|
|
||||||
assert r == bytes.fromhex("d4a0e058251cdc3845aaa5eb8409d8a189ac668db7c55a64eb3121b0db7fd8c0")
|
|
||||||
assert s == bytes.fromhex("3221800e4f45272c6fa8fafda5e94c848d1a4b90c442aa62afa8e8d6a9af0f00")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user