From 123648cc6b5c1cfcbb5994f7759a3d64ffaa2e3e Mon Sep 17 00:00:00 2001 From: fosse Date: Wed, 10 Jan 2024 13:14:38 -0500 Subject: [PATCH] refactor trocador to use proxywrapper --- lib/core/fiat_conversion_service.dart | 2 - .../provider/trocador_exchange_provider.dart | 107 ++++++++---------- 2 files changed, 50 insertions(+), 59 deletions(-) diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index e491be1a0..57f6fb86c 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -1,14 +1,12 @@ import 'dart:io'; import 'package:cake_wallet/di.dart'; -import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/utils/proxy_wrapper.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cake_wallet/entities/fiat_currency.dart'; import 'dart:convert'; import 'package:cake_wallet/.secrets.g.dart' as secrets; import 'package:flutter/foundation.dart'; -import 'package:http/http.dart'; const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com'; const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion'; diff --git a/lib/exchange/provider/trocador_exchange_provider.dart b/lib/exchange/provider/trocador_exchange_provider.dart index faa4cc060..4e9dbd3d5 100644 --- a/lib/exchange/provider/trocador_exchange_provider.dart +++ b/lib/exchange/provider/trocador_exchange_provider.dart @@ -1,6 +1,8 @@ import 'dart:convert'; +import 'dart:io'; import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/exchange/limits.dart'; import 'package:cake_wallet/exchange/provider/exchange_provider.dart'; @@ -8,12 +10,13 @@ import 'package:cake_wallet/exchange/trade.dart'; import 'package:cake_wallet/exchange/trade_request.dart'; import 'package:cake_wallet/exchange/trade_state.dart'; import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart'; +import 'package:cake_wallet/utils/proxy_wrapper.dart'; import 'package:cw_core/crypto_currency.dart'; -import 'package:http/http.dart'; class TrocadorExchangeProvider extends ExchangeProvider { TrocadorExchangeProvider({this.useTorOnly = false, this.providerStates = const {}}) - : _lastUsedRateId = '', _provider = [], + : _lastUsedRateId = '', + _provider = [], super(pairList: supportedPairs(_notSupported)); bool useTorOnly; @@ -24,7 +27,7 @@ class TrocadorExchangeProvider extends ExchangeProvider { 'StealthEx', 'Simpleswap', 'Swapuz' - 'ChangeNow', + 'ChangeNow', 'Changehero', 'FixedFloat', 'LetsExchange', @@ -83,13 +86,13 @@ class TrocadorExchangeProvider extends ExchangeProvider { 'name': from.name, }; - final uri = await _getUri(coinPath, params); - final response = await get(uri); + final response = await proxyGet(coinPath, params); + final responseBody = await utf8.decodeStream(response); if (response.statusCode != 200) throw Exception('Unexpected http status: ${response.statusCode}'); - final responseJSON = json.decode(response.body) as List; + final responseJSON = json.decode(responseBody) as List; if (responseJSON.isEmpty) throw Exception('No data'); @@ -124,9 +127,9 @@ class TrocadorExchangeProvider extends ExchangeProvider { 'markup': markup, }; - final uri = await _getUri(newRatePath, params); - final response = await get(uri); - final responseJSON = json.decode(response.body) as Map; + final response = await proxyGet(newRatePath, params); + final responseBody = await utf8.decodeStream(response); + final responseJSON = json.decode(responseBody) as Map; final fromAmount = double.parse(responseJSON['amount_from'].toString()); final toAmount = double.parse(responseJSON['amount_to'].toString()); final rateId = responseJSON['trade_id'] as String? ?? ''; @@ -145,7 +148,6 @@ class TrocadorExchangeProvider extends ExchangeProvider { @override Future createTrade({required TradeRequest request, required bool isFixedRateMode}) async { - final params = { 'api_key': apiKey, 'ticker_from': _normalizeCurrency(request.fromCurrency), @@ -172,7 +174,6 @@ class TrocadorExchangeProvider extends ExchangeProvider { params['id'] = _lastUsedRateId; } - String firstAvailableProvider = ''; for (var provider in _provider) { @@ -188,11 +189,11 @@ class TrocadorExchangeProvider extends ExchangeProvider { params['provider'] = firstAvailableProvider; - final uri = await _getUri(createTradePath, params); - final response = await get(uri); + final response = await proxyGet(createTradePath, params); + final responseBody = await utf8.decodeStream(response); if (response.statusCode == 400) { - final responseJSON = json.decode(response.body) as Map; + final responseJSON = json.decode(responseBody) as Map; final error = responseJSON['error'] as String; final message = responseJSON['message'] as String; throw Exception('${error}\n$message'); @@ -201,7 +202,7 @@ class TrocadorExchangeProvider extends ExchangeProvider { if (response.statusCode != 200) throw Exception('Unexpected http status: ${response.statusCode}'); - final responseJSON = json.decode(response.body) as Map; + final responseJSON = json.decode(responseBody) as Map; final id = responseJSON['trade_id'] as String; final inputAddress = responseJSON['address_provider'] as String; final refundAddress = responseJSON['refund_address'] as String; @@ -230,38 +231,37 @@ class TrocadorExchangeProvider extends ExchangeProvider { @override Future findTradeById({required String id}) async { - final uri = await _getUri(tradePath, {'api_key': apiKey, 'id': id}); - return get(uri).then((response) { - if (response.statusCode != 200) - throw Exception('Unexpected http status: ${response.statusCode}'); + final response = await proxyGet(tradePath, {'api_key': apiKey, 'id': id}); + if (response.statusCode != 200) + throw Exception('Unexpected http status: ${response.statusCode}'); + final String responseBody = await utf8.decodeStream(response); - final responseListJson = json.decode(response.body) as List; - final responseJSON = responseListJson.first; - final id = responseJSON['trade_id'] as String; - final payoutAddress = responseJSON['address_user'] as String; - final refundAddress = responseJSON['refund_address'] as String; - final inputAddress = responseJSON['address_provider'] as String; - final fromAmount = responseJSON['amount_from']?.toString() ?? '0'; - final password = responseJSON['password'] as String; - final providerId = responseJSON['id_provider'] as String; - final providerName = responseJSON['provider'] as String; + final responseListJson = json.decode(responseBody) as List; + final responseJSON = responseListJson.first; + final tradeId = responseJSON['trade_id'] as String; + final payoutAddress = responseJSON['address_user'] as String; + final refundAddress = responseJSON['refund_address'] as String; + final inputAddress = responseJSON['address_provider'] as String; + final fromAmount = responseJSON['amount_from']?.toString() ?? '0'; + final password = responseJSON['password'] as String; + final providerId = responseJSON['id_provider'] as String; + final providerName = responseJSON['provider'] as String; - return Trade( - id: id, - from: CryptoCurrency.fromString(responseJSON['ticker_from'] as String), - to: CryptoCurrency.fromString(responseJSON['ticker_to'] as String), - provider: description, - inputAddress: inputAddress, - refundAddress: refundAddress, - createdAt: DateTime.parse(responseJSON['date'] as String), - amount: fromAmount, - state: TradeState.deserialize(raw: responseJSON['status'] as String), - payoutAddress: payoutAddress, - password: password, - providerId: providerId, - providerName: providerName, - ); - }); + return Trade( + id: tradeId, + from: CryptoCurrency.fromString(responseJSON['ticker_from'] as String), + to: CryptoCurrency.fromString(responseJSON['ticker_to'] as String), + provider: description, + inputAddress: inputAddress, + refundAddress: refundAddress, + createdAt: DateTime.parse(responseJSON['date'] as String), + amount: fromAmount, + state: TradeState.deserialize(raw: responseJSON['status'] as String), + payoutAddress: payoutAddress, + password: password, + providerId: providerId, + providerName: providerName, + ); } String _networkFor(CryptoCurrency currency) { @@ -305,17 +305,10 @@ class TrocadorExchangeProvider extends ExchangeProvider { } } - Future _getUri(String path, Map queryParams) async { - final uri = Uri.http(onionApiAuthority, path, queryParams); - - if (useTorOnly) return uri; - - try { - await get(uri); - - return uri; - } catch (e) { - return Uri.https(clearNetAuthority, path, queryParams); - } + 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); + return await proxy.get(onionUri, torOnly: useTorOnly, clearnetUri: clearnetUri); } }