diff --git a/cw_core/lib/node.dart b/cw_core/lib/node.dart index aa7d27254..7d0c2411f 100644 --- a/cw_core/lib/node.dart +++ b/cw_core/lib/node.dart @@ -99,8 +99,8 @@ class Node extends HiveObject with Keyable { case WalletType.polygon: case WalletType.solana: case WalletType.tron: - return Uri.parse( - "http${isSSL ? "s" : ""}://$uriRaw${path!.startsWith("/") ? path : "/$path"}"); + return Uri.parse( + "http${isSSL ? "s" : ""}://$uriRaw${path!.startsWith("/") ? path : "/$path"}"); case WalletType.none: throw Exception('Unexpected type ${type.toString()} for Node uri'); } @@ -152,6 +152,7 @@ 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: @@ -198,14 +199,16 @@ class Node extends HiveObject with Keyable { ); client.close(); - if (( - response.body.contains("400 Bad Request") // Some other generic error - || response.body.contains("plain HTTP request was sent to HTTPS port") // Cloudflare - || response.headers["location"] != null // Generic reverse proxy - || response.body.contains("301 Moved Permanently") // Poorly configured generic reverse proxy - ) && !(useSSL??false) - ) { - + if ((response.body.contains("400 Bad Request") // Some other generic error + || + response.body.contains("plain HTTP request was sent to HTTPS port") // Cloudflare + || + response.headers["location"] != null // Generic reverse proxy + || + response.body + .contains("301 Moved Permanently") // Poorly configured generic reverse proxy + ) && + !(useSSL ?? false)) { final oldUseSSL = useSSL; useSSL = true; try { @@ -271,6 +274,35 @@ class Node extends HiveObject with Keyable { } } + Future requestNanoNode() async { + try { + final response = await http.post( + uri, + headers: { + "Content-Type": "application/json", + "nano-app": "cake-wallet" + }, + body: jsonEncode( + { + "action": "account_balance", + "account": "nano_38713x95zyjsqzx6nm1dsom1jmm668owkeb9913ax6nfgj15az3nu8xkx579", + }, + ), + ); + final data = await jsonDecode(response.body); + if (response.statusCode != 200 || + data["error"] != null || + data["balance"] == null || + data["receivable"] == null) { + throw Exception( + "Error while trying to get balance! ${data["error"] != null ? data["error"] : ""}"); + } + return true; + } catch (_) { + return false; + } + } + Future requestEthereumServer() async { try { final response = await http.get( diff --git a/cw_nano/lib/nano_client.dart b/cw_nano/lib/nano_client.dart index 8b62273da..b63c634ee 100644 --- a/cw_nano/lib/nano_client.dart +++ b/cw_nano/lib/nano_client.dart @@ -54,12 +54,12 @@ class NanoClient { } } - Map getHeaders() { + Map getHeaders(String host) { final headers = Map.from(CAKE_HEADERS); - if (_node!.uri.host == "rpc.nano.to") { + if (host == "rpc.nano.to") { headers["key"] = nano_secrets.nano2ApiKey; } - if (_node!.uri.host == "nano.nownodes.io") { + if (host == "nano.nownodes.io") { headers["api-key"] = nano_secrets.nanoNowNodesApiKey; } return headers; @@ -68,7 +68,7 @@ class NanoClient { Future getBalance(String address) async { final response = await http.post( _node!.uri, - headers: getHeaders(), + headers: getHeaders(_node!.uri.host), body: jsonEncode( { "action": "account_balance", @@ -95,7 +95,7 @@ class NanoClient { try { final response = await http.post( _node!.uri, - headers: getHeaders(), + headers: getHeaders(_node!.uri.host), body: jsonEncode( { "action": "account_info", @@ -116,7 +116,7 @@ class NanoClient { try { final response = await http.post( _node!.uri, - headers: CAKE_HEADERS, + headers: getHeaders(_node!.uri.host), body: jsonEncode( { "action": "block_info", @@ -183,7 +183,7 @@ class NanoClient { Future requestWork(String hash) async { final response = await http.post( _powNode!.uri, - headers: getHeaders(), + headers: getHeaders(_powNode!.uri.host), body: json.encode( { "action": "work_generate", @@ -226,7 +226,7 @@ class NanoClient { final processResponse = await http.post( _node!.uri, - headers: getHeaders(), + headers: getHeaders(_node!.uri.host), body: processBody, ); @@ -425,7 +425,7 @@ class NanoClient { }); final processResponse = await http.post( _node!.uri, - headers: getHeaders(), + headers: getHeaders(_node!.uri.host), body: processBody, ); @@ -441,7 +441,7 @@ class NanoClient { required String privateKey, }) async { final receivableResponse = await http.post(_node!.uri, - headers: getHeaders(), + headers: getHeaders(_node!.uri.host), body: jsonEncode({ "action": "receivable", "account": destinationAddress, @@ -493,7 +493,7 @@ class NanoClient { Future> fetchTransactions(String address) async { try { final response = await http.post(_node!.uri, - headers: getHeaders(), + headers: getHeaders(_node!.uri.host), body: jsonEncode({ "action": "account_history", "account": address, @@ -509,15 +509,16 @@ class NanoClient { .map((transaction) => NanoTransactionModel.fromJson(transaction)) .toList(); } catch (e) { - printV(e); - return []; + printV("error fetching transactions: $e"); + rethrow; } } Future> getN2Reps() async { + final uri = Uri.parse(N2_REPS_ENDPOINT); final response = await http.post( - Uri.parse(N2_REPS_ENDPOINT), - headers: CAKE_HEADERS, + uri, + headers: getHeaders(uri.host), body: jsonEncode({"action": "reps"}), ); try { @@ -531,9 +532,10 @@ class NanoClient { } Future getRepScore(String rep) async { + final uri = Uri.parse(N2_REPS_ENDPOINT); final response = await http.post( - Uri.parse(N2_REPS_ENDPOINT), - headers: CAKE_HEADERS, + uri, + headers: getHeaders(uri.host), body: jsonEncode({ "action": "rep_info", "account": rep, diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 63e70ce4d..25140f106 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -505,7 +505,7 @@ Future updateNanoNodeList({required Box nodes}) async { ]; // add new nodes: for (final node in nodeList) { - if (listOfNewEndpoints.contains(node.uriRaw)) { + if (listOfNewEndpoints.contains(node.uriRaw) && !nodes.values.contains(node)) { await nodes.add(node); } }