WIP: tor http connection

This commit is contained in:
ryleedavis 2023-09-08 15:09:59 -06:00
parent 5de7b76131
commit f240163a91
7 changed files with 229 additions and 113 deletions

View file

@ -12,12 +12,13 @@ import 'dart:async';
import 'dart:convert';
import 'package:decimal/decimal.dart';
import 'package:http/http.dart' as http;
import 'package:stackwallet/models/buy/response_objects/crypto.dart';
import 'package:stackwallet/models/buy/response_objects/fiat.dart';
import 'package:stackwallet/models/buy/response_objects/order.dart';
import 'package:stackwallet/models/buy/response_objects/quote.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/buy/buy_response.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/fiat_enum.dart';
import 'package:stackwallet/utilities/logger.dart';
@ -35,8 +36,7 @@ class SimplexAPI {
static final SimplexAPI _instance = SimplexAPI._();
static SimplexAPI get instance => _instance;
/// set this to override using standard http client. Useful for testing
http.Client? client;
HTTP client = HTTP();
Uri _buildUri(String path, Map<String, String>? params) {
if (scheme == "http") {
@ -55,10 +55,15 @@ class SimplexAPI {
};
Uri url = _buildUri('api.php', data);
var res = await http.post(url, headers: headers);
if (res.statusCode != 200) {
var res = await client.post(
url: url,
headers: headers,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
if (res.code != 200) {
throw Exception(
'getAvailableCurrencies exception: statusCode= ${res.statusCode}');
'getAvailableCurrencies exception: statusCode= ${res.code}');
}
final jsonArray = jsonDecode(res.body); // TODO handle if invalid json
@ -116,10 +121,15 @@ class SimplexAPI {
};
Uri url = _buildUri('api.php', data);
var res = await http.post(url, headers: headers);
if (res.statusCode != 200) {
var res = await client.post(
url: url,
headers: headers,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
if (res.code != 200) {
throw Exception(
'getAvailableCurrencies exception: statusCode= ${res.statusCode}');
'getAvailableCurrencies exception: statusCode= ${res.code}');
}
final jsonArray = jsonDecode(res.body); // TODO validate json
@ -192,9 +202,14 @@ class SimplexAPI {
}
Uri url = _buildUri('api.php', data);
var res = await http.get(url, headers: headers);
if (res.statusCode != 200) {
throw Exception('getQuote exception: statusCode= ${res.statusCode}');
var res = await client.get(
url: url,
headers: headers,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
if (res.code != 200) {
throw Exception('getQuote exception: statusCode= ${res.code}');
}
final jsonArray = jsonDecode(res.body);
if (jsonArray.containsKey('error') as bool) {
@ -294,9 +309,14 @@ class SimplexAPI {
}
Uri url = _buildUri('api.php', data);
var res = await http.get(url, headers: headers);
if (res.statusCode != 200) {
throw Exception('newOrder exception: statusCode= ${res.statusCode}');
var res = await client.get(
url: url,
headers: headers,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
if (res.code != 200) {
throw Exception('newOrder exception: statusCode= ${res.code}');
}
final jsonArray = jsonDecode(res.body); // TODO check if valid json
if (jsonArray.containsKey('error') as bool) {

View file

@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:isar/isar.dart';
import 'package:nanodart/nanodart.dart';
import 'package:stackwallet/db/hive/db.dart';
@ -10,6 +9,7 @@ import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
@ -19,6 +19,7 @@ import 'package:stackwallet/services/mixins/wallet_cache.dart';
import 'package:stackwallet/services/mixins/wallet_db.dart';
import 'package:stackwallet/services/nano_api.dart';
import 'package:stackwallet/services/node_service.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/services/transaction_notification_tracker.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/constants.dart';
@ -145,10 +146,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
Balance get balance => _balance ??= getCachedBalance();
Balance? _balance;
HTTP client = HTTP();
Future<String?> requestWork(String hash) async {
return http
return client
.post(
Uri.parse("https://rpc.nano.to"), // this should be a
url: Uri.parse("https://rpc.nano.to"), // this should be a
headers: {'Content-type': 'application/json'},
body: json.encode(
{
@ -156,17 +159,19 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"hash": hash,
},
),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
)
.then((http.Response response) {
if (response.statusCode == 200) {
.then((client) {
if (client.code == 200) {
final Map<String, dynamic> decoded =
json.decode(response.body) as Map<String, dynamic>;
json.decode(client.body) as Map<String, dynamic>;
if (decoded.containsKey("error")) {
throw Exception("Received error ${decoded["error"]}");
}
return decoded["work"] as String?;
} else {
throw Exception("Received error ${response.statusCode}");
throw Exception("Received error ${client.code}");
}
});
}
@ -185,10 +190,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
final headers = {
"Content-Type": "application/json",
};
final balanceResponse = await http.post(
Uri.parse(getCurrentNode().host),
final balanceResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: balanceBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final balanceData = jsonDecode(balanceResponse.body);
@ -203,10 +210,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"representative": "true",
"account": publicAddress,
});
final infoResponse = await http.post(
Uri.parse(getCurrentNode().host),
final infoResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: infoBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final String frontier =
@ -256,10 +265,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"subtype": "send",
"block": sendBlock,
});
final processResponse = await http.post(
Uri.parse(getCurrentNode().host),
final processResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: processBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final Map<String, dynamic> decoded =
@ -328,8 +339,13 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
final headers = {
"Content-Type": "application/json",
};
final response = await http.post(Uri.parse(getCurrentNode().host),
headers: headers, body: body);
final response = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: body,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final data = jsonDecode(response.body);
_balance = Balance(
total: Amount(
@ -367,10 +383,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"representative": "true",
"account": publicAddress,
});
final infoResponse = await http.post(
Uri.parse(getCurrentNode().host),
final infoResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: infoBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final infoData = jsonDecode(infoResponse.body);
@ -385,10 +403,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"account": publicAddress,
});
final balanceResponse = await http.post(
Uri.parse(getCurrentNode().host),
final balanceResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: balanceBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final balanceData = jsonDecode(balanceResponse.body);
@ -458,10 +478,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"subtype": "receive",
"block": receiveBlock,
});
final processResponse = await http.post(
Uri.parse(getCurrentNode().host),
final processResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: processBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final Map<String, dynamic> decoded =
@ -472,14 +494,18 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
}
Future<void> confirmAllReceivable() async {
final receivableResponse = await http.post(Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "receivable",
"source": "true",
"account": await currentReceivingAddress,
"count": "-1",
}));
final receivableResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "receivable",
"source": "true",
"account": await currentReceivingAddress,
"count": "-1",
}),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final receivableData = await jsonDecode(receivableResponse.body);
if (receivableData["blocks"] == "") {
@ -501,13 +527,17 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
await confirmAllReceivable();
final receivingAddress = (await _currentReceivingAddress)!;
final String publicAddress = receivingAddress.value;
final response = await http.post(Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "account_history",
"account": publicAddress,
"count": "-1",
}));
final response = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "account_history",
"account": publicAddress,
"count": "-1",
}),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final data = await jsonDecode(response.body);
final transactions =
data["history"] is List ? data["history"] as List<dynamic> : [];
@ -819,17 +849,19 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<bool> testNetworkConnection() async {
final uri = Uri.parse(getCurrentNode().host);
final response = await http.post(
uri,
final response = await client.post(
url: uri,
headers: {"Content-Type": "application/json"},
body: jsonEncode(
{
"action": "version",
},
),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
return response.statusCode == 200;
return response.code == 200;
}
Timer? _networkAliveTimer;
@ -915,10 +947,12 @@ class BananoWallet extends CoinServiceAPI with WalletCache, WalletDB {
final headers = {
"Content-Type": "application/json",
};
final infoResponse = await http.post(
Uri.parse(getCurrentNode().host),
final infoResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: infoBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final infoData = jsonDecode(infoResponse.body);

View file

@ -11,7 +11,6 @@
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:isar/isar.dart';
import 'package:nanodart/nanodart.dart';
import 'package:stackwallet/db/isar/main_db.dart';
@ -19,6 +18,7 @@ import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
@ -28,6 +28,7 @@ import 'package:stackwallet/services/mixins/wallet_cache.dart';
import 'package:stackwallet/services/mixins/wallet_db.dart';
import 'package:stackwallet/services/nano_api.dart';
import 'package:stackwallet/services/node_service.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/services/transaction_notification_tracker.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/constants.dart';
@ -154,10 +155,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
Balance get balance => _balance ??= getCachedBalance();
Balance? _balance;
HTTP client = HTTP();
Future<String?> requestWork(String hash) async {
return http
return client
.post(
Uri.parse("https://rpc.nano.to"), // this should be a
url: Uri.parse("https://rpc.nano.to"), // this should be a
headers: {'Content-type': 'application/json'},
body: json.encode(
{
@ -165,9 +168,11 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"hash": hash,
},
),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
)
.then((http.Response response) {
if (response.statusCode == 200) {
.then((Response response) {
if (response.code == 200) {
final Map<String, dynamic> decoded =
json.decode(response.body) as Map<String, dynamic>;
if (decoded.containsKey("error")) {
@ -175,7 +180,7 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
}
return decoded["work"] as String?;
} else {
throw Exception("Received error ${response.statusCode}");
throw Exception("Received error ${response.body}");
}
});
}
@ -194,10 +199,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
final headers = {
"Content-Type": "application/json",
};
final balanceResponse = await http.post(
Uri.parse(getCurrentNode().host),
final balanceResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: balanceBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final balanceData = jsonDecode(balanceResponse.body);
@ -212,10 +219,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"representative": "true",
"account": publicAddress,
});
final infoResponse = await http.post(
Uri.parse(getCurrentNode().host),
final infoResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: infoBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final String frontier =
@ -265,10 +274,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"subtype": "send",
"block": sendBlock,
});
final processResponse = await http.post(
Uri.parse(getCurrentNode().host),
final processResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: processBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final Map<String, dynamic> decoded =
@ -333,8 +344,13 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
final headers = {
"Content-Type": "application/json",
};
final response = await http.post(Uri.parse(getCurrentNode().host),
headers: headers, body: body);
final response = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: body,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final data = jsonDecode(response.body);
_balance = Balance(
total: Amount(
@ -372,10 +388,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"representative": "true",
"account": publicAddress,
});
final infoResponse = await http.post(
Uri.parse(getCurrentNode().host),
final infoResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: infoBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final infoData = jsonDecode(infoResponse.body);
@ -390,10 +408,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"account": publicAddress,
});
final balanceResponse = await http.post(
Uri.parse(getCurrentNode().host),
final balanceResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: balanceBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final balanceData = jsonDecode(balanceResponse.body);
@ -463,10 +483,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
"subtype": "receive",
"block": receiveBlock,
});
final processResponse = await http.post(
Uri.parse(getCurrentNode().host),
final processResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: processBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final Map<String, dynamic> decoded =
@ -477,14 +499,18 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
}
Future<void> confirmAllReceivable() async {
final receivableResponse = await http.post(Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "receivable",
"source": "true",
"account": await currentReceivingAddress,
"count": "-1",
}));
final receivableResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "receivable",
"source": "true",
"account": await currentReceivingAddress,
"count": "-1",
}),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final receivableData = await jsonDecode(receivableResponse.body);
if (receivableData["blocks"] == "") {
@ -506,13 +532,17 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
await confirmAllReceivable();
final receivingAddress = (await _currentReceivingAddress)!;
final String publicAddress = receivingAddress.value;
final response = await http.post(Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "account_history",
"account": publicAddress,
"count": "-1",
}));
final response = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "account_history",
"account": publicAddress,
"count": "-1",
}),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final data = await jsonDecode(response.body);
final transactions =
data["history"] is List ? data["history"] as List<dynamic> : [];
@ -830,17 +860,19 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<bool> testNetworkConnection() async {
final uri = Uri.parse(getCurrentNode().host);
final response = await http.post(
uri,
final response = await client.post(
url: uri,
headers: {"Content-Type": "application/json"},
body: jsonEncode(
{
"action": "version",
},
),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
return response.statusCode == 200;
return response.code == 200;
}
Timer? _networkAliveTimer;
@ -926,10 +958,12 @@ class NanoWallet extends CoinServiceAPI with WalletCache, WalletDB {
final headers = {
"Content-Type": "application/json",
};
final infoResponse = await http.post(
Uri.parse(getCurrentNode().host),
final infoResponse = await client.post(
url: Uri.parse(getCurrentNode().host),
headers: headers,
body: infoBody,
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final infoData = jsonDecode(infoResponse.body);

View file

@ -12,14 +12,16 @@ import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter_native_splash/cli_commands.dart';
import 'package:http/http.dart' as http;
import 'package:stackwallet/exceptions/exchange/exchange_exception.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/exchange/exchange_response.dart';
import 'package:stackwallet/services/exchange/trocador/response_objects/trocador_coin.dart';
import 'package:stackwallet/services/exchange/trocador/response_objects/trocador_rate.dart';
import 'package:stackwallet/services/exchange/trocador/response_objects/trocador_trade.dart';
import 'package:stackwallet/services/exchange/trocador/response_objects/trocador_trade_new.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
const kTrocadorApiKey = "8rFqf7QLxX1mUBiNPEMaLUpV2biz6n";
const kTrocadorRefCode = "9eHm9BkQfS";
@ -31,6 +33,7 @@ abstract class TrocadorAPI {
static const String markup = "1";
static const String minKYCRating = "C";
static HTTP client = HTTP();
static Uri _buildUri({
required String method,
@ -46,12 +49,14 @@ abstract class TrocadorAPI {
int code = -1;
try {
debugPrint("URI: $uri");
final response = await http.get(
uri,
final response = await client.get(
url: uri,
headers: {'Content-Type': 'application/json'},
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
code = response.statusCode;
code = response.code;
debugPrint("CODE: $code");
debugPrint("BODY: ${response.body}");

View file

@ -1,8 +1,10 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:stackwallet/dto/ordinals/inscription_data.dart';
import 'package:stackwallet/dto/ordinals/litescribe_response.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/prefs.dart';
class LitescribeAPI {
static final LitescribeAPI _instance = LitescribeAPI._internal();
@ -13,12 +15,17 @@ class LitescribeAPI {
}
LitescribeAPI._internal();
HTTP client = HTTP();
late String baseUrl;
Future<LitescribeResponse> _getResponse(String endpoint) async {
final response = await http.get(Uri.parse('$baseUrl$endpoint'));
if (response.statusCode == 200) {
final response = await client.get(
url: Uri.parse('$baseUrl$endpoint'),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
if (response.code == 200) {
return LitescribeResponse(data: _validateJson(response.body));
} else {
throw Exception(

View file

@ -1,7 +1,9 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:nanodart/nanodart.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/prefs.dart';
class NanoAPI {
static Future<
@ -16,9 +18,11 @@ class NanoAPI {
NAccountInfo? accountInfo;
Exception? exception;
HTTP client = HTTP();
try {
final response = await http.post(
server,
final response = await client.post(
url: server,
headers: {
"Content-Type": "application/json",
},
@ -27,6 +31,8 @@ class NanoAPI {
"representative": "true",
"account": account,
}),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
final map = jsonDecode(response.body);
@ -105,8 +111,10 @@ class NanoAPI {
required Uri server,
required Map<String, dynamic> block,
}) async {
final response = await http.post(
server,
HTTP client = HTTP();
final response = await client.post(
url: server,
headers: {
"Content-Type": "application/json",
},
@ -116,6 +124,8 @@ class NanoAPI {
"subtype": "change",
"block": block,
}),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
return jsonDecode(response.body);

View file

@ -11,13 +11,15 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:stackwallet/models/paynym/created_paynym.dart';
import 'package:stackwallet/models/paynym/paynym_account.dart';
import 'package:stackwallet/models/paynym/paynym_claim.dart';
import 'package:stackwallet/models/paynym/paynym_follow.dart';
import 'package:stackwallet/models/paynym/paynym_response.dart';
import 'package:stackwallet/models/paynym/paynym_unfollow.dart';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:tuple/tuple.dart';
// todo: better error message parsing (from response itself?)
@ -26,6 +28,8 @@ class PaynymIsApi {
static const String baseURL = "https://paynym.is/api";
static const String version = "/v1";
HTTP client = HTTP();
Future<Tuple2<Map<String, dynamic>, int>> _post(
String endpoint,
Map<String, dynamic> body, [
@ -38,21 +42,23 @@ class PaynymIsApi {
final headers = {
'Content-Type': 'application/json; charset=UTF-8',
}..addAll(additionalHeaders);
final response = await http.post(
uri,
final response = await client.post(
url: uri,
headers: headers,
body: jsonEncode(body),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
);
debugPrint("Paynym request uri: $uri");
debugPrint("Paynym request body: $body");
debugPrint("Paynym request headers: $headers");
debugPrint("Paynym response code: ${response.statusCode}");
debugPrint("Paynym response code: ${response.code}");
debugPrint("Paynym response body: ${response.body}");
return Tuple2(
jsonDecode(response.body) as Map<String, dynamic>,
response.statusCode,
response.code,
);
}