From 3ae3753e224284a630b38cd379dc641d3629ab50 Mon Sep 17 00:00:00 2001 From: fosse Date: Wed, 17 Jan 2024 20:37:10 -0500 Subject: [PATCH] review fixes --- lib/anonpay/anonpay_api.dart | 49 ++++++++----------- lib/core/fiat_conversion_service.dart | 22 ++------- lib/core/wallet_loading_service.dart | 2 - lib/di.dart | 5 +- lib/entities/default_settings_migration.dart | 15 ++++++ lib/entities/exchange_api_mode.dart | 9 +--- lib/entities/fiat_api_mode.dart | 9 +--- .../provider/trocador_exchange_provider.dart | 7 ++- lib/main.dart | 2 +- lib/reactions/fiat_rate_update.dart | 4 -- .../on_current_fiat_api_mode_change.dart | 2 - lib/reactions/on_current_fiat_change.dart | 2 - lib/reactions/on_current_wallet_change.dart | 4 -- .../settings/connection_sync_page.dart | 2 +- .../dashboard/home_settings_view_model.dart | 2 - .../exchange/exchange_view_model.dart | 10 ++-- 16 files changed, 54 insertions(+), 92 deletions(-) diff --git a/lib/anonpay/anonpay_api.dart b/lib/anonpay/anonpay_api.dart index 1a51e1001..5fdfcddd9 100644 --- a/lib/anonpay/anonpay_api.dart +++ b/lib/anonpay/anonpay_api.dart @@ -4,19 +4,19 @@ import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart'; import 'package:cake_wallet/anonpay/anonpay_request.dart'; import 'package:cake_wallet/anonpay/anonpay_status_response.dart'; import 'package:cake_wallet/core/fiat_conversion_service.dart'; +import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/entities/fiat_currency.dart'; import 'package:cake_wallet/exchange/limits.dart'; +import 'package:cake_wallet/utils/proxy_wrapper.dart'; import 'package:cw_core/wallet_base.dart'; -import 'package:http/http.dart'; +import 'package:flutter/foundation.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cake_wallet/.secrets.g.dart' as secrets; class AnonPayApi { const AnonPayApi({ - this.useTorOnly = false, required this.wallet, }); - final bool useTorOnly; final WalletBase wallet; static const anonpayRef = secrets.anonPayReferralCode; @@ -29,9 +29,9 @@ class AnonPayApi { static const apiKey = secrets.trocadorApiKey; Future paymentStatus(String id) async { - final authority = await _getAuthority(); - final response = await get(Uri.https(authority, "$anonPayStatus/$id")); - final responseJSON = json.decode(response.body) as Map; + final response = await proxyGet("$anonPayStatus/$id", {}); + final responseBody = await utf8.decodeStream(response); + final responseJSON = json.decode(responseBody) as Map; final status = responseJSON['Status'] as String; final fiatAmount = responseJSON['Fiat_Amount'] as double?; final fiatEquiv = responseJSON['Fiat_Equiv'] as String?; @@ -69,11 +69,10 @@ class AnonPayApi { if (request.fiatEquivalent != null) { body['fiat_equiv'] = request.fiatEquivalent; } - final authority = await _getAuthority(); - final response = await get(Uri.https(authority, anonPayPath, body)); - - final responseJSON = json.decode(response.body) as Map; + final response = await proxyGet(anonPayPath, body); + final responseBody = await utf8.decodeStream(response); + final responseJSON = json.decode(responseBody) as Map; final id = responseJSON['ID'] as String; final url = responseJSON['url'] as String; final urlOnion = responseJSON['url_onion'] as String; @@ -136,8 +135,6 @@ class AnonPayApi { fiatRate = await FiatConversionService.fetchPrice( crypto: cryptoCurrency, fiat: fiatCurrency, - torOnly: useTorOnly, - onionOnly: false,// TODO: CW-519 ); } @@ -147,16 +144,14 @@ class AnonPayApi { 'name': cryptoCurrency.name, }; - final String apiAuthority = await _getAuthority(); - final uri = Uri.https(apiAuthority, coinPath, params); - - final response = await get(uri); + final response = await proxyGet(coinPath, params); if (response.statusCode != 200) { throw Exception('Unexpected http status: ${response.statusCode}'); } - final responseJSON = json.decode(response.body) as List; + final responseBody = await utf8.decodeStream(response); + final responseJSON = json.decode(responseBody) as List; if (responseJSON.isEmpty) { throw Exception('No data'); @@ -199,16 +194,14 @@ class AnonPayApi { } } - Future _getAuthority() async { - try { - if (useTorOnly) { - return onionApiAuthority; - } - final uri = Uri.https(onionApiAuthority, '/anonpay'); - await get(uri); - return onionApiAuthority; - } catch (e) { - return clearNetAuthority; - } + Future proxyGet(String path, Map queryParams) async { + ProxyWrapper proxy = await getIt.get(); + Uri onionUri = Uri.http(onionApiAuthority, path, queryParams); + Uri clearnetUri = Uri.https(clearNetAuthority, path, queryParams); + return await proxy.get( + onionUri: onionUri, + clearnetUri: clearnetUri, + torOnly: false, + ); } } diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index 1165c0ebb..674682f49 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -15,7 +15,6 @@ const _fiatApiPath = '/v2/rates'; Future _fetchPrice(Map args) async { final crypto = args['crypto'] as String; final fiat = args['fiat'] as String; - final torOnly = args['torOnly'] as bool; final mainThreadProxyPort = args['port'] as int; final Map queryParams = { @@ -43,7 +42,7 @@ Future _fetchPrice(Map args) async { onionUri: onionUri, clearnetUri: clearnetUri, portOverride: mainThreadProxyPort, - torOnly: torOnly, + torOnly: false, ); responseBody = await utf8.decodeStream(httpResponse); statusCode = httpResponse.statusCode; @@ -70,30 +69,17 @@ Future _fetchPrice(Map args) 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, - // }); - _fetchPrice({ + CryptoCurrency crypto, FiatCurrency fiat) async => + compute(_fetchPrice, { 'fiat': fiat.toString(), 'crypto': crypto.toString(), - 'torOnly': torOnly, - 'onionOnly': onionOnly, 'port': ProxyWrapper.port, - 'torEnabled': ProxyWrapper.enabled, }); class FiatConversionService { static Future fetchPrice({ required CryptoCurrency crypto, required FiatCurrency fiat, - required bool torOnly, - required bool onionOnly, }) async => - await _fetchPriceAsync(crypto, fiat, torOnly, onionOnly); + await _fetchPriceAsync(crypto, fiat); } diff --git a/lib/core/wallet_loading_service.dart b/lib/core/wallet_loading_service.dart index e7a76d847..917186230 100644 --- a/lib/core/wallet_loading_service.dart +++ b/lib/core/wallet_loading_service.dart @@ -14,14 +14,12 @@ class WalletLoadingService { this.sharedPreferences, this.keyService, this.walletServiceFactory, - this.settingsStore, this.torViewModel, ); final SharedPreferences sharedPreferences; final KeyService keyService; final WalletService Function(WalletType type) walletServiceFactory; - final SettingsStore settingsStore; final TorViewModel torViewModel; Future renameWallet(WalletType type, String name, String newName) async { diff --git a/lib/di.dart b/lib/di.dart index bc0bcdafd..7c170be6f 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -352,7 +352,6 @@ Future setup({ getIt.get(), getIt.get(), (WalletType type) => getIt.get(param1: type), - getIt.get(), getIt.get(), )); @@ -1144,9 +1143,7 @@ Future setup({ getIt.registerFactory(() => IoniaAccountCardsPage(getIt.get())); - getIt.registerFactory(() => AnonPayApi( - useTorOnly: getIt.get().exchangeStatus == ExchangeApiMode.torOnly, - wallet: getIt.get().wallet!)); + getIt.registerFactory(() => AnonPayApi(wallet: getIt.get().wallet!)); getIt.registerFactory(() => DesktopWalletSelectionDropDown(getIt.get(), getIt.get())); diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 22868ae28..87a095635 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -185,6 +185,9 @@ Future defaultSettingsMigration( case 25: await rewriteSecureStoragePin(secureStorage: secureStorage); break; + case 26: + await migrateTorPreferences(sharedPreferences: sharedPreferences); + break; default: break; } @@ -378,6 +381,18 @@ Node getMoneroDefaultNode({required Box nodes}) { } } +Future migrateTorPreferences({required SharedPreferences sharedPreferences}) async { + + if (sharedPreferences.getInt(PreferencesKey.currentFiatApiModeKey) == 1) { + await sharedPreferences.setInt(PreferencesKey.currentFiatApiModeKey, 0); + } + + if (sharedPreferences.getInt(PreferencesKey.exchangeStatusKey) == 1) { + await sharedPreferences.setInt(PreferencesKey.currentFiatApiModeKey, 0); + } + +} + Future rewriteSecureStoragePin({required FlutterSecureStorage secureStorage}) async { // the bug only affects ios/mac: if (!Platform.isIOS && !Platform.isMacOS) { diff --git a/lib/entities/exchange_api_mode.dart b/lib/entities/exchange_api_mode.dart index 0b8b575a5..83d0f9dbc 100644 --- a/lib/entities/exchange_api_mode.dart +++ b/lib/entities/exchange_api_mode.dart @@ -4,18 +4,15 @@ import 'package:cw_core/enumerable_item.dart'; class ExchangeApiMode extends EnumerableItem with Serializable { const ExchangeApiMode({required String title, required int raw}) : super(title: title, raw: raw); - static const all = [ExchangeApiMode.enabled, ExchangeApiMode.torOnly, ExchangeApiMode.disabled]; + static const all = [ExchangeApiMode.enabled, ExchangeApiMode.disabled]; static const enabled = ExchangeApiMode(raw: 0, title: 'Enabled'); - static const torOnly = ExchangeApiMode(raw: 1, title: 'Tor only'); - static const disabled = ExchangeApiMode(raw: 2, title: 'Disabled'); + static const disabled = ExchangeApiMode(raw: 1, title: 'Disabled'); static ExchangeApiMode deserialize({required int raw}) { switch (raw) { case 0: return enabled; - case 1: - return torOnly; case 2: return disabled; default: @@ -28,8 +25,6 @@ class ExchangeApiMode extends EnumerableItem with Serializable { switch (this) { case ExchangeApiMode.enabled: return S.current.enabled; - case ExchangeApiMode.torOnly: - return S.current.tor_only; case ExchangeApiMode.disabled: return S.current.disabled; default: diff --git a/lib/entities/fiat_api_mode.dart b/lib/entities/fiat_api_mode.dart index bb16ca5a4..58a1aa62b 100644 --- a/lib/entities/fiat_api_mode.dart +++ b/lib/entities/fiat_api_mode.dart @@ -4,19 +4,16 @@ import 'package:cw_core/enumerable_item.dart'; class FiatApiMode extends EnumerableItem with Serializable { const FiatApiMode({required String title, required int raw}) : super(title: title, raw: raw); - static const all = [FiatApiMode.enabled, FiatApiMode.torOnly, FiatApiMode.disabled]; + static const all = [FiatApiMode.enabled, FiatApiMode.disabled]; static const enabled = FiatApiMode(raw: 0, title: 'Enabled'); - static const torOnly = FiatApiMode(raw: 1, title: 'Tor only'); - static const disabled = FiatApiMode(raw: 2, title: 'Disabled'); + static const disabled = FiatApiMode(raw: 1, title: 'Disabled'); static FiatApiMode deserialize({required int raw}) { switch (raw) { case 0: return enabled; case 1: - return torOnly; - case 2: return disabled; default: throw Exception('Unexpected token: $raw for FiatApiMode deserialize'); @@ -28,8 +25,6 @@ class FiatApiMode extends EnumerableItem with Serializable { switch (this) { case FiatApiMode.enabled: return S.current.enabled; - case FiatApiMode.torOnly: - return S.current.tor_only; case FiatApiMode.disabled: return S.current.disabled; default: diff --git a/lib/exchange/provider/trocador_exchange_provider.dart b/lib/exchange/provider/trocador_exchange_provider.dart index f567fc5bc..7a8c5c510 100644 --- a/lib/exchange/provider/trocador_exchange_provider.dart +++ b/lib/exchange/provider/trocador_exchange_provider.dart @@ -14,12 +14,11 @@ import 'package:cake_wallet/utils/proxy_wrapper.dart'; import 'package:cw_core/crypto_currency.dart'; class TrocadorExchangeProvider extends ExchangeProvider { - TrocadorExchangeProvider({this.useTorOnly = false, this.providerStates = const {}}) + TrocadorExchangeProvider({this.providerStates = const {}}) : _lastUsedRateId = '', _provider = [], super(pairList: supportedPairs(_notSupported)); - bool useTorOnly; final Map providerStates; static const List availableProviders = [ @@ -308,11 +307,11 @@ class TrocadorExchangeProvider extends ExchangeProvider { Future proxyGet(String path, Map queryParams) async { ProxyWrapper proxy = await getIt.get(); Uri onionUri = Uri.http(onionApiAuthority, path, queryParams); - Uri clearnetUri = Uri.http(onionApiAuthority, path, queryParams); + Uri clearnetUri = Uri.http(clearNetAuthority, path, queryParams); return await proxy.get( onionUri: onionUri, clearnetUri: clearnetUri, - torOnly: useTorOnly, + torOnly: false, ); } } diff --git a/lib/main.dart b/lib/main.dart index 165db1ddd..306b109a0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -163,7 +163,7 @@ Future initializeAppConfigs() async { transactionDescriptions: transactionDescriptions, secureStorage: secureStorage, anonpayInvoiceInfo: anonpayInvoiceInfo, - initialMigrationVersion: 25); + initialMigrationVersion: 26); } Future initialSetup( diff --git a/lib/reactions/fiat_rate_update.dart b/lib/reactions/fiat_rate_update.dart index 5ea60d5ea..96ff6ad20 100644 --- a/lib/reactions/fiat_rate_update.dart +++ b/lib/reactions/fiat_rate_update.dart @@ -33,8 +33,6 @@ Future startFiatRateUpdate( await FiatConversionService.fetchPrice( crypto: appStore.wallet!.currency, fiat: settingsStore.fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, - onionOnly: settingsStore.torConnectionMode == TorConnectionMode.onionOnly, ); } @@ -55,8 +53,6 @@ Future startFiatRateUpdate( fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice( crypto: currency, fiat: settingsStore.fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, - onionOnly: settingsStore.torConnectionMode == TorConnectionMode.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 c56651661..f7896d5ed 100644 --- a/lib/reactions/on_current_fiat_api_mode_change.dart +++ b/lib/reactions/on_current_fiat_api_mode_change.dart @@ -20,8 +20,6 @@ void startCurrentFiatApiModeChangeReaction( fiatConversionStore.prices[appStore.wallet!.currency] = await FiatConversionService.fetchPrice( crypto: appStore.wallet!.currency, fiat: settingsStore.fiatCurrency, - torOnly: fiatApiMode == FiatApiMode.torOnly, - onionOnly: settingsStore.torConnectionMode == TorConnectionMode.onionOnly, ); }); } diff --git a/lib/reactions/on_current_fiat_change.dart b/lib/reactions/on_current_fiat_change.dart index 7656ffce9..47b74df86 100644 --- a/lib/reactions/on_current_fiat_change.dart +++ b/lib/reactions/on_current_fiat_change.dart @@ -22,8 +22,6 @@ void startCurrentFiatChangeReaction( fiatConversionStore.prices[cryptoCurrency] = await FiatConversionService.fetchPrice( crypto: cryptoCurrency, fiat: fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, - onionOnly: settingsStore.torConnectionMode == TorConnectionMode.onionOnly, ); }); } diff --git a/lib/reactions/on_current_wallet_change.dart b/lib/reactions/on_current_wallet_change.dart index 0eb20bdc3..9b7bf5094 100644 --- a/lib/reactions/on_current_wallet_change.dart +++ b/lib/reactions/on_current_wallet_change.dart @@ -95,8 +95,6 @@ void startCurrentWalletChangeReaction( fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice( crypto: wallet.currency, fiat: settingsStore.fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, - onionOnly: settingsStore.torConnectionMode == TorConnectionMode.onionOnly, ); Iterable? currencies; @@ -114,8 +112,6 @@ void startCurrentWalletChangeReaction( fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice( crypto: currency, fiat: settingsStore.fiatCurrency, - torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly, - onionOnly: settingsStore.torConnectionMode == TorConnectionMode.onionOnly, ); }.call(); } diff --git a/lib/src/screens/settings/connection_sync_page.dart b/lib/src/screens/settings/connection_sync_page.dart index dffbe429c..c115d67d2 100644 --- a/lib/src/screens/settings/connection_sync_page.dart +++ b/lib/src/screens/settings/connection_sync_page.dart @@ -92,7 +92,7 @@ class ConnectionSyncPage extends BasePage { ), const StandardListSeparator(padding: EdgeInsets.symmetric(horizontal: 24)), ], - if (FeatureFlag.isInAppTorEnabled) ...[ + if (FeatureFlag.isInAppTorEnabled && !DeviceInfo.instance.isMobile) ...[ Container( padding: EdgeInsets.symmetric(horizontal: 12, vertical: 10), child: Column(children: [ diff --git a/lib/view_model/dashboard/home_settings_view_model.dart b/lib/view_model/dashboard/home_settings_view_model.dart index 8dff30fa6..61e5245d2 100644 --- a/lib/view_model/dashboard/home_settings_view_model.dart +++ b/lib/view_model/dashboard/home_settings_view_model.dart @@ -89,8 +89,6 @@ abstract class HomeSettingsViewModelBase with Store { await FiatConversionService.fetchPrice( crypto: token, fiat: _settingsStore.fiatCurrency, - torOnly: _settingsStore.fiatApiMode == FiatApiMode.torOnly, - onionOnly: _settingsStore.torConnectionMode == TorConnectionMode.onionOnly, ); } catch (_) {} } diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index 57d47fed6..8ef89569f 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -30,6 +30,8 @@ import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/store/templates/exchange_template_store.dart'; import 'package:cake_wallet/utils/feature_flag.dart'; import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart'; +import 'package:cake_wallet/view_model/settings/tor_connection.dart'; +import 'package:cake_wallet/view_model/settings/tor_view_model.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/sync_status.dart'; import 'package:cw_core/transaction_priority.dart'; @@ -67,7 +69,6 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with depositAddress = '', isDepositAddressEnabled = false, isReceiveAmountEditable = false, - _useTorOnly = false, receiveCurrencies = [], depositCurrencies = [], limits = Limits(min: 0, max: 0), @@ -78,7 +79,6 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with providerList = [], selectedProviders = ObservableList(), super(appStore: appStore) { - _useTorOnly = _settingsStore.exchangeStatus == ExchangeApiMode.torOnly; _setProviders(); const excludeDepositCurrencies = [CryptoCurrency.btt]; const excludeReceiveCurrencies = [ @@ -135,7 +135,6 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with }); } - bool _useTorOnly; final Box trades; final ExchangeTemplateStore _exchangeTemplateStore; final TradesStore tradesStore; @@ -145,8 +144,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with ChangeNowExchangeProvider(settingsStore: _settingsStore), SideShiftExchangeProvider(), SimpleSwapExchangeProvider(), - TrocadorExchangeProvider( - useTorOnly: _useTorOnly, providerStates: _settingsStore.trocadorProviderStates), + TrocadorExchangeProvider(providerStates: _settingsStore.trocadorProviderStates), if (FeatureFlag.isExolixEnabled) ExolixExchangeProvider(), ]; @@ -721,7 +719,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with } void _setProviders() { - if (_settingsStore.exchangeStatus == ExchangeApiMode.torOnly) + if (_settingsStore.torConnectionMode == TorConnectionMode.onionOnly) providerList = _allProviders.where((provider) => provider.supportsOnionAddress).toList(); else providerList = _allProviders;