From ea6bdd02df2cc1251fc3abaa3b86a3da7b2fbf1b Mon Sep 17 00:00:00 2001
From: Matthew Fosse <matt@fosse.co>
Date: Thu, 4 Jan 2024 12:06:55 -0500
Subject: [PATCH] proxy node + theme fixes

---
 assets/images/tor_icon_disabled.png           | Bin 0 -> 925 bytes
 lib/core/wallet_loading_service.dart          |  34 +++++++++++++-----
 lib/di.dart                                   |  30 ++++++++--------
 .../dashboard/widgets/sync_indicator.dart     |   7 ++--
 .../settings/connection_sync_page.dart        |   7 ++--
 lib/store/settings_store.dart                 |   9 +++++
 6 files changed, 58 insertions(+), 29 deletions(-)
 create mode 100644 assets/images/tor_icon_disabled.png

diff --git a/assets/images/tor_icon_disabled.png b/assets/images/tor_icon_disabled.png
new file mode 100644
index 0000000000000000000000000000000000000000..116fa52e7fb67e781c03f47c250af0f1be0e0cc7
GIT binary patch
literal 925
zcmV;O17iG%P)<h;3K|Lk000e1NJLTq000#L000;W1^@s66^XV;00009a7bBm000XU
z000XU0RWnu7ytkO0drDELIAGL9O(c600d`2O+f$vv5yP<VFdsH12RcOK~#7FrIkNV
zlTj4LZ{NO!QVMh_15wjBIU5EO!yt`1khu5(bkxCU*cw9ZfPuJ>xHuW2a}yFY!N5Ql
z6A6P~0Z9lV#0r$QK>zss-t;t8qBQX)H|O@f=RD^*&$;(D?7A2W3k!*fiHWVz(b00D
zP}sQ0-*a7zo}Qlf&1N&HWwIOpu0TVfP+Xwc-fsN61^pThhgn|j#=k32jUnInv3GL0
zT;jh$7Z(=+NqL^<`uqD6vd=Gy3H=S|?Ck8W%Dz#r*B?sHM6FhH`sON?iaR+u`BEyC
zUKNYQrTO{!AO8deOiZ2%=vlQ|jZsOX(Fh4JJTx@q1myG^jh%d)K))Rw9laSC7<e%`
zIaxUey1cxclH=Ox>1hf^3M`3HL`lM0`wDchRWjE)2|*l*c6N3)Baz6hOeVA0))YXQ
zH3bELu~^I<A0Nk~(Ws|08gz*6_xAP%Oylu*z*Ku~aB$F#jEoSsHZwDGxeY3vUTa=L
zF93IUcb&?umdoV`fdJU#x5{=fKR7sWG>~LUlv2WXLHYT7{=SY&9>-)d>Gt;aTwh-w
z01dGVl2SJ8Y>;$#c$kI-6FEq@y}gYrw`jy8l^QTWA2wCCQv*CoHVu)*ChSy2APE+K
zlTBmV6Ug}C;bEN(*}y@}8n7b)&gj@F0iYx^IUq&jkU<2l`wE7wYiS#3rII{j2RI;1
z8-Vd0K){S`3n$?lBb%lVmOUnAVmm<%7$BQA0#L-p0wf6<YE+X0Z$Vf)mkcVfq@8O6
z_6^K*k)=UpImFDdbs5^AkU&57k&HDGm=BFA=r*|h{e5(+(E!b;z-qc(ZBUbf$#*+y
zowBu(fAj<}jRPNbq<r*~^>lW9SXj9R2ur*BRENJ+Niu~!4zH}NY^BrbiZ*ZK2s09x
zHBh?GH$Zt?0kEdB*GiV1nwnY)p7Yh!)lcK&<N6l5DW|Z3lHD4?u=8psz#bN@e`=k{
zX0tDUd!-n&v$H9cn3KZ;>m)1y;F@ydIvYmm^8KKmJh2z*9H<TbW$-wp1#nNDy)rg7
zc1^EQRPRG+eSQ7A`mmwB&+7Qwxw*OGpYgu{dj_>r#H9|R00000NkvXXu0mjfkXM`6

