stack_wallet/lib/utilities/connection_check/electrum_connection_check.dart

68 lines
2.1 KiB
Dart
Raw Normal View History

2024-04-18 23:17:45 +00:00
import 'dart:io';
import 'package:electrum_adapter/electrum_adapter.dart';
2024-05-27 23:56:22 +00:00
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(
2024-05-27 23:56:22 +00:00
"Tor preference and killswitch set but Tor is not enabled, not connecting to Electrum adapter",
);
2024-04-18 23:17:45 +00:00
// 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 && !host.endsWith('.onion'),
2024-04-18 23:17:45 +00:00
proxyInfo: proxyInfo,
).timeout(
Duration(seconds: (proxyInfo == null ? 5 : 30)),
2024-04-18 23:17:45 +00:00
onTimeout: () => throw Exception(
"The checkElectrumServer connect() call timed out.",
),
);
await client
.ping()
.timeout(Duration(seconds: (proxyInfo == null ? 5 : 30)));
2024-04-18 23:17:45 +00:00
return true;
} catch (_) {
return false;
}
}