2023-08-22 16:33:24 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-09-11 20:29:10 +00:00
|
|
|
import 'package:stackwallet/networking/http.dart' as http;
|
|
|
|
import 'package:stackwallet/services/tor_service.dart';
|
|
|
|
import 'package:stackwallet/utilities/prefs.dart';
|
2023-08-23 13:13:17 +00:00
|
|
|
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';
|
2023-08-22 16:33:24 +00:00
|
|
|
|
2023-08-23 14:59:00 +00:00
|
|
|
Future<bool> testStellarNodeConnection(String host, int port) async {
|
2023-09-11 20:29:10 +00:00
|
|
|
http.HTTP client = http.HTTP();
|
2023-08-23 14:59:00 +00:00
|
|
|
Uri uri = Uri.parse("$host:$port");
|
2023-08-22 16:33:24 +00:00
|
|
|
|
2023-09-11 20:29:10 +00:00
|
|
|
final response = await client
|
|
|
|
.get(
|
|
|
|
url: uri,
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
proxyInfo:
|
|
|
|
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
|
|
|
|
)
|
|
|
|
.timeout(const Duration(milliseconds: 2000),
|
|
|
|
onTimeout: () async => http.Response(utf8.encode('Error'), 408));
|
|
|
|
|
|
|
|
if (response.code == 200) {
|
2023-08-23 13:13:17 +00:00
|
|
|
//Get chain height for sdk
|
|
|
|
StellarSDK stellarSdk = StellarSDK(host);
|
|
|
|
final height = await stellarSdk.ledgers
|
|
|
|
.order(RequestBuilderOrder.DESC)
|
|
|
|
.limit(1)
|
|
|
|
.execute()
|
|
|
|
.then((value) => value.records!.first.sequence)
|
|
|
|
.onError((error, stackTrace) => throw ("Error getting chain height"));
|
2023-08-22 16:33:24 +00:00
|
|
|
|
2023-08-23 13:13:17 +00:00
|
|
|
if (height > 0) {
|
2023-09-11 20:29:10 +00:00
|
|
|
return true;
|
2023-08-23 13:13:17 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-22 16:33:24 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-11 20:29:10 +00:00
|
|
|
}
|