From 4e3390a7c09317b7082783a9c704d7c26714117e Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 15 Sep 2023 13:51:20 -0600 Subject: [PATCH] refactor TorService --- lib/electrumx_rpc/electrumx.dart | 6 +- lib/main.dart | 4 +- lib/pages/buy_view/buy_view.dart | 6 +- lib/pages/ordinals/ordinal_details_view.dart | 15 +-- .../global_settings_view/about_view.dart | 10 +- .../tor_settings/tor_settings_view.dart | 19 ++-- .../wallet_network_settings_view.dart | 101 +++++++++--------- .../desktop_buy/desktop_buy_view.dart | 13 +-- .../desktop_ordinal_details_view.dart | 5 +- .../tor_settings/tor_settings.dart | 16 +-- lib/services/buy/simplex/simplex_api.dart | 8 +- lib/services/coins/banano/banano_wallet.dart | 24 ++--- lib/services/coins/nano/nano_wallet.dart | 24 ++--- lib/services/coins/tezos/tezos_wallet.dart | 21 ++-- lib/services/ethereum/ethereum_api.dart | 50 +++++---- .../exchange/change_now/change_now_api.dart | 15 +-- .../majestic_bank/majestic_bank_api.dart | 5 +- .../exchange/simpleswap/simpleswap_api.dart | 10 +- .../exchange/trocador/trocador_api.dart | 5 +- lib/services/litescribe_api.dart | 5 +- lib/services/monkey_service.dart | 5 +- lib/services/nano_api.dart | 10 +- lib/services/price.dart | 15 +-- lib/services/tor_service.dart | 80 ++++++-------- lib/themes/theme_service.dart | 10 +- lib/utilities/paynym_is_api.dart | 5 +- lib/utilities/test_epic_box_connection.dart | 2 +- .../test_stellar_node_connection.dart | 5 +- .../desktop/desktop_tor_status_button.dart | 23 +--- lib/widgets/small_tor_icon.dart | 4 +- test/electrumx_test.mocks.dart | 2 +- .../node_options_sheet_test.mocks.dart | 2 +- 32 files changed, 257 insertions(+), 268 deletions(-) diff --git a/lib/electrumx_rpc/electrumx.dart b/lib/electrumx_rpc/electrumx.dart index 292679342..dc07890cf 100644 --- a/lib/electrumx_rpc/electrumx.dart +++ b/lib/electrumx_rpc/electrumx.dart @@ -187,8 +187,8 @@ class ElectrumX { void _checkRpcClient() { // If we're supposed to use Tor... if (_prefs.useTor) { - // But Tor isn't enabled... - if (!_torService.enabled) { + // 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. @@ -203,7 +203,7 @@ class ElectrumX { } } else { // Get the proxy info from the TorService. - final proxyInfo = _torService.proxyInfo; + final proxyInfo = _torService.getProxyInfo(); if (currentFailoverIndex == -1) { _rpcClient ??= JsonRPC( diff --git a/lib/main.dart b/lib/main.dart index 9d9b3e879..dbab8bbb4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -173,7 +173,9 @@ void main() async { // Some refactoring will need to be done here to make sure we don't make any // network calls before starting up tor if (Prefs.instance.useTor) { - TorService.sharedInstance.init(); + TorService.sharedInstance.init( + torDataDirPath: (await StackFileSystem.applicationTorDirectory()).path, + ); await TorService.sharedInstance.start(); } diff --git a/lib/pages/buy_view/buy_view.dart b/lib/pages/buy_view/buy_view.dart index fa8b5d58c..e87fa3398 100644 --- a/lib/pages/buy_view/buy_view.dart +++ b/lib/pages/buy_view/buy_view.dart @@ -38,14 +38,16 @@ class BuyView extends ConsumerStatefulWidget { class _BuyViewState extends ConsumerState { Coin? coin; EthContract? tokenContract; - late bool torEnabled = false; + + late bool torEnabled; @override void initState() { coin = widget.coin; tokenContract = widget.tokenContract; - torEnabled = ref.read(pTorService).enabled; + torEnabled = + ref.read(pTorService).status != TorConnectionStatus.disconnected; super.initState(); } diff --git a/lib/pages/ordinals/ordinal_details_view.dart b/lib/pages/ordinals/ordinal_details_view.dart index 17a518bf9..b0e478535 100644 --- a/lib/pages/ordinals/ordinal_details_view.dart +++ b/lib/pages/ordinals/ordinal_details_view.dart @@ -12,6 +12,7 @@ import 'package:stackwallet/models/isar/ordinal.dart'; import 'package:stackwallet/networking/http.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/providers/db/main_db_provider.dart'; +import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart'; import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/themes/stack_colors.dart'; @@ -20,7 +21,6 @@ import 'package:stackwallet/utilities/amount/amount_formatter.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; -import 'package:stackwallet/utilities/prefs.dart'; import 'package:stackwallet/utilities/show_loading.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/widgets/background.dart'; @@ -219,7 +219,7 @@ class _DetailsItemWCopy extends StatelessWidget { } } -class _OrdinalImageGroup extends StatelessWidget { +class _OrdinalImageGroup extends ConsumerWidget { const _OrdinalImageGroup({ Key? key, required this.walletId, @@ -231,13 +231,14 @@ class _OrdinalImageGroup extends StatelessWidget { static const _spacing = 12.0; - Future _savePngToFile() async { + Future _savePngToFile(WidgetRef ref) async { HTTP client = HTTP(); final response = await client.get( url: Uri.parse(ordinal.content), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: ref.read(prefsChangeNotifierProvider).useTor + ? ref.read(pTorService).getProxyInfo() + : null, ); if (response.code != 200) { @@ -268,7 +269,7 @@ class _OrdinalImageGroup extends StatelessWidget { } @override - Widget build(BuildContext context) { + Widget build(BuildContext context, WidgetRef ref) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, @@ -318,7 +319,7 @@ class _OrdinalImageGroup extends StatelessWidget { onPressed: () async { bool didError = false; final filePath = await showLoading( - whileFuture: _savePngToFile(), + whileFuture: _savePngToFile(ref), context: context, isDesktop: true, message: "Saving ordinal image", diff --git a/lib/pages/settings_views/global_settings_view/about_view.dart b/lib/pages/settings_views/global_settings_view/about_view.dart index 0d3e8d811..1c00b2891 100644 --- a/lib/pages/settings_views/global_settings_view/about_view.dart +++ b/lib/pages/settings_views/global_settings_view/about_view.dart @@ -50,8 +50,9 @@ Future doesCommitExist( final commitQuery = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final response = jsonDecode(commitQuery.body.toString()); @@ -89,8 +90,9 @@ Future isHeadCommit( final commitQuery = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final response = jsonDecode(commitQuery.body.toString()); diff --git a/lib/pages/settings_views/global_settings_view/tor_settings/tor_settings_view.dart b/lib/pages/settings_views/global_settings_view/tor_settings/tor_settings_view.dart index 097a68b93..5bcec8571 100644 --- a/lib/pages/settings_views/global_settings_view/tor_settings/tor_settings_view.dart +++ b/lib/pages/settings_views/global_settings_view/tor_settings/tor_settings_view.dart @@ -20,6 +20,7 @@ import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/conditional_parent.dart'; @@ -270,9 +271,7 @@ class _TorIconState extends ConsumerState { @override void initState() { - _status = ref.read(pTorService).enabled - ? TorConnectionStatus.connected - : TorConnectionStatus.disconnected; + _status = ref.read(pTorService).status; super.initState(); } @@ -399,9 +398,7 @@ class _TorButtonState extends ConsumerState { @override void initState() { - _status = ref.read(pTorService).enabled - ? TorConnectionStatus.connected - : TorConnectionStatus.disconnected; + _status = ref.read(pTorService).status; super.initState(); } @@ -455,11 +452,11 @@ class _TorButtonState extends ConsumerState { /// /// Returns a Future that completes when the Tor service has started. Future _connectTor(WidgetRef ref, BuildContext context) async { - // Init the Tor service if it hasn't already been. - ref.read(pTorService).init(); - - // Start the Tor service. try { + // Init the Tor service if it hasn't already been. + final torDir = await StackFileSystem.applicationTorDirectory(); + ref.read(pTorService).init(torDataDirPath: torDir.path); + // Start the Tor service. await ref.read(pTorService).start(); // Toggle the useTor preference on success. @@ -485,7 +482,7 @@ Future _connectTor(WidgetRef ref, BuildContext context) async { Future _disconnectTor(WidgetRef ref, BuildContext context) async { // Stop the Tor service. try { - await ref.read(pTorService).stop(); + await ref.read(pTorService).disable(); // Toggle the useTor preference on success. ref.read(prefsChangeNotifierProvider).useTor = false; diff --git a/lib/pages/settings_views/wallet_settings_view/wallet_network_settings_view/wallet_network_settings_view.dart b/lib/pages/settings_views/wallet_settings_view/wallet_network_settings_view/wallet_network_settings_view.dart index 48f805691..ad91b3b07 100644 --- a/lib/pages/settings_views/wallet_settings_view/wallet_network_settings_view/wallet_network_settings_view.dart +++ b/lib/pages/settings_views/wallet_settings_view/wallet_network_settings_view/wallet_network_settings_view.dart @@ -36,6 +36,7 @@ import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/animated_text.dart'; @@ -48,6 +49,7 @@ import 'package:stackwallet/widgets/progress_bar.dart'; import 'package:stackwallet/widgets/rounded_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/stack_dialog.dart'; +import 'package:stackwallet/widgets/tor_subscription.dart'; import 'package:tuple/tuple.dart'; import 'package:wakelock/wakelock.dart'; @@ -98,10 +100,6 @@ class _WalletNetworkSettingsViewState /// The current status of the Tor connection. late TorConnectionStatus _torConnectionStatus; - /// The subscription to the TorConnectionStatusChangedEvent. - late final StreamSubscription - _torConnectionStatusSubscription; - Future _attemptRescan() async { if (!Platform.isLinux) await Wakelock.enable(); @@ -280,22 +278,7 @@ class _WalletNetworkSettingsViewState // ); // Initialize the TorConnectionStatus. - _torConnectionStatus = ref.read(pTorService).enabled - ? TorConnectionStatus.connected - : TorConnectionStatus.disconnected; - - // Subscribe to the TorConnectionStatusChangedEvent. - _torConnectionStatusSubscription = - eventBus.on().listen( - (event) async { - // Rebuild the widget. - setState(() { - _torConnectionStatus = event.newStatus; - }); - - // TODO implement spinner or animations and control from here - }, - ); + _torConnectionStatus = ref.read(pTorService).status; super.initState(); } @@ -306,7 +289,6 @@ class _WalletNetworkSettingsViewState _syncStatusSubscription.cancel(); _refreshSubscription.cancel(); _blocksRemainingSubscription?.cancel(); - _torConnectionStatusSubscription.cancel(); super.dispose(); } @@ -793,7 +775,7 @@ class _WalletNetworkSettingsViewState onTap: () async { // Stop the Tor service. try { - await ref.read(pTorService).stop(); + await ref.read(pTorService).disable(); // Toggle the useTor preference on success. ref.read(prefsChangeNotifierProvider).useTor = false; @@ -813,11 +795,12 @@ class _WalletNetworkSettingsViewState prefsChangeNotifierProvider.select((value) => value.useTor))) GestureDetector( onTap: () async { - // Init the Tor service if it hasn't already been. - ref.read(pTorService).init(); - - // Start the Tor service. try { + // Init the Tor service if it hasn't already been. + final torDir = + await StackFileSystem.applicationTorDirectory(); + ref.read(pTorService).init(torDataDirPath: torDir.path); + // Start the Tor service. await ref.read(pTorService).start(); // Toggle the useTor preference on success. @@ -827,6 +810,7 @@ class _WalletNetworkSettingsViewState "Error starting tor: $e\n$s", level: LogLevel.Error, ); + // TODO: show dialog with error message } }, child: Text( @@ -896,35 +880,46 @@ class _WalletNetworkSettingsViewState SizedBox( width: _boxPadding, ), - Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Tor status", - style: STextStyles.desktopTextExtraExtraSmall(context) - .copyWith( - color: Theme.of(context) - .extension()! - .textDark, - ), - ), - if (_torConnectionStatus == TorConnectionStatus.connected) + TorSubscription( + onTorStatusChanged: (status) { + setState(() { + _torConnectionStatus = status; + }); + }, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ Text( - "Connected", - style: STextStyles.desktopTextExtraExtraSmall(context), + "Tor status", + style: STextStyles.desktopTextExtraExtraSmall(context) + .copyWith( + color: Theme.of(context) + .extension()! + .textDark, + ), ), - if (_torConnectionStatus == TorConnectionStatus.connecting) - Text( - "Connecting...", - style: STextStyles.desktopTextExtraExtraSmall(context), - ), - if (_torConnectionStatus == - TorConnectionStatus.disconnected) - Text( - "Disconnected", - style: STextStyles.desktopTextExtraExtraSmall(context), - ), - ], + if (_torConnectionStatus == TorConnectionStatus.connected) + Text( + "Connected", + style: + STextStyles.desktopTextExtraExtraSmall(context), + ), + if (_torConnectionStatus == + TorConnectionStatus.connecting) + Text( + "Connecting...", + style: + STextStyles.desktopTextExtraExtraSmall(context), + ), + if (_torConnectionStatus == + TorConnectionStatus.disconnected) + Text( + "Disconnected", + style: + STextStyles.desktopTextExtraExtraSmall(context), + ), + ], + ), ), ], ), diff --git a/lib/pages_desktop_specific/desktop_buy/desktop_buy_view.dart b/lib/pages_desktop_specific/desktop_buy/desktop_buy_view.dart index 08beccd17..b9c00c762 100644 --- a/lib/pages_desktop_specific/desktop_buy/desktop_buy_view.dart +++ b/lib/pages_desktop_specific/desktop_buy/desktop_buy_view.dart @@ -31,11 +31,12 @@ class DesktopBuyView extends ConsumerStatefulWidget { } class _DesktopBuyViewState extends ConsumerState { - late bool torEnabled = false; + late bool torEnabled; @override void initState() { - torEnabled = ref.read(pTorService).enabled; + torEnabled = + ref.read(pTorService).status != TorConnectionStatus.disconnected; super.initState(); } @@ -62,8 +63,8 @@ class _DesktopBuyViewState extends ConsumerState { ), ), ), - body: Padding( - padding: const EdgeInsets.only( + body: const Padding( + padding: EdgeInsets.only( left: 24, right: 24, bottom: 24, @@ -75,7 +76,7 @@ class _DesktopBuyViewState extends ConsumerState { child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, - children: const [ + children: [ SizedBox( height: 16, ), @@ -86,7 +87,7 @@ class _DesktopBuyViewState extends ConsumerState { ], ), ), - const SizedBox( + SizedBox( width: 16, ), // Expanded( diff --git a/lib/pages_desktop_specific/ordinals/desktop_ordinal_details_view.dart b/lib/pages_desktop_specific/ordinals/desktop_ordinal_details_view.dart index f1c6093aa..44135c3b2 100644 --- a/lib/pages_desktop_specific/ordinals/desktop_ordinal_details_view.dart +++ b/lib/pages_desktop_specific/ordinals/desktop_ordinal_details_view.dart @@ -56,8 +56,9 @@ class _DesktopOrdinalDetailsViewState final response = await client.get( url: Uri.parse(widget.ordinal.content), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code != 200) { diff --git a/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart b/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart index 212387035..c58e81121 100644 --- a/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart +++ b/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart @@ -22,6 +22,7 @@ import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/custom_buttons/draggable_switch_button.dart'; @@ -59,11 +60,11 @@ class _TorSettingsState extends ConsumerState { width: 200, buttonHeight: ButtonHeight.m, onPressed: () async { - // Init the Tor service if it hasn't already been. - ref.read(pTorService).init(); - - // Start the Tor service. try { + // Init the Tor service if it hasn't already been. + final torDir = await StackFileSystem.applicationTorDirectory(); + ref.read(pTorService).init(torDataDirPath: torDir.path); + // Start the Tor service. await ref.read(pTorService).start(); // Toggle the useTor preference on success. @@ -73,6 +74,7 @@ class _TorSettingsState extends ConsumerState { "Error starting tor: $e\n$s", level: LogLevel.Error, ); + // TODO: show dialog with error message } }, ); @@ -93,7 +95,7 @@ class _TorSettingsState extends ConsumerState { onPressed: () async { // Stop the Tor service. try { - await ref.read(pTorService).stop(); + await ref.read(pTorService).disable(); // Toggle the useTor preference on success. ref.read(prefsChangeNotifierProvider).useTor = false; @@ -114,9 +116,7 @@ class _TorSettingsState extends ConsumerState { eventBus = GlobalEventBus.instance; // Set the initial Tor connection status. - _torConnectionStatus = ref.read(pTorService).enabled - ? TorConnectionStatus.connected - : TorConnectionStatus.disconnected; + _torConnectionStatus = ref.read(pTorService).status; // Subscribe to the TorConnectionStatusChangedEvent. _torConnectionStatusSubscription = diff --git a/lib/services/buy/simplex/simplex_api.dart b/lib/services/buy/simplex/simplex_api.dart index 1b2d26e26..6ab2736e4 100644 --- a/lib/services/buy/simplex/simplex_api.dart +++ b/lib/services/buy/simplex/simplex_api.dart @@ -59,7 +59,7 @@ class SimplexAPI { url: url, headers: headers, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + Prefs.instance.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); if (res.code != 200) { throw Exception( @@ -125,7 +125,7 @@ class SimplexAPI { url: url, headers: headers, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + Prefs.instance.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); if (res.code != 200) { throw Exception( @@ -206,7 +206,7 @@ class SimplexAPI { url: url, headers: headers, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + Prefs.instance.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); if (res.code != 200) { throw Exception('getQuote exception: statusCode= ${res.code}'); @@ -313,7 +313,7 @@ class SimplexAPI { url: url, headers: headers, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + Prefs.instance.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); if (res.code != 200) { throw Exception('newOrder exception: statusCode= ${res.code}'); diff --git a/lib/services/coins/banano/banano_wallet.dart b/lib/services/coins/banano/banano_wallet.dart index 9f314f3a3..735336824 100644 --- a/lib/services/coins/banano/banano_wallet.dart +++ b/lib/services/coins/banano/banano_wallet.dart @@ -160,7 +160,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { }, ), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ) .then((client) { if (client.code == 200) { @@ -195,7 +195,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: balanceBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final balanceData = jsonDecode(balanceResponse.body); @@ -215,7 +215,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: infoBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final String frontier = @@ -270,7 +270,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: processBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final Map decoded = @@ -344,7 +344,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: body, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final data = jsonDecode(response.body); _balance = Balance( @@ -388,7 +388,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: infoBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final infoData = jsonDecode(infoResponse.body); @@ -408,7 +408,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: balanceBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final balanceData = jsonDecode(balanceResponse.body); @@ -483,7 +483,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: processBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final Map decoded = @@ -504,7 +504,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { "count": "-1", }), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final receivableData = await jsonDecode(receivableResponse.body); @@ -536,7 +536,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { "count": "-1", }), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final data = await jsonDecode(response.body); final transactions = @@ -858,7 +858,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { }, ), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); return response.code == 200; @@ -952,7 +952,7 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: infoBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final infoData = jsonDecode(infoResponse.body); diff --git a/lib/services/coins/nano/nano_wallet.dart b/lib/services/coins/nano/nano_wallet.dart index 56404ed1a..bf324a95d 100644 --- a/lib/services/coins/nano/nano_wallet.dart +++ b/lib/services/coins/nano/nano_wallet.dart @@ -169,7 +169,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { }, ), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ) .then((Response response) { if (response.code == 200) { @@ -204,7 +204,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: balanceBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final balanceData = jsonDecode(balanceResponse.body); @@ -224,7 +224,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: infoBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final String frontier = @@ -279,7 +279,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: processBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final Map decoded = @@ -349,7 +349,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: body, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final data = jsonDecode(response.body); _balance = Balance( @@ -393,7 +393,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: infoBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final infoData = jsonDecode(infoResponse.body); @@ -413,7 +413,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: balanceBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final balanceData = jsonDecode(balanceResponse.body); @@ -488,7 +488,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: processBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final Map decoded = @@ -509,7 +509,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { "count": "-1", }), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final receivableData = await jsonDecode(receivableResponse.body); @@ -541,7 +541,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { "count": "-1", }), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final data = await jsonDecode(response.body); final transactions = @@ -869,7 +869,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { }, ), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); return response.code == 200; @@ -963,7 +963,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB { headers: headers, body: infoBody, proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); final infoData = jsonDecode(infoResponse.body); diff --git a/lib/services/coins/tezos/tezos_wallet.dart b/lib/services/coins/tezos/tezos_wallet.dart index 130671264..fa5abc30e 100644 --- a/lib/services/coins/tezos/tezos_wallet.dart +++ b/lib/services/coins/tezos/tezos_wallet.dart @@ -245,7 +245,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { var response = jsonDecode((await client.get( url: Uri.parse(api), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, )) .body)[0]; double totalFees = response[4] as double; @@ -270,7 +270,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { var response = jsonDecode((await client.get( url: Uri.parse(api), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, )) .body); double totalFees = response[0][4] as double; @@ -509,9 +509,8 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { var response = jsonDecode(await client .get( url: Uri.parse(balanceCall), - proxyInfo: Prefs.instance.useTor - ? TorService.sharedInstance.proxyInfo - : null, + proxyInfo: + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ) .then((value) => value.body)); Amount balanceInAmount = Amount( @@ -538,9 +537,8 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { var response = jsonDecode(await client .get( url: Uri.parse(transactionsCall), - proxyInfo: Prefs.instance.useTor - ? TorService.sharedInstance.proxyInfo - : null, + proxyInfo: + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ) .then((value) => value.body)); List> txs = []; @@ -619,9 +617,8 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { var jsonParsedResponse = jsonDecode(await client .get( url: Uri.parse(api), - proxyInfo: Prefs.instance.useTor - ? TorService.sharedInstance.proxyInfo - : null, + proxyInfo: + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ) .then((value) => value.body)); final int intHeight = int.parse(jsonParsedResponse["level"].toString()); @@ -707,7 +704,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { url: Uri.parse( "${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell"), proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + _prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, ); return true; } catch (e) { diff --git a/lib/services/ethereum/ethereum_api.dart b/lib/services/ethereum/ethereum_api.dart index 19fa6b081..13e11f7ab 100644 --- a/lib/services/ethereum/ethereum_api.dart +++ b/lib/services/ethereum/ethereum_api.dart @@ -61,8 +61,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/export?addrs=$address&firstBlock=$firstBlock", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -184,8 +185,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/transactions?transactions=${txns.map((e) => e.hash).join(" ")}&raw=true", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -244,8 +246,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/transactions?transactions=${txids.join(" ")}", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -303,8 +306,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/export?addrs=$address&emitter=$tokenContractAddress&logs=true", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -437,8 +441,9 @@ abstract class EthereumAPI { ); final response = await client.get( url: uri, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -488,8 +493,9 @@ abstract class EthereumAPI { ); final response = await client.get( url: uri, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -535,8 +541,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/gas-prices", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -606,8 +613,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/tokens?addrs=$contractAddress&parts=all", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -675,8 +683,9 @@ abstract class EthereumAPI { url: Uri.parse( "$stackBaseServer/abis?addrs=$contractAddress&verbose=true", ), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { @@ -717,8 +726,9 @@ abstract class EthereumAPI { final response = await client.get( url: Uri.parse( "$stackBaseServer/state?addrs=$contractAddress&parts=proxy"), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { final json = jsonDecode(response.body); diff --git a/lib/services/exchange/change_now/change_now_api.dart b/lib/services/exchange/change_now/change_now_api.dart index d8b260495..b847482a4 100644 --- a/lib/services/exchange/change_now/change_now_api.dart +++ b/lib/services/exchange/change_now/change_now_api.dart @@ -60,8 +60,9 @@ class ChangeNowAPI { final response = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); String? data; try { @@ -90,8 +91,9 @@ class ChangeNowAPI { // 'Content-Type': 'application/json', 'x-changenow-api-key': apiKey, }, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final data = response.body; @@ -114,8 +116,9 @@ class ChangeNowAPI { url: uri, headers: {'Content-Type': 'application/json'}, body: jsonEncode(body), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); String? data; diff --git a/lib/services/exchange/majestic_bank/majestic_bank_api.dart b/lib/services/exchange/majestic_bank/majestic_bank_api.dart index d32209ae5..695bfeb08 100644 --- a/lib/services/exchange/majestic_bank/majestic_bank_api.dart +++ b/lib/services/exchange/majestic_bank/majestic_bank_api.dart @@ -49,8 +49,9 @@ class MajesticBankAPI { try { final response = await client.get( url: uri, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); code = response.code; diff --git a/lib/services/exchange/simpleswap/simpleswap_api.dart b/lib/services/exchange/simpleswap/simpleswap_api.dart index d52f8aa21..816c57a01 100644 --- a/lib/services/exchange/simpleswap/simpleswap_api.dart +++ b/lib/services/exchange/simpleswap/simpleswap_api.dart @@ -47,8 +47,9 @@ class SimpleSwapAPI { try { final response = await client.get( url: uri, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); code = response.code; @@ -74,8 +75,9 @@ class SimpleSwapAPI { url: uri, headers: {'Content-Type': 'application/json'}, body: jsonEncode(body), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { diff --git a/lib/services/exchange/trocador/trocador_api.dart b/lib/services/exchange/trocador/trocador_api.dart index 8dadfb499..ac01af5a4 100644 --- a/lib/services/exchange/trocador/trocador_api.dart +++ b/lib/services/exchange/trocador/trocador_api.dart @@ -52,8 +52,9 @@ abstract class TrocadorAPI { final response = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); code = response.code; diff --git a/lib/services/litescribe_api.dart b/lib/services/litescribe_api.dart index ea4013457..65f1783d1 100644 --- a/lib/services/litescribe_api.dart +++ b/lib/services/litescribe_api.dart @@ -22,8 +22,9 @@ class LitescribeAPI { Future _getResponse(String endpoint) async { final response = await client.get( url: Uri.parse('$baseUrl$endpoint'), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { return LitescribeResponse(data: _validateJson(response.body)); diff --git a/lib/services/monkey_service.dart b/lib/services/monkey_service.dart index eb3687fcd..f6f804cb3 100644 --- a/lib/services/monkey_service.dart +++ b/lib/services/monkey_service.dart @@ -25,8 +25,9 @@ class MonKeyService { final response = await client.get( url: Uri.parse(url), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); if (response.code == 200) { diff --git a/lib/services/nano_api.dart b/lib/services/nano_api.dart index c0d194a31..e9035c8e9 100644 --- a/lib/services/nano_api.dart +++ b/lib/services/nano_api.dart @@ -31,8 +31,9 @@ class NanoAPI { "representative": "true", "account": account, }), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final map = jsonDecode(response.body); @@ -124,8 +125,9 @@ class NanoAPI { "subtype": "change", "block": block, }), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); return jsonDecode(response.body); diff --git a/lib/services/price.dart b/lib/services/price.dart index a4ffff961..ce1a57301 100644 --- a/lib/services/price.dart +++ b/lib/services/price.dart @@ -107,8 +107,9 @@ class PriceAPI { final coinGeckoResponse = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final coinGeckoData = jsonDecode(coinGeckoResponse.body) as List; @@ -154,8 +155,9 @@ class PriceAPI { final response = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final json = jsonDecode(response.body) as List; @@ -195,8 +197,9 @@ class PriceAPI { final coinGeckoResponse = await client.get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final coinGeckoData = jsonDecode(coinGeckoResponse.body) as Map; diff --git a/lib/services/tor_service.dart b/lib/services/tor_service.dart index ba5400ca6..c4e7a0117 100644 --- a/lib/services/tor_service.dart +++ b/lib/services/tor_service.dart @@ -10,38 +10,45 @@ final pTorService = Provider((_) => TorService.sharedInstance); class TorService { Tor? _tor; + String? _torDataDirPath; /// Current status. Same as that fired on the event bus TorConnectionStatus get status => _status; - // set to some default value TorConnectionStatus _status = TorConnectionStatus.disconnected; - /// Flag to indicate that a Tor circuit is thought to have been established. - bool _enabled = false; - - /// Getter for the enabled flag. - bool get enabled => _enabled; - - TorService._(); - /// Singleton instance of the TorService. /// /// Use this to access the TorService and its properties. static final sharedInstance = TorService._(); + // private constructor for singleton + TorService._(); + /// Getter for the proxyInfo. + /// + /// Returns null if disabled on the stack wallet level. ({ InternetAddress host, int port, - }) get proxyInfo => ( + }) getProxyInfo() { + if (status == TorConnectionStatus.connected) { + return ( host: InternetAddress.loopbackIPv4, - port: _tor!.port!, + port: _tor!.port, ); + } else { + throw Exception("Tor proxy info fetched while not connected!"); + } + } /// Initialize the tor ffi lib instance if it hasn't already been set. Nothing /// changes if _tor is already been set. - void init({Tor? mockableOverride}) { + void init({ + required String torDataDirPath, + Tor? mockableOverride, + }) { _tor ??= mockableOverride ?? Tor.instance; + _torDataDirPath ??= torDataDirPath; } /// Start the Tor service. @@ -52,31 +59,25 @@ class TorService { /// /// Returns a Future that completes when the Tor service has started. Future start() async { - if (_tor == null) { + if (_tor == null || _torDataDirPath == null) { throw Exception("TorService.init has not been called!"); } - if (_enabled) { - // already started so just return - return; - } - // Start the Tor service. try { _updateStatusAndFireEvent( status: TorConnectionStatus.connecting, - message: "Tor connection status changed: connecting", + message: "TorService.start call in progress", ); - await _tor!.start(); + await _tor!.start(torDataDirPath: _torDataDirPath!); + // no exception or error so we can (probably?) assume tor // has started successfully - _enabled = true; - // Fire a TorConnectionStatusChangedEvent on the event bus. _updateStatusAndFireEvent( status: TorConnectionStatus.connected, - message: "Tor connection status changed: connect ($_enabled)", + message: "TorService.start call success", ); } catch (e, s) { Logging.instance.log( @@ -88,42 +89,27 @@ class TorService { // Fire a TorConnectionStatusChangedEvent on the event bus. _updateStatusAndFireEvent( status: TorConnectionStatus.disconnected, - message: "Tor connection status changed: $_enabled (failed)", + message: "TorService.start call failed", ); rethrow; } } - Future stop() async { + /// disable tor + Future disable() async { if (_tor == null) { throw Exception("TorService.init has not been called!"); } - if (!_enabled) { - // already stopped so just return - // could throw an exception here or something so the caller - // is explicitly made aware of this - // TODO make sure to kill + // no need to update status and fire event if status won't change + if (_status == TorConnectionStatus.disconnected) { return; } - // Stop the Tor service. - try { - _tor!.disable(); - // no exception or error so we can (probably?) assume tor - // has started successfully - _enabled = false; - _updateStatusAndFireEvent( - status: TorConnectionStatus.disconnected, - message: "Tor connection status changed: $_enabled (disabled)", - ); - } catch (e, s) { - Logging.instance.log( - "TorService.stop failed: $e\n$s", - level: LogLevel.Warning, - ); - rethrow; - } + _updateStatusAndFireEvent( + status: TorConnectionStatus.disconnected, + message: "TorService.disable call success", + ); } void _updateStatusAndFireEvent({ diff --git a/lib/themes/theme_service.dart b/lib/themes/theme_service.dart index 62165d646..01134da66 100644 --- a/lib/themes/theme_service.dart +++ b/lib/themes/theme_service.dart @@ -213,8 +213,9 @@ class ThemeService { try { final response = await client.get( url: Uri.parse("$baseServerUrl/themes"), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final jsonList = jsonDecode(response.body) as List; @@ -240,8 +241,9 @@ class ThemeService { try { final response = await client.get( url: Uri.parse("$baseServerUrl/theme/${themeMetaData.id}"), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); final bytes = Uint8List.fromList(response.bodyBytes); diff --git a/lib/utilities/paynym_is_api.dart b/lib/utilities/paynym_is_api.dart index 860848cac..1393af4f1 100644 --- a/lib/utilities/paynym_is_api.dart +++ b/lib/utilities/paynym_is_api.dart @@ -52,8 +52,9 @@ class PaynymIsApi { url: uri, headers: headers, body: jsonEncode(body), - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ); debugPrint("Paynym request uri: $uri"); diff --git a/lib/utilities/test_epic_box_connection.dart b/lib/utilities/test_epic_box_connection.dart index ed2fd4828..600438451 100644 --- a/lib/utilities/test_epic_box_connection.dart +++ b/lib/utilities/test_epic_box_connection.dart @@ -24,7 +24,7 @@ Future _testEpicBoxNodeConnection(Uri uri) async { url: uri, headers: {'Content-Type': 'application/json'}, proxyInfo: Prefs.instance.useTor - ? TorService.sharedInstance.proxyInfo + ? TorService.sharedInstance.getProxyInfo() : null, ) .timeout(const Duration(milliseconds: 2000), diff --git a/lib/utilities/test_stellar_node_connection.dart b/lib/utilities/test_stellar_node_connection.dart index 32f756025..73d20a8aa 100644 --- a/lib/utilities/test_stellar_node_connection.dart +++ b/lib/utilities/test_stellar_node_connection.dart @@ -13,8 +13,9 @@ Future testStellarNodeConnection(String host, int port) async { .get( url: uri, headers: {'Content-Type': 'application/json'}, - proxyInfo: - Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, ) .timeout(const Duration(milliseconds: 2000), onTimeout: () async => http.Response(utf8.encode('Error'), 408)); diff --git a/lib/widgets/desktop/desktop_tor_status_button.dart b/lib/widgets/desktop/desktop_tor_status_button.dart index 42677aa0f..8b7e96ec2 100644 --- a/lib/widgets/desktop/desktop_tor_status_button.dart +++ b/lib/widgets/desktop/desktop_tor_status_button.dart @@ -81,9 +81,7 @@ class _DesktopTorStatusButtonState extends ConsumerState eventBus = GlobalEventBus.instance; // Initialize the TorConnectionStatus. - _torConnectionStatus = ref.read(pTorService).enabled - ? TorConnectionStatus.connected - : TorConnectionStatus.disconnected; + _torConnectionStatus = ref.read(pTorService).status; // Subscribe to the TorConnectionStatusChangedEvent. _torConnectionStatusSubscription = @@ -93,25 +91,6 @@ class _DesktopTorStatusButtonState extends ConsumerState setState(() { _torConnectionStatus = event.newStatus; }); - - // TODO implement spinner or animations and control from here - // switch (event.newStatus) { - // case TorConnectionStatus.disconnected: - // // if (_spinController.hasLoadedAnimation) { - // // _spinController.stop?.call(); - // // } - // break; - // case TorConnectionStatus.connecting: - // // if (_spinController.hasLoadedAnimation) { - // // _spinController.repeat?.call(); - // // } - // break; - // case TorConnectionStatus.connected: - // // if (_spinController.hasLoadedAnimation) { - // // _spinController.stop?.call(); - // // } - // break; - // } }, ); diff --git a/lib/widgets/small_tor_icon.dart b/lib/widgets/small_tor_icon.dart index c032fbdae..711a77ba5 100644 --- a/lib/widgets/small_tor_icon.dart +++ b/lib/widgets/small_tor_icon.dart @@ -37,9 +37,7 @@ class _SmallTorIconState extends ConsumerState { @override void initState() { - _status = ref.read(pTorService).enabled - ? TorConnectionStatus.connected - : TorConnectionStatus.disconnected; + _status = ref.read(pTorService).status; super.initState(); } diff --git a/test/electrumx_test.mocks.dart b/test/electrumx_test.mocks.dart index d77c3e76d..521aff526 100644 --- a/test/electrumx_test.mocks.dart +++ b/test/electrumx_test.mocks.dart @@ -693,7 +693,7 @@ class MockTorService extends _i1.Mock implements _i11.TorService { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); @override - _i4.Future stop() => (super.noSuchMethod( + _i4.Future disable() => (super.noSuchMethod( Invocation.method( #stop, [], diff --git a/test/widget_tests/node_options_sheet_test.mocks.dart b/test/widget_tests/node_options_sheet_test.mocks.dart index 203e196a0..d6d630335 100644 --- a/test/widget_tests/node_options_sheet_test.mocks.dart +++ b/test/widget_tests/node_options_sheet_test.mocks.dart @@ -1059,7 +1059,7 @@ class MockTorService extends _i1.Mock implements _i19.TorService { returnValueForMissingStub: _i12.Future.value(), ) as _i12.Future); @override - _i12.Future stop() => (super.noSuchMethod( + _i12.Future disable() => (super.noSuchMethod( Invocation.method( #stop, [],