literal 0
HcmV?d00001

diff --git a/lib/core/wallet_loading_service.dart b/lib/core/wallet_loading_service.dart
index 3323e7831..24585f6c0 100644
--- a/lib/core/wallet_loading_service.dart
+++ b/lib/core/wallet_loading_service.dart
@@ -1,27 +1,36 @@
+import 'dart:io';
+
 import 'package:cake_wallet/core/generate_wallet_password.dart';
 import 'package:cake_wallet/core/key_service.dart';
 import 'package:cake_wallet/entities/preferences_key.dart';
+import 'package:cake_wallet/store/app_store.dart';
+import 'package:cake_wallet/store/settings_store.dart';
+import 'package:cake_wallet/view_model/settings/tor_connection.dart';
 import 'package:cw_core/wallet_base.dart';
 import 'package:cw_core/wallet_service.dart';
 import 'package:cw_core/wallet_type.dart';
 import 'package:shared_preferences/shared_preferences.dart';
+import 'package:tor/tor.dart';
 
 class WalletLoadingService {
   WalletLoadingService(
-      this.sharedPreferences, this.keyService, this.walletServiceFactory);
+    this.sharedPreferences,
+    this.keyService,
+    this.walletServiceFactory,
+    this.settingsStore,
+  );
 
   final SharedPreferences sharedPreferences;
   final KeyService keyService;
   final WalletService Function(WalletType type) walletServiceFactory;
+  final SettingsStore settingsStore;
 
