diff --git a/cmd/crates/stellar-ledger/src/lib.rs b/cmd/crates/stellar-ledger/src/lib.rs index 1cfc5d1333..5f9004a9c5 100644 --- a/cmd/crates/stellar-ledger/src/lib.rs +++ b/cmd/crates/stellar-ledger/src/lib.rs @@ -62,7 +62,10 @@ pub enum Error { #[error("Error occurred while initializing Ledger HID transport: {0}")] LedgerHidError(#[from] LedgerHIDError), - #[error("Error with ADPU exchange with Ledger device: {0}")] + #[error("Make sure the ledger device is unlocked: {0}")] + DeviceLocked(String), + + #[error("Error exchanging with Ledger device: {0}")] APDUExchangeError(String), #[error("Error occurred while exchanging with Ledger device: {0}")] @@ -76,6 +79,15 @@ pub enum Error { #[error(transparent)] DecodeError(#[from] DecodeError), + + #[error("Blind signing not enabled for Stellar app on the Ledger device: {0}")] + BlindSigningModeNotEnabled(String), + + #[error("Stellar app is not opened on the Ledger device. Open the app and try again. {0}")] + StellarAppNotOpen(String), + + #[error("The tx was rejected by the user. {0}")] + TxRejectedByUser(String), } pub struct LedgerSigner { @@ -243,8 +255,7 @@ where } let retcode = response.retcode(); - let error_string = format!("Ledger APDU retcode: 0x{retcode:X}"); - Err(Error::APDUExchangeError(error_string)) + Err(handle_error(retcode)) } Err(_err) => Err(Error::LedgerConnectionError( "Error connecting to ledger device".to_string(), @@ -253,6 +264,17 @@ where } } +fn handle_error(retcode: u16) -> Error { + let error_string = format!("Ledger APDU retcode: 0x{retcode:X}"); + match retcode { + 0x6C66 => Error::BlindSigningModeNotEnabled(error_string), + 0x6511 => Error::StellarAppNotOpen(error_string), + 0x6985 => Error::TxRejectedByUser(error_string), + 0x5515 => Error::DeviceLocked(error_string), + _ => Error::APDUExchangeError(error_string), + } +} + #[async_trait::async_trait] impl Blob for LedgerSigner where @@ -454,7 +476,8 @@ mod test { let test_hash = b"3389e9f0f1a65f19736cacf544c2e825313e8447f569233bb8db39aa607c8889"; let err = ledger.sign_blob(&path.into(), test_hash).await.unwrap_err(); - if let Error::APDUExchangeError(msg) = err { + + if let Error::BlindSigningModeNotEnabled(msg) = err { assert_eq!(msg, "Ledger APDU retcode: 0x6C66"); } else { panic!("Unexpected error: {err:?}"); diff --git a/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs index 880ac95ff7..b45f7b4270 100644 --- a/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs +++ b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs @@ -165,7 +165,8 @@ async fn test_sign_tx_hash_when_hash_signing_is_not_enabled(ledger_device_model: let test_hash = b"313e8447f569233bb8db39aa607c8889"; let result = ledger.sign_transaction_hash(path, test_hash).await; - if let Err(Error::APDUExchangeError(msg)) = result { + + if let Err(Error::BlindSigningModeNotEnabled(msg)) = result { assert_eq!(msg, "Ledger APDU retcode: 0x6C66"); // this error code is SW_TX_HASH_SIGNING_MODE_NOT_ENABLED https://github.com/LedgerHQ/app-stellar/blob/develop/docs/COMMANDS.md } else {