Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions cmd/crates/stellar-ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand All @@ -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<T: Exchange> {
Expand Down Expand Up @@ -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(),
Expand All @@ -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<T> Blob for LedgerSigner<T>
where
Expand Down Expand Up @@ -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:?}");
Expand Down
3 changes: 2 additions & 1 deletion cmd/crates/stellar-ledger/tests/test/emulator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading