diff --git a/lib/anonpay/anonpay_api.dart b/lib/anonpay/anonpay_api.dart index e46499407..1a51e1001 100644 --- a/lib/anonpay/anonpay_api.dart +++ b/lib/anonpay/anonpay_api.dart @@ -137,6 +137,7 @@ class AnonPayApi { crypto: cryptoCurrency, fiat: fiatCurrency, torOnly: useTorOnly, + onionOnly: false,// TODO: CW-519 ); } diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index b3b97e8cd..ce777a3c1 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -17,6 +17,7 @@ Future _fetchPrice(Map args) async { final crypto = args['crypto'] as String; final fiat = args['fiat'] as String; final torOnly = args['torOnly'] as bool; + final onionOnly = args['onionOnly'] as bool; final mainThreadProxyPort = args['port'] as int; final Map queryParams = { @@ -42,30 +43,36 @@ Future _fetchPrice(Map args) async { // we might have tor enabled (no way of knowing), so we try to use it first try { + // connect through onion url first: try { final request = await client.getUrl(onionUri); httpResponse = await request.close(); responseBody = await utf8.decodeStream(httpResponse); } catch (e) { - // if the onion url fails, and not set to tor only, try the clearnet url, (still using tor!): - if (!torOnly) { + // if the onion url fails, try the clearnet url, (still using tor!): + // only do this if we are not onionOnly, otherwise we will fail + if (!onionOnly) { final request = await client.getUrl(clearnetUri); httpResponse = await request.close(); responseBody = await utf8.decodeStream(httpResponse); + } else { + // we failed to connect through onionOnly + return 0.0; } } statusCode = httpResponse.statusCode; } catch (e) { - // connections all failed / tor is not enabled, so we use the clearnet url directly as normal: if (torOnly) { - // we failed to connect through tor + // we failed to connect through torOnly return 0.0; } + + // connections all failed / tor is not enabled, so we use the clearnet url directly as normal: final response = await get(clearnetUri); responseBody = response.body; statusCode = response.statusCode; } - + if (statusCode != 200) { return 0.0; } @@ -83,12 +90,12 @@ Future _fetchPrice(Map args) async { } } -Future _fetchPriceAsync( - CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async => +Future _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly, bool onionOnly) async => compute(_fetchPrice, { 'fiat': fiat.toString(), 'crypto': crypto.toString(), 'torOnly': torOnly, + 'onionOnly': onionOnly, 'port': ProxyWrapper.port, 'torEnabled': ProxyWrapper.enabled, }); @@ -98,6 +105,7 @@ class FiatConversionService { required CryptoCurrency crypto, required FiatCurrency fiat, required bool torOnly, + required bool onionOnly, }) async => - await _fetchPriceAsync(crypto, fiat, torOnly); + await _fetchPriceAsync(crypto, fiat, torOnly, onionOnly); } diff --git a/lib/reactions/fiat_rate_update.dart b/lib/reactions/fiat_rate_update.dart index 828516978..42988ba1c 100644 --- a/lib/reactions/fiat_rate_update.dart +++ b/lib/reactions/fiat_rate_update.dart @@ -7,6 +7,7 @@ import 'package:cake_wallet/polygon/polygon.dart'; import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/view_model/settings/tor_connection.dart'; import 'package:cw_core/erc20_token.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:mobx/mobx.dart'; @@ -33,6 +34,7 @@ Future startFiatRateUpdate( crypto: appStore.wallet!.currency, fiat: settingsStore.fiatCurrency, torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, + onionOnly: settingsStore.currentTorConnection == TorConnectionType.onionOnly, ); } @@ -54,6 +56,7 @@ Future startFiatRateUpdate( crypto: currency, fiat: settingsStore.fiatCurrency, torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, + onionOnly: settingsStore.currentTorConnection == TorConnectionType.onionOnly, ); }.call(); } diff --git a/lib/reactions/on_current_fiat_api_mode_change.dart b/lib/reactions/on_current_fiat_api_mode_change.dart index 5bcaeef4c..cf5688ff5 100644 --- a/lib/reactions/on_current_fiat_api_mode_change.dart +++ b/lib/reactions/on_current_fiat_api_mode_change.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/entities/fiat_api_mode.dart'; +import 'package:cake_wallet/view_model/settings/tor_connection.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/core/fiat_conversion_service.dart'; import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; @@ -7,19 +8,20 @@ import 'package:cake_wallet/store/app_store.dart'; ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer; -void startCurrentFiatApiModeChangeReaction(AppStore appStore, - SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { +void startCurrentFiatApiModeChangeReaction( + AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { _onCurrentFiatCurrencyChangeDisposer?.reaction.dispose(); - _onCurrentFiatCurrencyChangeDisposer = reaction( - (_) => settingsStore.fiatApiMode, (FiatApiMode fiatApiMode) async { + _onCurrentFiatCurrencyChangeDisposer = + reaction((_) => settingsStore.fiatApiMode, (FiatApiMode fiatApiMode) async { if (appStore.wallet == null || fiatApiMode == FiatApiMode.disabled) { return; } - fiatConversionStore.prices[appStore.wallet!.currency] = - await FiatConversionService.fetchPrice( - crypto: appStore.wallet!.currency, - fiat: settingsStore.fiatCurrency, - torOnly: fiatApiMode == FiatApiMode.torOnly); + fiatConversionStore.prices[appStore.wallet!.currency] = await FiatConversionService.fetchPrice( + crypto: appStore.wallet!.currency, + fiat: settingsStore.fiatCurrency, + torOnly: fiatApiMode == FiatApiMode.torOnly, + onionOnly: settingsStore.currentTorConnection == TorConnectionType.onionOnly, + ); }); } diff --git a/lib/reactions/on_current_fiat_change.dart b/lib/reactions/on_current_fiat_change.dart index 873984940..c1b66f099 100644 --- a/lib/reactions/on_current_fiat_change.dart +++ b/lib/reactions/on_current_fiat_change.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/entities/fiat_api_mode.dart'; +import 'package:cake_wallet/view_model/settings/tor_connection.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/core/fiat_conversion_service.dart'; import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; @@ -8,20 +9,21 @@ import 'package:cake_wallet/entities/fiat_currency.dart'; ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer; -void startCurrentFiatChangeReaction(AppStore appStore, - SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { +void startCurrentFiatChangeReaction( + AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { _onCurrentFiatCurrencyChangeDisposer?.reaction.dispose(); - _onCurrentFiatCurrencyChangeDisposer = reaction( - (_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async { + _onCurrentFiatCurrencyChangeDisposer = + reaction((_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async { if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) { return; } final cryptoCurrency = appStore.wallet!.currency; - fiatConversionStore.prices[cryptoCurrency] = - await FiatConversionService.fetchPrice( - crypto: cryptoCurrency, - fiat: fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); + fiatConversionStore.prices[cryptoCurrency] = await FiatConversionService.fetchPrice( + crypto: cryptoCurrency, + fiat: fiatCurrency, + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, + onionOnly: settingsStore.currentTorConnection == TorConnectionType.onionOnly, + ); }); } diff --git a/lib/reactions/on_current_wallet_change.dart b/lib/reactions/on_current_wallet_change.dart index e7be4d05a..243f4ab85 100644 --- a/lib/reactions/on_current_wallet_change.dart +++ b/lib/reactions/on_current_wallet_change.dart @@ -3,6 +3,7 @@ import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/entities/update_haven_rate.dart'; import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/polygon/polygon.dart'; +import 'package:cake_wallet/view_model/settings/tor_connection.dart'; import 'package:cw_core/erc20_token.dart'; import 'package:cw_core/transaction_history.dart'; import 'package:cw_core/balance.dart'; @@ -92,9 +93,11 @@ void startCurrentWalletChangeReaction( fiatConversionStore.prices[wallet.currency] = 0; fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice( - crypto: wallet.currency, - fiat: settingsStore.fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); + crypto: wallet.currency, + fiat: settingsStore.fiatCurrency, + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, + onionOnly: settingsStore.currentTorConnection == TorConnectionType.onionOnly, + ); Iterable? currencies; if (wallet.type == WalletType.ethereum) { @@ -109,9 +112,11 @@ void startCurrentWalletChangeReaction( for (final currency in currencies) { () async { fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice( - crypto: currency, - fiat: settingsStore.fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); + crypto: currency, + fiat: settingsStore.fiatCurrency, + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, + onionOnly: settingsStore.currentTorConnection == TorConnectionType.onionOnly, + ); }.call(); } } diff --git a/lib/src/screens/nodes/widgets/node_form.dart b/lib/src/screens/nodes/widgets/node_form.dart index ab8dcafdf..dd339f729 100644 --- a/lib/src/screens/nodes/widgets/node_form.dart +++ b/lib/src/screens/nodes/widgets/node_form.dart @@ -19,7 +19,7 @@ class NodeForm extends StatelessWidget { _portController = TextEditingController(text: editingNode?.uri.port.toString()), _loginController = TextEditingController(text: editingNode?.login), _passwordController = TextEditingController(text: editingNode?.password), - _socksAddressController = TextEditingController(text: editingNode?.socksProxyAddress){ + _socksAddressController = TextEditingController(text: editingNode?.socksProxyAddress) { if (editingNode != null) { nodeViewModel ..setAddress((editingNode!.uri.host.toString())) @@ -60,7 +60,8 @@ class NodeForm extends StatelessWidget { _portController.addListener(() => nodeViewModel.port = _portController.text); _loginController.addListener(() => nodeViewModel.login = _loginController.text); _passwordController.addListener(() => nodeViewModel.password = _passwordController.text); - _socksAddressController.addListener(() => nodeViewModel.socksProxyAddress = _socksAddressController.text); + _socksAddressController + .addListener(() => nodeViewModel.socksProxyAddress = _socksAddressController.text); } final NodeCreateOrEditViewModel nodeViewModel; @@ -103,6 +104,26 @@ class NodeForm extends StatelessWidget { ], ), SizedBox(height: 10.0), + Padding( + padding: EdgeInsets.only(top: 20), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + mainAxisSize: MainAxisSize.max, + children: [ + Observer( + builder: (_) => StandardCheckbox( + value: nodeViewModel.useSSL, + gradientBackground: true, + borderColor: Theme.of(context).dividerColor, + iconColor: Colors.white, + onChanged: (value) => nodeViewModel.useSSL = value, + caption: S.of(context).use_ssl, + ), + ) + ], + ), + ), + SizedBox(height: 10.0), if (nodeViewModel.hasAuthCredentials) ...[ Row( children: [ @@ -123,25 +144,6 @@ class NodeForm extends StatelessWidget { )) ], ), - Padding( - padding: EdgeInsets.only(top: 20), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - mainAxisSize: MainAxisSize.max, - children: [ - Observer( - builder: (_) => StandardCheckbox( - value: nodeViewModel.useSSL, - gradientBackground: true, - borderColor: Theme.of(context).dividerColor, - iconColor: Colors.white, - onChanged: (value) => nodeViewModel.useSSL = value, - caption: S.of(context).use_ssl, - ), - ) - ], - ), - ), Padding( padding: EdgeInsets.only(top: 20), child: Row( @@ -163,44 +165,44 @@ class NodeForm extends StatelessWidget { ), Observer( builder: (_) => Column( - children: [ - Padding( - padding: EdgeInsets.only(top: 20), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - mainAxisSize: MainAxisSize.max, - children: [ - StandardCheckbox( - value: nodeViewModel.useSocksProxy, - gradientBackground: true, - borderColor: Theme.of(context).dividerColor, - iconColor: Colors.white, - onChanged: (value) { - if (!value) { - _socksAddressController.text = ''; - } - nodeViewModel.useSocksProxy = value; - }, - caption: 'SOCKS Proxy', - ), - ], - ), - ), - if (nodeViewModel.useSocksProxy) ...[ - SizedBox(height: 10.0), - Row( - children: [ - Expanded( - child: BaseTextFormField( + children: [ + Padding( + padding: EdgeInsets.only(top: 20), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + mainAxisSize: MainAxisSize.max, + children: [ + StandardCheckbox( + value: nodeViewModel.useSocksProxy, + gradientBackground: true, + borderColor: Theme.of(context).dividerColor, + iconColor: Colors.white, + onChanged: (value) { + if (!value) { + _socksAddressController.text = ''; + } + nodeViewModel.useSocksProxy = value; + }, + caption: 'SOCKS Proxy', + ), + ], + ), + ), + if (nodeViewModel.useSocksProxy) ...[ + SizedBox(height: 10.0), + Row( + children: [ + Expanded( + child: BaseTextFormField( controller: _socksAddressController, hintText: '[:]', validator: SocksProxyNodeAddressValidator(), )) - ], - ), - ] - ], - )), + ], + ), + ] + ], + )), ] ], ), diff --git a/lib/src/screens/nodes/widgets/node_indicator.dart b/lib/src/screens/nodes/widgets/node_indicator.dart index 64a1ba07f..6c7abb1af 100644 --- a/lib/src/screens/nodes/widgets/node_indicator.dart +++ b/lib/src/screens/nodes/widgets/node_indicator.dart @@ -3,9 +3,10 @@ import 'package:flutter/material.dart'; import 'package:cake_wallet/palette.dart'; class NodeIndicator extends StatelessWidget { - NodeIndicator({this.isLive = false}); + NodeIndicator({this.isLive = false, this.showText = false}); final bool isLive; + final bool showText; @override Widget build(BuildContext context) { @@ -17,11 +18,13 @@ class NodeIndicator extends StatelessWidget { decoration: BoxDecoration(shape: BoxShape.circle, color: isLive ? Palette.green : Palette.red), ), - const SizedBox(width: 8.0), - Text( - isLive ? S.current.connected : S.current.disconnected, - style: TextStyle(fontSize: 14.0), - ) + if (showText) ...[ + const SizedBox(width: 8.0), + Text( + isLive ? S.current.connected : S.current.disconnected, + style: TextStyle(fontSize: 14.0), + ) + ], ], ); } diff --git a/lib/src/screens/settings/connection_sync_page.dart b/lib/src/screens/settings/connection_sync_page.dart index 10473fa44..650d4761f 100644 --- a/lib/src/screens/settings/connection_sync_page.dart +++ b/lib/src/screens/settings/connection_sync_page.dart @@ -108,7 +108,7 @@ class ConnectionSyncPage extends BasePage { ), ); }), - TorListRow( + TorStatus( decoration: BoxDecoration( borderRadius: BorderRadius.only(bottomLeft: Radius.circular(25), bottomRight: Radius.circular(25)), color: const Color.fromARGB(255, 236, 244, 255), diff --git a/lib/src/screens/settings/widgets/settings_tor_status.dart b/lib/src/screens/settings/widgets/settings_tor_status.dart index 3ca024e8e..394b18997 100644 --- a/lib/src/screens/settings/widgets/settings_tor_status.dart +++ b/lib/src/screens/settings/widgets/settings_tor_status.dart @@ -1,34 +1,23 @@ -import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/src/screens/nodes/widgets/node_indicator.dart'; import 'package:cake_wallet/src/widgets/standard_list.dart'; -import 'package:cake_wallet/themes/extensions/receive_page_theme.dart'; -import 'package:cw_core/node.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/themes/extensions/filter_theme.dart'; import 'package:tor/tor.dart'; -class TorListRow extends StandardListRow { - TorListRow( +class TorStatus extends StandardListRow { + TorStatus( {required String title, required void Function(BuildContext context) onTap, required bool isSelected, BoxDecoration? decoration}) : super(title: title, onTap: onTap, isSelected: isSelected, decoration: decoration); - @override Widget buildTrailing(BuildContext context) { - // return FutureBuilder( - // future: node.requestNode(), - // builder: (context, snapshot) { - // switch (snapshot.connectionState) { - // case ConnectionState.done: - // return NodeIndicator(isLive: (snapshot.data as bool?) ?? false); - // default: - // return NodeIndicator(isLive: false); - // } - // }); - return NodeIndicator(isLive: Tor.instance.started); + return NodeIndicator( + isLive: Tor.instance.started && Tor.instance.enabled, + showText: true, + ); } // @override diff --git a/lib/view_model/dashboard/home_settings_view_model.dart b/lib/view_model/dashboard/home_settings_view_model.dart index fc2c27a7c..e78da747d 100644 --- a/lib/view_model/dashboard/home_settings_view_model.dart +++ b/lib/view_model/dashboard/home_settings_view_model.dart @@ -5,6 +5,7 @@ import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/polygon/polygon.dart'; import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart'; +import 'package:cake_wallet/view_model/settings/tor_connection.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/erc20_token.dart'; import 'package:cw_core/wallet_type.dart'; @@ -86,9 +87,11 @@ abstract class HomeSettingsViewModelBase with Store { try { _balanceViewModel.fiatConvertationStore.prices[token] = await FiatConversionService.fetchPrice( - crypto: token, - fiat: _settingsStore.fiatCurrency, - torOnly: _settingsStore.fiatApiMode == FiatApiMode.torOnly); + crypto: token, + fiat: _settingsStore.fiatCurrency, + torOnly: _settingsStore.fiatApiMode == FiatApiMode.torOnly, + onionOnly: _settingsStore.currentTorConnection == TorConnectionType.onionOnly, + ); } catch (_) {} }