From 8f48d215ae930d1d2955095e3d61319983503a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20H=C3=A9riveaux?= Date: Mon, 11 Dec 2023 11:08:48 +0100 Subject: [PATCH] Minor fixes following code review --- src/main.rs | 28 ++++++++----------- .../boilerplate_command_sender.py | 3 +- tests/test_error_cmd.py | 4 +-- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8f10f4f3..708e38cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,15 +36,7 @@ use handlers::{ get_version::handler_get_version, sign_tx::{handler_sign_tx, TxContext}, }; -use ledger_device_sdk::{ - io::{ApduHeader, Comm, Event, Reply, StatusWords}, - ui::{ - gadgets::clear_screen, - layout::{Layout, Location, StringPlace}, - screen_util::screen_update, - }, -}; -use ledger_secure_sdk_sys::buttons::ButtonEvent; +use ledger_device_sdk::io::{ApduHeader, Comm, Event, Reply, StatusWords}; ledger_device_sdk::set_panic!(ledger_device_sdk::exiting_panic); @@ -75,7 +67,7 @@ pub enum AppSW { TxSignFail = 0xB008, KeyDeriveFail = 0xB009, VersionParsingFail = 0xB00A, - BadLen = StatusWords::BadLen as u16, + WrongApduLength = StatusWords::BadLen as u16, } impl From for Reply { @@ -126,16 +118,20 @@ impl TryFrom for Instruction { // Developer mode / pending review popup // must be cleared with user interaction fn display_pending_review(comm: &mut Comm) { + use ledger_device_sdk::buttons::ButtonEvent::{ + BothButtonsRelease, LeftButtonRelease, RightButtonRelease, + }; + use ledger_device_sdk::ui::gadgets::clear_screen; + use ledger_device_sdk::ui::layout::{Layout, Location, StringPlace}; + use ledger_device_sdk::ui::screen_util::screen_update; + clear_screen(); "Pending Review".place(Location::Middle, Layout::Centered, false); screen_update(); loop { - if let Event::Button( - ButtonEvent::LeftButtonRelease - | ButtonEvent::RightButtonRelease - | ButtonEvent::BothButtonsRelease, - ) = comm.next_event::() + if let Event::Button(LeftButtonRelease | RightButtonRelease | BothButtonsRelease) = + comm.next_event::() { break; } @@ -156,7 +152,7 @@ extern "C" fn sample_main() { if let Event::Command(ins) = ui_menu_main(&mut comm) { match handle_apdu(&mut comm, ins, &mut tx_ctx) { Ok(()) => comm.reply_ok(), - Err(sw) => comm.reply(Reply::from(sw)), + Err(sw) => comm.reply(sw), } } } diff --git a/tests/application_client/boilerplate_command_sender.py b/tests/application_client/boilerplate_command_sender.py index 65aa9dd9..a2f6bef4 100644 --- a/tests/application_client/boilerplate_command_sender.py +++ b/tests/application_client/boilerplate_command_sender.py @@ -33,10 +33,9 @@ class InsType(IntEnum): class Errors(IntEnum): SW_DENY = 0x6985 SW_WRONG_P1P2 = 0x6A86 - SW_WRONG_DATA_LENGTH = 0x6A87 SW_INS_NOT_SUPPORTED = 0x6D00 SW_CLA_NOT_SUPPORTED = 0x6E00 - SW_BAD_LENGTH = 0x6E03 + SW_WRONG_APDU_LENGTH = 0x6E03 SW_WRONG_RESPONSE_LENGTH = 0xB000 SW_DISPLAY_BIP32_PATH_FAIL = 0xB001 SW_DISPLAY_ADDRESS_FAIL = 0xB002 diff --git a/tests/test_error_cmd.py b/tests/test_error_cmd.py index 5f1dab9d..aef68e6f 100644 --- a/tests/test_error_cmd.py +++ b/tests/test_error_cmd.py @@ -38,11 +38,11 @@ def test_wrong_data_length(backend): # APDUs must be at least 4 bytes: CLA, INS, P1, P2. with pytest.raises(ExceptionRAPDU) as e: backend.exchange_raw(bytes.fromhex("E00300")) - assert e.value.status == Errors.SW_BAD_LENGTH + assert e.value.status == Errors.SW_WRONG_APDU_LENGTH # APDUs advertises a too long length with pytest.raises(ExceptionRAPDU) as e: backend.exchange_raw(bytes.fromhex("E003000005")) - assert e.value.status == Errors.SW_BAD_LENGTH + assert e.value.status == Errors.SW_WRONG_APDU_LENGTH # Ensure there is no state confusion when trying wrong APDU sequences