From cfa4515a826674f7987db972cb9a74b9d67ec683 Mon Sep 17 00:00:00 2001 From: Rafael Saes Date: Fri, 24 May 2024 15:36:53 -0300 Subject: [PATCH] feat: check if node is electrs, and supports sp --- cw_bitcoin/lib/electrum.dart | 8 ++-- lib/bitcoin/cw_bitcoin.dart | 43 +++++++++++++++---- .../screens/dashboard/pages/balance_page.dart | 3 +- lib/src/screens/rescan/rescan_page.dart | 3 +- tool/configure.dart | 2 +- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index 464b56735..0c88e234e 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -150,7 +150,8 @@ class ElectrumClient { } } - Future> version() => call(method: 'server.version').then((dynamic result) { + Future> version() => + call(method: 'server.version', params: ["", "1.4"]).then((dynamic result) { if (result is List) { return result.map((dynamic val) => val.toString()).toList(); } @@ -287,9 +288,8 @@ class ElectrumClient { ); } - Future> getTweaks({required int height}) async => - await callWithTimeout(method: 'blockchain.tweaks.get', params: [height], timeout: 10000) - as Map; + Future getTweaks({required int height}) async => + await callWithTimeout(method: 'blockchain.tweaks.subscribe', params: [height, 1, false]); Future estimatefee({required int p}) => call(method: 'blockchain.estimatefee', params: [p]).then((dynamic result) { diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index 75e7eb2a6..6bdba2c61 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -509,8 +509,7 @@ class CWBitcoin extends Bitcoin { Future setScanningActive(Object wallet, bool active) async { final bitcoinWallet = wallet as ElectrumWallet; - // TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore - if (!getNodeIsCakeElectrs(wallet)) { + if (!(await getNodeIsElectrsSPEnabled(wallet))) { final node = Node( useSSL: false, uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}', @@ -535,8 +534,7 @@ class CWBitcoin extends Bitcoin { @override Future rescan(Object wallet, {required int height, bool? doSingleScan}) async { final bitcoinWallet = wallet as ElectrumWallet; - // TODO: always when setting to scanning active, will force switch nodes. Remove when not needed anymore - if (!getNodeIsCakeElectrs(wallet)) { + if (!(await getNodeIsElectrsSPEnabled(wallet))) { final node = Node( useSSL: false, uri: 'electrs.cakewallet.com:${(wallet.network == BitcoinNetwork.testnet ? 50002 : 50001)}', @@ -547,13 +545,40 @@ class CWBitcoin extends Bitcoin { bitcoinWallet.rescan(height: height, doSingleScan: doSingleScan); } - @override - bool getNodeIsCakeElectrs(Object wallet) { + Future getNodeIsElectrs(Object wallet) async { final bitcoinWallet = wallet as ElectrumWallet; - final node = bitcoinWallet.node; - return node?.uri.host == 'electrs.cakewallet.com' && - node?.uri.port == (wallet.network == BitcoinNetwork.testnet ? 50002 : 50001); + final version = await bitcoinWallet.electrumClient.version(); + + if (version.isEmpty) { + return false; + } + + final server = version[0]; + + if (server.toLowerCase().contains('electrs')) { + return true; + } + + return false; + } + + @override + Future getNodeIsElectrsSPEnabled(Object wallet) async { + if (!(await getNodeIsElectrs(wallet))) { + return false; + } + + final bitcoinWallet = wallet as ElectrumWallet; + final tweaksResponse = await bitcoinWallet.electrumClient.getTweaks(height: 0); + + print('tweaksResponse: $tweaksResponse'); + + if (tweaksResponse != null) { + return true; + } + + return false; } @override diff --git a/lib/src/screens/dashboard/pages/balance_page.dart b/lib/src/screens/dashboard/pages/balance_page.dart index 2f6a10bb3..3ed2a603f 100644 --- a/lib/src/screens/dashboard/pages/balance_page.dart +++ b/lib/src/screens/dashboard/pages/balance_page.dart @@ -329,7 +329,8 @@ class CryptoBalanceWidget extends StatelessWidget { final isSilentPaymentsScanningActive = dashboardViewModel.silentPaymentsScanningActive; final newValue = !isSilentPaymentsScanningActive; - final needsToSwitch = bitcoin!.getNodeIsCakeElectrs(dashboardViewModel.wallet) == false; + final needsToSwitch = !isSilentPaymentsScanningActive && + await bitcoin!.getNodeIsElectrsSPEnabled(dashboardViewModel.wallet) == false; if (needsToSwitch) { return showPopUp( diff --git a/lib/src/screens/rescan/rescan_page.dart b/lib/src/screens/rescan/rescan_page.dart index b633a127e..521e314b3 100644 --- a/lib/src/screens/rescan/rescan_page.dart +++ b/lib/src/screens/rescan/rescan_page.dart @@ -55,7 +55,8 @@ class RescanPage extends BasePage { } Future _toggleSilentPaymentsScanning(BuildContext context) async { - final needsToSwitch = bitcoin!.getNodeIsCakeElectrs(_rescanViewModel.wallet) == false; + final needsToSwitch = + await bitcoin!.getNodeIsElectrsSPEnabled(_rescanViewModel.wallet) == false; if (needsToSwitch) { return showPopUp( diff --git a/tool/configure.dart b/tool/configure.dart index cf250fb54..5e4e64439 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -213,7 +213,7 @@ abstract class Bitcoin { {int? outputsCount, int? size}); int getHeightByDate({required DateTime date}); Future rescan(Object wallet, {required int height, bool? doSingleScan}); - bool getNodeIsCakeElectrs(Object wallet); + bool getNodeIsElectrsSPEnabled(Object wallet); void deleteSilentPaymentAddress(Object wallet, String address); Future updateFeeRates(Object wallet); int getMaxCustomFeeRate(Object wallet);