feat: total tor support for cardano

This commit is contained in:
dethe 2024-09-01 16:19:46 +03:00
parent 02dc5c9416
commit 1ec7ee95d2
4 changed files with 60 additions and 39 deletions

View file

@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:on_chain/ada/ada.dart'; import 'package:on_chain/ada/ada.dart';
import 'package:on_chain/ada/src/provider/provider/provider.dart'; import 'package:on_chain/ada/src/provider/provider/provider.dart';
import 'package:socks5_proxy/socks.dart';
import '../networking/http.dart'; import '../networking/http.dart';
import '../pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart'; import '../pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart';
@ -243,9 +244,21 @@ Future<bool> testNodeConnection({
case Cardano(): case Cardano():
try { try {
final client = HttpClient();
if (ref
.read(prefsChangeNotifierProvider)
.useTor) {
final proxyInfo = TorService.sharedInstance.getProxyInfo();
final proxySettings = ProxySettings(
proxyInfo.host,
proxyInfo.port,
);
SocksTCPClient.assignToHttpClient(client, [proxySettings]);
}
final blockfrostProvider = BlockforestProvider( final blockfrostProvider = BlockforestProvider(
BlockfrostHttpProvider( BlockfrostHttpProvider(
url: "${formData.host!}:${formData.port!}/api/v0", url: "${formData.host!}:${formData.port!}/api/v0",
client: client,
), ),
); );

View file

@ -1,51 +1,49 @@
import 'dart:convert'; import 'dart:convert';
import 'package:http/http.dart' as http; import 'dart:io';
import 'package:on_chain/ada/src/provider/blockfrost/core/core.dart'; import 'package:on_chain/ada/src/provider/blockfrost/core/core.dart';
import 'package:on_chain/ada/src/provider/service/service.dart'; import 'package:on_chain/ada/src/provider/service/service.dart';
import '../../../utilities/logger.dart';
class BlockfrostHttpProvider implements BlockfrostServiceProvider { class BlockfrostHttpProvider implements BlockfrostServiceProvider {
BlockfrostHttpProvider( BlockfrostHttpProvider({
{required this.url, required this.url,
this.version = "v0", this.version = "v0",
this.projectId, this.projectId,
http.Client? client, HttpClient? client,
this.defaultRequestTimeout = const Duration(seconds: 30)}) this.defaultRequestTimeout = const Duration(seconds: 30),
: client = client ?? http.Client(); }) : client = client ?? HttpClient();
@override @override
final String url; final String url;
final String version; final String version;
final String? projectId; final String? projectId;
final http.Client client; final HttpClient client;
final Duration defaultRequestTimeout; final Duration defaultRequestTimeout;
@override @override
Future<dynamic> get(BlockforestRequestDetails params, Future<dynamic> get(BlockforestRequestDetails params,
[Duration? timeout]) async { [Duration? timeout,]) async {
final response = final response = await client.getUrl(Uri.parse(params.url(url, "api/$version"))).timeout(timeout ?? defaultRequestTimeout);
await client.get(Uri.parse(params.url(url, "api/$version")), headers: { response.headers.add("Content-Type", "application/json");
'Content-Type': 'application/json', response.headers.add("Accept", "application/json");
"Accept": "application/json", if (projectId != null) {
if (projectId != null) ...{"project_id": projectId!}, response.headers.add("project_id", projectId!);
}).timeout(timeout ?? defaultRequestTimeout); }
final data = json.decode(response.body); final responseStream = await response.close();
final data = json.decode(await responseStream.transform(utf8.decoder).join());
return data; return data;
} }
@override @override
Future<dynamic> post(BlockforestRequestDetails params, Future<dynamic> post(BlockforestRequestDetails params,
[Duration? timeout]) async { [Duration? timeout,]) async {
final response = await client final request = await client.postUrl(Uri.parse(params.url(url, "api/$version"))).timeout(timeout ?? defaultRequestTimeout);
.post(Uri.parse(params.url(url, "api/$version")), request.headers.add("Content-Type", "application/json");
headers: { request.headers.add("Accept", "application/json");
"Accept": "application/json", if (projectId != null) {
if (projectId != null) ...{"project_id": projectId!}, request.headers.add("project_id", projectId!);
...params.header }
}, request.write(json.encode(params.body));
body: params.body) final response = await request.close();
.timeout(timeout ?? defaultRequestTimeout); final data = json.decode(await response.transform(utf8.decoder).join());
final data = json.decode(response.body);
return data; return data;
} }
} }

View file

@ -111,6 +111,9 @@ class Cardano extends Bip39Currency {
@override @override
int get targetBlockTimeSeconds => 20; int get targetBlockTimeSeconds => 20;
@override
bool get torSupport => true;
@override @override
bool validateAddress(String address) { bool validateAddress(String address) {
switch (network) { switch (network) {

View file

@ -1,4 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart'; import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart';
import 'package:blockchain_utils/bip/cardano/bip32/cardano_icarus_bip32.dart'; import 'package:blockchain_utils/bip/cardano/bip32/cardano_icarus_bip32.dart';
@ -7,6 +8,7 @@ import 'package:blockchain_utils/bip/cardano/cip1852/conf/cip1852_coins.dart';
import 'package:blockchain_utils/bip/cardano/mnemonic/cardano_icarus_seed_generator.dart'; import 'package:blockchain_utils/bip/cardano/mnemonic/cardano_icarus_seed_generator.dart';
import 'package:blockchain_utils/bip/cardano/shelley/cardano_shelley.dart'; import 'package:blockchain_utils/bip/cardano/shelley/cardano_shelley.dart';
import 'package:on_chain/ada/ada.dart'; import 'package:on_chain/ada/ada.dart';
import 'package:socks5_proxy/socks.dart';
import '../../../models/balance.dart'; import '../../../models/balance.dart';
import '../../../models/isar/models/blockchain_data/address.dart'; import '../../../models/isar/models/blockchain_data/address.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
@ -84,12 +86,7 @@ class CardanoWallet extends Bip39Wallet<Cardano> {
@override @override
Future<bool> pingCheck() async { Future<bool> pingCheck() async {
try { try {
final currentNode = getCurrentNode(); await updateProvider();
blockfrostProvider = BlockforestProvider(
BlockfrostHttpProvider(
url: "${currentNode.host}:${currentNode.port}/api/v0",
),
);
final health = await blockfrostProvider!.request( final health = await blockfrostProvider!.request(
BlockfrostRequestBackendHealthStatus(), BlockfrostRequestBackendHealthStatus(),
@ -176,7 +173,7 @@ class CardanoWallet extends Bip39Wallet<Cardano> {
); );
var leftAmountForUtxos = txData.amount!.raw; var leftAmountForUtxos = txData.amount!.raw;
var listOfUtxosToBeUsed = <ADAAccountUTXOResponse>[]; final listOfUtxosToBeUsed = <ADAAccountUTXOResponse>[];
var totalBalance = BigInt.zero; var totalBalance = BigInt.zero;
for (final utxo in utxos) { for (final utxo in utxos) {
@ -271,7 +268,7 @@ class CardanoWallet extends Bip39Wallet<Cardano> {
var leftAmountForUtxos = txData.amount!.raw + txData.fee!.raw; var leftAmountForUtxos = txData.amount!.raw + txData.fee!.raw;
var listOfUtxosToBeUsed = <ADAAccountUTXOResponse>[]; final listOfUtxosToBeUsed = <ADAAccountUTXOResponse>[];
var totalBalance = BigInt.zero; var totalBalance = BigInt.zero;
for (final utxo in utxos) { for (final utxo in utxos) {
@ -551,9 +548,19 @@ class CardanoWallet extends Bip39Wallet<Cardano> {
Future<void> updateProvider() async { Future<void> updateProvider() async {
final currentNode = getCurrentNode(); final currentNode = getCurrentNode();
final client = HttpClient();
if (prefs.useTor) {
final proxyInfo = TorService.sharedInstance.getProxyInfo();
final proxySettings = ProxySettings(
proxyInfo.host,
proxyInfo.port,
);
SocksTCPClient.assignToHttpClient(client, [proxySettings]);
}
blockfrostProvider = BlockforestProvider( blockfrostProvider = BlockforestProvider(
BlockfrostHttpProvider( BlockfrostHttpProvider(
url: "${currentNode.host}:${currentNode.port}/", url: "${currentNode.host}:${currentNode.port}/",
client: client,
), ),
); );
} }