From ab5849c57df2af3e8f23ba4b4502aa45c3720eee Mon Sep 17 00:00:00 2001 From: Omar Hatem Date: Sat, 12 Jul 2025 19:58:11 +0300 Subject: [PATCH] Make SP scanning, wallet-specific (#2383) --- cw_bitcoin/lib/bitcoin_wallet.dart | 3 +-- cw_bitcoin/lib/bitcoin_wallet_service.dart | 6 +----- cw_bitcoin/lib/electrum_wallet.dart | 1 + cw_bitcoin/lib/litecoin_wallet.dart | 1 - cw_bitcoin/lib/litecoin_wallet_service.dart | 6 +----- lib/bitcoin/cw_bitcoin.dart | 16 ++++++++++---- lib/core/address_validator.dart | 2 +- lib/di.dart | 3 --- lib/entities/preferences_key.dart | 1 - lib/store/settings_store.dart | 14 ------------- lib/view_model/buy/buy_sell_view_model.dart | 21 ++++++++++--------- .../silent_payments_settings_view_model.dart | 4 ++-- tool/configure.dart | 6 ++++-- 13 files changed, 34 insertions(+), 50 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_wallet.dart b/cw_bitcoin/lib/bitcoin_wallet.dart index 9231022f6..d286df424 100644 --- a/cw_bitcoin/lib/bitcoin_wallet.dart +++ b/cw_bitcoin/lib/bitcoin_wallet.dart @@ -168,7 +168,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { required Box payjoinBox, required String password, required EncryptionFileUtils encryptionFileUtils, - required bool alwaysScan, }) async { final network = walletInfo.network != null ? BasedUtxoNetwork.fromName(walletInfo.network!) @@ -252,7 +251,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { initialChangeAddressIndex: snp?.changeAddressIndex, addressPageType: snp?.addressPageType, networkParam: network, - alwaysScan: alwaysScan, + alwaysScan: snp?.alwaysScan, payjoinBox: payjoinBox); } diff --git a/cw_bitcoin/lib/bitcoin_wallet_service.dart b/cw_bitcoin/lib/bitcoin_wallet_service.dart index 317b25bcd..33113d7ef 100644 --- a/cw_bitcoin/lib/bitcoin_wallet_service.dart +++ b/cw_bitcoin/lib/bitcoin_wallet_service.dart @@ -23,12 +23,11 @@ class BitcoinWalletService extends WalletService< BitcoinRestoreWalletFromWIFCredentials, BitcoinRestoreWalletFromHardware> { BitcoinWalletService(this.walletInfoSource, this.unspentCoinsInfoSource, - this.payjoinSessionSource, this.alwaysScan, this.isDirect); + this.payjoinSessionSource, this.isDirect); final Box walletInfoSource; final Box unspentCoinsInfoSource; final Box payjoinSessionSource; - final bool alwaysScan; final bool isDirect; @override @@ -84,7 +83,6 @@ class BitcoinWalletService extends WalletService< walletInfo: walletInfo, unspentCoinsInfo: unspentCoinsInfoSource, payjoinBox: payjoinSessionSource, - alwaysScan: alwaysScan, encryptionFileUtils: encryptionFileUtilsFor(isDirect), ); await wallet.init(); @@ -98,7 +96,6 @@ class BitcoinWalletService extends WalletService< walletInfo: walletInfo, unspentCoinsInfo: unspentCoinsInfoSource, payjoinBox: payjoinSessionSource, - alwaysScan: alwaysScan, encryptionFileUtils: encryptionFileUtilsFor(isDirect), ); await wallet.init(); @@ -133,7 +130,6 @@ class BitcoinWalletService extends WalletService< walletInfo: currentWalletInfo, unspentCoinsInfo: unspentCoinsInfoSource, payjoinBox: payjoinSessionSource, - alwaysScan: alwaysScan, encryptionFileUtils: encryptionFileUtilsFor(isDirect), ); diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 5938cc00b..bf20cac80 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -150,6 +150,7 @@ abstract class ElectrumWalletBase } } + @observable bool? alwaysScan; final Bip32Slip10Secp256k1 accountHD; diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index 08c56c600..d3ba83a61 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -210,7 +210,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { required WalletInfo walletInfo, required Box unspentCoinsInfo, required String password, - required bool alwaysScan, required EncryptionFileUtils encryptionFileUtils, }) async { final hasKeysFile = await WalletKeysFile.hasKeysFile(name, walletInfo.type); diff --git a/cw_bitcoin/lib/litecoin_wallet_service.dart b/cw_bitcoin/lib/litecoin_wallet_service.dart index 89ae384d4..84056225e 100644 --- a/cw_bitcoin/lib/litecoin_wallet_service.dart +++ b/cw_bitcoin/lib/litecoin_wallet_service.dart @@ -23,11 +23,10 @@ class LitecoinWalletService extends WalletService< BitcoinRestoreWalletFromWIFCredentials, BitcoinRestoreWalletFromHardware> { LitecoinWalletService( - this.walletInfoSource, this.unspentCoinsInfoSource, this.alwaysScan, this.isDirect); + this.walletInfoSource, this.unspentCoinsInfoSource, this.isDirect); final Box walletInfoSource; final Box unspentCoinsInfoSource; - final bool alwaysScan; final bool isDirect; @override @@ -78,7 +77,6 @@ class LitecoinWalletService extends WalletService< name: name, walletInfo: walletInfo, unspentCoinsInfo: unspentCoinsInfoSource, - alwaysScan: alwaysScan, encryptionFileUtils: encryptionFileUtilsFor(isDirect), ); await wallet.init(); @@ -91,7 +89,6 @@ class LitecoinWalletService extends WalletService< name: name, walletInfo: walletInfo, unspentCoinsInfo: unspentCoinsInfoSource, - alwaysScan: alwaysScan, encryptionFileUtils: encryptionFileUtilsFor(isDirect), ); await wallet.init(); @@ -146,7 +143,6 @@ class LitecoinWalletService extends WalletService< name: currentName, walletInfo: currentWalletInfo, unspentCoinsInfo: unspentCoinsInfoSource, - alwaysScan: alwaysScan, encryptionFileUtils: encryptionFileUtilsFor(isDirect), ); diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index 131bc3a02..630474f22 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -231,15 +231,14 @@ class CWBitcoin extends Bitcoin { Box walletInfoSource, Box unspentCoinSource, Box payjoinSessionSource, - bool alwaysScan, bool isDirect) { return BitcoinWalletService(walletInfoSource, unspentCoinSource, - payjoinSessionSource, alwaysScan, isDirect); + payjoinSessionSource, isDirect); } WalletService createLitecoinWalletService(Box walletInfoSource, - Box unspentCoinSource, bool alwaysScan, bool isDirect) { - return LitecoinWalletService(walletInfoSource, unspentCoinSource, alwaysScan, isDirect); + Box unspentCoinSource, bool isDirect) { + return LitecoinWalletService(walletInfoSource, unspentCoinSource, isDirect); } @override @@ -580,6 +579,15 @@ class CWBitcoin extends Bitcoin { bitcoinWallet.setSilentPaymentsScanning(active); } + Future setIsAlwaysScanningSP(Object wallet, bool active) async { + final bitcoinWallet = wallet as ElectrumWallet; + bitcoinWallet.alwaysScan = active; + bitcoinWallet.save(); + } + + @computed + bool getIsAlwaysScanningSP(Object wallet) => (wallet as ElectrumWallet).alwaysScan ?? false; + @override bool isTestnet(Object wallet) { final bitcoinWallet = wallet as ElectrumWallet; diff --git a/lib/core/address_validator.dart b/lib/core/address_validator.dart index ab2464069..044741ce1 100644 --- a/lib/core/address_validator.dart +++ b/lib/core/address_validator.dart @@ -28,7 +28,7 @@ class AddressValidator extends TextValidator { pattern: getPattern(type, isTestnet: isTestnet), length: getLength(type)); - static String getPattern(CryptoCurrency type, {required bool isTestnet}) { + static String getPattern(CryptoCurrency type, {bool isTestnet = false}) { var pattern = ""; if (type is Erc20Token) { pattern = '0x[0-9a-zA-Z]+'; diff --git a/lib/di.dart b/lib/di.dart index 6a7c118ff..8aa6b62ce 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -76,7 +76,6 @@ import 'package:cake_wallet/entities/qr_view_data.dart'; import 'package:cake_wallet/entities/template.dart'; import 'package:cake_wallet/entities/transaction_description.dart'; import 'package:cake_wallet/ethereum/ethereum.dart'; -import 'package:cake_wallet/cake_pay/src/models/cake_pay_card.dart'; import 'package:cake_wallet/exchange/exchange_template.dart'; import 'package:cake_wallet/exchange/trade.dart'; import 'package:cake_wallet/monero/monero.dart'; @@ -1119,14 +1118,12 @@ Future setup({ _walletInfoSource, _unspentCoinsInfoSource, _payjoinSessionSource, - getIt.get().silentPaymentsAlwaysScan, SettingsStoreBase.walletPasswordDirectInput, ); case WalletType.litecoin: return bitcoin!.createLitecoinWalletService( _walletInfoSource, _unspentCoinsInfoSource, - getIt.get().mwebAlwaysScan, SettingsStoreBase.walletPasswordDirectInput, ); case WalletType.ethereum: diff --git a/lib/entities/preferences_key.dart b/lib/entities/preferences_key.dart index cda299ec2..aca8f79b4 100644 --- a/lib/entities/preferences_key.dart +++ b/lib/entities/preferences_key.dart @@ -54,7 +54,6 @@ class PreferencesKey { static const decredTransactionPriority = 'current_fee_priority_decred'; static const customBitcoinFeeRate = 'custom_electrum_fee_rate'; static const silentPaymentsCardDisplay = 'silentPaymentsCardDisplay'; - static const silentPaymentsAlwaysScan = 'silentPaymentsAlwaysScan'; static const mwebCardDisplay = 'mwebCardDisplay'; static const mwebEnabled = 'mwebEnabled'; static const hasEnabledMwebBefore = 'hasEnabledMwebBefore'; diff --git a/lib/store/settings_store.dart b/lib/store/settings_store.dart index a714b72d1..219ef962d 100644 --- a/lib/store/settings_store.dart +++ b/lib/store/settings_store.dart @@ -123,7 +123,6 @@ abstract class SettingsStoreBase with Store { required this.showPayjoinCard, required this.customBitcoinFeeRate, required this.silentPaymentsCardDisplay, - required this.silentPaymentsAlwaysScan, required this.mwebAlwaysScan, required this.mwebCardDisplay, required this.mwebEnabled, @@ -592,11 +591,6 @@ abstract class SettingsStoreBase with Store { PreferencesKey.silentPaymentsCardDisplay, silentPaymentsCardDisplay); }); - reaction( - (_) => silentPaymentsAlwaysScan, - (bool silentPaymentsAlwaysScan) => _sharedPreferences.setBool( - PreferencesKey.silentPaymentsAlwaysScan, silentPaymentsAlwaysScan)); - reaction( (_) => mwebAlwaysScan, (bool mwebAlwaysScan) => @@ -850,9 +844,6 @@ abstract class SettingsStoreBase with Store { @observable bool silentPaymentsCardDisplay; - @observable - bool silentPaymentsAlwaysScan; - @observable bool mwebAlwaysScan; @@ -1036,8 +1027,6 @@ abstract class SettingsStoreBase with Store { final customBitcoinFeeRate = sharedPreferences.getInt(PreferencesKey.customBitcoinFeeRate) ?? 1; final silentPaymentsCardDisplay = sharedPreferences.getBool(PreferencesKey.silentPaymentsCardDisplay) ?? true; - final silentPaymentsAlwaysScan = - sharedPreferences.getBool(PreferencesKey.silentPaymentsAlwaysScan) ?? false; final mwebAlwaysScan = sharedPreferences.getBool(PreferencesKey.mwebAlwaysScan) ?? false; final mwebCardDisplay = sharedPreferences.getBool(PreferencesKey.mwebCardDisplay) ?? true; final mwebEnabled = sharedPreferences.getBool(PreferencesKey.mwebEnabled) ?? false; @@ -1341,7 +1330,6 @@ abstract class SettingsStoreBase with Store { showPayjoinCard: showPayjoinCard, customBitcoinFeeRate: customBitcoinFeeRate, silentPaymentsCardDisplay: silentPaymentsCardDisplay, - silentPaymentsAlwaysScan: silentPaymentsAlwaysScan, mwebAlwaysScan: mwebAlwaysScan, mwebCardDisplay: mwebCardDisplay, mwebEnabled: mwebEnabled, @@ -1525,8 +1513,6 @@ abstract class SettingsStoreBase with Store { customBitcoinFeeRate = sharedPreferences.getInt(PreferencesKey.customBitcoinFeeRate) ?? 1; silentPaymentsCardDisplay = sharedPreferences.getBool(PreferencesKey.silentPaymentsCardDisplay) ?? true; - silentPaymentsAlwaysScan = - sharedPreferences.getBool(PreferencesKey.silentPaymentsAlwaysScan) ?? false; mwebAlwaysScan = sharedPreferences.getBool(PreferencesKey.mwebAlwaysScan) ?? false; mwebCardDisplay = sharedPreferences.getBool(PreferencesKey.mwebCardDisplay) ?? true; mwebEnabled = sharedPreferences.getBool(PreferencesKey.mwebEnabled) ?? false; diff --git a/lib/view_model/buy/buy_sell_view_model.dart b/lib/view_model/buy/buy_sell_view_model.dart index fbc091ef3..1874fe39e 100644 --- a/lib/view_model/buy/buy_sell_view_model.dart +++ b/lib/view_model/buy/buy_sell_view_model.dart @@ -12,7 +12,6 @@ import 'package:cake_wallet/entities/provider_types.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/store/app_store.dart'; -import 'package:cake_wallet/themes/core/material_base_theme.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:flutter/cupertino.dart'; import 'package:intl/intl.dart'; @@ -89,7 +88,7 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S return isBuyAction ? formattedFiatAmount : formattedCryptoAmount; } - AppStore _appStore; + final AppStore _appStore; Quote? bestRateQuote; @@ -315,14 +314,16 @@ abstract class BuySellViewModelBase extends WalletChangeListenerViewModel with S ...outOfLimitQuotes, ]); - await Navigator.of(context).pushNamed( - Routes.buyOptionsPage, - arguments: [ - updatedQuoteOptions, - changeOption, - launchTrade, - ], - ).then((value) => calculateBestRate()); + if (context.mounted) { + await Navigator.of(context).pushNamed( + Routes.buyOptionsPage, + arguments: [ + updatedQuoteOptions, + changeOption, + launchTrade, + ], + ).then((value) => calculateBestRate()); + } } void _onPairChange() { diff --git a/lib/view_model/settings/silent_payments_settings_view_model.dart b/lib/view_model/settings/silent_payments_settings_view_model.dart index 5d20230d2..e39b18c5d 100644 --- a/lib/view_model/settings/silent_payments_settings_view_model.dart +++ b/lib/view_model/settings/silent_payments_settings_view_model.dart @@ -18,7 +18,7 @@ abstract class SilentPaymentsSettingsViewModelBase with Store { bool get silentPaymentsCardDisplay => _settingsStore.silentPaymentsCardDisplay; @computed - bool get silentPaymentsAlwaysScan => _settingsStore.silentPaymentsAlwaysScan; + bool get silentPaymentsAlwaysScan => bitcoin!.getIsAlwaysScanningSP(_wallet); @action void setSilentPaymentsCardDisplay(bool value) { @@ -27,7 +27,7 @@ abstract class SilentPaymentsSettingsViewModelBase with Store { @action void setSilentPaymentsAlwaysScan(bool value) { - _settingsStore.silentPaymentsAlwaysScan = value; + bitcoin!.setIsAlwaysScanningSP(_wallet, value); if (value) bitcoin!.setScanningActive(_wallet, true); } } diff --git a/tool/configure.dart b/tool/configure.dart index ca56d1fce..dc3f594cf 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -192,8 +192,8 @@ abstract class Bitcoin { List getUnspents(Object wallet, {UnspentCoinType coinTypeToSpendFrom = UnspentCoinType.any}); Future updateUnspents(Object wallet); WalletService createBitcoinWalletService( - Box walletInfoSource, Box unspentCoinSource, Box payjoinSessionSource, bool alwaysScan, bool isDirect); - WalletService createLitecoinWalletService(Box walletInfoSource, Box unspentCoinSource, bool alwaysScan, bool isDirect); + Box walletInfoSource, Box unspentCoinSource, Box payjoinSessionSource, bool isDirect); + WalletService createLitecoinWalletService(Box walletInfoSource, Box unspentCoinSource, bool isDirect); TransactionPriority getBitcoinTransactionPriorityMedium(); TransactionPriority getBitcoinTransactionPriorityCustom(); TransactionPriority getLitecoinTransactionPriorityMedium(); @@ -216,6 +216,8 @@ abstract class Bitcoin { bool hasTaprootInput(PendingTransaction pendingTransaction); bool getScanningActive(Object wallet); Future setScanningActive(Object wallet, bool active); + Future setIsAlwaysScanningSP(Object wallet, bool active); + bool getIsAlwaysScanningSP(Object wallet); bool isTestnet(Object wallet); Future replaceByFee(Object wallet, String transactionHash, String fee);