From 20d30013d06d605e51d8283eb59b1c753af07c25 Mon Sep 17 00:00:00 2001 From: Omar Hatem Date: Wed, 25 Dec 2024 21:26:15 +0200 Subject: [PATCH 1/4] support Adding query params to node url (#1901) * support Adding query params to node url * minor ui fix [skip ci] --- cw_core/lib/node.dart | 17 +++++------------ .../seed_verification_step_view.dart | 4 +++- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/cw_core/lib/node.dart b/cw_core/lib/node.dart index 18d2ffc44..aa7d27254 100644 --- a/cw_core/lib/node.dart +++ b/cw_core/lib/node.dart @@ -22,8 +22,8 @@ class Node extends HiveObject with Keyable { this.useSSL, this.trusted = false, this.socksProxyAddress, + this.path = '', String? uri, - String? path, WalletType? type, }) { if (uri != null) { @@ -32,9 +32,6 @@ class Node extends HiveObject with Keyable { if (type != null) { this.type = type; } - if (path != null) { - this.path = path; - } } Node.fromMap(Map map) @@ -95,19 +92,15 @@ class Node extends HiveObject with Keyable { case WalletType.bitcoin: case WalletType.litecoin: case WalletType.bitcoinCash: - return createUriFromElectrumAddress(uriRaw, path ?? ''); + return createUriFromElectrumAddress(uriRaw, path!); case WalletType.nano: case WalletType.banano: - if (isSSL) { - return Uri.https(uriRaw, path ?? ''); - } else { - return Uri.http(uriRaw, path ?? ''); - } case WalletType.ethereum: case WalletType.polygon: case WalletType.solana: case WalletType.tron: - return Uri.https(uriRaw, path ?? ''); + return Uri.parse( + "http${isSSL ? "s" : ""}://$uriRaw${path!.startsWith("/") ? path : "/$path"}"); case WalletType.none: throw Exception('Unexpected type ${type.toString()} for Node uri'); } @@ -247,7 +240,7 @@ class Node extends HiveObject with Keyable { if (proxy == null) { return false; } - final proxyAddress = proxy!.split(':')[0]; + final proxyAddress = proxy.split(':')[0]; final proxyPort = int.parse(proxy.split(':')[1]); try { final socket = await Socket.connect(proxyAddress, proxyPort, timeout: Duration(seconds: 5)); diff --git a/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart b/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart index b8c1500dc..8eb5e2cb7 100644 --- a/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart +++ b/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart @@ -81,7 +81,9 @@ class SeedVerificationStepView extends StatelessWidget { ); if (isSecondWrongEntry) { - Navigator.pop(context); + if (context.mounted) { + Navigator.pop(context); + } } } }, From b79ef988c80a6e7000b2d906b49791a69e6733f7 Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 25 Dec 2024 21:27:28 +0200 Subject: [PATCH 2/4] Cw 868 fix false synchronised status when the socket connection fails due to network issues (#1892) * prevent setting Synced status when the connection is lost * fallback for UTXO fetch failures * minor fix --- cw_bitcoin/lib/electrum.dart | 4 +- cw_bitcoin/lib/electrum_wallet.dart | 71 +++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index 4fc4c1ad8..1f5c369e3 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -235,7 +235,7 @@ class ElectrumClient { return []; }); - Future>> getListUnspent(String scriptHash) async { + Future>?> getListUnspent(String scriptHash) async { final result = await call(method: 'blockchain.scripthash.listunspent', params: [scriptHash]); if (result is List) { @@ -248,7 +248,7 @@ class ElectrumClient { }).toList(); } - return []; + return null; } Future>> getMempool(String scriptHash) => diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 64eafb021..eae830db1 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -478,6 +478,7 @@ abstract class ElectrumWalletBase if (alwaysScan == true) { _setListeners(walletInfo.restoreHeight); } else { + if (syncStatus is LostConnectionSyncStatus) return; syncStatus = SyncedSyncStatus(); } } catch (e, stacktrace) { @@ -1361,6 +1362,10 @@ abstract class ElectrumWalletBase Future updateAllUnspents() async { List updatedUnspentCoins = []; + final previousUnspentCoins = List.from(unspentCoins.where((utxo) => + utxo.bitcoinAddressRecord.type != SegwitAddresType.mweb && + utxo.bitcoinAddressRecord is! BitcoinSilentPaymentAddressRecord)); + if (hasSilentPaymentsScanning) { // Update unspents stored from scanned silent payment transactions transactionHistory.transactions.values.forEach((tx) { @@ -1377,13 +1382,27 @@ abstract class ElectrumWalletBase if (addr is! BitcoinSilentPaymentAddressRecord) addr.balance = 0; }); - await Future.wait(walletAddresses.allAddresses + final addressFutures = walletAddresses.allAddresses .where((element) => element.type != SegwitAddresType.mweb) - .map((address) async { - updatedUnspentCoins.addAll(await fetchUnspent(address)); - })); + .map((address) => fetchUnspent(address)) + .toList(); - unspentCoins = updatedUnspentCoins; + final results = await Future.wait(addressFutures); + final failedCount = results.where((result) => result == null).length; + + if (failedCount == 0) { + for (final result in results) { + updatedUnspentCoins.addAll(result!); + } + unspentCoins = updatedUnspentCoins; + } else { + unspentCoins = handleFailedUtxoFetch( + failedCount: failedCount, + previousUnspentCoins: previousUnspentCoins, + updatedUnspentCoins: updatedUnspentCoins, + results: results, + ); + } final currentWalletUnspentCoins = unspentCoinsInfo.values.where((element) => element.walletId == id); @@ -1396,6 +1415,38 @@ abstract class ElectrumWalletBase await _refreshUnspentCoinsInfo(); } + List handleFailedUtxoFetch({ + required int failedCount, + required List previousUnspentCoins, + required List updatedUnspentCoins, + required List?> results, + }) { + + if (failedCount == results.length) { + printV("All UTXOs failed to fetch, falling back to previous UTXOs"); + return previousUnspentCoins; + } + + final successfulUtxos = []; + for (final result in results) { + if (result != null) { + successfulUtxos.addAll(result); + } + } + + if (failedCount > 0 && successfulUtxos.isEmpty) { + printV("Some UTXOs failed, but no successful UTXOs, falling back to previous UTXOs"); + return previousUnspentCoins; + } + + if (failedCount > 0) { + printV("Some UTXOs failed, updating with successful UTXOs"); + updatedUnspentCoins.addAll(successfulUtxos); + } + + return updatedUnspentCoins; + } + Future updateCoins(List newUnspentCoins) async { if (newUnspentCoins.isEmpty) { return; @@ -1427,15 +1478,17 @@ abstract class ElectrumWalletBase @action Future updateUnspentsForAddress(BitcoinAddressRecord address) async { final newUnspentCoins = await fetchUnspent(address); - await updateCoins(newUnspentCoins); + await updateCoins(newUnspentCoins ?? []); } @action - Future> fetchUnspent(BitcoinAddressRecord address) async { - List> unspents = []; + Future?> fetchUnspent(BitcoinAddressRecord address) async { List updatedUnspentCoins = []; - unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); + final unspents = await electrumClient.getListUnspent(address.getScriptHash(network)); + + // Failed to fetch unspents + if (unspents == null) return null; await Future.wait(unspents.map((unspent) async { try { From c6b9f054cc761a0fbb21f983724805e8374d71ab Mon Sep 17 00:00:00 2001 From: Omar Hatem Date: Wed, 25 Dec 2024 21:27:46 +0200 Subject: [PATCH 3/4] Switch to SSL for Cake's Electrum and Monero nodes (#1899) * Force SSL for Electrum and Monero nodes Some Cleanup * minor [skip ci] * potential fix for transactions not cleared correctly [skip ci] * minor fix [skip ci] --- lib/core/wallet_loading_service.dart | 2 - lib/entities/default_settings_migration.dart | 69 ++++++++----------- .../evm_transaction_error_fees_handler.dart | 14 ++-- lib/main.dart | 2 +- .../wallet_connect/utils/string_parsing.dart | 5 ++ lib/utils/exception_handler.dart | 4 +- .../dashboard/dashboard_view_model.dart | 8 +-- 7 files changed, 49 insertions(+), 55 deletions(-) diff --git a/lib/core/wallet_loading_service.dart b/lib/core/wallet_loading_service.dart index 6b1553443..f1996bae8 100644 --- a/lib/core/wallet_loading_service.dart +++ b/lib/core/wallet_loading_service.dart @@ -2,12 +2,10 @@ import 'dart:async'; import 'package:cake_wallet/core/generate_wallet_password.dart'; import 'package:cake_wallet/core/key_service.dart'; -import 'package:cake_wallet/core/secure_storage.dart'; import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/reactions/on_authentication_state_change.dart'; -import 'package:cake_wallet/src/screens/auth/auth_page.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; import 'package:cake_wallet/utils/exception_handler.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 64370503f..9e06d25da 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -167,7 +167,11 @@ Future defaultSettingsMigration( break; case 18: - await addOnionNode(nodes); + await updateWalletTypeNodesWithNewNode( + nodes: nodes, + newNodeUri: "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion:18081", + type: WalletType.monero, + ); break; case 19: @@ -261,15 +265,15 @@ Future defaultSettingsMigration( await updateTronNodesWithNowNodes(sharedPreferences: sharedPreferences, nodes: nodes); break; case 42: - updateBtcElectrumNodeToUseSSL(nodes, sharedPreferences); + _fixNodesUseSSLFlag(nodes); break; case 43: - await _updateCakeXmrNode(nodes); + _fixNodesUseSSLFlag(nodes); _deselectExchangeProvider(sharedPreferences, "THORChain"); _deselectExchangeProvider(sharedPreferences, "SimpleSwap"); break; case 44: - await _updateCakeXmrNode(nodes); + _fixNodesUseSSLFlag(nodes); await _changeDefaultNode( nodes: nodes, sharedPreferences: sharedPreferences, @@ -297,14 +301,12 @@ Future defaultSettingsMigration( updateWalletTypeNodesWithNewNode( newNodeUri: 'matic.nownodes.io', - sharedPreferences: sharedPreferences, nodes: nodes, type: WalletType.polygon, useSSL: true, ); updateWalletTypeNodesWithNewNode( newNodeUri: 'eth.nownodes.io', - sharedPreferences: sharedPreferences, nodes: nodes, type: WalletType.ethereum, useSSL: true, @@ -330,6 +332,22 @@ Future defaultSettingsMigration( useSSL: true, oldUri: ['rpc.ankr.com'], ); + break; + case 46: + _fixNodesUseSSLFlag(nodes); + updateWalletTypeNodesWithNewNode( + newNodeUri: 'litecoin.stackwallet.com:20063', + nodes: nodes, + type: WalletType.litecoin, + useSSL: true, + ); + updateWalletTypeNodesWithNewNode( + newNodeUri: 'electrum-ltc.bysh.me:50002', + nodes: nodes, + type: WalletType.litecoin, + useSSL: true, + ); + break; default: break; } @@ -361,7 +379,8 @@ Future _changeDefaultNode({ required String newDefaultUri, required String currentNodePreferenceKey, required bool useSSL, - required List oldUri, // leave empty if you want to force replace the node regardless of the user's current node + required List + oldUri, // leave empty if you want to force replace the node regardless of the user's current node }) async { final currentNodeId = sharedPreferences.getInt(currentNodePreferenceKey); final currentNode = nodes.values.firstWhere((node) => node.key == currentNodeId); @@ -389,11 +408,10 @@ Future _changeDefaultNode({ /// Generic function for adding a new Node for a Wallet Type. Future updateWalletTypeNodesWithNewNode({ - required SharedPreferences sharedPreferences, required Box nodes, required WalletType type, required String newNodeUri, - required bool useSSL, + bool? useSSL, }) async { // If it already exists in the box of nodes, no need to add it annymore. if (nodes.values.any((node) => node.uriRaw == newNodeUri)) return; @@ -407,26 +425,6 @@ Future updateWalletTypeNodesWithNewNode({ ); } -Future _updateCakeXmrNode(Box nodes) async { - final node = nodes.values.firstWhereOrNull((element) => element.uriRaw == newCakeWalletMoneroUri); - - if (node != null) { - node.trusted = true; - node.useSSL = true; - await node.save(); - } -} - -void updateBtcElectrumNodeToUseSSL(Box nodes, SharedPreferences sharedPreferences) { - final btcElectrumNode = - nodes.values.firstWhereOrNull((element) => element.uriRaw == newCakeWalletBitcoinUri); - - if (btcElectrumNode != null) { - btcElectrumNode.useSSL = true; - btcElectrumNode.save(); - } -} - void _deselectExchangeProvider(SharedPreferences sharedPreferences, String providerName) { final Map exchangeProvidersSelection = json.decode(sharedPreferences.getString(PreferencesKey.exchangeProvidersSelection) ?? "{}") @@ -445,8 +443,10 @@ void _fixNodesUseSSLFlag(Box nodes) { switch (node.uriRaw) { case cakeWalletLitecoinElectrumUri: case cakeWalletBitcoinElectrumUri: + case newCakeWalletBitcoinUri: + case newCakeWalletMoneroUri: node.useSSL = true; - break; + node.trusted = true; } } } @@ -580,15 +580,6 @@ Future validateBitcoinSavedTransactionPriority(SharedPreferences sharedPre } } -Future addOnionNode(Box nodes) async { - final onionNodeUri = "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion:18081"; - - // check if the user has this node before (added it manually) - if (nodes.values.firstWhereOrNull((element) => element.uriRaw == onionNodeUri) == null) { - await nodes.add(Node(uri: onionNodeUri, type: WalletType.monero)); - } -} - Future replaceNodesMigration({required Box nodes}) async { final replaceNodes = { 'eu-node.cakewallet.io:18081': diff --git a/lib/entities/evm_transaction_error_fees_handler.dart b/lib/entities/evm_transaction_error_fees_handler.dart index 63f6e164d..b802f9883 100644 --- a/lib/entities/evm_transaction_error_fees_handler.dart +++ b/lib/entities/evm_transaction_error_fees_handler.dart @@ -1,3 +1,5 @@ +import 'package:cake_wallet/src/screens/wallet_connect/utils/string_parsing.dart'; + class EVMTransactionErrorFeesHandler { EVMTransactionErrorFeesHandler({ this.balanceWei, @@ -64,14 +66,14 @@ class EVMTransactionErrorFeesHandler { return EVMTransactionErrorFeesHandler( balanceWei: balanceWei.toString(), - balanceEth: balanceEth.toString().substring(0, 12), - balanceUsd: balanceUsd.toString().substring(0, 4), + balanceEth: balanceEth.toString().safeSubString(0, 12), + balanceUsd: balanceUsd.toString().safeSubString(0, 4), txCostWei: txCostWei.toString(), - txCostEth: txCostEth.toString().substring(0, 12), - txCostUsd: txCostUsd.toString().substring(0, 4), + txCostEth: txCostEth.toString().safeSubString(0, 12), + txCostUsd: txCostUsd.toString().safeSubString(0, 4), overshotWei: overshotWei.toString(), - overshotEth: overshotEth.toString().substring(0, 12), - overshotUsd: overshotUsd.toString().substring(0, 4), + overshotEth: overshotEth.toString().safeSubString(0, 12), + overshotUsd: overshotUsd.toString().safeSubString(0, 4), ); } else { // If any value is missing, return an error message diff --git a/lib/main.dart b/lib/main.dart index 510705105..fd25a1e9c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -215,7 +215,7 @@ Future initializeAppConfigs() async { secureStorage: secureStorage, anonpayInvoiceInfo: anonpayInvoiceInfo, havenSeedStore: havenSeedStore, - initialMigrationVersion: 45, + initialMigrationVersion: 46, ); } diff --git a/lib/src/screens/wallet_connect/utils/string_parsing.dart b/lib/src/screens/wallet_connect/utils/string_parsing.dart index b9fdca7b2..0aed1b9e9 100644 --- a/lib/src/screens/wallet_connect/utils/string_parsing.dart +++ b/lib/src/screens/wallet_connect/utils/string_parsing.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:math'; import 'package:convert/convert.dart'; @@ -13,4 +14,8 @@ extension StringParsing on String { return this; } + + String safeSubString(int start, int end) { + return this.substring(0, min(this.toString().length, 12)); + } } diff --git a/lib/utils/exception_handler.dart b/lib/utils/exception_handler.dart index 357a69fa6..66cbc61a0 100644 --- a/lib/utils/exception_handler.dart +++ b/lib/utils/exception_handler.dart @@ -219,9 +219,9 @@ class ExceptionHandler { // probably when the device was locked and then opened on Cake // this is solved by a restart of the app // just ignoring until we find a solution to this issue or migrate from flutter secure storage - "core/auth_service.dart:63", + "core/auth_service.dart:64", "core/key_service.dart:14", - "core/wallet_loading_service.dart:133", + "core/wallet_loading_service.dart:131", ]; static Future _addDeviceInfo(File file) async { diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 808657f66..387c66511 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -4,13 +4,11 @@ import 'dart:io' show Platform; import 'package:cake_wallet/.secrets.g.dart' as secrets; import 'package:cake_wallet/bitcoin/bitcoin.dart'; -import 'package:cake_wallet/buy/buy_provider.dart'; import 'package:cake_wallet/core/key_service.dart'; import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart'; import 'package:cake_wallet/entities/balance_display_mode.dart'; import 'package:cake_wallet/entities/exchange_api_mode.dart'; import 'package:cake_wallet/entities/preferences_key.dart'; -import 'package:cake_wallet/entities/provider_types.dart'; import 'package:cake_wallet/entities/service_status.dart'; import 'package:cake_wallet/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -643,7 +641,7 @@ abstract class DashboardViewModelBase with Store { transactions.clear(); - transactions.addAll( + transactions = ObservableList.of( wallet.transactionHistory.transactions.values.map( (transaction) => TransactionListItem( transaction: transaction, @@ -705,7 +703,7 @@ abstract class DashboardViewModelBase with Store { monero!.getTransactionInfoAccountId(tx) == monero!.getCurrentAccount(wallet).id) .toList(); - transactions.addAll( + transactions = ObservableList.of( _accountTransactions.map( (transaction) => TransactionListItem( transaction: transaction, @@ -725,7 +723,7 @@ abstract class DashboardViewModelBase with Store { wow.wownero!.getCurrentAccount(wallet).id) .toList(); - transactions.addAll( + transactions = ObservableList.of( _accountTransactions.map( (transaction) => TransactionListItem( transaction: transaction, From 3e93a5ecb883b7be4289f88a60a6343da1ea567a Mon Sep 17 00:00:00 2001 From: David Adegoke <64401859+Blazebrain@users.noreply.github.com> Date: Wed, 25 Dec 2024 20:28:05 +0100 Subject: [PATCH 4/4] fix: Generic fixes (#1897) --- .../seed/seed_verification/seed_verification_step_view.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart b/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart index 8eb5e2cb7..9fd70be05 100644 --- a/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart +++ b/lib/src/screens/seed/seed_verification/seed_verification_step_view.dart @@ -70,6 +70,8 @@ class SeedVerificationStepView extends StatelessWidget { (option) { return GestureDetector( onTap: () async { + if (walletSeedViewModel.wrongEntries > 2) return; + final isCorrectWord = walletSeedViewModel.isChosenWordCorrect(option); final isSecondWrongEntry = walletSeedViewModel.wrongEntries >= 2; if (!isCorrectWord) {