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]

Omar Hatem committed Dec 25, 2024 at 21:27 UTC c6b9f054cc761a0fbb21f983724805e8374d71ab
7 files changed +49 -55
lib/core/wallet_loading_service.dart
-2
@@ -2,12 +2,10 @@ import 'dart:async';
2
3 import 'package:cake_wallet/core/generate_wallet_password.dart';
4 import 'package:cake_wallet/core/key_service.dart';
5 -import 'package:cake_wallet/core/secure_storage.dart';
5 import 'package:cake_wallet/entities/preferences_key.dart';
6 import 'package:cake_wallet/generated/i18n.dart';
7 import 'package:cake_wallet/main.dart';
8 import 'package:cake_wallet/reactions/on_authentication_state_change.dart';
10 -import 'package:cake_wallet/src/screens/auth/auth_page.dart';
9 import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
10 import 'package:cake_wallet/utils/exception_handler.dart';
11 import 'package:cake_wallet/utils/show_pop_up.dart';
lib/entities/default_settings_migration.dart
+30 -39
@@ -167,7 +167,11 @@ Future<void> defaultSettingsMigration(
167 break;
168
169 case 18:
170 - await addOnionNode(nodes);
170 + await updateWalletTypeNodesWithNewNode(
171 + nodes: nodes,
172 + newNodeUri: "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion:18081",
173 + type: WalletType.monero,
174 + );
175 break;
176
177 case 19:
@@ -261,15 +265,15 @@ Future<void> defaultSettingsMigration(
265 await updateTronNodesWithNowNodes(sharedPreferences: sharedPreferences, nodes: nodes);
266 break;
267 case 42:
264 - updateBtcElectrumNodeToUseSSL(nodes, sharedPreferences);
268 + _fixNodesUseSSLFlag(nodes);
269 break;
270 case 43:
267 - await _updateCakeXmrNode(nodes);
271 + _fixNodesUseSSLFlag(nodes);
272 _deselectExchangeProvider(sharedPreferences, "THORChain");
273 _deselectExchangeProvider(sharedPreferences, "SimpleSwap");
274 break;
275 case 44:
272 - await _updateCakeXmrNode(nodes);
276 + _fixNodesUseSSLFlag(nodes);
277 await _changeDefaultNode(
278 nodes: nodes,
279 sharedPreferences: sharedPreferences,
@@ -297,14 +301,12 @@ Future<void> defaultSettingsMigration(
301
302 updateWalletTypeNodesWithNewNode(
303 newNodeUri: 'matic.nownodes.io',
300 - sharedPreferences: sharedPreferences,
304 nodes: nodes,
305 type: WalletType.polygon,
306 useSSL: true,
307 );
308 updateWalletTypeNodesWithNewNode(
309 newNodeUri: 'eth.nownodes.io',
307 - sharedPreferences: sharedPreferences,
310 nodes: nodes,
311 type: WalletType.ethereum,
312 useSSL: true,
@@ -330,6 +332,22 @@ Future<void> defaultSettingsMigration(
332 useSSL: true,
333 oldUri: ['rpc.ankr.com'],
334 );
335 + break;
336 + case 46:
337 + _fixNodesUseSSLFlag(nodes);
338 + updateWalletTypeNodesWithNewNode(
339 + newNodeUri: 'litecoin.stackwallet.com:20063',
340 + nodes: nodes,
341 + type: WalletType.litecoin,
342 + useSSL: true,
343 + );
344 + updateWalletTypeNodesWithNewNode(
345 + newNodeUri: 'electrum-ltc.bysh.me:50002',
346 + nodes: nodes,
347 + type: WalletType.litecoin,
348 + useSSL: true,
349 + );
350 + break;
351 default:
352 break;
353 }
@@ -361,7 +379,8 @@ Future<void> _changeDefaultNode({
379 required String newDefaultUri,
380 required String currentNodePreferenceKey,
381 required bool useSSL,
364 - required List<String> oldUri, // leave empty if you want to force replace the node regardless of the user's current node
382 + required List<String>
383 + oldUri, // leave empty if you want to force replace the node regardless of the user's current node
384 }) async {
385 final currentNodeId = sharedPreferences.getInt(currentNodePreferenceKey);
386 final currentNode = nodes.values.firstWhere((node) => node.key == currentNodeId);
@@ -389,11 +408,10 @@ Future<void> _changeDefaultNode({
408
409 /// Generic function for adding a new Node for a Wallet Type.
410 Future<void> updateWalletTypeNodesWithNewNode({
392 - required SharedPreferences sharedPreferences,
411 required Box<Node> nodes,
412 required WalletType type,
413 required String newNodeUri,
396 - required bool useSSL,
414 + bool? useSSL,
415 }) async {
416 // If it already exists in the box of nodes, no need to add it annymore.
417 if (nodes.values.any((node) => node.uriRaw == newNodeUri)) return;
@@ -407,26 +425,6 @@ Future<void> updateWalletTypeNodesWithNewNode({
425 );
426 }
427
410 -Future<void> _updateCakeXmrNode(Box<Node> nodes) async {
411 - final node = nodes.values.firstWhereOrNull((element) => element.uriRaw == newCakeWalletMoneroUri);
412 -
413 - if (node != null) {
414 - node.trusted = true;
415 - node.useSSL = true;
416 - await node.save();
417 - }
418 -}
419 -
420 -void updateBtcElectrumNodeToUseSSL(Box<Node> nodes, SharedPreferences sharedPreferences) {
421 - final btcElectrumNode =
422 - nodes.values.firstWhereOrNull((element) => element.uriRaw == newCakeWalletBitcoinUri);
423 -
424 - if (btcElectrumNode != null) {
425 - btcElectrumNode.useSSL = true;
426 - btcElectrumNode.save();
427 - }
428 -}
429 -
428 void _deselectExchangeProvider(SharedPreferences sharedPreferences, String providerName) {
429 final Map<String, dynamic> exchangeProvidersSelection =
430 json.decode(sharedPreferences.getString(PreferencesKey.exchangeProvidersSelection) ?? "{}")
@@ -445,8 +443,10 @@ void _fixNodesUseSSLFlag(Box<Node> nodes) {
443 switch (node.uriRaw) {
444 case cakeWalletLitecoinElectrumUri:
445 case cakeWalletBitcoinElectrumUri:
446 + case newCakeWalletBitcoinUri:
447 + case newCakeWalletMoneroUri:
448 node.useSSL = true;
449 - break;
449 + node.trusted = true;
450 }
451 }
452 }
@@ -580,15 +580,6 @@ Future<void> validateBitcoinSavedTransactionPriority(SharedPreferences sharedPre
580 }
581 }
582
583 -Future<void> addOnionNode(Box<Node> nodes) async {
584 - final onionNodeUri = "cakexmrl7bonq7ovjka5kuwuyd3f7qnkz6z6s6dmsy3uckwra7bvggyd.onion:18081";
585 -
586 - // check if the user has this node before (added it manually)
587 - if (nodes.values.firstWhereOrNull((element) => element.uriRaw == onionNodeUri) == null) {
588 - await nodes.add(Node(uri: onionNodeUri, type: WalletType.monero));
589 - }
590 -}
591 -
583 Future<void> replaceNodesMigration({required Box<Node> nodes}) async {
584 final replaceNodes = <String, Node>{
585 'eu-node.cakewallet.io:18081':
lib/entities/evm_transaction_error_fees_handler.dart
+8 -6
@@ -1,3 +1,5 @@
1 +import 'package:cake_wallet/src/screens/wallet_connect/utils/string_parsing.dart';
2 +
3 class EVMTransactionErrorFeesHandler {
4 EVMTransactionErrorFeesHandler({
5 this.balanceWei,
@@ -64,14 +66,14 @@ class EVMTransactionErrorFeesHandler {
66
67 return EVMTransactionErrorFeesHandler(
68 balanceWei: balanceWei.toString(),
67 - balanceEth: balanceEth.toString().substring(0, 12),
68 - balanceUsd: balanceUsd.toString().substring(0, 4),
69 + balanceEth: balanceEth.toString().safeSubString(0, 12),
70 + balanceUsd: balanceUsd.toString().safeSubString(0, 4),
71 txCostWei: txCostWei.toString(),
70 - txCostEth: txCostEth.toString().substring(0, 12),
71 - txCostUsd: txCostUsd.toString().substring(0, 4),
72 + txCostEth: txCostEth.toString().safeSubString(0, 12),
73 + txCostUsd: txCostUsd.toString().safeSubString(0, 4),
74 overshotWei: overshotWei.toString(),
73 - overshotEth: overshotEth.toString().substring(0, 12),
74 - overshotUsd: overshotUsd.toString().substring(0, 4),
75 + overshotEth: overshotEth.toString().safeSubString(0, 12),
76 + overshotUsd: overshotUsd.toString().safeSubString(0, 4),
77 );
78 } else {
79 // If any value is missing, return an error message
lib/main.dart
+1 -1
@@ -215,7 +215,7 @@ Future<void> initializeAppConfigs() async {
215 secureStorage: secureStorage,
216 anonpayInvoiceInfo: anonpayInvoiceInfo,
217 havenSeedStore: havenSeedStore,
218 - initialMigrationVersion: 45,
218 + initialMigrationVersion: 46,
219 );
220 }
221
lib/src/screens/wallet_connect/utils/string_parsing.dart
+5
@@ -1,4 +1,5 @@
1 import 'dart:convert';
2 +import 'dart:math';
3
4 import 'package:convert/convert.dart';
5
@@ -13,4 +14,8 @@ extension StringParsing on String {
14
15 return this;
16 }
17 +
18 + String safeSubString(int start, int end) {
19 + return this.substring(0, min(this.toString().length, 12));
20 + }
21 }
lib/utils/exception_handler.dart
+2 -2
@@ -219,9 +219,9 @@ class ExceptionHandler {
219 // probably when the device was locked and then opened on Cake
220 // this is solved by a restart of the app
221 // just ignoring until we find a solution to this issue or migrate from flutter secure storage
222 - "core/auth_service.dart:63",
222 + "core/auth_service.dart:64",
223 "core/key_service.dart:14",
224 - "core/wallet_loading_service.dart:133",
224 + "core/wallet_loading_service.dart:131",
225 ];
226
227 static Future<void> _addDeviceInfo(File file) async {
lib/view_model/dashboard/dashboard_view_model.dart
+3 -5
@@ -4,13 +4,11 @@ import 'dart:io' show Platform;
4
5 import 'package:cake_wallet/.secrets.g.dart' as secrets;
6 import 'package:cake_wallet/bitcoin/bitcoin.dart';
7 -import 'package:cake_wallet/buy/buy_provider.dart';
7 import 'package:cake_wallet/core/key_service.dart';
8 import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart';
9 import 'package:cake_wallet/entities/balance_display_mode.dart';
10 import 'package:cake_wallet/entities/exchange_api_mode.dart';
11 import 'package:cake_wallet/entities/preferences_key.dart';
13 -import 'package:cake_wallet/entities/provider_types.dart';
12 import 'package:cake_wallet/entities/service_status.dart';
13 import 'package:cake_wallet/exchange/exchange_provider_description.dart';
14 import 'package:cake_wallet/generated/i18n.dart';
@@ -643,7 +641,7 @@ abstract class DashboardViewModelBase with Store {
641
642 transactions.clear();
643
646 - transactions.addAll(
644 + transactions = ObservableList.of(
645 wallet.transactionHistory.transactions.values.map(
646 (transaction) => TransactionListItem(
647 transaction: transaction,
@@ -705,7 +703,7 @@ abstract class DashboardViewModelBase with Store {
703 monero!.getTransactionInfoAccountId(tx) == monero!.getCurrentAccount(wallet).id)
704 .toList();
705
708 - transactions.addAll(
706 + transactions = ObservableList.of(
707 _accountTransactions.map(
708 (transaction) => TransactionListItem(
709 transaction: transaction,
@@ -725,7 +723,7 @@ abstract class DashboardViewModelBase with Store {
723 wow.wownero!.getCurrentAccount(wallet).id)
724 .toList();
725
728 - transactions.addAll(
726 + transactions = ObservableList.of(
727 _accountTransactions.map(
728 (transaction) => TransactionListItem(
729 transaction: transaction,