Skip to content

Commit

Permalink
Now uses Web3 to check EIP-712 signatures in Ragger tests
Browse files Browse the repository at this point in the history
Fixed platformFeeRecipient address length in 14-rabby_bug-data.json since Web3 does not handle addresses with a length != 20B
  • Loading branch information
apaillier-ledger committed May 2, 2024
1 parent 6df0ad5 commit a2107b8
Show file tree
Hide file tree
Showing 79 changed files with 61 additions and 130 deletions.
4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/00-simple_mail.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/01-addresses_array_mail.ini

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
{ "name": "chainId", "type": "uint256" },
{ "name": "verifyingContract", "type": "address" }
],
"Group": [
{ "name": "name", "type": "string" },
{ "name": "members", "type": "Person[]" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person[]" },
Expand Down
4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/02-recipients_array_mail.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/03-long_string-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
{ "name": "chainId", "type": "uint256" },
{ "name": "verifyingContract", "type": "address" }
],
"Group": [
{ "name": "name", "type": "string" },
{ "name": "members", "type": "Person[]" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person[]" },
Expand Down
4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/03-long_string.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/04-long_bytes-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
{ "name": "chainId", "type": "uint256" },
{ "name": "verifyingContract", "type": "address" }
],
"Group": [
{ "name": "name", "type": "string" },
{ "name": "members", "type": "Person[]" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person[]" },
Expand Down
4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/04-long_bytes.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/05-signed_ints.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/06-boolean.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/07-fixed_bytes.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/08-opensea.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/09-rarible.ini

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/11-complex_structs.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/12-sign_in.ini

This file was deleted.

4 changes: 0 additions & 4 deletions tests/ragger/eip712_input_files/13-empty_arrays.ini

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ragger/eip712_input_files/14-rabby_bug-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"expiryTime": "1709280466",
"startNonce": "7",
"erc20Token": "0x09bc4e0d864854c6afb6eb9a9cdf58ac190d0df9",
"platformFeeRecipient": "0x7538262ae993ca117a0e481f908209137a4626e",
"platformFeeRecipient": "0x07538262ae993ca117a0e481f908209137a4626e",
"basicCollections": [
{
"nftAddress": "0xaaaea1fb9f3de3f70e89f37b69ab11b47eb9ce6f",
Expand Down
121 changes: 60 additions & 61 deletions tests/ragger/test_eip712.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fnmatch
import os
import time
from configparser import ConfigParser
from functools import partial
from pathlib import Path
import json
from typing import Optional
import pytest
from eth_account.messages import encode_typed_data

import client.response_parser as ResponseParser
from client.utils import recover_message
Expand All @@ -29,7 +29,8 @@ def __init__(self, test_name: str, idx: int = 0):


BIP32_PATH = "m/44'/60'/0'/0/0"
snaps_config: Optional[SnapshotsConfig] = None
SNAPS_CONFIG: Optional[SnapshotsConfig] = None
WALLET_ADDR: Optional[bytes] = None


def eip712_json_path() -> str:
Expand Down Expand Up @@ -59,33 +60,44 @@ def filtering_fixture(request) -> bool:
return request.param


def get_wallet_addr(client: EthAppClient) -> bytes:
global WALLET_ADDR

# don't ask again if we already have it
if WALLET_ADDR is None:
with client.get_public_addr(display=False):
pass
_, WALLET_ADDR, _ = ResponseParser.pk_addr(client.response().data)
return WALLET_ADDR


def test_eip712_legacy(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator):
app_client = EthAppClient(backend)
with app_client.eip712_sign_legacy(
BIP32_PATH,
bytes.fromhex('6137beb405d9ff777172aa879e33edb34a1460e701802746c5ef96e741710e59'),
bytes.fromhex('eb4221181ff3f1a83ea7313993ca9218496e424604ba9492bb4052c03d5c3df8')):
moves = []
if firmware.device.startswith("nano"):
moves += [NavInsID.RIGHT_CLICK]
if firmware.device == "nanos":
screens_per_hash = 4

with open(input_files()[0]) as file:
data = json.load(file)
smsg = encode_typed_data(full_message=data)
with app_client.eip712_sign_legacy(BIP32_PATH, smsg.header, smsg.body):
moves = []
if firmware.device.startswith("nano"):
moves += [NavInsID.RIGHT_CLICK]
if firmware.device == "nanos":
screens_per_hash = 4
else:
screens_per_hash = 2
moves += [NavInsID.RIGHT_CLICK] * screens_per_hash * 2
moves += [NavInsID.BOTH_CLICK]
else:
screens_per_hash = 2
moves += [NavInsID.RIGHT_CLICK] * screens_per_hash * 2
moves += [NavInsID.BOTH_CLICK]
else:
moves += [NavInsID.USE_CASE_REVIEW_TAP] * 2
moves += [NavInsID.USE_CASE_REVIEW_CONFIRM]
navigator.navigate(moves)
moves += [NavInsID.USE_CASE_REVIEW_TAP] * 2
moves += [NavInsID.USE_CASE_REVIEW_CONFIRM]
navigator.navigate(moves)

v, r, s = ResponseParser.signature(app_client.response().data)
vrs = ResponseParser.signature(app_client.response().data)
recovered_addr = recover_message(data, vrs)

assert v == bytes.fromhex("1c")
assert r == bytes.fromhex("ea66f747173762715751c889fea8722acac3fc35db2c226d37a2e58815398f64")
assert s == bytes.fromhex("52d8ba9153de9255da220ffd36762c0b027701a3b5110f0a765f94b16a9dfb55")
assert recovered_addr == get_wallet_addr(app_client)


def autonext(firmware: Firmware, navigator: Navigator, default_screenshot_path: Path):
Expand All @@ -94,18 +106,18 @@ def autonext(firmware: Firmware, navigator: Navigator, default_screenshot_path:
moves = [NavInsID.RIGHT_CLICK]
else:
moves = [NavInsID.USE_CASE_REVIEW_TAP]
if snaps_config is not None:
if SNAPS_CONFIG is not None:
navigator.navigate_and_compare(default_screenshot_path,
snaps_config.test_name,
SNAPS_CONFIG.test_name,
moves,
screen_change_before_first_instruction=False,
screen_change_after_last_instruction=False,
snap_start_idx=snaps_config.idx)
snaps_config.idx += 1
snap_start_idx=SNAPS_CONFIG.idx)
SNAPS_CONFIG.idx += 1
else:
navigator.navigate(moves,
screen_change_before_first_instruction=False,
screen_change_after_last_instruction=False)
screen_change_before_first_instruction=False,
screen_change_after_last_instruction=False)


def eip712_new_common(firmware: Firmware,
Expand All @@ -132,12 +144,12 @@ def eip712_new_common(firmware: Firmware,
if not verbose and filters is None:
moves += [NavInsID.USE_CASE_REVIEW_TAP]
moves += [NavInsID.USE_CASE_REVIEW_CONFIRM]
if snaps_config is not None:
if SNAPS_CONFIG is not None:
navigator.navigate_and_compare(default_screenshot_path,
snaps_config.test_name,
moves,
snap_start_idx=snaps_config.idx)
snaps_config.idx += 1
SNAPS_CONFIG.test_name,
moves,
snap_start_idx=SNAPS_CONFIG.idx)
SNAPS_CONFIG.idx += 1
else:
navigator.navigate(moves)
return ResponseParser.signature(app_client.response().data)
Expand All @@ -155,7 +167,6 @@ def test_eip712_new(firmware: Firmware,
pytest.skip("Not supported on LNS")

test_path = f"{input_file.parent}/{'-'.join(input_file.stem.split('-')[:-1])}"
conf_file = f"{test_path}.ini"

filters = None
if filtering:
Expand All @@ -166,51 +177,39 @@ def test_eip712_new(firmware: Firmware,
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,
default_screenshot_path,
app_client,
json.load(file),
filters,
verbose)
data = json.load(file)
vrs = eip712_new_common(firmware,
navigator,
default_screenshot_path,
app_client,
data,
filters,
verbose)

assert v == bytes.fromhex(config["signature"]["v"])
assert r == bytes.fromhex(config["signature"]["r"])
assert s == bytes.fromhex(config["signature"]["s"])
recovered_addr = recover_message(data, vrs)

assert recovered_addr == get_wallet_addr(app_client)


def test_eip712_address_substitution(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
default_screenshot_path: Path,
test_name: str,
verbose: bool):
global snaps_config
global SNAPS_CONFIG

app_client = EthAppClient(backend)
if firmware.device == "nanos":
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)
SNAPS_CONFIG = SnapshotsConfig(test_name)
with open(f"{eip712_json_path()}/address_substitution.json", encoding="utf-8") as file:
data = json.load(file)

Expand Down Expand Up @@ -247,4 +246,4 @@ def test_eip712_address_substitution(firmware: Firmware,

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

0 comments on commit a2107b8

Please sign in to comment.