From 73492ad8655d8f78b3d351d654fe90ec364cada4 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Thu, 4 Jul 2024 21:44:08 +0200 Subject: [PATCH] update nano default node (#1408) * update nano default node * fix node indicator * Update pr_test_build.yml * Update pr_test_build.yml * update default nano node for new wallets * support extra args on tool script * remove nano secrets from node.dart --------- Co-authored-by: Omar Hatem --- .github/workflows/pr_test_build.yml | 3 + assets/nano_node_list.yml | 5 +- cw_core/lib/node.dart | 19 +--- cw_nano/lib/nano_client.dart | 14 +-- lib/entities/default_settings_migration.dart | 26 +++++- lib/main.dart | 2 +- tool/generate_secrets_config.dart | 91 +++++++------------- tool/utils/secret_key.dart | 3 + 8 files changed, 75 insertions(+), 88 deletions(-) diff --git a/.github/workflows/pr_test_build.yml b/.github/workflows/pr_test_build.yml index 99a45287f..788d02126 100644 --- a/.github/workflows/pr_test_build.yml +++ b/.github/workflows/pr_test_build.yml @@ -121,6 +121,8 @@ jobs: touch lib/.secrets.g.dart touch cw_evm/lib/.secrets.g.dart touch cw_solana/lib/.secrets.g.dart + touch cw_core/lib/.secrets.g.dart + touch cw_nano/lib/.secrets.g.dart touch cw_tron/lib/.secrets.g.dart echo "const salt = '${{ secrets.SALT }}';" > lib/.secrets.g.dart echo "const keychainSalt = '${{ secrets.KEY_CHAIN_SALT }}';" >> lib/.secrets.g.dart @@ -165,6 +167,7 @@ jobs: echo "const CSRFToken = '${{ secrets.CSRF_TOKEN }}';" >> lib/.secrets.g.dart 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 nanoNowNodesApiKey = '${{ secrets.NANO_NOW_NODES_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 diff --git a/assets/nano_node_list.yml b/assets/nano_node_list.yml index 2e4d1ec3c..be550177e 100644 --- a/assets/nano_node_list.yml +++ b/assets/nano_node_list.yml @@ -1,7 +1,10 @@ +- + uri: nano.nownodes.io + useSSL: true + is_default: true - uri: rpc.nano.to useSSL: true - is_default: true - uri: node.nautilus.io path: /api diff --git a/cw_core/lib/node.dart b/cw_core/lib/node.dart index 3bbbf38de..a82479c86 100644 --- a/cw_core/lib/node.dart +++ b/cw_core/lib/node.dart @@ -6,6 +6,7 @@ import 'package:hive/hive.dart'; import 'package:cw_core/hive_type_ids.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:http/io_client.dart' as ioc; + // import 'package:tor/tor.dart'; part 'node.g.dart'; @@ -148,7 +149,6 @@ class Node extends HiveObject with Keyable { return requestMoneroNode(); case WalletType.nano: case WalletType.banano: - return requestNanoNode(); case WalletType.bitcoin: case WalletType.litecoin: case WalletType.bitcoinCash: @@ -203,23 +203,6 @@ class Node extends HiveObject with Keyable { } } - Future requestNanoNode() async { - http.Response response = await http.post( - uri, - headers: {'Content-type': 'application/json'}, - body: json.encode( - { - "action": "block_count", - }, - ), - ); - if (response.statusCode == 200) { - return true; - } else { - return false; - } - } - Future requestNodeWithProxy() async { if (!isValidProxyAddress /* && !Tor.instance.enabled*/) { return false; diff --git a/cw_nano/lib/nano_client.dart b/cw_nano/lib/nano_client.dart index 3b388e5e8..8d8bef13d 100644 --- a/cw_nano/lib/nano_client.dart +++ b/cw_nano/lib/nano_client.dart @@ -10,7 +10,7 @@ import 'package:nanodart/nanodart.dart'; import 'package:cw_core/node.dart'; import 'package:nanoutil/nanoutil.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:cw_nano/.secrets.g.dart' as secrets; +import 'package:cw_nano/.secrets.g.dart' as nano_secrets; class NanoClient { static const Map CAKE_HEADERS = { @@ -54,12 +54,14 @@ class NanoClient { } Map getHeaders() { - if (_node!.uri == "https://rpc.nano.to") { - return CAKE_HEADERS..addAll({ - "key": secrets.nano2ApiKey, - }); + final headers = Map.from(CAKE_HEADERS); + if (_node!.uri.host == "rpc.nano.to") { + headers["key"] = nano_secrets.nano2ApiKey; } - return CAKE_HEADERS; + if (_node!.uri.host == "nano.nownodes.io") { + headers["api-key"] = nano_secrets.nanoNowNodesApiKey; + } + return headers; } Future getBalance(String address) async { diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 71a971a9a..144ca456d 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -34,7 +34,7 @@ const havenDefaultNodeUri = 'nodes.havenprotocol.org:443'; const ethereumDefaultNodeUri = 'ethereum.publicnode.com'; const polygonDefaultNodeUri = 'polygon-bor.publicnode.com'; const cakeWalletBitcoinCashDefaultNodeUri = 'bitcoincash.stackwallet.com:50002'; -const nanoDefaultNodeUri = 'rpc.nano.to'; +const nanoDefaultNodeUri = 'nano.nownodes.io'; const nanoDefaultPowNodeUri = 'rpc.nano.to'; const solanaDefaultNodeUri = 'rpc.ankr.com'; const tronDefaultNodeUri = 'trx.nownodes.io'; @@ -241,6 +241,9 @@ Future defaultSettingsMigration( case 38: await fixBtcDerivationPaths(walletInfoSource); break; + case 39: + await changeDefaultNanoNode(nodes, sharedPreferences); + break; default: break; } @@ -836,6 +839,25 @@ Future updateBtcNanoWalletInfos(Box walletsInfoSource) async { } } +Future changeDefaultNanoNode( + Box nodeSource, SharedPreferences sharedPreferences) async { + const oldNanoNodeUriPattern = 'rpc.nano.to'; + final currentNanoNodeId = sharedPreferences.getInt(PreferencesKey.currentNodeIdKey); + final currentNanoNode = nodeSource.values.firstWhere((node) => node.key == currentNanoNodeId); + + final newCakeWalletNode = Node( + uri: nanoDefaultNodeUri, + type: WalletType.nano, + useSSL: true, + ); + + await nodeSource.add(newCakeWalletNode); + + if (currentNanoNode.uri.toString().contains(oldNanoNodeUriPattern)) { + await sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, newCakeWalletNode.key as int); + } +} + Future changeDefaultBitcoinNode( Box nodeSource, SharedPreferences sharedPreferences) async { const cakeWalletBitcoinNodeUriPattern = '.cakewallet.com'; @@ -1225,4 +1247,4 @@ Future replaceTronDefaultNode({ // 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 8539ac803..014d5f011 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -203,7 +203,7 @@ Future initializeAppConfigs() async { transactionDescriptions: transactionDescriptions, secureStorage: secureStorage, anonpayInvoiceInfo: anonpayInvoiceInfo, - initialMigrationVersion: 38, + initialMigrationVersion: 39, ); } diff --git a/tool/generate_secrets_config.dart b/tool/generate_secrets_config.dart index cab41ca69..735e8e359 100644 --- a/tool/generate_secrets_config.dart +++ b/tool/generate_secrets_config.dart @@ -3,7 +3,8 @@ import 'dart:io'; import 'utils/secret_key.dart'; import 'utils/utils.dart'; -const configPath = 'tool/.secrets-config.json'; +const baseConfigPath = 'tool/.secrets-config.json'; +const coreConfigPath = 'tool/.core-secrets-config.json'; const evmChainsConfigPath = 'tool/.evm-secrets-config.json'; const solanaConfigPath = 'tool/.solana-secrets-config.json'; const nanoConfigPath = 'tool/.nano-secrets-config.json'; @@ -11,6 +12,23 @@ const tronConfigPath = 'tool/.tron-secrets-config.json'; Future main(List args) async => generateSecretsConfig(args); +Future writeConfig( + File configFile, + List newSecrets, { + Map? existingSecrets, +}) async { + final secrets = existingSecrets ?? {}; + newSecrets.forEach((sec) { + if (secrets[sec.name] != null) { + return; + } + secrets[sec.name] = sec.generate(); + }); + String secretsJson = JsonEncoder.withIndent(' ').convert(secrets); + await configFile.writeAsString(secretsJson); + secrets.clear(); +} + Future generateSecretsConfig(List args) async { final extraInfo = args.fold({}, (Map acc, String arg) { final parts = arg.split('='); @@ -19,7 +37,8 @@ Future generateSecretsConfig(List args) async { return acc; }); - final configFile = File(configPath); + final baseConfigFile = File(baseConfigPath); + final coreConfigFile = File(coreConfigPath); final evmChainsConfigFile = File(evmChainsConfigPath); final solanaConfigFile = File(solanaConfigPath); final nanoConfigFile = File(nanoConfigPath); @@ -32,70 +51,22 @@ Future generateSecretsConfig(List args) async { if (key.contains('--')) { return true; } - return false; }); - if (configFile.existsSync()) { + if (baseConfigFile.existsSync()) { if (extraInfo['--force'] == 1) { - await configFile.delete(); + await baseConfigFile.delete(); } else { return; } } - - // base: - SecretKey.base.forEach((sec) { - if (secrets[sec.name] != null) { - return; - } - secrets[sec.name] = sec.generate(); - }); - var secretsJson = JsonEncoder.withIndent(' ').convert(secrets); - await configFile.writeAsString(secretsJson); - secrets.clear(); - - // evm chains: - SecretKey.evmChainsSecrets.forEach((sec) { - if (secrets[sec.name] != null) { - return; - } - secrets[sec.name] = sec.generate(); - }); - secretsJson = JsonEncoder.withIndent(' ').convert(secrets); - await evmChainsConfigFile.writeAsString(secretsJson); - secrets.clear(); - - // solana: - SecretKey.solanaSecrets.forEach((sec) { - if (secrets[sec.name] != null) { - return; - } - secrets[sec.name] = sec.generate(); - }); - secretsJson = JsonEncoder.withIndent(' ').convert(secrets); - await solanaConfigFile.writeAsString(secretsJson); - secrets.clear(); - - // nano: - SecretKey.nanoSecrets.forEach((sec) { - if (secrets[sec.name] != null) { - return; - } - secrets[sec.name] = sec.generate(); - }); - secretsJson = JsonEncoder.withIndent(' ').convert(secrets); - await nanoConfigFile.writeAsString(secretsJson); - secrets.clear(); - - SecretKey.tronSecrets.forEach((sec) { - if (secrets[sec.name] != null) { - return; - } - - secrets[sec.name] = sec.generate(); - }); - secretsJson = JsonEncoder.withIndent(' ').convert(secrets); - await tronConfigFile.writeAsString(secretsJson); - secrets.clear(); + + await writeConfig(baseConfigFile, SecretKey.base, existingSecrets: secrets); + + await writeConfig(coreConfigFile, SecretKey.coreSecrets); + await writeConfig(evmChainsConfigFile, SecretKey.evmChainsSecrets); + await writeConfig(solanaConfigFile, SecretKey.solanaSecrets); + await writeConfig(nanoConfigFile, SecretKey.nanoSecrets); + await writeConfig(tronConfigFile, SecretKey.tronSecrets); } diff --git a/tool/utils/secret_key.dart b/tool/utils/secret_key.dart index 7261478a6..ed22d152a 100644 --- a/tool/utils/secret_key.dart +++ b/tool/utils/secret_key.dart @@ -45,6 +45,8 @@ class SecretKey { SecretKey('authorization', () => ''), ]; + static final coreSecrets = []; + static final evmChainsSecrets = [ SecretKey('etherScanApiKey', () => ''), SecretKey('polygonScanApiKey', () => ''), @@ -57,6 +59,7 @@ class SecretKey { static final nanoSecrets = [ SecretKey('nano2ApiKey', () => ''), + SecretKey('nanoNowNodesApiKey', () => ''), ]; static final tronSecrets = [