2023-05-26 16:33:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Stack Wallet.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'dart:convert';
|
2022-11-08 16:18:48 +00:00
|
|
|
import 'dart:io';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-08 16:18:48 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:stackwallet/utilities/format.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2022-11-08 16:18:48 +00:00
|
|
|
import 'package:stackwallet/widgets/desktop/primary_button.dart';
|
|
|
|
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
|
|
|
|
import 'package:stackwallet/widgets/stack_dialog.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-08 16:18:48 +00:00
|
|
|
class MoneroNodeConnectionResponse {
|
|
|
|
final X509Certificate? cert;
|
|
|
|
final String? url;
|
|
|
|
final int? port;
|
|
|
|
final bool success;
|
|
|
|
|
|
|
|
MoneroNodeConnectionResponse(this.cert, this.url, this.port, this.success);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<MoneroNodeConnectionResponse> testMoneroNodeConnection(
|
|
|
|
Uri uri,
|
|
|
|
bool allowBadX509Certificate,
|
|
|
|
) async {
|
|
|
|
final client = HttpClient();
|
|
|
|
MoneroNodeConnectionResponse? badCertResponse;
|
2022-08-26 08:11:35 +00:00
|
|
|
try {
|
2022-11-08 16:18:48 +00:00
|
|
|
client.badCertificateCallback = (cert, url, port) {
|
|
|
|
if (allowBadX509Certificate) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badCertResponse == null) {
|
|
|
|
badCertResponse = MoneroNodeConnectionResponse(cert, url, port, false);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
final request = await client.postUrl(uri);
|
|
|
|
|
|
|
|
final body = utf8.encode(
|
|
|
|
jsonEncode({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": "0",
|
|
|
|
"method": "get_info",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
request.headers.add(
|
|
|
|
'Content-Length',
|
|
|
|
body.length.toString(),
|
|
|
|
preserveHeaderCase: true,
|
|
|
|
);
|
|
|
|
request.headers.set(
|
|
|
|
'Content-Type',
|
|
|
|
'application/json',
|
|
|
|
preserveHeaderCase: true,
|
|
|
|
);
|
|
|
|
|
|
|
|
request.add(body);
|
|
|
|
|
|
|
|
final response = await request.close();
|
|
|
|
final result = await response.transform(utf8.decoder).join();
|
2022-08-26 08:11:35 +00:00
|
|
|
// TODO: json decoded without error so assume connection exists?
|
|
|
|
// or we can check for certain values in the response to decide
|
2022-11-08 16:18:48 +00:00
|
|
|
return MoneroNodeConnectionResponse(null, null, null, true);
|
2022-08-26 08:11:35 +00:00
|
|
|
} catch (e, s) {
|
2022-11-08 16:18:48 +00:00
|
|
|
if (badCertResponse != null) {
|
|
|
|
return badCertResponse!;
|
|
|
|
} else {
|
|
|
|
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
|
|
|
|
return MoneroNodeConnectionResponse(null, null, null, false);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
client.close(force: true);
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 16:18:48 +00:00
|
|
|
|
|
|
|
Future<bool> showBadX509CertificateDialog(
|
|
|
|
X509Certificate cert,
|
|
|
|
String url,
|
|
|
|
int port,
|
|
|
|
BuildContext context,
|
|
|
|
) async {
|
2022-11-08 17:41:12 +00:00
|
|
|
final chars = Format.uint8listToString(cert.sha1)
|
|
|
|
.toUpperCase()
|
|
|
|
.characters
|
|
|
|
.toList(growable: false);
|
|
|
|
|
|
|
|
String sha1 = chars.sublist(0, 2).join();
|
|
|
|
for (int i = 2; i < chars.length; i += 2) {
|
|
|
|
sha1 += ":${chars.sublist(i, i + 2).join()}";
|
|
|
|
}
|
|
|
|
|
2022-11-08 16:18:48 +00:00
|
|
|
final result = await showDialog<bool>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (context) {
|
|
|
|
return StackDialog(
|
|
|
|
title: "Untrusted X509Certificate",
|
2022-11-08 17:41:12 +00:00
|
|
|
message: "SHA1:\n$sha1",
|
2022-11-08 16:18:48 +00:00
|
|
|
leftButton: SecondaryButton(
|
|
|
|
label: "Cancel",
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop(false);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
rightButton: PrimaryButton(
|
|
|
|
label: "Trust",
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop(true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return result ?? false;
|
|
|
|
}
|