From 25b57bb830f51dc8a2616043316e0f123b7bc343 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 27 Feb 2023 18:15:41 +0100 Subject: [PATCH] Small refactoring : renamed the Ragger client --- .../ragger/{ethereum_client => app}/client.py | 26 +++++++------- .../command_builder.py | 36 ++++++++++--------- .../ragger/{ethereum_client => app}/eip712.py | 2 +- .../response_parser.py | 2 +- .../{ethereum_client => app}/setting.py | 4 +-- tests/ragger/conftest.py | 2 +- tests/ragger/eip712/InputData.py | 2 +- tests/ragger/test_eip712.py | 26 +++++++------- 8 files changed, 50 insertions(+), 50 deletions(-) rename tests/ragger/{ethereum_client => app}/client.py (91%) rename tests/ragger/{ethereum_client => app}/command_builder.py (89%) rename tests/ragger/{ethereum_client => app}/eip712.py (84%) rename tests/ragger/{ethereum_client => app}/response_parser.py (89%) rename tests/ragger/{ethereum_client => app}/setting.py (83%) diff --git a/tests/ragger/ethereum_client/client.py b/tests/ragger/app/client.py similarity index 91% rename from tests/ragger/ethereum_client/client.py rename to tests/ragger/app/client.py index 9622a05..7e25093 100644 --- a/tests/ragger/ethereum_client/client.py +++ b/tests/ragger/app/client.py @@ -1,17 +1,17 @@ from enum import IntEnum, auto -from typing import Iterator, Dict, List +from typing import Optional from ragger.backend import BackendInterface from ragger.utils import RAPDU -from ethereum_client.command_builder import EthereumCmdBuilder -from ethereum_client.setting import SettingType, SettingImpl -from ethereum_client.eip712 import EIP712FieldType -from ethereum_client.response_parser import EthereumRespParser +from .command_builder import EthereumCmdBuilder +from .setting import SettingType, SettingImpl +from .eip712 import EIP712FieldType +from .response_parser import EthereumRespParser import signal import time -class EthereumClient: - _settings: Dict[SettingType, SettingImpl] = { +class EthereumClient: + _settings: dict[SettingType, SettingImpl] = { SettingType.BLIND_SIGNING: SettingImpl( [ "nanos", "nanox", "nanosp" ] ), @@ -65,11 +65,11 @@ class EthereumClient: array_levels: [], key_name: str): with self._send(self._cmd_builder.eip712_send_struct_def_struct_field( - field_type, - type_name, - type_size, - array_levels, - key_name)): + field_type, + type_name, + type_size, + array_levels, + key_name)): pass return self._recv() @@ -125,7 +125,7 @@ class EthereumClient: assert resp.status == 0x9000 return self._resp_parser.sign(resp.data) - def settings_set(self, new_values: Dict[SettingType, bool]): + def settings_set(self, new_values: dict[SettingType, bool]): # Go to settings for _ in range(2): self._client.right_click() diff --git a/tests/ragger/ethereum_client/command_builder.py b/tests/ragger/app/command_builder.py similarity index 89% rename from tests/ragger/ethereum_client/command_builder.py rename to tests/ragger/app/command_builder.py index 134405f..04fdc20 100644 --- a/tests/ragger/ethereum_client/command_builder.py +++ b/tests/ragger/app/command_builder.py @@ -1,18 +1,19 @@ from enum import IntEnum, auto -from typing import Iterator -from ethereum_client.eip712 import EIP712FieldType +from typing import Iterator, Optional +from .eip712 import EIP712FieldType +import struct -class InsType(IntEnum): +class InsType(IntEnum): EIP712_SEND_STRUCT_DEF = 0x1a EIP712_SEND_STRUCT_IMPL = 0x1c EIP712_SEND_FILTERING = 0x1e EIP712_SIGN = 0x0c -class P1Type(IntEnum): +class P1Type(IntEnum): COMPLETE_SEND = 0x00 PARTIAL_SEND = 0x01 -class P2Type(IntEnum): +class P2Type(IntEnum): STRUCT_NAME = 0x00 STRUCT_FIELD = 0xff ARRAY = 0x0f @@ -22,7 +23,7 @@ class P2Type(IntEnum): FILTERING_CONTRACT_NAME = 0x0f FILTERING_FIELD_NAME = 0xff -class EthereumCmdBuilder: +class EthereumCmdBuilder: _CLA: int = 0xE0 def _serialize(self, @@ -109,27 +110,28 @@ class EthereumCmdBuilder: data_w_length[:0xff]) data_w_length = data_w_length[0xff:] - def _format_bip32(self, bip32, data: bytearray) -> bytearray: - data.append(len(bip32)) - for idx in bip32: - data.append((idx & 0xff000000) >> 24) - data.append((idx & 0x00ff0000) >> 16) - data.append((idx & 0x0000ff00) >> 8) - data.append((idx & 0x000000ff)) + def _format_bip32(self, path: list[Optional[int]], data: bytearray) -> bytearray: + data.append(len(path)) + for p in path: + if p == None: + val = 0 + else: + val = (0x8 << 28) | p + data += struct.pack(">I", val) return data - def eip712_sign_new(self, bip32) -> bytes: - data = self._format_bip32(bip32, bytearray()) + def eip712_sign_new(self, bip32_path: list[Optional[int]]) -> bytes: + data = self._format_bip32(bip32_path, bytearray()) return self._serialize(InsType.EIP712_SIGN, P1Type.COMPLETE_SEND, P2Type.NEW_IMPLEM, data) def eip712_sign_legacy(self, - bip32, + bip32_path: list[Optional[int]], domain_hash: bytes, message_hash: bytes) -> bytes: - data = self._format_bip32(bip32, bytearray()) + data = self._format_bip32(bip32_path, bytearray()) data += domain_hash data += message_hash return self._serialize(InsType.EIP712_SIGN, diff --git a/tests/ragger/ethereum_client/eip712.py b/tests/ragger/app/eip712.py similarity index 84% rename from tests/ragger/ethereum_client/eip712.py rename to tests/ragger/app/eip712.py index 3438a1c..f719c6e 100644 --- a/tests/ragger/ethereum_client/eip712.py +++ b/tests/ragger/app/eip712.py @@ -1,6 +1,6 @@ from enum import IntEnum, auto -class EIP712FieldType(IntEnum): +class EIP712FieldType(IntEnum): CUSTOM = 0, INT = auto() UINT = auto() diff --git a/tests/ragger/ethereum_client/response_parser.py b/tests/ragger/app/response_parser.py similarity index 89% rename from tests/ragger/ethereum_client/response_parser.py rename to tests/ragger/app/response_parser.py index 681c18d..9d1c9bd 100644 --- a/tests/ragger/ethereum_client/response_parser.py +++ b/tests/ragger/app/response_parser.py @@ -1,4 +1,4 @@ -class EthereumRespParser: +class EthereumRespParser: def sign(self, data: bytes): assert len(data) == (1 + 32 + 32) diff --git a/tests/ragger/ethereum_client/setting.py b/tests/ragger/app/setting.py similarity index 83% rename from tests/ragger/ethereum_client/setting.py rename to tests/ragger/app/setting.py index a965fe3..a51f988 100644 --- a/tests/ragger/ethereum_client/setting.py +++ b/tests/ragger/app/setting.py @@ -1,13 +1,13 @@ from enum import IntEnum, auto from typing import List -class SettingType(IntEnum): +class SettingType(IntEnum): BLIND_SIGNING = 0, DEBUG_DATA = auto() NONCE = auto() VERBOSE_EIP712 = auto() -class SettingImpl: +class SettingImpl: devices: List[str] value: bool diff --git a/tests/ragger/conftest.py b/tests/ragger/conftest.py index 60fb393..35e3fd3 100644 --- a/tests/ragger/conftest.py +++ b/tests/ragger/conftest.py @@ -1,7 +1,7 @@ import pytest from ragger.conftest import configuration from ragger.backend import BackendInterface -from ethereum_client.client import EthereumClient +from app.client import EthereumClient # This final fixture will return the properly configured app client, to be used in tests @pytest.fixture diff --git a/tests/ragger/eip712/InputData.py b/tests/ragger/eip712/InputData.py index 38ca8c0..40a3882 100644 --- a/tests/ragger/eip712/InputData.py +++ b/tests/ragger/eip712/InputData.py @@ -4,7 +4,7 @@ import json import sys import re import hashlib -from ethereum_client.client import EthereumClient, EIP712FieldType +from app.client import EthereumClient, EIP712FieldType from cal import cal # global variables diff --git a/tests/ragger/test_eip712.py b/tests/ragger/test_eip712.py index 8dfa442..51cc773 100644 --- a/tests/ragger/test_eip712.py +++ b/tests/ragger/test_eip712.py @@ -2,17 +2,17 @@ import pytest import os import fnmatch from typing import List -from ethereum_client.client import EthereumClient, SettingType +from app.client import EthereumClient, SettingType from eip712 import InputData from pathlib import Path from configparser import ConfigParser -bip32 = [ - 0x8000002c, - 0x8000003c, - 0x80000000, +bip32_path = [ + 44, + 60, 0, - 0 + None, + None ] @@ -38,7 +38,7 @@ def filtering(request) -> bool: def test_eip712_legacy(app_client: EthereumClient): v, r, s = app_client.eip712_sign_legacy( - bip32, + bip32_path, bytes.fromhex('6137beb405d9ff777172aa879e33edb34a1460e701802746c5ef96e741710e59'), bytes.fromhex('eb4221181ff3f1a83ea7313993ca9218496e424604ba9492bb4052c03d5c3df8') ) @@ -47,10 +47,10 @@ def test_eip712_legacy(app_client: EthereumClient): assert r == bytes.fromhex("ea66f747173762715751c889fea8722acac3fc35db2c226d37a2e58815398f64") assert s == bytes.fromhex("52d8ba9153de9255da220ffd36762c0b027701a3b5110f0a765f94b16a9dfb55") - def test_eip712_new(app_client: EthereumClient, input_file: Path, verbose: bool, filtering: bool): - print("=====> %s" % (input_file)) - if app_client._client.firmware.device != "nanos": + if app_client._client.firmware.device == "nanos": + pytest.skip("Not supported on LNS") + else: test_path = "%s/%s" % (input_file.parent, "-".join(input_file.stem.split("-")[:-1])) conf_file = "%s.ini" % (test_path) filter_file = None @@ -74,7 +74,7 @@ def test_eip712_new(app_client: EthereumClient, input_file: Path, verbose: bool, }) assert InputData.process_file(app_client, input_file, filter_file) == True - v, r, s = app_client.eip712_sign_new(bip32) + v, r, s = app_client.eip712_sign_new(bip32_path) #print("[signature]") #print("v = %s" % (v.hex())) #print("r = %s" % (r.hex())) @@ -84,6 +84,4 @@ def test_eip712_new(app_client: EthereumClient, input_file: Path, verbose: bool, assert r == bytes.fromhex(config["signature"]["r"]) assert s == bytes.fromhex(config["signature"]["s"]) else: - print("No filter file found, skipping...") - else: - print("Not supported by LNS, skipping...") + pytest.skip("No filter file found")