From 1312bd00de6146875570829f7c304ee4044fbe08 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Mon, 4 Aug 2025 03:47:52 +0300 Subject: [PATCH] handle more ledger cases [skip ci] --- cw_monero/lib/monero_wallet_service.dart | 1 - lib/entities/load_current_wallet.dart | 13 +++---------- lib/reactions/on_authentication_state_change.dart | 11 +++-------- lib/utils/exception_handler.dart | 1 + .../hardware_wallet/ledger_view_model.dart | 9 ++++++++- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/cw_monero/lib/monero_wallet_service.dart b/cw_monero/lib/monero_wallet_service.dart index d2e393213..6fc827068 100644 --- a/cw_monero/lib/monero_wallet_service.dart +++ b/cw_monero/lib/monero_wallet_service.dart @@ -22,7 +22,6 @@ import 'package:cw_monero/ledger.dart'; import 'package:cw_monero/monero_wallet.dart'; import 'package:hive/hive.dart'; import 'package:ledger_flutter_plus/ledger_flutter_plus.dart'; -import 'package:monero/src/monero.dart' as m; import 'package:monero/monero.dart' as monero; import 'package:polyseed/polyseed.dart'; diff --git a/lib/entities/load_current_wallet.dart b/lib/entities/load_current_wallet.dart index a421168d0..251fa88e2 100644 --- a/lib/entities/load_current_wallet.dart +++ b/lib/entities/load_current_wallet.dart @@ -7,12 +7,8 @@ import 'package:cake_wallet/core/wallet_loading_service.dart'; Future loadCurrentWallet({String? password}) async { final appStore = getIt.get(); - final name = getIt - .get() - .getString(PreferencesKey.currentWalletName); - final typeRaw = - getIt.get().getInt(PreferencesKey.currentWalletType) ?? - 0; + final name = getIt.get().getString(PreferencesKey.currentWalletName); + final typeRaw = getIt.get().getInt(PreferencesKey.currentWalletType) ?? 0; if (name == null) { throw Exception('Incorrect current wallet name: $name'); @@ -20,9 +16,6 @@ Future loadCurrentWallet({String? password}) async { final type = deserializeFromInt(typeRaw); final walletLoadingService = getIt.get(); - final wallet = await walletLoadingService.load( - type, - name, - password: password); + final wallet = await walletLoadingService.load(type, name, password: password); await appStore.changeCurrentWallet(wallet); } diff --git a/lib/reactions/on_authentication_state_change.dart b/lib/reactions/on_authentication_state_change.dart index 8cde67731..16e8421e0 100644 --- a/lib/reactions/on_authentication_state_change.dart +++ b/lib/reactions/on_authentication_state_change.dart @@ -78,18 +78,13 @@ void startAuthenticationStateChange( await loadCurrentWallet(); tryOpening = false; } on Exception catch (e) { - final errorCode = RegExp(r'0x\S*?(?= )').firstMatch( - e.toString()); - - final errorMessage = ledgerVM.interpretErrorCode( - errorCode?.group(0).toString().replaceAll("0x", "") ?? - ""); - if (errorMessage != null) { + final ledgerErrorMessage = ledgerVM.interpretErrorCode(e.toString()); + if (ledgerErrorMessage != null) { await showPopUp( context: context, builder: (context) => AlertWithTwoActions( alertTitle: "Ledger Error", - alertContent: errorMessage, + alertContent: ledgerErrorMessage, leftButtonText: S.of(context).try_again, alertBarrierDismissible: false, actionLeftButton: () => Navigator.of(context).pop(), diff --git a/lib/utils/exception_handler.dart b/lib/utils/exception_handler.dart index bb2e4c14e..0950b4ffd 100644 --- a/lib/utils/exception_handler.dart +++ b/lib/utils/exception_handler.dart @@ -191,6 +191,7 @@ class ExceptionHandler { 'PlatformException(133, Failed to write: (Unknown Error: 133), null, null)', 'PlatformException(IllegalArgument, Unknown deviceId:', 'ServiceNotSupportedException(ConnectionType.ble, Required service not supported. Write characteristic: false, Notify characteristic: false)', + 'Make sure no other program is communicating with the Ledger', 'Exception: 6e01', // Wrong App 'Exception: 6d02', 'Exception: 6511', diff --git a/lib/view_model/hardware_wallet/ledger_view_model.dart b/lib/view_model/hardware_wallet/ledger_view_model.dart index 1bc00fb02..6012e1873 100644 --- a/lib/view_model/hardware_wallet/ledger_view_model.dart +++ b/lib/view_model/hardware_wallet/ledger_view_model.dart @@ -173,7 +173,14 @@ abstract class LedgerViewModelBase with Store { } } - String? interpretErrorCode(String errorCode) { + String? interpretErrorCode(String error) { + if (error.contains("Make sure no other program is communicating with the Ledger")) { + return error; + } + + var errorRegex = RegExp(r'0x\S*?(?= )').firstMatch(error.toString()); + + String errorCode = errorRegex?.group(0).toString().replaceAll("0x", "") ?? ""; if (errorCode.contains("6985")) { return S.current.ledger_error_tx_rejected_by_user; } else if (errorCode.contains("5515")) {