Skip to content

Commit

Permalink
Refactor of 'ragger' test 'eip712'
Browse files Browse the repository at this point in the history
  • Loading branch information
cedelavergne-ledger committed Mar 29, 2024
1 parent 4d8cb98 commit 779f891
Showing 1 changed file with 79 additions and 73 deletions.
152 changes: 79 additions & 73 deletions tests/ragger/test_eip712.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest

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.eip712 import InputData
from ledger_app_clients.ethereum.settings import SettingID, settings_toggle
Expand Down Expand Up @@ -151,37 +152,38 @@ def test_eip712_new(firmware: Firmware,
app_client = EthAppClient(backend)
if firmware.device == "nanos":
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
if filtering:
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()
config.read(conf_file)

# sanity check
assert "signature" in config.sections()
assert "v" in config["signature"]
assert "r" in config["signature"]
assert "s" in config["signature"]

if verbose:
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])

with open(input_file, encoding="utf-8") as file:
v, r, s = eip712_new_common(firmware,
navigator,
app_client,
json.load(file),
filters,
verbose)
test_path = f"{input_file.parent}/{'-'.join(input_file.stem.split('-')[:-1])}"
conf_file = f"{test_path}.ini"

filters = None
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}")

config = ConfigParser()
config.read(conf_file)

# sanity check
assert "signature" in config.sections()
assert "v" in config["signature"]
assert "r" in config["signature"]
assert "s" in config["signature"]

if verbose:
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])

with open(input_file, encoding="utf-8") as file:
v, r, s = eip712_new_common(firmware,
navigator,
app_client,
json.load(file),
filters,
verbose)

assert v == bytes.fromhex(config["signature"]["v"])
assert r == bytes.fromhex(config["signature"]["r"])
Expand All @@ -197,48 +199,52 @@ def test_eip712_address_substitution(firmware: Firmware,
app_client = EthAppClient(backend)
if firmware.device == "nanos":
pytest.skip("Not supported on LNS")
else:
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)

with app_client.provide_token_metadata("DAI",
bytes.fromhex(data["message"]["token"][2:]),
18,
1):
pass

with app_client.get_challenge():
pass
challenge = ResponseParser.challenge(app_client.response().data)
with app_client.provide_domain_name(challenge,
"vitalik.eth",
bytes.fromhex(data["message"]["to"][2:])):
pass

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

with app_client.provide_token_metadata("DAI",
bytes.fromhex(data["message"]["token"][2:]),
18,
1):
pass

with app_client.get_challenge():
pass
challenge = ResponseParser.challenge(app_client.response().data)
with app_client.provide_domain_name(challenge,
"vitalik.eth",
bytes.fromhex(data["message"]["to"][2:])):
pass

assert v == bytes.fromhex("1b")
assert r == bytes.fromhex("d4a0e058251cdc3845aaa5eb8409d8a189ac668db7c55a64eb3121b0db7fd8c0")
assert s == bytes.fromhex("3221800e4f45272c6fa8fafda5e94c848d1a4b90c442aa62afa8e8d6a9af0f00")
if verbose:
settings_toggle(firmware, navigator, [SettingID.VERBOSE_EIP712])
filters = None
else:
filters = {
"name": "Token test",
"fields": {
"amount": "Amount",
"token": "Token",
"to": "To",
}
}

vrs = eip712_new_common(firmware,
navigator,
app_client,
data,
filters,
verbose)

# verify signature
addr = recover_message(data, vrs)
assert addr == DEVICE_ADDR

0 comments on commit 779f891

Please sign in to comment.