Cw 872 nano enhancements (#1909)

* fix headers on all api calls

* fix duplicate nano nodes on fresh install, api key fixes

* fix liveness indicators + false positive responses to queries
This commit is contained in:
Matthew Fosse 2024-12-30 11:46:09 -05:00 committed by GitHub
parent 214fc7113c
commit 831a6d9f9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 28 deletions

View file

@ -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<bool> 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<bool> requestEthereumServer() async {
try {
final response = await http.get(

View file

@ -54,12 +54,12 @@ class NanoClient {
}
}
Map<String, String> getHeaders() {
Map<String, String> getHeaders(String host) {
final headers = Map<String, String>.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<NanoBalance> 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<String> 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<List<NanoTransactionModel>> 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<NanoTransactionModel>((transaction) => NanoTransactionModel.fromJson(transaction))
.toList();
} catch (e) {
printV(e);
return [];
printV("error fetching transactions: $e");
rethrow;
}
}
Future<List<N2Node>> 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<int> 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,

View file

@ -505,7 +505,7 @@ Future<void> updateNanoNodeList({required Box<Node> 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);
}
}