Skip to content

Commit

Permalink
Merge pull request #139 from LedgerHQ/fbe/review_from_lucas
Browse files Browse the repository at this point in the history
Review of the previous PRs
  • Loading branch information
fbeutin-ledger authored Jul 24, 2023
2 parents 9eb159b + 1c88984 commit 6a0604f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion test/python/apps/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def eth_amount_to_wei(eth_amount: int) -> int:
return round(eth_amount * 10**18)

def eth_amount_to_wei_hex_string(eth_amount: int) -> str:
hex:str = '{:x}'.format(eth_amount_to_wei(eth_amount))
hex = '{:x}'.format(eth_amount_to_wei(eth_amount))
if (len(hex) % 2 != 0):
hex = "0" + hex
return hex
17 changes: 3 additions & 14 deletions test/python/apps/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from ragger.backend.interface import BackendInterface, RAPDU
from ragger.utils import prefix_with_len

from ..utils import handle_lib_call_start_or_stop
from ..utils import handle_lib_call_start_or_stop, int_to_minimally_sized_bytes
from .exchange_transaction_builder import SubCommand


class Command(IntEnum):
Expand All @@ -25,12 +26,6 @@ class Rate(IntEnum):
FLOATING = 0x01


class SubCommand(IntEnum):
SWAP = 0x00
SELL = 0x01
FUND = 0x02


class Errors(IntEnum):
INCORRECT_COMMAND_DATA = 0x6A80
DESERIALIZATION_FAILED = 0x6A81
Expand All @@ -45,12 +40,6 @@ class Errors(IntEnum):
SIGN_VERIFICATION_FAIL = 0x9D1A


def _int_to_bytes(n: int) -> bytes:
if n == 0:
return b'\0'
else:
return n.to_bytes((n.bit_length() + 7) // 8, 'big')

class ExchangeClient:
CLA = 0xE0
def __init__(self,
Expand Down Expand Up @@ -100,7 +89,7 @@ def check_partner_key(self, signed_credentials: bytes) -> RAPDU:
return self._exchange(Command.CHECK_PARTNER, signed_credentials)

def process_transaction(self, transaction: bytes, fees: int) -> RAPDU:
fees_bytes = _int_to_bytes(fees)
fees_bytes = int_to_minimally_sized_bytes(fees)
payload = prefix_with_len(transaction) + prefix_with_len(fees_bytes)
return self._exchange(Command.PROCESS_TRANSACTION_RESPONSE, payload=payload)

Expand Down
7 changes: 2 additions & 5 deletions test/python/apps/exchange_navigation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ def _navigate_and_compare(self, accept: bool):
snapshots_dir_name = self._test_name
screen_change_after_last_instruction = True

if self._backend.firmware.device.startswith("nano"):
if self._backend.firmware.is_nano:
navigate_instruction = NavInsID.RIGHT_CLICK
validation_instructions = [NavInsID.BOTH_CLICK]
if accept:
text = "Accept"
else:
text = "Reject"
text = "Accept" if accept else "Reject"
else:
navigate_instruction = NavInsID.USE_CASE_REVIEW_TAP
text = "Hold to sign"
Expand Down
11 changes: 9 additions & 2 deletions test/python/apps/exchange_transaction_builder.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from base64 import urlsafe_b64encode
from typing import Optional, Dict, Callable, Iterable
from enum import Enum, auto
from enum import Enum, auto, IntEnum
from dataclasses import dataclass

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature

from .pb.exchange_pb2 import NewFundResponse, NewSellResponse, NewTransactionResponse
from .exchange import SubCommand
from .signing_authority import SigningAuthority

class SignatureComputation(Enum):
Expand All @@ -26,12 +25,20 @@ class PayloadEncoding(Enum):
BYTES_ARRAY = auto()
BASE_64_URL = auto()


class SubCommand(IntEnum):
SWAP = 0x00
SELL = 0x01
FUND = 0x02


SUBCOMMAND_TO_CURVE = {
SubCommand.SWAP: ec.SECP256K1(),
SubCommand.SELL: ec.SECP256R1(),
SubCommand.FUND: ec.SECP256R1(),
}


def get_partner_curve(sub_command: SubCommand) -> ec.EllipticCurve:
return SUBCOMMAND_TO_CURVE[sub_command]

Expand Down
4 changes: 4 additions & 0 deletions test/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ def handle_lib_call_start_or_stop(backend):

# The USB stack will be reset by the called app
backend.handle_usb_reset()


def int_to_minimally_sized_bytes(n: int) -> bytes:
return n.to_bytes((n.bit_length() + 7) // 8, 'big') or b'\0' # case n is 0

0 comments on commit 6a0604f

Please sign in to comment.