From e1c6878390f94b3907eead447e09c86eee29eb11 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Tue, 10 Nov 2026 17:17:04 -0600 Subject: [PATCH 1/3] Show an error when the Epicbox relay cannot be reached The pre-send relay check awaited sink.close() in a finally block. After a failed connection that future never completes, so a send to an unreachable or misconfigured relay left the sending dialog up forever instead of reporting that the relay could not be reached. Move the check into probeEpicboxServer, close the channel without waiting, and bound the connect with a timeout. --- lib/wallets/wallet/impl/epiccash_wallet.dart | 21 ++----- .../wallet/supporting/epicbox_probe.dart | 23 ++++++++ test/wallets/epicbox_probe_test.dart | 59 +++++++++++++++++++ 3 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 lib/wallets/wallet/supporting/epicbox_probe.dart create mode 100644 test/wallets/epicbox_probe_test.dart 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/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())), + ); + }); +} From 104e01000e039c089e3affb4d9d303305ef516af Mon Sep 17 00:00:00 2001 From: sneurlax Date: Tue, 10 Nov 2026 17:17:04 -0600 Subject: [PATCH 2/3] Store the Epicbox insecure flag under the plugin's key EpicboxConfig in the plugin reads epicbox_protocol_unsecure, but the model wrote epicbox_protocol_insecure, so the flag never reached it. Write the plugin's key and keep reading the two older spellings. Also make copyWith on the Epicbox and MWCMQS config models apply their arguments. --- lib/models/epicbox_config_model.dart | 21 +++--- lib/models/mwcmqs_config_model.dart | 2 +- test/models/epicbox_config_model_test.dart | 86 ++++++++++++++++++++++ 3 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 test/models/epicbox_config_model_test.dart 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/test/models/epicbox_config_model_test.dart b/test/models/epicbox_config_model_test.dart new file mode 100644 index 0000000000..b76919ad4a --- /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', () { + const 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', () { + const 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', () { + const 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); + }); +} From de92122ac28bc6069abe6c518570a2a1d0a8a629 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Fri, 13 Nov 2026 15:47:46 -0600 Subject: [PATCH 3/3] test: construct Epicbox configs on staging --- test/models/epicbox_config_model_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/models/epicbox_config_model_test.dart b/test/models/epicbox_config_model_test.dart index b76919ad4a..0f4170a85c 100644 --- a/test/models/epicbox_config_model_test.dart +++ b/test/models/epicbox_config_model_test.dart @@ -5,7 +5,7 @@ import 'package:stackwallet/models/epicbox_config_model.dart'; void main() { test('serializes the insecure flag under the key the plugin reads', () { - const config = EpicBoxConfigModel( + final config = EpicBoxConfigModel( host: 'relay.example', port: 8443, protocolInsecure: true, @@ -22,7 +22,7 @@ void main() { }); test('round-trips through fromString', () { - const config = EpicBoxConfigModel( + final config = EpicBoxConfigModel( host: 'relay.example', port: 8443, protocolInsecure: true, @@ -69,7 +69,7 @@ void main() { }); test('copyWith applies its arguments', () { - const config = EpicBoxConfigModel(host: 'relay.example', port: 443); + final config = EpicBoxConfigModel(host: 'relay.example', port: 443); final changed = config.copyWith( port: 8443,