From 3fe7f47d8b5afc42026007a328c68ebc1f71edc3 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 22 Apr 2024 17:04:57 -0600 Subject: [PATCH] clean up test epicbox connection --- lib/wallets/wallet/impl/epiccash_wallet.dart | 76 ++++++++------------ pubspec.lock | 24 +++---- pubspec.yaml | 2 +- 3 files changed, 44 insertions(+), 58 deletions(-) diff --git a/lib/wallets/wallet/impl/epiccash_wallet.dart b/lib/wallets/wallet/impl/epiccash_wallet.dart index 8228ab7c8..a6365321d 100644 --- a/lib/wallets/wallet/impl/epiccash_wallet.dart +++ b/lib/wallets/wallet/impl/epiccash_wallet.dart @@ -36,7 +36,7 @@ import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; import 'package:stackwallet/wallets/models/tx_data.dart'; import 'package:stackwallet/wallets/wallet/intermediate/bip39_wallet.dart'; import 'package:stackwallet/wallets/wallet/supporting/epiccash_wallet_info_extension.dart'; -import 'package:websocket_universal/websocket_universal.dart'; +import 'package:web_socket_channel/web_socket_channel.dart'; // // refactor of https://github.com/cypherstack/stack_wallet/blob/1d9fb4cd069f22492ece690ac788e05b8f8b1209/lib/services/coins/epiccash/epiccash_wallet.dart @@ -103,15 +103,16 @@ class EpiccashWallet extends Bip39Wallet { } Future getEpicBoxConfig() async { - EpicBoxConfigModel? _epicBoxConfig = - EpicBoxConfigModel.fromServer(DefaultEpicBoxes.defaultEpicBoxServer); + EpicBoxConfigModel? _epicBoxConfig = EpicBoxConfigModel.fromServer( + DefaultEpicBoxes.defaultEpicBoxServer, + ); //Get the default Epicbox server and check if it's conected // bool isEpicboxConnected = await _testEpicboxServer( // DefaultEpicBoxes.defaultEpicBoxServer.host, DefaultEpicBoxes.defaultEpicBoxServer.port ?? 443); // if (isEpicboxConnected) { - //Use default server for as Epicbox config + //Use default server for as Epicbox config // } // else { @@ -231,48 +232,33 @@ class EpiccashWallet extends Bip39Wallet { ); } - Future _testEpicboxServer(String host, int port) async { - // TODO use an EpicBoxServerModel as the only param - final websocketConnectionUri = 'wss://$host:$port'; - const connectionOptions = SocketConnectionOptions( - pingIntervalMs: 3000, - timeoutConnectionMs: 4000, + Future _testEpicboxServer(EpicBoxConfigModel epicboxConfig) async { + final host = epicboxConfig.host; + final port = epicboxConfig.port ?? 443; + WebSocketChannel? channel; + try { + final uri = Uri.parse('wss://$host:$port'); - /// see ping/pong messages in [logEventStream] stream - skipPingMessages: true, + channel = WebSocketChannel.connect( + uri, + ); - /// Set this attribute to `true` if do not need any ping/pong - /// messages and ping measurement. Default is `false` - pingRestrictionForce: true, - ); + await channel.ready; - final IMessageProcessor textSocketProcessor = - SocketSimpleTextProcessor(); - final textSocketHandler = IWebSocketHandler.createClient( - websocketConnectionUri, - textSocketProcessor, - connectionOptions: connectionOptions, - ); + final response = await channel.stream.first.timeout( + const Duration(seconds: 2), + ); - // Listening to server responses: - bool isConnected = true; - textSocketHandler.incomingMessagesStream.listen((inMsg) { + return response is String && response.contains("Challenge"); + } catch (_) { Logging.instance.log( - '> webSocket got text message from server: "$inMsg" ' - '[ping: ${textSocketHandler.pingDelayMs}]', - level: LogLevel.Info); - }); - - // Connecting to server: - final isTextSocketConnected = await textSocketHandler.connect(); - if (!isTextSocketConnected) { - // ignore: avoid_print - Logging.instance.log( - 'Connection to [$websocketConnectionUri] failed for some reason!', - level: LogLevel.Error); - isConnected = false; + "_testEpicBoxConnection failed on \"$host:$port\"", + level: LogLevel.Info, + ); + return false; + } finally { + await channel?.sink.close(); } - return isConnected; } Future _putSendToAddresses( @@ -335,9 +321,9 @@ class EpiccashWallet extends Bip39Wallet { return address; } - Future
thisWalletAddress(int index, EpicBoxConfigModel epicboxConfig) async { - final wallet = - await secureStorageInterface.read(key: '${walletId}_wallet'); + Future
thisWalletAddress( + int index, EpicBoxConfigModel epicboxConfig) async { + final wallet = await secureStorageInterface.read(key: '${walletId}_wallet'); // EpicBoxConfigModel epicboxConfig = await getEpicBoxConfig(); final walletAddress = await epiccash.LibEpiccash.getAddressInfo( @@ -594,7 +580,8 @@ class EpiccashWallet extends Bip39Wallet { if (!receiverAddress.startsWith("http://") || !receiverAddress.startsWith("https://")) { bool isEpicboxConnected = await _testEpicboxServer( - epicboxConfig.host, epicboxConfig.port ?? 443); + epicboxConfig, + ); if (!isEpicboxConnected) { throw Exception("Failed to send TX : Unable to reach epicbox server"); } @@ -934,7 +921,6 @@ class EpiccashWallet extends Bip39Wallet { .findAll(); final myAddressesSet = myAddresses.toSet(); - final transactions = await epiccash.LibEpiccash.getTransactions( wallet: wallet!, refreshFromNode: refreshFromNode, diff --git a/pubspec.lock b/pubspec.lock index d5445f376..31815a754 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1970,6 +1970,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" + web: + dependency: transitive + description: + name: web + sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27" + url: "https://pub.dev" + source: hosted + version: "0.5.1" web3dart: dependency: "direct main" description: @@ -1979,13 +1987,13 @@ packages: source: hosted version: "2.6.1" web_socket_channel: - dependency: transitive + dependency: "direct main" description: name: web_socket_channel - sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b + sha256: "58c6666b342a38816b2e7e50ed0f1e261959630becd4c879c4f26bfa14aa5a42" url: "https://pub.dev" source: hosted - version: "2.4.0" + version: "2.4.5" webdriver: dependency: transitive description: @@ -2002,14 +2010,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.1" - websocket_universal: - dependency: "direct main" - description: - name: websocket_universal - sha256: "681e3050bd70b9c94617394f87a021746d24d4b3c8302be8c6b0b2a3e4209d7f" - url: "https://pub.dev" - source: hosted - version: "0.5.2" win32: dependency: transitive description: @@ -2076,5 +2076,5 @@ packages: source: hosted version: "1.0.0" sdks: - dart: ">=3.2.0 <4.0.0" + dart: ">=3.3.0 <4.0.0" flutter: ">=3.16.0" diff --git a/pubspec.yaml b/pubspec.yaml index 795c34a90..7680653a6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,7 +22,7 @@ dependencies: sdk: flutter ffi: ^2.0.1 mutex: ^3.0.0 - websocket_universal: ^0.5.1 + web_socket_channel: ^2.4.5 lelantus: path: ./crypto_plugins/flutter_liblelantus