From a319e101564ec9023b4807ccafa3e61bb602549f Mon Sep 17 00:00:00 2001
From: Adegoke David <64401859+Blazebrain@users.noreply.github.com>
Date: Fri, 28 Jun 2024 22:36:12 +0100
Subject: [PATCH] CW-653-Migrate-Tron-And-Solana-To-NowNodes (#1492)

* chore: Setup

* feat: Add NowNodes for Tron Wallet and switch it to be the default node for Tron

* feat: Add NowNodes for Solana Wallet and switch it to be the default node for Solana

* fix: Add nownodes entry to secrets

* fix: Remove pubspec.lock in shared external

* fix conflicts with main

* change secrets names

* feat: Remove Solana NowNodes config

* feat: Remove Solana NowNodes config

* feat: Revert commented out code

---------

Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
---
 .github/workflows/pr_test_build.yml           |  1 +
 assets/tron_node_list.yml                     |  4 +++
 cw_tron/lib/tron_http_provider.dart           |  2 ++
 devtools_options.yaml                         |  1 +
 ios/Runner.xcodeproj/project.pbxproj          |  8 ++---
 .../xcshareddata/xcschemes/Runner.xcscheme    |  2 +-
 lib/entities/default_settings_migration.dart  | 32 ++++++++++++++++++-
 lib/main.dart                                 |  2 +-
 tool/utils/secret_key.dart                    |  7 +++-
 9 files changed, 51 insertions(+), 8 deletions(-)
 create mode 100644 devtools_options.yaml

diff --git a/.github/workflows/pr_test_build.yml b/.github/workflows/pr_test_build.yml
index 841ea570d..5c8ea82a7 100644
--- a/.github/workflows/pr_test_build.yml
+++ b/.github/workflows/pr_test_build.yml
@@ -158,6 +158,7 @@ jobs:
           echo "const quantexExchangeMarkup = '${{ secrets.QUANTEX_EXCHANGE_MARKUP }}';" >> lib/.secrets.g.dart
           echo "const nano2ApiKey = '${{ secrets.NANO2_API_KEY }}';" >> cw_nano/lib/.secrets.g.dart
           echo "const tronGridApiKey = '${{ secrets.TRON_GRID_API_KEY }}';" >> cw_tron/lib/.secrets.g.dart
+          echo "const tronNowNodesApiKey = '${{ secrets.TRON_NOW_NODES_API_KEY }}';" >> cw_tron/lib/.secrets.g.dart
 
       - name: Rename app
         run: |
diff --git a/assets/tron_node_list.yml b/assets/tron_node_list.yml
index b12a82dbe..f9fd91179 100644
--- a/assets/tron_node_list.yml
+++ b/assets/tron_node_list.yml
@@ -4,5 +4,9 @@
   useSSL: true
 -
   uri: api.trongrid.io
+  is_default: false
+  useSSL: true
+- 
+  uri: trx.nownodes.io
   is_default: true
   useSSL: true
\ No newline at end of file
diff --git a/cw_tron/lib/tron_http_provider.dart b/cw_tron/lib/tron_http_provider.dart
index 58d313378..8a3301f87 100644
--- a/cw_tron/lib/tron_http_provider.dart
+++ b/cw_tron/lib/tron_http_provider.dart
@@ -20,6 +20,7 @@ class TronHTTPProvider implements TronServiceProvider {
     final response = await client.get(Uri.parse(params.url(url)), headers: {
       'Content-Type': 'application/json',
       if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey,
+      if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey,
     }).timeout(timeout ?? defaultRequestTimeout);
     final data = json.decode(response.body) as Map<String, dynamic>;
     return data;
@@ -32,6 +33,7 @@ class TronHTTPProvider implements TronServiceProvider {
             headers: {
               'Content-Type': 'application/json',
               if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey,
+              if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey,
             },
             body: params.toRequestBody())
         .timeout(timeout ?? defaultRequestTimeout);
diff --git a/devtools_options.yaml b/devtools_options.yaml
new file mode 100644
index 000000000..7e7e7f67d
--- /dev/null
+++ b/devtools_options.yaml
@@ -0,0 +1 @@
+extensions:
diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj
index 8ed46a028..2196bc289 100644
--- a/ios/Runner.xcodeproj/project.pbxproj
+++ b/ios/Runner.xcodeproj/project.pbxproj
@@ -164,7 +164,7 @@
 		97C146E61CF9000F007C117D /* Project object */ = {
 			isa = PBXProject;
 			attributes = {
-				LastUpgradeCheck = 1300;
+				LastUpgradeCheck = 1510;
 				ORGANIZATIONNAME = "";
 				TargetAttributes = {
 					97C146ED1CF9000F007C117D = {
@@ -355,7 +355,7 @@
 				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
 				GCC_WARN_UNUSED_FUNCTION = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 12.0;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = iphoneos;
 				SUPPORTED_PLATFORMS = iphoneos;
@@ -449,7 +449,7 @@
 				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
 				GCC_WARN_UNUSED_FUNCTION = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 12.0;
 				MTL_ENABLE_DEBUG_INFO = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = iphoneos;
@@ -499,7 +499,7 @@
 				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
 				GCC_WARN_UNUSED_FUNCTION = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 12.0;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = iphoneos;
 				SUPPORTED_PLATFORMS = iphoneos;
diff --git a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
index c87d15a33..5e31d3d34 100644
--- a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
+++ b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <Scheme
-   LastUpgradeVersion = "1300"
+   LastUpgradeVersion = "1510"
    version = "1.3">
    <BuildAction
       parallelizeBuildables = "YES"
diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart
index d25c76dc7..891d53f59 100644
--- a/lib/entities/default_settings_migration.dart
+++ b/lib/entities/default_settings_migration.dart
@@ -37,7 +37,7 @@ const cakeWalletBitcoinCashDefaultNodeUri = 'bitcoincash.stackwallet.com:50002';
 const nanoDefaultNodeUri = 'rpc.nano.to';
 const nanoDefaultPowNodeUri = 'rpc.nano.to';
 const solanaDefaultNodeUri = 'rpc.ankr.com';
-const tronDefaultNodeUri = 'api.trongrid.io';
+const tronDefaultNodeUri = 'trx.nownodes.io';
 const newCakeWalletBitcoinUri = 'btc-electrum.cakewallet.com:50002';
 
 Future<void> defaultSettingsMigration(
@@ -234,7 +234,11 @@ Future<void> defaultSettingsMigration(
           await changeTronCurrentNodeToDefault(sharedPreferences: sharedPreferences, nodes: nodes);
           break;
         case 37:
+          await replaceTronDefaultNode(sharedPreferences: sharedPreferences, nodes: nodes);
+          break;
+        case 38:
           await fixBtcDerivationPaths(walletInfoSource);
+          break;
         default:
           break;
       }
@@ -1142,3 +1146,29 @@ Future<void> changeTronCurrentNodeToDefault(
 
   await sharedPreferences.setInt(PreferencesKey.currentTronNodeIdKey, nodeId);
 }
+
+Future<void> replaceTronDefaultNode({
+  required SharedPreferences sharedPreferences,
+  required Box<Node> nodes,
+}) async {
+  // Get the currently active node
+  final currentTronNodeId = sharedPreferences.getInt(PreferencesKey.currentTronNodeIdKey);
+  final currentTronNode =
+      nodes.values.firstWhereOrNull((Node node) => node.key == currentTronNodeId);
+
+  //Confirm if this node is part of the default nodes from CakeWallet
+  final tronDefaultNodeList = [
+    'tron-rpc.publicnode.com:443',
+    'api.trongrid.io',
+  ];
+  bool needsToBeReplaced =
+      currentTronNode == null ? true : tronDefaultNodeList.contains(currentTronNode.uriRaw);
+
+  // If it's a custom node, return. We don't want to switch users from their custom nodes
+  if (!needsToBeReplaced) {
+    return;
+  }
+
+  // If it's not, we switch user to the new default node: NowNodes
+  await changeTronCurrentNodeToDefault(sharedPreferences: sharedPreferences, nodes: nodes);
+}
\ No newline at end of file
diff --git a/lib/main.dart b/lib/main.dart
index 46bd7c608..fbe77bd31 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -202,7 +202,7 @@ Future<void> initializeAppConfigs() async {
     transactionDescriptions: transactionDescriptions,
     secureStorage: secureStorage,
     anonpayInvoiceInfo: anonpayInvoiceInfo,
-    initialMigrationVersion: 37,
+    initialMigrationVersion: 38,
   );
 }
 
diff --git a/tool/utils/secret_key.dart b/tool/utils/secret_key.dart
index 9559e83b3..7261478a6 100644
--- a/tool/utils/secret_key.dart
+++ b/tool/utils/secret_key.dart
@@ -39,6 +39,10 @@ class SecretKey {
     SecretKey('moralisApiKey', () => ''),
     SecretKey('ankrApiKey', () => ''),
     SecretKey('quantexExchangeMarkup', () => ''),
+    SecretKey('testCakePayApiKey', () => ''),
+    SecretKey('cakePayApiKey', () => ''),
+    SecretKey('CSRFToken', () => ''),
+    SecretKey('authorization', () => ''),
   ];
 
   static final evmChainsSecrets = [
@@ -54,9 +58,10 @@ class SecretKey {
   static final nanoSecrets = [
     SecretKey('nano2ApiKey', () => ''),
   ];
-  
+
   static final tronSecrets = [
     SecretKey('tronGridApiKey', () => ''),
+    SecretKey('tronNowNodesApiKey', () => ''),
   ];
 
   final String name;