Skip to content

Commit

Permalink
Better Ledger Connection handling (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinullrich authored Nov 13, 2024
1 parent c469857 commit 0adb69d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
6 changes: 1 addition & 5 deletions cw_monero/lib/ledger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ void enableLedgerExchange(monero.wallet ptr, LedgerConnection connection) {

void keepAlive(LedgerConnection connection) {
if (connection.connectionType == ConnectionType.ble) {
UniversalBle.onConnectionChange = (String deviceId, bool isConnected) {
print("[Monero] Ledger Disconnected");
_ledgerKeepAlive?.cancel();
};
_ledgerKeepAlive = Timer.periodic(Duration(seconds: 10), (_) async {
try {
UniversalBle.setNotifiable(
Expand All @@ -59,7 +55,7 @@ void keepAlive(LedgerConnection connection) {
connection.device.deviceInfo.notifyCharacteristicKey,
BleInputProperty.notification,
);
} catch (_){}
} catch (_) {}
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions cw_monero/lib/monero_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ class MoneroWalletService extends WalletService<
password: password);
final isValid = wallet.walletAddresses.validate();

if (wallet.isHardwareWallet) {
wallet.setLedgerConnection(gLedger!);
gLedger = null;
}

if (!isValid) {
await restoreOrResetWalletFiles(name);
wallet.close(shouldCleanup: false);
Expand Down
4 changes: 4 additions & 0 deletions lib/monero/cw_monero.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ class CWMonero extends Monero {
moneroWallet.setLedgerConnection(connection);
}

void resetLedgerConnection() {
disableLedgerExchange();
}

@override
void setGlobalLedgerConnection(ledger.LedgerConnection connection) {
gLedger = connection;
Expand Down
4 changes: 3 additions & 1 deletion lib/view_model/hardware_wallet/ledger_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ abstract class LedgerViewModelBase with Store {
print('Ledger Device State Changed: $event');
if (event == sdk.BleConnectionState.disconnected) {
_connection = null;
_connectionChangeListener?.cancel();
if (type == WalletType.monero) {
monero!.resetLedgerConnection();
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions tool/configure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ abstract class Monero {
WalletService createMoneroWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);
Map<String, String> pendingTransactionInfo(Object transaction);
void setLedgerConnection(Object wallet, ledger.LedgerConnection connection);
void resetLedgerConnection();
void setGlobalLedgerConnection(ledger.LedgerConnection connection);
}
Expand Down

0 comments on commit 0adb69d

Please sign in to comment.