From 1dd2c7da56f8e63958188d4b59b5018bf05a1346 Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 10 Jun 2024 04:22:57 -0300 Subject: [PATCH] Sp fixes (#1487) * feat: missing desktop setting menu * fix: sp utxo pending * fix: change to electrs only scanning, initial migration, and btc-electrum as null ssl --- cw_bitcoin/lib/electrum.dart | 2 +- cw_bitcoin/lib/electrum_wallet.dart | 33 +++++++++++++----- cw_core/lib/get_height_by_date.dart | 1 + lib/bitcoin/cw_bitcoin.dart | 24 +++----------- lib/entities/default_settings_migration.dart | 35 ++++++++++++++++++++ lib/main.dart | 2 +- 6 files changed, 67 insertions(+), 30 deletions(-) diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index afd5e2440..b52015794 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -64,7 +64,7 @@ class ElectrumClient { await socket?.close(); } catch (_) {} - if (useSSL == false) { + if (useSSL == false || (useSSL == null && uri.toString().contains("btc-electrum"))) { socket = await Socket.connect(host, port, timeout: connectionTimeout); } else { socket = await SecureSocket.connect(host, port, diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 43bae2e19..64aa81722 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -197,7 +197,7 @@ abstract class ElectrumWalletBase bool silentPaymentsScanningActive = false; @action - Future setSilentPaymentsScanning(bool active) async { + Future setSilentPaymentsScanning(bool active, bool usingElectrs) async { silentPaymentsScanningActive = active; if (active) { @@ -210,7 +210,11 @@ abstract class ElectrumWalletBase } if (tip > walletInfo.restoreHeight) { - _setListeners(walletInfo.restoreHeight, chainTipParam: _currentChainTip); + _setListeners( + walletInfo.restoreHeight, + chainTipParam: _currentChainTip, + usingElectrs: usingElectrs, + ); } } else { alwaysScan = false; @@ -277,7 +281,12 @@ abstract class ElectrumWalletBase } @action - Future _setListeners(int height, {int? chainTipParam, bool? doSingleScan}) async { + Future _setListeners( + int height, { + int? chainTipParam, + bool? doSingleScan, + bool? usingElectrs, + }) async { final chainTip = chainTipParam ?? await getUpdatedChainTip(); if (chainTip == height) { @@ -303,7 +312,7 @@ abstract class ElectrumWalletBase chainTip: chainTip, electrumClient: ElectrumClient(), transactionHistoryIds: transactionHistory.transactions.keys.toList(), - node: ScanNode(node!.uri, node!.useSSL), + node: usingElectrs == true ? ScanNode(node!.uri, node!.useSSL) : null, labels: walletAddresses.labels, labelIndexes: walletAddresses.silentAddresses .where((addr) => addr.type == SilentPaymentsAddresType.p2sp && addr.index >= 1) @@ -1122,8 +1131,13 @@ abstract class ElectrumWalletBase @action @override - Future rescan( - {required int height, int? chainTip, ScanData? scanData, bool? doSingleScan}) async { + Future rescan({ + required int height, + int? chainTip, + ScanData? scanData, + bool? doSingleScan, + bool? usingElectrs, + }) async { silentPaymentsScanningActive = true; _setListeners(height, doSingleScan: doSingleScan); } @@ -1820,7 +1834,7 @@ class ScanData { final SendPort sendPort; final SilentPaymentOwner silentAddress; final int height; - final ScanNode node; + final ScanNode? node; final BasedUtxoNetwork network; final int chainTip; final ElectrumClient electrumClient; @@ -1881,7 +1895,10 @@ Future startRefresh(ScanData scanData) async { scanData.sendPort.send(SyncResponse(syncHeight, syncingStatus)); final electrumClient = scanData.electrumClient; - await electrumClient.connectToUri(scanData.node.uri, useSSL: scanData.node.useSSL); + await electrumClient.connectToUri( + scanData.node?.uri ?? Uri.parse("tcp://electrs.cakewallet.com:50001"), + useSSL: scanData.node?.useSSL ?? false, + ); if (tweaksSubscription == null) { final count = scanData.isSingleScan ? 1 : TWEAKS_COUNT; diff --git a/cw_core/lib/get_height_by_date.dart b/cw_core/lib/get_height_by_date.dart index a3dd51b68..d62a78468 100644 --- a/cw_core/lib/get_height_by_date.dart +++ b/cw_core/lib/get_height_by_date.dart @@ -245,6 +245,7 @@ Future getHavenCurrentHeight() async { // Data taken from https://timechaincalendar.com/ const bitcoinDates = { + "2024-06": 846005, "2024-05": 841590, "2024-04": 837182, "2024-03": 832623, diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index 634919952..bdefe2ea9 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -514,18 +514,10 @@ class CWBitcoin extends Bitcoin { @override Future setScanningActive(Object wallet, bool active) async { final bitcoinWallet = wallet as ElectrumWallet; - - if (active && !(await getNodeIsElectrsSPEnabled(wallet))) { - final node = Node( - useSSL: false, - uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}', - ); - node.type = WalletType.bitcoin; - - await bitcoinWallet.connectToNode(node: node); - } - - bitcoinWallet.setSilentPaymentsScanning(active); + bitcoinWallet.setSilentPaymentsScanning( + active, + active && (await getNodeIsElectrsSPEnabled(wallet)), + ); } @override @@ -540,14 +532,6 @@ class CWBitcoin extends Bitcoin { @override Future rescan(Object wallet, {required int height, bool? doSingleScan}) async { final bitcoinWallet = wallet as ElectrumWallet; - if (!(await getNodeIsElectrsSPEnabled(wallet))) { - final node = Node( - useSSL: false, - uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}', - ); - node.type = WalletType.bitcoin; - await bitcoinWallet.connectToNode(node: node); - } bitcoinWallet.rescan(height: height, doSingleScan: doSingleScan); } diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 697685767..806285a81 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -227,6 +227,8 @@ Future defaultSettingsMigration( break; case 34: await _addElectRsNode(nodes, sharedPreferences); + case 35: + await _switchElectRsNode(nodes, sharedPreferences); break; default: break; @@ -825,6 +827,39 @@ Future _addElectRsNode(Box nodeSource, SharedPreferences sharedPrefe } } +Future _switchElectRsNode(Box nodeSource, SharedPreferences sharedPreferences) async { + final currentBitcoinNodeId = + sharedPreferences.getInt(PreferencesKey.currentBitcoinElectrumSererIdKey); + final currentBitcoinNode = + nodeSource.values.firstWhere((node) => node.key == currentBitcoinNodeId); + final needToReplaceCurrentBitcoinNode = + currentBitcoinNode.uri.toString().contains('electrs.cakewallet.com'); + + if (!needToReplaceCurrentBitcoinNode) return; + + final btcElectrumNode = nodeSource.values.firstWhereOrNull( + (node) => node.uri.toString().contains('btc-electrum.cakewallet.com'), + ); + + if (btcElectrumNode == null) { + final newBtcElectrumBitcoinNode = Node( + uri: newCakeWalletBitcoinUri, + type: WalletType.bitcoin, + useSSL: false, + ); + await nodeSource.add(newBtcElectrumBitcoinNode); + await sharedPreferences.setInt( + PreferencesKey.currentBitcoinElectrumSererIdKey, + newBtcElectrumBitcoinNode.key as int, + ); + } else { + await sharedPreferences.setInt( + PreferencesKey.currentBitcoinElectrumSererIdKey, + btcElectrumNode.key as int, + ); + } +} + Future checkCurrentNodes( Box nodeSource, Box powNodeSource, SharedPreferences sharedPreferences) async { final currentMoneroNodeId = sharedPreferences.getInt(PreferencesKey.currentNodeIdKey); diff --git a/lib/main.dart b/lib/main.dart index 776c2aa69..09f0cf432 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -202,7 +202,7 @@ Future initializeAppConfigs() async { transactionDescriptions: transactionDescriptions, secureStorage: secureStorage, anonpayInvoiceInfo: anonpayInvoiceInfo, - initialMigrationVersion: 34, + initialMigrationVersion: 35, ); }