diff --git a/lib/models/epicbox_config_model.dart b/lib/models/epicbox_config_model.dart index 71fe82281b..630add10f9 100644 --- a/lib/models/epicbox_config_model.dart +++ b/lib/models/epicbox_config_model.dart @@ -49,9 +49,9 @@ class EpicBoxConfigModel { }) { return EpicBoxConfigModel( host: host, - port: this.port ?? 443, - protocolInsecure: this.protocolInsecure ?? false, - addressIndex: this.addressIndex ?? 0, + port: port ?? this.port ?? 443, + protocolInsecure: protocolInsecure ?? this.protocolInsecure ?? false, + addressIndex: addressIndex ?? this.addressIndex ?? 0, // id: id ?? this.id, // name: name ?? this.name, ); @@ -72,7 +72,8 @@ class EpicBoxConfigModel { return { 'epicbox_domain': host, 'epicbox_port': port, - 'epicbox_protocol_insecure': protocolInsecure, + // The plugin's EpicboxConfig field is spelled "unsecure". + 'epicbox_protocol_unsecure': protocolInsecure, 'epicbox_address_index': addressIndex, // 'id': id, // 'name': name, @@ -96,22 +97,22 @@ class EpicBoxConfigModel { if (oldPort != "empty") { _epicBox['epicbox_port'] = _epicBox['port']; } - final oldProtocolInsecure = _epicBox["protocol_insecure"] ?? "empty"; - if (oldProtocolInsecure != "empty") { - _epicBox['epicbox_protocol_insecure'] = _epicBox['protocol_insecure']; - } + // Configs saved before the key matched the plugin's use + // "epicbox_protocol_insecure", and older ones "protocol_insecure". + _epicBox['epicbox_protocol_unsecure'] ??= + _epicBox['epicbox_protocol_insecure'] ?? _epicBox['protocol_insecure']; final oldAddressIndex = _epicBox["address_index"] ?? "empty"; if (oldAddressIndex != "empty") { _epicBox['epicbox_address_index'] = _epicBox['address_index']; } - _epicBox['epicbox_protocol_insecure'] ??= false; + _epicBox['epicbox_protocol_unsecure'] ??= false; _epicBox['epicbox_address_index'] ??= 0; return EpicBoxConfigModel( host: _epicBox['epicbox_domain'] as String, port: _epicBox['epicbox_port'] as int, - protocolInsecure: _epicBox['epicbox_protocol_insecure'] as bool, + protocolInsecure: _epicBox['epicbox_protocol_unsecure'] as bool, addressIndex: _epicBox['epicbox_address_index'] as int, // name: fields[5] as String, // id: fields[6] as String, diff --git a/lib/models/mwcmqs_config_model.dart b/lib/models/mwcmqs_config_model.dart index eb37702f5d..d27cd95e28 100644 --- a/lib/models/mwcmqs_config_model.dart +++ b/lib/models/mwcmqs_config_model.dart @@ -26,7 +26,7 @@ class MwcMqsConfigModel { MwcMqsConfigModel({required this.host, this.port}); MwcMqsConfigModel copyWith({int? port, bool? protocolInsecure}) { - return MwcMqsConfigModel(host: host, port: this.port ?? 443); + return MwcMqsConfigModel(host: host, port: port ?? this.port ?? 443); } Map toMap() { diff --git a/lib/wallets/wallet/impl/epiccash_wallet.dart b/lib/wallets/wallet/impl/epiccash_wallet.dart index 4212299207..8604a0c8b3 100644 --- a/lib/wallets/wallet/impl/epiccash_wallet.dart +++ b/lib/wallets/wallet/impl/epiccash_wallet.dart @@ -7,7 +7,6 @@ import 'package:flutter/foundation.dart'; import 'package:isar_community/isar.dart'; import 'package:mutex/mutex.dart'; import 'package:stack_wallet_backup/generate_password.dart'; -import 'package:web_socket_channel/web_socket_channel.dart'; import '../../../exceptions/main_db/main_db_exception.dart'; import '../../../exceptions/wallet/node_tor_mismatch_config_exception.dart'; @@ -41,6 +40,7 @@ import '../../crypto_currency/crypto_currency.dart'; import '../../models/tx_data.dart'; import '../intermediate/bip39_wallet.dart'; import '../supporting/epiccash_wallet_info_extension.dart'; +import '../supporting/epicbox_probe.dart'; import '../supporting/restore_progress.dart'; // @@ -556,21 +556,10 @@ class EpiccashWallet extends Bip39Wallet { Future _testEpicboxServer(EpicBoxConfigModel epicboxConfig) async { _hackedCheckTorNodePrefs(); - final host = epicboxConfig.host; - final port = epicboxConfig.port ?? 443; - WebSocketChannel? channel; + final String host = epicboxConfig.host; + final int port = epicboxConfig.port ?? 443; try { - final uri = Uri.parse('wss://$host:$port'); - - channel = WebSocketChannel.connect(uri); - - await channel.ready; - - final response = await channel.stream.first.timeout( - const Duration(seconds: 2), - ); - - return response is String && response.contains("Challenge"); + return await probeEpicboxServer(Uri.parse('wss://$host:$port')); } catch (e, s) { Logging.instance.w( "_testEpicboxServer failed on \"$host:$port\"", @@ -578,8 +567,6 @@ class EpiccashWallet extends Bip39Wallet { stackTrace: s, ); return false; - } finally { - await channel?.sink.close(); } } diff --git a/lib/wallets/wallet/supporting/epicbox_probe.dart b/lib/wallets/wallet/supporting/epicbox_probe.dart new file mode 100644 index 0000000000..b7370ae079 --- /dev/null +++ b/lib/wallets/wallet/supporting/epicbox_probe.dart @@ -0,0 +1,23 @@ +import 'dart:async'; + +import 'package:web_socket_channel/web_socket_channel.dart'; + +/// Connects to an Epicbox relay at [uri] and returns whether it greets with +/// a challenge. Throws if the connection fails or times out. +Future probeEpicboxServer( + Uri uri, { + Duration connectTimeout = const Duration(seconds: 10), + Duration replyTimeout = const Duration(seconds: 2), +}) async { + WebSocketChannel? channel; + try { + channel = WebSocketChannel.connect(uri); + await channel.ready.timeout(connectTimeout); + final dynamic response = await channel.stream.first.timeout(replyTimeout); + return response is String && response.contains("Challenge"); + } finally { + // After a failed connection sink.close() never completes, so awaiting it + // here would leave the caller (a send in progress) waiting forever. + channel?.sink.close().ignore(); + } +} diff --git a/test/models/epicbox_config_model_test.dart b/test/models/epicbox_config_model_test.dart new file mode 100644 index 0000000000..0f4170a85c --- /dev/null +++ b/test/models/epicbox_config_model_test.dart @@ -0,0 +1,86 @@ +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:stackwallet/models/epicbox_config_model.dart'; + +void main() { + test('serializes the insecure flag under the key the plugin reads', () { + final config = EpicBoxConfigModel( + host: 'relay.example', + port: 8443, + protocolInsecure: true, + addressIndex: 2, + ); + + final json = jsonDecode(config.toString()) as Map; + + expect(json['epicbox_protocol_unsecure'], true); + expect(json.containsKey('epicbox_protocol_insecure'), isFalse); + expect(json['epicbox_domain'], 'relay.example'); + expect(json['epicbox_port'], 8443); + expect(json['epicbox_address_index'], 2); + }); + + test('round-trips through fromString', () { + final config = EpicBoxConfigModel( + host: 'relay.example', + port: 8443, + protocolInsecure: true, + addressIndex: 1, + ); + + final restored = EpicBoxConfigModel.fromString(config.toString()); + + expect(restored.host, 'relay.example'); + expect(restored.port, 8443); + expect(restored.protocolInsecure, true); + expect(restored.addressIndex, 1); + }); + + test('reads configs saved with the earlier key names', () { + final saved = EpicBoxConfigModel.fromString( + jsonEncode({ + 'epicbox_domain': 'relay.example', + 'epicbox_port': 443, + 'epicbox_protocol_insecure': true, + 'epicbox_address_index': 0, + }), + ); + expect(saved.protocolInsecure, true); + + final legacy = EpicBoxConfigModel.fromString( + jsonEncode({ + 'domain': 'relay.example', + 'port': 443, + 'protocol_insecure': true, + 'address_index': 0, + }), + ); + expect(legacy.host, 'relay.example'); + expect(legacy.protocolInsecure, true); + }); + + test('defaults to a secure connection when no flag was saved', () { + final config = EpicBoxConfigModel.fromString( + jsonEncode({'epicbox_domain': 'relay.example', 'epicbox_port': 443}), + ); + expect(config.protocolInsecure, false); + expect(config.addressIndex, 0); + }); + + test('copyWith applies its arguments', () { + final config = EpicBoxConfigModel(host: 'relay.example', port: 443); + + final changed = config.copyWith( + port: 8443, + protocolInsecure: true, + addressIndex: 3, + ); + + expect(changed.host, 'relay.example'); + expect(changed.port, 8443); + expect(changed.protocolInsecure, true); + expect(changed.addressIndex, 3); + expect(config.copyWith().port, 443); + }); +} diff --git a/test/wallets/epicbox_probe_test.dart b/test/wallets/epicbox_probe_test.dart new file mode 100644 index 0000000000..ed1683c3fc --- /dev/null +++ b/test/wallets/epicbox_probe_test.dart @@ -0,0 +1,59 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:stackwallet/wallets/wallet/supporting/epicbox_probe.dart'; + +void main() { + test('accepts a relay that greets with a challenge', () async { + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() => server.close(force: true)); + server.listen((request) async { + final socket = await WebSocketTransformer.upgrade(request); + socket.add('{"type":"Challenge","str":"abc"}'); + }); + + expect( + await probeEpicboxServer(Uri.parse('ws://127.0.0.1:${server.port}')), + isTrue, + ); + }); + + test('rejects a relay that greets with something else', () async { + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() => server.close(force: true)); + server.listen((request) async { + final socket = await WebSocketTransformer.upgrade(request); + socket.add('hello'); + }); + + expect( + await probeEpicboxServer(Uri.parse('ws://127.0.0.1:${server.port}')), + isFalse, + ); + }); + + test('fails promptly when nothing listens on the port', () async { + final reserved = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0); + final port = reserved.port; + await reserved.close(); + + await expectLater( + probeEpicboxServer(Uri.parse('ws://127.0.0.1:$port')) + .timeout(const Duration(seconds: 15)), + throwsA(isNot(isA())), + ); + }); + + test('fails promptly on a TLS handshake with a plain server', () async { + final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + addTearDown(() => server.close(force: true)); + server.listen((request) => request.response.close()); + + await expectLater( + probeEpicboxServer(Uri.parse('wss://127.0.0.1:${server.port}')) + .timeout(const Duration(seconds: 15)), + throwsA(isNot(isA())), + ); + }); +}