Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/models/epicbox_config_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/models/mwcmqs_config_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> toMap() {
Expand Down
21 changes: 4 additions & 17 deletions lib/wallets/wallet/impl/epiccash_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';

//
Expand Down Expand Up @@ -556,30 +556,17 @@ class EpiccashWallet extends Bip39Wallet {

Future<bool> _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\"",
error: e,
stackTrace: s,
);
return false;
} finally {
await channel?.sink.close();
}
}

Expand Down
23 changes: 23 additions & 0 deletions lib/wallets/wallet/supporting/epicbox_probe.dart
Original file line number Diff line number Diff line change
@@ -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<bool> 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();
}
}
86 changes: 86 additions & 0 deletions test/models/epicbox_config_model_test.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic>;

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);
});
}
59 changes: 59 additions & 0 deletions test/wallets/epicbox_probe_test.dart
Original file line number Diff line number Diff line change
@@ -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<TimeoutException>())),
);
});

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<TimeoutException>())),
);
});
}
Loading