-  Future<void> renameWallet(
-      WalletType type, String name, String newName) async {
+  Future<void> renameWallet(WalletType type, String name, String newName) async {
     final walletService = walletServiceFactory.call(type);
     final password = await keyService.getWalletPassword(walletName: name);
 
     // Save the current wallet's password to the new wallet name's key
-    await keyService.saveWalletPassword(
-        walletName: newName, password: password);
+    await keyService.saveWalletPassword(walletName: newName, password: password);
     // Delete previous wallet name from keyService to keep only new wallet's name
     // otherwise keeps duplicate (old and new names)
     await keyService.deleteWalletPassword(walletName: name);
@@ -46,6 +55,15 @@ class WalletLoadingService {
       await updateMoneroWalletPassword(wallet);
     }
 
+    final mode = settingsStore.torConnectionMode;
+    if (mode == TorConnectionMode.enabled || mode == TorConnectionMode.onionOnly && (Tor.instance.port != -1)) {
+      final node = settingsStore.getCurrentNode(wallet.type);
+      if (node.socksProxyAddress?.isEmpty ?? true) {
+        node.socksProxyAddress = "${InternetAddress.loopbackIPv4.address}:${Tor.instance.port}";
+      }
+      wallet.connectToNode(node: node);
+    }
+
     return wallet;
   }
 
@@ -61,11 +79,9 @@ class WalletLoadingService {
     // Save new generated password with backup key for case where
     // wallet will change password, but it will fail to update in secure storage
     final bakWalletName = '#__${wallet.name}_bak__#';
-    await keyService.saveWalletPassword(
-        walletName: bakWalletName, password: password);
+    await keyService.saveWalletPassword(walletName: bakWalletName, password: password);
     await wallet.changePassword(password);
-    await keyService.saveWalletPassword(
-        walletName: wallet.name, password: password);
+    await keyService.saveWalletPassword(walletName: wallet.name, password: password);
     isPasswordUpdated = true;
     await sharedPreferences.setBool(key, isPasswordUpdated);
   }
diff --git a/lib/di.dart b/lib/di.dart
index d43b43f88..bc3ce952f 100644
--- a/lib/di.dart
+++ b/lib/di.dart
@@ -352,17 +352,21 @@ Future<void> setup({
           walletInfoSource: _walletInfoSource));
 
   getIt.registerFactoryParam<AdvancedPrivacySettingsViewModel, WalletType, void>(
-          (type, _) => AdvancedPrivacySettingsViewModel(type, getIt.get<SettingsStore>()));
+      (type, _) => AdvancedPrivacySettingsViewModel(type, getIt.get<SettingsStore>()));
 
   getIt.registerFactory<WalletLoadingService>(() => WalletLoadingService(
-      getIt.get<SharedPreferences>(),
-      getIt.get<KeyService>(),
-      (WalletType type) => getIt.get<WalletService>(param1: type)));
-  
-  getIt.registerFactoryParam<WalletNewVM, WalletType, void>((type, _) =>
-      WalletNewVM(getIt.get<AppStore>(),
-          getIt.get<WalletCreationService>(param1: type), _walletInfoSource,
-          getIt.get<AdvancedPrivacySettingsViewModel>(param1: type),type: type));
+        getIt.get<SharedPreferences>(),
+        getIt.get<KeyService>(),
+        (WalletType type) => getIt.get<WalletService>(param1: type),
+        getIt.get<SettingsStore>(),
+      ));
+
+  getIt.registerFactoryParam<WalletNewVM, WalletType, void>((type, _) => WalletNewVM(
+      getIt.get<AppStore>(),
+      getIt.get<WalletCreationService>(param1: type),
+      _walletInfoSource,
+      getIt.get<AdvancedPrivacySettingsViewModel>(param1: type),
+      type: type));
 
   getIt.registerFactoryParam<WalletRestorationFromQRVM, WalletType, void>((WalletType type, _) {
     return WalletRestorationFromQRVM(getIt.get<AppStore>(),
@@ -815,8 +819,7 @@ Future<void> setup({
       .registerFactory<DFXBuyProvider>(() => DFXBuyProvider(wallet: getIt.get<AppStore>().wallet!));
 
   getIt.registerFactory<MoonPaySellProvider>(() => MoonPaySellProvider(
-              settingsStore: getIt.get<AppStore>().settingsStore,
-              wallet: getIt.get<AppStore>().wallet!));
+      settingsStore: getIt.get<AppStore>().settingsStore, wallet: getIt.get<AppStore>().wallet!));
 
   getIt.registerFactory<OnRamperBuyProvider>(() => OnRamperBuyProvider(
         getIt.get<AppStore>().settingsStore,
@@ -929,8 +932,7 @@ Future<void> setup({
       (param1, isCreate) => NewWalletTypePage(onTypeSelected: param1, isCreate: isCreate ?? true));
 
   getIt.registerFactoryParam<PreSeedPage, int, void>(
-      (seedPhraseLength, _)
-      => PreSeedPage(seedPhraseLength));
+      (seedPhraseLength, _) => PreSeedPage(seedPhraseLength));
 
   getIt.registerFactoryParam<TradeDetailsViewModel, Trade, void>((trade, _) =>
       TradeDetailsViewModel(
@@ -964,7 +966,7 @@ Future<void> setup({
   getIt.registerFactory(() => BuyAmountViewModel());
 
   getIt.registerFactoryParam<BuySellOptionsPage, bool, void>(
-        (isBuyOption, _) => BuySellOptionsPage(getIt.get<DashboardViewModel>(), isBuyOption));
+      (isBuyOption, _) => BuySellOptionsPage(getIt.get<DashboardViewModel>(), isBuyOption));
 
   getIt.registerFactory(() {
     final wallet = getIt.get<AppStore>().wallet;
diff --git a/lib/src/screens/dashboard/widgets/sync_indicator.dart b/lib/src/screens/dashboard/widgets/sync_indicator.dart
index 5509495d1..b65cfb8b3 100644
--- a/lib/src/screens/dashboard/widgets/sync_indicator.dart
+++ b/lib/src/screens/dashboard/widgets/sync_indicator.dart
@@ -60,10 +60,9 @@ class SyncIndicator extends StatelessWidget {
                         return Container(
                           width: 15,
                           margin: EdgeInsets.only(left: 12, bottom: 2),
-                          child: Image.asset(
-                            'assets/images/tor_icon.png',
-                            color: dashboardViewModel.isTorConnected ? null : Colors.white,
-                          ),
+                          child: dashboardViewModel.isTorConnected
+                              ? Image.asset('assets/images/tor_icon.png')
+                              : Image.asset('assets/images/tor_icon_disabled.png'),
                         );
                       }),
                     ],
diff --git a/lib/src/screens/settings/connection_sync_page.dart b/lib/src/screens/settings/connection_sync_page.dart
index da7b0450c..226e3313f 100644
--- a/lib/src/screens/settings/connection_sync_page.dart
+++ b/lib/src/screens/settings/connection_sync_page.dart
@@ -4,6 +4,9 @@ import 'package:cake_wallet/src/screens/settings/widgets/settings_picker_cell.da
 import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
 import 'package:cake_wallet/src/screens/settings/widgets/settings_tor_status.dart';
 import 'package:cake_wallet/src/screens/settings/widgets/wallet_connect_button.dart';
+import 'package:cake_wallet/themes/extensions/balance_page_theme.dart';
+import 'package:cake_wallet/themes/extensions/cake_text_theme.dart';
+import 'package:cake_wallet/themes/extensions/dashboard_page_theme.dart';
 import 'package:cake_wallet/utils/device_info.dart';
 import 'package:cake_wallet/utils/feature_flag.dart';
 import 'package:cake_wallet/utils/show_pop_up.dart';
@@ -105,7 +108,7 @@ class ConnectionSyncPage extends BasePage {
                     decoration: BoxDecoration(
                       borderRadius: BorderRadius.only(
                           topLeft: Radius.circular(25), topRight: Radius.circular(25)),
-                      color: const Color.fromARGB(255, 236, 244, 255),
+                      color: Theme.of(context).extension<CakeTextTheme>()!.textfieldUnderlineColor,
                     ),
                   );
                 }),
@@ -115,7 +118,7 @@ class ConnectionSyncPage extends BasePage {
                     decoration: BoxDecoration(
                       borderRadius: BorderRadius.only(
                           bottomLeft: Radius.circular(25), bottomRight: Radius.circular(25)),
-                      color: const Color.fromARGB(255, 236, 244, 255),
+                      color: Theme.of(context).extension<CakeTextTheme>()!.textfieldUnderlineColor,
                     ),
                     title: S.current.tor_status,
                     isSelected: false,
diff --git a/lib/store/settings_store.dart b/lib/store/settings_store.dart
index be82444b8..f9adaafb0 100644
--- a/lib/store/settings_store.dart
+++ b/lib/store/settings_store.dart
@@ -16,6 +16,7 @@ import 'package:cake_wallet/entities/sort_balance_types.dart';
 import 'package:cake_wallet/entities/wallet_list_order_types.dart';
 import 'package:cake_wallet/polygon/polygon.dart';
 import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
+import 'package:cake_wallet/store/app_store.dart';
 import 'package:cake_wallet/view_model/settings/sync_mode.dart';
 import 'package:cake_wallet/utils/device_info.dart';
 import 'package:cake_wallet/ethereum/ethereum.dart';
@@ -325,6 +326,14 @@ abstract class SettingsStoreBase with Store {
         await Tor.init();
         await Tor.instance.enable();
 
+        // connect to node through the proxy:
+        final appStore = getIt.get<AppStore>();
+        final node = getCurrentNode(appStore.wallet!.type);
+        if (node.socksProxyAddress?.isEmpty ?? true) {
+          node.socksProxyAddress = "${InternetAddress.loopbackIPv4.address}:${Tor.instance.port}";
+        }
+        appStore.wallet!.connectToNode(node: node);
+
         shouldStartTorOnLaunch = true;
       } else {
         Tor.instance.disable();