From 362c0bd2314c170f9aff3e8865b2ca1e9843c053 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Thu, 23 May 2024 12:20:22 -0700 Subject: [PATCH] breez fixes --- cw_bitcoin/lib/electrum_wallet.dart | 2 +- cw_core/lib/wallet_base.dart | 2 +- cw_evm/lib/evm_chain_wallet.dart | 2 +- cw_lightning/lib/lightning_wallet.dart | 15 +++++++++------ cw_monero/lib/monero_wallet.dart | 2 +- cw_nano/lib/nano_wallet.dart | 2 +- cw_solana/lib/solana_wallet.dart | 2 +- cw_tron/lib/tron_wallet.dart | 2 +- lib/core/wallet_creation_service.dart | 1 + lib/store/app_store.dart | 4 +++- 10 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 94145c1d7..7142c1064 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -831,7 +831,7 @@ abstract class ElectrumWalletBase Future rescan({required int height}) async => throw UnimplementedError(); @override - Future close() async { + Future close({bool? switchingToSameWalletType}) async { try { await electrumClient.close(); } catch (_) {} diff --git a/cw_core/lib/wallet_base.dart b/cw_core/lib/wallet_base.dart index 709462fa1..b83c1311b 100644 --- a/cw_core/lib/wallet_base.dart +++ b/cw_core/lib/wallet_base.dart @@ -80,7 +80,7 @@ abstract class WalletBase rescan({required int height}); - void close(); + void close({bool? switchingToSameWalletType}); Future changePassword(String password); diff --git a/cw_evm/lib/evm_chain_wallet.dart b/cw_evm/lib/evm_chain_wallet.dart index 56b58d400..8fef99518 100644 --- a/cw_evm/lib/evm_chain_wallet.dart +++ b/cw_evm/lib/evm_chain_wallet.dart @@ -177,7 +177,7 @@ abstract class EVMChainWalletBase } @override - void close() { + void close({bool? switchingToSameWalletType}) { _client.stop(); _transactionsUpdateTimer?.cancel(); } diff --git a/cw_lightning/lib/lightning_wallet.dart b/cw_lightning/lib/lightning_wallet.dart index e0ddd2df1..9624509ef 100644 --- a/cw_lightning/lib/lightning_wallet.dart +++ b/cw_lightning/lib/lightning_wallet.dart @@ -233,10 +233,12 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { print("initialized breez: ${(await sdk.isInitialized())}"); } - Future stopBreez() async { - final sdk = await BreezSDK(); - if (await sdk.isInitialized()) { - await sdk.disconnect(); + Future stopBreez(bool disconnect) async { + if (disconnect) { + final sdk = await BreezSDK(); + if (await sdk.isInitialized()) { + await sdk.disconnect(); + } } await _nodeStateSub?.cancel(); await _paymentsSub?.cancel(); @@ -363,12 +365,13 @@ abstract class LightningWalletBase extends ElectrumWallet with Store { Future makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type); @override - Future close() async { + Future close({bool? switchingToSameWalletType}) async { try { await electrumClient.close(); } catch (_) {} try { - await stopBreez(); + bool shouldDisconnect = switchingToSameWalletType == null || !switchingToSameWalletType; + await stopBreez(shouldDisconnect); } catch (e, s) { print("Error stopping breez: $e\n$s"); } diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index c270bb113..aa2747112 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -142,7 +142,7 @@ abstract class MoneroWalletBase Future? updateBalance() => null; @override - void close() { + void close({bool? switchingToSameWalletType}) { _listener?.stop(); _onAccountChangeReaction?.reaction.dispose(); _autoSaveTimer?.cancel(); diff --git a/cw_nano/lib/nano_wallet.dart b/cw_nano/lib/nano_wallet.dart index 5efe3006d..50f2d999e 100644 --- a/cw_nano/lib/nano_wallet.dart +++ b/cw_nano/lib/nano_wallet.dart @@ -138,7 +138,7 @@ abstract class NanoWalletBase } @override - void close() { + void close({bool? switchingToSameWalletType}) { _client.stop(); _receiveTimer?.cancel(); } diff --git a/cw_solana/lib/solana_wallet.dart b/cw_solana/lib/solana_wallet.dart index 401968698..5aa6032aa 100644 --- a/cw_solana/lib/solana_wallet.dart +++ b/cw_solana/lib/solana_wallet.dart @@ -165,7 +165,7 @@ abstract class SolanaWalletBase Future changePassword(String password) => throw UnimplementedError("changePassword"); @override - void close() { + void close({bool? switchingToSameWalletType}) { _client.stop(); _transactionsUpdateTimer?.cancel(); } diff --git a/cw_tron/lib/tron_wallet.dart b/cw_tron/lib/tron_wallet.dart index 96f92e450..efdf9571a 100644 --- a/cw_tron/lib/tron_wallet.dart +++ b/cw_tron/lib/tron_wallet.dart @@ -186,7 +186,7 @@ abstract class TronWalletBase } @override - void close() { + void close({bool? switchingToSameWalletType}) { _transactionsUpdateTimer?.cancel(); } diff --git a/lib/core/wallet_creation_service.dart b/lib/core/wallet_creation_service.dart index 1fa50a6be..81eeac9e5 100644 --- a/lib/core/wallet_creation_service.dart +++ b/lib/core/wallet_creation_service.dart @@ -80,6 +80,7 @@ class WalletCreationService { case WalletType.solana: case WalletType.tron: return true; + case WalletType.lightning: case WalletType.monero: case WalletType.none: case WalletType.bitcoin: diff --git a/lib/store/app_store.dart b/lib/store/app_store.dart index 7d61abfc5..703282d11 100644 --- a/lib/store/app_store.dart +++ b/lib/store/app_store.dart @@ -3,6 +3,7 @@ import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/reactions/wallet_connect.dart'; import 'package:cake_wallet/utils/exception_handler.dart'; import 'package:cw_core/transaction_info.dart'; +import 'package:cw_core/wallet_type.dart'; import 'package:mobx/mobx.dart'; import 'package:cw_core/balance.dart'; import 'package:cw_core/wallet_base.dart'; @@ -37,7 +38,8 @@ abstract class AppStoreBase with Store { @action Future changeCurrentWallet( WalletBase, TransactionInfo> wallet) async { - this.wallet?.close(); + bool switchingToSameWalletType = this.wallet?.type == wallet.type; + this.wallet?.close(switchingToSameWalletType: switchingToSameWalletType); this.wallet = wallet; this.wallet!.setExceptionHandler(ExceptionHandler.onError);