Better Ledger Connection handling (#1802)

Konstantin Ullrich committed Nov 13, 2024 at 10:44 UTC 0adb69d095a80526c02101877b2141ac20e133ab
5 files changed +14 -6
cw_monero/lib/ledger.dart
+1 -5
@@ -47,10 +47,6 @@ void enableLedgerExchange(monero.wallet ptr, LedgerConnection connection) {
47
48 void keepAlive(LedgerConnection connection) {
49 if (connection.connectionType == ConnectionType.ble) {
50 - UniversalBle.onConnectionChange = (String deviceId, bool isConnected) {
51 - print("[Monero] Ledger Disconnected");
52 - _ledgerKeepAlive?.cancel();
53 - };
50 _ledgerKeepAlive = Timer.periodic(Duration(seconds: 10), (_) async {
51 try {
52 UniversalBle.setNotifiable(
@@ -59,7 +55,7 @@ void keepAlive(LedgerConnection connection) {
55 connection.device.deviceInfo.notifyCharacteristicKey,
56 BleInputProperty.notification,
57 );
62 - } catch (_){}
58 + } catch (_) {}
59 });
60 }
61 }
cw_monero/lib/monero_wallet_service.dart
+5
@@ -146,6 +146,11 @@ class MoneroWalletService extends WalletService<
146 password: password);
147 final isValid = wallet.walletAddresses.validate();
148
149 + if (wallet.isHardwareWallet) {
150 + wallet.setLedgerConnection(gLedger!);
151 + gLedger = null;
152 + }
153 +
154 if (!isValid) {
155 await restoreOrResetWalletFiles(name);
156 wallet.close(shouldCleanup: false);
lib/monero/cw_monero.dart
+4
@@ -402,6 +402,10 @@ class CWMonero extends Monero {
402 moneroWallet.setLedgerConnection(connection);
403 }
404
405 + void resetLedgerConnection() {
406 + disableLedgerExchange();
407 + }
408 +
409 @override
410 void setGlobalLedgerConnection(ledger.LedgerConnection connection) {
411 gLedger = connection;
lib/view_model/hardware_wallet/ledger_view_model.dart
+3 -1
@@ -98,7 +98,9 @@ abstract class LedgerViewModelBase with Store {
98 print('Ledger Device State Changed: $event');
99 if (event == sdk.BleConnectionState.disconnected) {
100 _connection = null;
101 - _connectionChangeListener?.cancel();
101 + if (type == WalletType.monero) {
102 + monero!.resetLedgerConnection();
103 + }
104 }
105 });
106 }
tool/configure.dart
+1
@@ -415,6 +415,7 @@ abstract class Monero {
415 WalletService createMoneroWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);
416 Map<String, String> pendingTransactionInfo(Object transaction);
417 void setLedgerConnection(Object wallet, ledger.LedgerConnection connection);
418 + void resetLedgerConnection();
419 void setGlobalLedgerConnection(ledger.LedgerConnection connection);
420 }
421