From 179dd405c578e359ebac9a9d30a30e969c8d7096 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Tue, 14 May 2024 16:03:45 -0700 Subject: [PATCH] fixes when switching wallets --- cw_lightning/lib/lightning_wallet.dart | 34 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/cw_lightning/lib/lightning_wallet.dart b/cw_lightning/lib/lightning_wallet.dart index d1e81f366..7eedceac6 100644 --- a/cw_lightning/lib/lightning_wallet.dart +++ b/cw_lightning/lib/lightning_wallet.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -92,6 +93,8 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { } late final ObservableMap _balance; + StreamSubscription>? _paymentsSub; + StreamSubscription? _nodeStateSub; @override @computed @@ -177,14 +180,16 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { ); String workingDir = (await getApplicationDocumentsDirectory()).path; - workingDir = "$workingDir/wallets/lightning/${walletInfo.name}/breez/"; + workingDir = "$workingDir/wallets/lightning/${walletInfo.address}/breez/"; new Directory(workingDir).createSync(recursive: true); breezConfig = breezConfig.copyWith(workingDir: workingDir); // disconnect if already connected try { await sdk.disconnect(); - } catch (_) {} + } catch (e, s) { + print("ERROR disconnecting from Breez: $e\n$s"); + } try { await sdk.connect( @@ -197,7 +202,7 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { print("Error connecting to Breez: $e"); } - sdk.nodeStateStream.listen((event) { + _nodeStateSub = sdk.nodeStateStream.listen((event) { if (event == null) return; _balance[CryptoCurrency.btcln] = LightningBalance( confirmed: event.maxPayableMsat ~/ 1000, @@ -206,7 +211,7 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { ); }); - sdk.paymentsStream.listen((payments) { + _paymentsSub = sdk.paymentsStream.listen((payments) { _isTransactionUpdating = true; final txs = convertToTxInfo(payments); transactionHistory.addMany(txs); @@ -225,6 +230,13 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { print("initialized breez: ${(await sdk.isInitialized())}"); } + Future stopBreez() async { + final sdk = await BreezSDK(); + await sdk.disconnect(); + await _nodeStateSub?.cancel(); + await _paymentsSub?.cancel(); + } + @action @override Future startSync() async { @@ -344,4 +356,18 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { String get seed => mnemonic; Future makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type); + + @override + Future close() async { + try { + await electrumClient.close(); + } catch (_) {} + try { + print("stopping breez"); + await stopBreez(); + print("stopped breez @@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + } catch (e, s) { + print("Error stopping breez: $e\n$s"); + } + } }