stack_wallet/lib/utilities/connection_check/electrum_connection_check.dart

64 lines
2 KiB
Dart
Raw Normal View History

2024-04-18 23:17:45 +00:00
import 'dart:io';
import 'package:electrum_adapter/electrum_adapter.dart';
import '../../services/event_bus/events/global/tor_connection_status_changed_event.dart';
import '../../services/tor_service.dart';
import '../logger.dart';
import '../prefs.dart';
2024-04-18 23:17:45 +00:00
Future<bool> checkElectrumServer({
required String host,
required int port,
required bool useSSL,
Prefs? overridePrefs,
TorService? overrideTorService,
}) async {
final _prefs = overridePrefs ?? Prefs.instance;
final _torService = overrideTorService ?? TorService.sharedInstance;
({InternetAddress host, int port})? proxyInfo;
try {
// If we're supposed to use Tor...
if (_prefs.useTor) {
// But Tor isn't running...
if (_torService.status != TorConnectionStatus.connected) {
// And the killswitch isn't set...
if (!_prefs.torKillSwitch) {
// Then we'll just proceed and connect to ElectrumX through clearnet at the bottom of this function.
Logging.instance.log(
"Tor preference set but Tor is not enabled, killswitch not set, connecting to Electrum adapter through clearnet",
level: LogLevel.Warning,
);
} else {
// ... But if the killswitch is set, then we throw an exception.
throw Exception(
"Tor preference and killswitch set but Tor is not enabled, not connecting to Electrum adapter");
// TODO [prio=low]: Try to start Tor.
}
} else {
// Get the proxy info from the TorService.
proxyInfo = _torService.getProxyInfo();
}
}
final client = await ElectrumClient.connect(
host: host,
port: port,
useSSL: useSSL,
proxyInfo: proxyInfo,
).timeout(
const Duration(seconds: 5),
onTimeout: () => throw Exception(
"The checkElectrumServer connect() call timed out.",
),
);
await client.ping().timeout(const Duration(seconds: 5));
return true;
} catch (_) {
return false;
}
}