From ce4b1cd4aefbee242e2492e62e9d6154af969ea5 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Tue, 28 Feb 2023 18:23:21 +0200 Subject: [PATCH 1/5] Update fiat API version Enable Tor support for Fiat --- lib/core/fiat_conversion_service.dart | 46 +++++++++++------- lib/reactions/bootstrap.dart | 2 + lib/reactions/fiat_rate_update.dart | 4 +- .../on_current_fiat_api_mode_change.dart | 25 ++++++++++ lib/reactions/on_current_fiat_change.dart | 7 ++- lib/reactions/on_current_wallet_change.dart | 6 ++- lib/src/screens/settings/privacy_page.dart | 48 +++++++++++-------- .../settings/privacy_settings_view_model.dart | 10 +--- 8 files changed, 97 insertions(+), 51 deletions(-) create mode 100644 lib/reactions/on_current_fiat_api_mode_change.dart diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index f4ec3775b..96dff9614 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -4,18 +4,30 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart'; -const fiatApiAuthority = 'fiat-api.cakewallet.com'; -const fiatApiPath = '/v1/rates'; +const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com'; +const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion'; +const _fiatApiPath = '/v2/rates'; Future _fetchPrice(Map args) async { final crypto = args['crypto'] as CryptoCurrency; final fiat = args['fiat'] as FiatCurrency; + final torOnly = args['torOnly'] as bool; double price = 0.0; + print("@@@@@@@@@@@@@@"); + print(crypto); + print(fiat); + print(torOnly); try { - final fiatStringified = fiat.toString(); - final uri = Uri.https(fiatApiAuthority, fiatApiPath, - {'convert': fiatStringified}); + final uri = Uri.https( + torOnly ? _fiatApiOnionAuthority : _fiatApiClearNetAuthority, + _fiatApiPath, + { + 'interval_count': '1', + 'base': crypto.toString(), + 'quote': fiat.toString(), + }, + ); final response = await get(uri); if (response.statusCode != 200) { @@ -23,14 +35,12 @@ Future _fetchPrice(Map args) async { } final responseJSON = json.decode(response.body) as Map; - final data = responseJSON['data'] as List; + final results = responseJSON['results'] as Map; - for (final item in data) { - if (item['symbol'] == crypto.title) { - price = item['quote'][fiatStringified]['price'] as double; - break; - } + if (results.isNotEmpty) { + price = results.values.first as double; } + print(price); return price; } catch (e) { @@ -38,12 +48,14 @@ Future _fetchPrice(Map args) async { } } -Future _fetchPriceAsync( - CryptoCurrency crypto, FiatCurrency fiat) async => - compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto}); +Future _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async => + compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto, 'torOnly': torOnly}); class FiatConversionService { - static Future fetchPrice( - CryptoCurrency crypto, FiatCurrency fiat) async => - await _fetchPriceAsync(crypto, fiat); + static Future fetchPrice({ + required CryptoCurrency crypto, + required FiatCurrency fiat, + required bool torOnly, + }) async => + await _fetchPriceAsync(crypto, fiat, torOnly); } diff --git a/lib/reactions/bootstrap.dart b/lib/reactions/bootstrap.dart index 4b65ed9d2..6451c92c5 100644 --- a/lib/reactions/bootstrap.dart +++ b/lib/reactions/bootstrap.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'package:cake_wallet/reactions/fiat_rate_update.dart'; +import 'package:cake_wallet/reactions/on_current_fiat_api_mode_change.dart'; import 'package:cake_wallet/reactions/on_current_node_change.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/widgets.dart'; @@ -31,6 +32,7 @@ Future bootstrap(GlobalKey navigatorKey) async { startCurrentWalletChangeReaction( appStore, settingsStore, fiatConversionStore); startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore); + startCurrentFiatApiModeChangeReaction(appStore, settingsStore, fiatConversionStore); startOnCurrentNodeChangeReaction(appStore); startFiatRateUpdate(appStore, settingsStore, fiatConversionStore); } diff --git a/lib/reactions/fiat_rate_update.dart b/lib/reactions/fiat_rate_update.dart index 48c61d8ca..f9ddbef52 100644 --- a/lib/reactions/fiat_rate_update.dart +++ b/lib/reactions/fiat_rate_update.dart @@ -26,7 +26,9 @@ Future startFiatRateUpdate( } else { fiatConversionStore.prices[appStore.wallet!.currency] = await FiatConversionService.fetchPrice( - appStore.wallet!.currency, settingsStore.fiatCurrency); + crypto: appStore.wallet!.currency, + fiat: settingsStore.fiatCurrency, + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); } } catch (e) { print(e); diff --git a/lib/reactions/on_current_fiat_api_mode_change.dart b/lib/reactions/on_current_fiat_api_mode_change.dart new file mode 100644 index 000000000..5bcaeef4c --- /dev/null +++ b/lib/reactions/on_current_fiat_api_mode_change.dart @@ -0,0 +1,25 @@ +import 'package:cake_wallet/entities/fiat_api_mode.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'; +import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/store/app_store.dart'; + +ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer; + +void startCurrentFiatApiModeChangeReaction(AppStore appStore, + SettingsStore settingsStore, FiatConversionStore fiatConversionStore) { + _onCurrentFiatCurrencyChangeDisposer?.reaction.dispose(); + _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); + }); +} diff --git a/lib/reactions/on_current_fiat_change.dart b/lib/reactions/on_current_fiat_change.dart index 5170c4576..873984940 100644 --- a/lib/reactions/on_current_fiat_change.dart +++ b/lib/reactions/on_current_fiat_change.dart @@ -18,7 +18,10 @@ void startCurrentFiatChangeReaction(AppStore appStore, } final cryptoCurrency = appStore.wallet!.currency; - fiatConversionStore.prices[appStore.wallet!.currency] = - await FiatConversionService.fetchPrice(cryptoCurrency, fiatCurrency); + fiatConversionStore.prices[cryptoCurrency] = + await FiatConversionService.fetchPrice( + crypto: cryptoCurrency, + fiat: fiatCurrency, + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); }); } diff --git a/lib/reactions/on_current_wallet_change.dart b/lib/reactions/on_current_wallet_change.dart index 270415820..2325fe382 100644 --- a/lib/reactions/on_current_wallet_change.dart +++ b/lib/reactions/on_current_wallet_change.dart @@ -71,7 +71,7 @@ void startCurrentWalletChangeReaction(AppStore appStore, await updateHavenRate(fiatConversionStore); } - if (wallet.walletInfo.address?.isEmpty ?? true) { + if (wallet.walletInfo.address.isEmpty) { wallet.walletInfo.address = wallet.walletAddresses.address; if (wallet.walletInfo.isInBox) { @@ -95,7 +95,9 @@ void startCurrentWalletChangeReaction(AppStore appStore, fiatConversionStore.prices[wallet.currency] = 0; fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice( - wallet.currency, settingsStore.fiatCurrency); + crypto: wallet.currency, + fiat: settingsStore.fiatCurrency, + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly); } catch (e) { print(e.toString()); } diff --git a/lib/src/screens/settings/privacy_page.dart b/lib/src/screens/settings/privacy_page.dart index 2f15cc225..8fd6bab82 100644 --- a/lib/src/screens/settings/privacy_page.dart +++ b/lib/src/screens/settings/privacy_page.dart @@ -1,6 +1,9 @@ +import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; +import 'package:cake_wallet/src/screens/settings/widgets/settings_choices_cell.dart'; import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart'; +import 'package:cake_wallet/view_model/settings/choices_list_item.dart'; import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -18,29 +21,32 @@ class PrivacyPage extends BasePage { return Container( padding: EdgeInsets.only(top: 10), child: Observer(builder: (_) { - return Column( - mainAxisSize: MainAxisSize.min, - children: [ - SettingsSwitcherCell( - title: S.current.disable_fiat, - value: _privacySettingsViewModel.isFiatDisabled, + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + SettingsChoicesCell( + ChoicesListItem( + title: S.current.fiat_api, + items: FiatApiMode.all, + selectedItem: _privacySettingsViewModel.fiatApiMode, + onItemSelected: (FiatApiMode fiatApiMode) => + _privacySettingsViewModel.setFiatMode(fiatApiMode), + ), + ), + SettingsSwitcherCell( + title: S.current.disable_exchange, + value: _privacySettingsViewModel.disableExchange, onValueChange: (BuildContext context, bool value) { - _privacySettingsViewModel.setFiatMode(value); + _privacySettingsViewModel.setEnableExchange(value); }), - SettingsSwitcherCell( - title: S.current.disable_exchange, - value: _privacySettingsViewModel.disableExchange, - onValueChange: (BuildContext context, bool value) { - _privacySettingsViewModel.setEnableExchange(value); - }), - SettingsSwitcherCell( - title: S.current.settings_save_recipient_address, - value: _privacySettingsViewModel.shouldSaveRecipientAddress, - onValueChange: (BuildContext _, bool value) { - _privacySettingsViewModel.setShouldSaveRecipientAddress(value); - }) - ], - ); + SettingsSwitcherCell( + title: S.current.settings_save_recipient_address, + value: _privacySettingsViewModel.shouldSaveRecipientAddress, + onValueChange: (BuildContext _, bool value) { + _privacySettingsViewModel.setShouldSaveRecipientAddress(value); + }), + ], + ); }), ); } diff --git a/lib/view_model/settings/privacy_settings_view_model.dart b/lib/view_model/settings/privacy_settings_view_model.dart index f8c3e5b50..efa7cdc36 100644 --- a/lib/view_model/settings/privacy_settings_view_model.dart +++ b/lib/view_model/settings/privacy_settings_view_model.dart @@ -18,7 +18,7 @@ abstract class PrivacySettingsViewModelBase with Store { bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress; @computed - bool get isFiatDisabled => _settingsStore.fiatApiMode == FiatApiMode.disabled; + FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode; @action void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value; @@ -27,12 +27,6 @@ abstract class PrivacySettingsViewModelBase with Store { void setEnableExchange(bool value) => _settingsStore.disableExchange = value; @action - void setFiatMode(bool value) { - if (value) { - _settingsStore.fiatApiMode = FiatApiMode.disabled; - return; - } - _settingsStore.fiatApiMode = FiatApiMode.enabled; - } + void setFiatMode(FiatApiMode fiatApiMode) => _settingsStore.fiatApiMode = fiatApiMode; } From 18af7e7661bfbcecc859e5270536172a750c0d0b Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Tue, 28 Feb 2023 18:36:39 +0200 Subject: [PATCH 2/5] remove print statements [skip ci] --- lib/core/fiat_conversion_service.dart | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index 96dff9614..95c1e23a4 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -13,10 +13,6 @@ Future _fetchPrice(Map args) async { final fiat = args['fiat'] as FiatCurrency; final torOnly = args['torOnly'] as bool; double price = 0.0; - print("@@@@@@@@@@@@@@"); - print(crypto); - print(fiat); - print(torOnly); try { final uri = Uri.https( @@ -40,7 +36,6 @@ Future _fetchPrice(Map args) async { if (results.isNotEmpty) { price = results.values.first as double; } - print(price); return price; } catch (e) { From 861616fda2da5006d5ed6efcb32ca45d1f82743e Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Wed, 1 Mar 2023 23:24:52 +0200 Subject: [PATCH 3/5] use Http protocol for onion connection --- lib/core/fiat_conversion_service.dart | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index 95c1e23a4..66997675d 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -12,18 +12,23 @@ Future _fetchPrice(Map args) async { final crypto = args['crypto'] as CryptoCurrency; final fiat = args['fiat'] as FiatCurrency; final torOnly = args['torOnly'] as bool; + + final Map queryParams = { + 'interval_count': '1', + 'base': crypto.toString(), + 'quote': fiat.toString(), + }; + double price = 0.0; try { - final uri = Uri.https( - torOnly ? _fiatApiOnionAuthority : _fiatApiClearNetAuthority, - _fiatApiPath, - { - 'interval_count': '1', - 'base': crypto.toString(), - 'quote': fiat.toString(), - }, - ); + late final Uri uri; + if (torOnly) { + uri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams); + } else { + uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams); + } + final response = await get(uri); if (response.statusCode != 200) { From 8f85f99a98f937cdc785bb8f6a48860ee67ab109 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Thu, 2 Mar 2023 16:58:04 +0200 Subject: [PATCH 4/5] Fixate MobX version to avoid un-expected behavior --- pubspec_base.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec_base.yaml b/pubspec_base.yaml index 2caa9052f..1e6a27cb7 100644 --- a/pubspec_base.yaml +++ b/pubspec_base.yaml @@ -22,7 +22,7 @@ dependencies: barcode_scan2: ^4.2.1 http: ^0.13.4 path_provider: ^2.0.11 - mobx: ^2.0.7+4 + mobx: 2.0.7+4 flutter_mobx: ^2.0.6+1 flutter_slidable: ^2.0.0 share_plus: ^4.0.10 From de629873386403c29b2d5f64462193c5ac81702e Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Thu, 2 Mar 2023 19:13:00 +0200 Subject: [PATCH 5/5] Add Fiat API mode to advanced privacy settings page --- .../advanced_privacy_settings_page.dart | 68 +++++++++---------- .../advanced_privacy_settings_view_model.dart | 18 ++--- 2 files changed, 36 insertions(+), 50 deletions(-) diff --git a/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart b/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart index a82ddaf4e..05ff65889 100644 --- a/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart +++ b/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart @@ -52,39 +52,37 @@ class _AdvancedPrivacySettingsBodyState extends State( title: S.current.disable_fiat, - value: widget.privacySettingsViewModel.fiatApi == FiatApiMode.disabled, - onValueChange: (BuildContext context, bool value) { - widget.privacySettingsViewModel.setFiatMode(value); - }); - } - ), - Observer( - builder: (_) { - return SettingsChoicesCell( - ChoicesListItem( - title: S.current.exchange, - items: ExchangeApiMode.all, - selectedItem: widget.privacySettingsViewModel.exchangeStatus, - onItemSelected: (ExchangeApiMode mode) => - widget.privacySettingsViewModel.setExchangeApiMode(mode), + items: FiatApiMode.all, + selectedItem: widget.privacySettingsViewModel.fiatApiMode, + onItemSelected: (FiatApiMode mode) => + widget.privacySettingsViewModel.setFiatApiMode(mode), + ), + ); + }), + Observer(builder: (_) { + return SettingsChoicesCell( + ChoicesListItem( + title: S.current.exchange, + items: ExchangeApiMode.all, + selectedItem: widget.privacySettingsViewModel.exchangeStatus, + onItemSelected: (ExchangeApiMode mode) => + widget.privacySettingsViewModel.setExchangeApiMode(mode), + ), + ); + }), + Observer(builder: (_) { + return Column( + children: [ + SettingsSwitcherCell( + title: S.current.add_custom_node, + value: widget.privacySettingsViewModel.addCustomNode, + onValueChange: (_, __) => widget.privacySettingsViewModel.toggleAddCustomNode(), ), - ); - } - ), - Observer( - builder: (_) { - return Column( - children: [ - SettingsSwitcherCell( - title: S.current.add_custom_node, - value: widget.privacySettingsViewModel.addCustomNode, - onValueChange: (_, __) => widget.privacySettingsViewModel.toggleAddCustomNode(), - ), - if (widget.privacySettingsViewModel.addCustomNode) + if (widget.privacySettingsViewModel.addCustomNode) Padding( padding: EdgeInsets.only(left: 24, right: 24, top: 24), child: NodeForm( @@ -92,11 +90,9 @@ class _AdvancedPrivacySettingsBodyState extends State _settingsStore.exchangeStatus; @computed - FiatApiMode get fiatApi => _settingsStore.fiatApiMode; + FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode; @observable bool _addCustomNode = false; @@ -29,21 +29,11 @@ abstract class AdvancedPrivacySettingsViewModelBase with Store { bool get addCustomNode => _addCustomNode; @action - void setFiatMode(bool value) { - if (value) { - _settingsStore.fiatApiMode = FiatApiMode.disabled; - return; - } - _settingsStore.fiatApiMode = FiatApiMode.enabled; - } + void setFiatApiMode(FiatApiMode fiatApiMode) => _settingsStore.fiatApiMode = fiatApiMode; @action - void setExchangeApiMode(ExchangeApiMode value) { - _settingsStore.exchangeStatus = value; - } + void setExchangeApiMode(ExchangeApiMode value) => _settingsStore.exchangeStatus = value; @action - void toggleAddCustomNode() { - _addCustomNode = !_addCustomNode; - } + void toggleAddCustomNode() => _addCustomNode = !_addCustomNode; }