Skip to content

Commit

Permalink
Route everything I was able to catch trough the built-in tor node
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCyjaneK committed Jan 16, 2025
1 parent 70c7a9d commit f4431fd
Show file tree
Hide file tree
Showing 31 changed files with 344 additions and 261 deletions.
45 changes: 24 additions & 21 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'dart:isolate';

import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:cw_core/utils/http_client.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart';
import 'package:cw_bitcoin/litecoin_wallet.dart';
Expand Down Expand Up @@ -491,11 +492,12 @@ abstract class ElectrumWalletBase
Future<void> updateFeeRates() async {
if (await checkIfMempoolAPIIsEnabled() && type == WalletType.bitcoin) {
try {
final response = await http
.get(Uri.parse("https://mempool.cakewallet.com/api/v1/fees/recommended"))
.timeout(Duration(seconds: 5));

final result = json.decode(response.body) as Map<String, dynamic>;
final req = await getHttpClient()
.getUrl(Uri.parse("https://mempool.cakewallet.com/api/v1/fees/recommended"))
.timeout(Duration(seconds: 15));
final response = await req.close();
final stringData = await response.transform(utf8.decoder).join();
final result = json.decode(stringData) as Map<String, dynamic>;
final slowFee = (result['economyFee'] as num?)?.toInt() ?? 0;
int mediumFee = (result['hourFee'] as num?)?.toInt() ?? 0;
int fastFee = (result['fastestFee'] as num?)?.toInt() ?? 0;
Expand Down Expand Up @@ -1873,24 +1875,25 @@ abstract class ElectrumWalletBase

if (height != null && height > 0 && await checkIfMempoolAPIIsEnabled()) {
try {
final blockHash = await http.get(
Uri.parse(
"https://mempool.cakewallet.com/api/v1/block-height/$height",
),
);
final req = await getHttpClient()
.getUrl(Uri.parse("https://mempool.cakewallet.com/api/v1/block-height/$height"))
.timeout(Duration(seconds: 15));
final blockHash = await req.close();
final stringData = await blockHash.transform(utf8.decoder).join();

if (blockHash.statusCode == 200 &&
blockHash.body.isNotEmpty &&
jsonDecode(blockHash.body) != null) {
final blockResponse = await http.get(
Uri.parse(
"https://mempool.cakewallet.com/api/v1/block/${blockHash.body}",
),
);
if (blockResponse.statusCode == 200 &&
blockResponse.body.isNotEmpty &&
jsonDecode(blockResponse.body)['timestamp'] != null) {
time = int.parse(jsonDecode(blockResponse.body)['timestamp'].toString());
stringData.isNotEmpty &&
jsonDecode(stringData) != null) {
final blockResponseReq = await getHttpClient()
.getUrl(Uri.parse("https://mempool.cakewallet.com/api/v1/block/${stringData}"))
.timeout(Duration(seconds: 15));
final blockResponseRes = await blockResponseReq.close();
final blockResponse = await blockResponseRes.transform(utf8.decoder).join();

if (blockResponseRes == 200 &&
blockResponse.isNotEmpty &&
jsonDecode(blockResponse)['timestamp'] != null) {
time = int.parse(jsonDecode(blockResponse)['timestamp'].toString());
}
}
} catch (_) {}
Expand Down
21 changes: 13 additions & 8 deletions cw_core/lib/get_height_by_date.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cw_core/utils/http_client.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:intl/intl.dart';
import 'dart:convert';
Expand Down Expand Up @@ -234,10 +235,14 @@ int getHavenHeightByDate({required DateTime date}) {
}

Future<int> getHavenCurrentHeight() async {
final response = await http.get(Uri.parse('https://explorer.havenprotocol.org/api/networkinfo'));
final req = await getHttpClient()
.getUrl(Uri.parse('https://explorer.havenprotocol.org/api/networkinfo'))
.timeout(Duration(seconds: 15));
final response = await req.close();
final stringResponse = await response.transform(utf8.decoder).join();

if (response.statusCode == 200) {
final info = jsonDecode(response.body);
final info = jsonDecode(stringResponse);
return info['data']['height'] as int;
} else {
throw Exception('Failed to load current blockchain height');
Expand Down Expand Up @@ -269,13 +274,13 @@ const bitcoinDates = {
};

Future<int> getBitcoinHeightByDateAPI({required DateTime date}) async {
final response = await http.get(
Uri.parse(
"https://mempool.cakewallet.com/api/v1/mining/blocks/timestamp/${(date.millisecondsSinceEpoch / 1000).round()}",
),
);
final req = await getHttpClient()
.getUrl(Uri.parse("https://mempool.cakewallet.com/api/v1/mining/blocks/timestamp/${(date.millisecondsSinceEpoch / 1000).round()}"))
.timeout(Duration(seconds: 15));
final response = await req.close();
final stringResponse = await response.transform(utf8.decoder).join();

return jsonDecode(response.body)['height'] as int;
return jsonDecode(stringResponse)['height'] as int;
}

int getBitcoinHeightByDate({required DateTime date}) {
Expand Down
11 changes: 6 additions & 5 deletions cw_core/lib/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,14 @@ class Node extends HiveObject with Keyable {

Future<bool> requestEthereumServer() async {
try {
final response = await http.get(
uri,
headers: {'Content-Type': 'application/json'},
);
final req = await getHttpClient()
.getUrl(uri,)
.timeout(Duration(seconds: 15));
final response = await req.close();

return response.statusCode >= 200 && response.statusCode < 300;
} catch (_) {
} catch (err) {
printV("Failed to request ethereum server: $err");
return false;
}
}
Expand Down
21 changes: 11 additions & 10 deletions lib/anonpay/anonpay_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:cake_wallet/anonpay/anonpay_status_response.dart';
import 'package:cake_wallet/core/fiat_conversion_service.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:cw_core/crypto_currency.dart';
Expand All @@ -30,8 +31,9 @@ class AnonPayApi {

Future<AnonpayStatusResponse> 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<String, dynamic>;
final response = await ProxyWrapper().get(clearnetUri: Uri.https(authority, "$anonPayStatus/$id"));
final responseString = await response.transform(utf8.decoder).join();
final responseJSON = json.decode(responseString) as Map<String, dynamic>;
final status = responseJSON['Status'] as String;
final fiatAmount = responseJSON['Fiat_Amount'] as double?;
final fiatEquiv = responseJSON['Fiat_Equiv'] as String?;
Expand Down Expand Up @@ -71,9 +73,9 @@ class AnonPayApi {
}
final authority = await _getAuthority();

final response = await get(Uri.https(authority, anonPayPath, body));

final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final response = await ProxyWrapper().get(clearnetUri: Uri.https(authority, anonPayPath, body));
final responseString = await response.transform(utf8.decoder).join();
final responseJSON = json.decode(responseString) as Map<String, dynamic>;
final id = responseJSON['ID'] as String;
final url = responseJSON['url'] as String;
final urlOnion = responseJSON['url_onion'] as String;
Expand Down Expand Up @@ -149,14 +151,13 @@ class AnonPayApi {
final String apiAuthority = await _getAuthority();
final uri = Uri.https(apiAuthority, coinPath, params);

final response = await get(uri);

final response = await ProxyWrapper().get(clearnetUri: uri);
final responseString = await response.transform(utf8.decoder).join();
final responseJSON = json.decode(responseString) as List<dynamic>;
if (response.statusCode != 200) {
throw Exception('Unexpected http status: ${response.statusCode}');
}

final responseJSON = json.decode(response.body) as List<dynamic>;

if (responseJSON.isEmpty) {
throw Exception('No data');
}
Expand Down Expand Up @@ -204,7 +205,7 @@ class AnonPayApi {
return onionApiAuthority;
}
final uri = Uri.https(onionApiAuthority, '/anonpay');
await get(uri);
await ProxyWrapper().get(clearnetUri: uri);
return onionApiAuthority;
} catch (e) {
return clearNetAuthority;
Expand Down
15 changes: 9 additions & 6 deletions lib/buy/dfx/dfx_buy_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/connect_device/connect_device_page.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/proxy_wrapper.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/view_model/hardware_wallet/ledger_view_model.dart';
import 'package:cw_core/crypto_currency.dart';
Expand Down Expand Up @@ -123,10 +124,12 @@ class DFXBuyProvider extends BuyProvider {
final url = Uri.https(_baseUrl, '/v1/fiat');

try {
final response = await http.get(url, headers: {'accept': 'application/json'});

final response = await ProxyWrapper().get(
clearnetUri: url,
headers: {'accept': 'application/json'});
final responseString = await response.transform(utf8.decoder).join();
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as List<dynamic>;
final data = jsonDecode(responseString) as List<dynamic>;
for (final item in data) {
if (item['name'] == fiatCurrency) return item as Map<String, dynamic>;
}
Expand All @@ -146,10 +149,10 @@ class DFXBuyProvider extends BuyProvider {
final url = Uri.https(_baseUrl, '/v1/asset', {'blockchains': blockchain});

try {
final response = await http.get(url, headers: {'accept': 'application/json'});

final response = await ProxyWrapper().get(clearnetUri: url, headers: {'accept': 'application/json'});
final responseString = await response.transform(utf8.decoder).join();
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
final responseData = jsonDecode(responseString);

if (responseData is List && responseData.isNotEmpty) {
return responseData.first as Map<String, dynamic>;
Expand Down
8 changes: 5 additions & 3 deletions lib/buy/meld/meld_buy_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:cake_wallet/buy/payment_method.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/proxy_wrapper.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/utils/print_verbose.dart';
Expand Down Expand Up @@ -60,18 +61,19 @@ class MeldBuyProvider extends BuyProvider {
final url = Uri.https(_baseUrl, path, params);

try {
final response = await http.get(
url,
final response = await ProxyWrapper().get(
clearnetUri: url,
headers: {
'Authorization': _isProduction ? '' : _testApiKey,
'Meld-Version': '2023-12-19',
'accept': 'application/json',
'content-type': 'application/json',
},
);
final responseString = await response.transform(utf8.decoder).join();

if (response.statusCode == 200) {
final data = jsonDecode(response.body) as List<dynamic>;
final data = jsonDecode(responseString) as List<dynamic>;
final paymentMethods =
data.map((e) => PaymentMethod.fromMeldJson(e as Map<String, dynamic>)).toList();
return paymentMethods;
Expand Down
17 changes: 10 additions & 7 deletions lib/buy/moonpay/moonpay_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:cake_wallet/palette.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/themes/theme_base.dart';
import 'package:cake_wallet/utils/proxy_wrapper.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart';
Expand Down Expand Up @@ -110,9 +111,10 @@ class MoonPayProvider extends BuyProvider {
final url = Uri.https(_baseUrl, path, params);

try {
final response = await get(url, headers: {'accept': 'application/json'});
final response = await ProxyWrapper().get(clearnetUri: url, headers: {'accept': 'application/json'});
final responseString = await response.transform(utf8.decoder).join();
if (response.statusCode == 200) {
return jsonDecode(response.body) as Map<String, dynamic>;
return jsonDecode(responseString) as Map<String, dynamic>;
} else {
printV('MoonPay does not support fiat: $fiatCurrency');
return {};
Expand Down Expand Up @@ -180,10 +182,10 @@ class MoonPayProvider extends BuyProvider {
final path = '$_currenciesPath/$formattedCryptoCurrency$quotePath';
final url = Uri.https(_baseUrl, path, params);
try {
final response = await get(url);

final response = await ProxyWrapper().get(clearnetUri: url);
final responseString = await response.transform(utf8.decoder).join();
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final data = jsonDecode(responseString) as Map<String, dynamic>;

// Check if the response is for the correct fiat currency
if (isBuyAction) {
Expand Down Expand Up @@ -300,13 +302,14 @@ class MoonPayProvider extends BuyProvider {
Future<Order> findOrderById(String id) async {
final url = _apiUrl + _transactionsSuffix + '/$id' + '?apiKey=' + _apiKey;
final uri = Uri.parse(url);
final response = await get(uri);
final response = await ProxyWrapper().get(clearnetUri: uri);
final responseString = await response.transform(utf8.decoder).join();

if (response.statusCode != 200) {
throw BuyException(title: providerDescription, content: 'Transaction $id is not found!');
}

final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final responseJSON = json.decode(responseString) as Map<String, dynamic>;
final status = responseJSON['status'] as String;
final state = TradeState.deserialize(raw: status);
final createdAtRaw = responseJSON['createdAt'] as String;
Expand Down
18 changes: 10 additions & 8 deletions lib/buy/onramper/onramper_buy_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/themes/extensions/cake_text_theme.dart';
import 'package:cake_wallet/utils/proxy_wrapper.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/currency.dart';
import 'package:cw_core/utils/print_verbose.dart';
Expand Down Expand Up @@ -59,10 +60,10 @@ class OnRamperBuyProvider extends BuyProvider {

try {
final response =
await http.get(url, headers: {'Authorization': _apiKey, 'accept': 'application/json'});

await ProxyWrapper().get(clearnetUri: url, headers: {'Authorization': _apiKey, 'accept': 'application/json'});
final responseString = await response.transform(utf8.decoder).join();
if (response.statusCode == 200) {
final Map<String, dynamic> data = jsonDecode(response.body) as Map<String, dynamic>;
final Map<String, dynamic> data = jsonDecode(responseString) as Map<String, dynamic>;
final List<dynamic> message = data['message'] as List<dynamic>;
return message
.map((item) => PaymentMethod.fromOnramperJson(item as Map<String, dynamic>))
Expand All @@ -82,10 +83,11 @@ class OnRamperBuyProvider extends BuyProvider {

try {
final response =
await http.get(url, headers: {'Authorization': _apiKey, 'accept': 'application/json'});
await ProxyWrapper().get(clearnetUri: url, headers: {'Authorization': _apiKey, 'accept': 'application/json'});
final responseString = await response.transform(utf8.decoder).join();

if (response.statusCode == 200) {
final Map<String, dynamic> data = jsonDecode(response.body) as Map<String, dynamic>;
final Map<String, dynamic> data = jsonDecode(responseString) as Map<String, dynamic>;

final List<dynamic> onramps = data['message'] as List<dynamic>;

Expand Down Expand Up @@ -147,10 +149,10 @@ class OnRamperBuyProvider extends BuyProvider {
final headers = {'Authorization': _apiKey, 'accept': 'application/json'};

try {
final response = await http.get(url, headers: headers);

final response = await ProxyWrapper().get(clearnetUri: url, headers: headers);
final responseString = await response.transform(utf8.decoder).join();
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as List<dynamic>;
final data = jsonDecode(responseString) as List<dynamic>;
if (data.isEmpty) return null;

List<Quote> validQuotes = [];
Expand Down
6 changes: 4 additions & 2 deletions lib/buy/robinhood/robinhood_buy_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/connect_device/connect_device_page.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/proxy_wrapper.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/view_model/hardware_wallet/ledger_view_model.dart';
import 'package:cw_core/crypto_currency.dart';
Expand Down Expand Up @@ -165,8 +166,9 @@ class RobinhoodBuyProvider extends BuyProvider {
Uri.https('api.robinhood.com', '/catpay/v1/${cryptoCurrency.title}/quote/', queryParams);

try {
final response = await http.get(uri, headers: {'accept': 'application/json'});
final responseData = jsonDecode(response.body) as Map<String, dynamic>;
final response = await ProxyWrapper().get(clearnetUri: uri, headers: {'accept': 'application/json'});
final responseString = await response.transform(utf8.decoder).join();
final responseData = jsonDecode(responseString) as Map<String, dynamic>;

if (response.statusCode == 200) {
final paymentType = _getPaymentTypeByString(responseData['paymentMethod'] as String?);
Expand Down
Loading

0 comments on commit f4431fd

Please sign in to comment.