mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
Cw 314 trocador receive screen update (#823)
* Change receive screen ui * Upgrade flutter packages * revert Upgrade flutter packages * revert Upgrade flutter packages * Adjust flow for anon invoice page navigation * Add receive screen ui * Implement anonpay invoice * Add invoice detail to transactions page * Implement donation link * Fix transaction filter and details view * Save donation link * Fix transaction display issues * Fix formatting * Fix merge conflict * Fix localization * Fix transaction amount display * Fix transaction limit for fiat * Update fix from code review * Fix issues from code review * Make amountTo nullable to avoid potential * Remove encoding for description in donation link * Remove optional params from request * Fix QR image version * Refactor QRCode, fix issues from code review * Pass version to QRCode full page --------- Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
This commit is contained in:
co-authored by
OmarHatem
parent
893a267de6
commit
3006679560
@@ -0,0 +1,211 @@
|
||||
import 'dart:convert';
|
||||
import 'package:cake_wallet/anonpay/anonpay_donation_link_info.dart';
|
||||
import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart';
|
||||
import 'package:cake_wallet/anonpay/anonpay_request.dart';
|
||||
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:cw_core/wallet_base.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||
|
||||
class AnonPayApi {
|
||||
const AnonPayApi({
|
||||
this.useTorOnly = false,
|
||||
required this.wallet,
|
||||
});
|
||||
final bool useTorOnly;
|
||||
final WalletBase wallet;
|
||||
|
||||
static const anonpayRef = secrets.anonPayReferralCode;
|
||||
static const onionApiAuthority = 'trocadorfyhlu27aefre5u7zri66gudtzdyelymftvr4yjwcxhfaqsid.onion';
|
||||
static const clearNetAuthority = 'trocador.app';
|
||||
static const markup = secrets.trocadorExchangeMarkup;
|
||||
static const anonPayPath = '/anonpay';
|
||||
static const anonPayStatus = '/anonpay/status';
|
||||
static const coinPath = 'api/coin';
|
||||
static const apiKey = secrets.trocadorApiKey;
|
||||
|
||||
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 status = responseJSON['Status'] as String;
|
||||
final fiatAmount = responseJSON['Fiat_Amount'] as double?;
|
||||
final fiatEquiv = responseJSON['Fiat_Equiv'] as String?;
|
||||
final amountTo = responseJSON['AmountTo'] as double?;
|
||||
final coinTo = responseJSON['CoinTo'] as String;
|
||||
final address = responseJSON['Address'] as String;
|
||||
|
||||
return AnonpayStatusResponse(
|
||||
status: status,
|
||||
fiatAmount: fiatAmount,
|
||||
amountTo: amountTo,
|
||||
coinTo: coinTo,
|
||||
address: address,
|
||||
fiatEquiv: fiatEquiv,
|
||||
);
|
||||
}
|
||||
|
||||
Future<AnonpayInvoiceInfo> createInvoice(AnonPayRequest request) async {
|
||||
final description = Uri.encodeComponent(request.description);
|
||||
final body = <String, dynamic>{
|
||||
'ticker_to': request.cryptoCurrency.title.toLowerCase(),
|
||||
'network_to': _networkFor(request.cryptoCurrency),
|
||||
'address': request.address,
|
||||
'name': request.name,
|
||||
'description': description,
|
||||
'email': request.email,
|
||||
'ref': anonpayRef,
|
||||
'markup': markup,
|
||||
'direct': 'False',
|
||||
};
|
||||
|
||||
if (request.amount != null) {
|
||||
body['amount'] = request.amount;
|
||||
}
|
||||
if (request.fiatEquivalent != null) {
|
||||
body['fiat_equiv'] = request.fiatEquivalent;
|
||||
}
|
||||
final authority = await _getAuthority();
|
||||
|
||||
final response = await get(Uri.https(authority, anonPayPath, body));
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final id = responseJSON['ID'] as String;
|
||||
final url = responseJSON['url'] as String;
|
||||
final urlOnion = responseJSON['url_onion'] as String;
|
||||
final statusUrl = responseJSON['status_url'] as String;
|
||||
final statusUrlOnion = responseJSON['status_url_onion'] as String;
|
||||
|
||||
final statusInfo = await paymentStatus(id);
|
||||
|
||||
return AnonpayInvoiceInfo(
|
||||
invoiceId: id,
|
||||
clearnetUrl: url,
|
||||
onionUrl: urlOnion,
|
||||
status: statusInfo.status,
|
||||
fiatAmount: statusInfo.fiatAmount,
|
||||
fiatEquiv: statusInfo.fiatEquiv,
|
||||
amountTo: statusInfo.amountTo,
|
||||
coinTo: statusInfo.coinTo,
|
||||
address: statusInfo.address,
|
||||
clearnetStatusUrl: statusUrl,
|
||||
onionStatusUrl: statusUrlOnion,
|
||||
walletId: wallet.id,
|
||||
createdAt: DateTime.now(),
|
||||
provider: 'Trocador AnonPay invoice',
|
||||
);
|
||||
}
|
||||
|
||||
Future<AnonpayDonationLinkInfo> generateDonationLink(AnonPayRequest request) async {
|
||||
final body = <String, dynamic>{
|
||||
'ticker_to': request.cryptoCurrency.title.toLowerCase(),
|
||||
'network_to': _networkFor(request.cryptoCurrency),
|
||||
'address': request.address,
|
||||
'ref': anonpayRef,
|
||||
'direct': 'True',
|
||||
};
|
||||
if (request.name.isNotEmpty) {
|
||||
body['name'] = request.name;
|
||||
}
|
||||
if (request.description.isNotEmpty) {
|
||||
body['description'] = request.description;
|
||||
}
|
||||
if (request.email.isNotEmpty) {
|
||||
body['email'] = request.email;
|
||||
}
|
||||
|
||||
final clearnetUrl = Uri.https(clearNetAuthority, anonPayPath, body);
|
||||
final onionUrl = Uri.https(onionApiAuthority, anonPayPath, body);
|
||||
return AnonpayDonationLinkInfo(
|
||||
clearnetUrl: clearnetUrl.toString(),
|
||||
onionUrl: onionUrl.toString(),
|
||||
address: request.address,
|
||||
);
|
||||
}
|
||||
|
||||
Future<Limits> fetchLimits({
|
||||
FiatCurrency? fiatCurrency,
|
||||
required CryptoCurrency cryptoCurrency,
|
||||
}) async {
|
||||
double fiatRate = 0.0;
|
||||
if (fiatCurrency != null) {
|
||||
fiatRate = await FiatConversionService.fetchPrice(
|
||||
crypto: cryptoCurrency,
|
||||
fiat: fiatCurrency,
|
||||
torOnly: useTorOnly,
|
||||
);
|
||||
}
|
||||
|
||||
final params = <String, String>{
|
||||
'api_key': apiKey,
|
||||
'ticker': cryptoCurrency.title.toLowerCase(),
|
||||
'name': cryptoCurrency.name,
|
||||
};
|
||||
|
||||
final String apiAuthority = await _getAuthority();
|
||||
final uri = Uri.https(apiAuthority, coinPath, params);
|
||||
|
||||
final response = await get(uri);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
final coinJson = responseJSON.first as Map<String, dynamic>;
|
||||
final minimum = coinJson['minimum'] as double;
|
||||
final maximum = coinJson['maximum'] as double;
|
||||
|
||||
if (fiatCurrency != null) {
|
||||
return Limits(
|
||||
min: double.tryParse((minimum * fiatRate).toStringAsFixed(2)),
|
||||
max: double.tryParse((maximum * fiatRate).toStringAsFixed(2)),
|
||||
);
|
||||
}
|
||||
|
||||
return Limits(
|
||||
min: minimum,
|
||||
max: maximum,
|
||||
);
|
||||
}
|
||||
|
||||
String _networkFor(CryptoCurrency currency) {
|
||||
switch (currency) {
|
||||
case CryptoCurrency.usdt:
|
||||
return CryptoCurrency.btc.title.toLowerCase();
|
||||
default:
|
||||
return currency.tag != null ? _normalizeTag(currency.tag!) : 'Mainnet';
|
||||
}
|
||||
}
|
||||
|
||||
String _normalizeTag(String tag) {
|
||||
switch (tag) {
|
||||
case 'ETH':
|
||||
return 'ERC20';
|
||||
default:
|
||||
return tag.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _getAuthority() async {
|
||||
try {
|
||||
if (useTorOnly) {
|
||||
return onionApiAuthority;
|
||||
}
|
||||
final uri = Uri.https(onionApiAuthority, '/anonpay');
|
||||
await get(uri);
|
||||
return onionApiAuthority;
|
||||
} catch (e) {
|
||||
return clearNetAuthority;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import 'package:cake_wallet/anonpay/anonpay_info_base.dart';
|
||||
|
||||
class AnonpayDonationLinkInfo implements AnonpayInfoBase{
|
||||
final String clearnetUrl;
|
||||
final String onionUrl;
|
||||
final String address;
|
||||
|
||||
AnonpayDonationLinkInfo({
|
||||
required this.clearnetUrl,
|
||||
required this.onionUrl,
|
||||
required this.address,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
abstract class AnonpayInfoBase {
|
||||
String get clearnetUrl;
|
||||
String get onionUrl;
|
||||
String get address;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:cake_wallet/anonpay/anonpay_info_base.dart';
|
||||
import 'package:cw_core/keyable.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'anonpay_invoice_info.g.dart';
|
||||
|
||||
@HiveType(typeId: AnonpayInvoiceInfo.typeId)
|
||||
class AnonpayInvoiceInfo extends HiveObject with Keyable implements AnonpayInfoBase {
|
||||
@HiveField(0)
|
||||
final String invoiceId;
|
||||
@HiveField(1)
|
||||
String status;
|
||||
@HiveField(2)
|
||||
final double? fiatAmount;
|
||||
@HiveField(3)
|
||||
final String? fiatEquiv;
|
||||
@HiveField(4)
|
||||
final double? amountTo;
|
||||
@HiveField(5)
|
||||
final String coinTo;
|
||||
@HiveField(6)
|
||||
final String address;
|
||||
@HiveField(7)
|
||||
final String clearnetUrl;
|
||||
@HiveField(8)
|
||||
final String onionUrl;
|
||||
@HiveField(9)
|
||||
final String clearnetStatusUrl;
|
||||
@HiveField(10)
|
||||
final String onionStatusUrl;
|
||||
@HiveField(11)
|
||||
final DateTime createdAt;
|
||||
@HiveField(12)
|
||||
final String walletId;
|
||||
@HiveField(13)
|
||||
final String provider;
|
||||
|
||||
static const typeId = 10;
|
||||
static const boxName = 'AnonpayInvoiceInfo';
|
||||
|
||||
AnonpayInvoiceInfo({
|
||||
required this.invoiceId,
|
||||
required this.clearnetUrl,
|
||||
required this.onionUrl,
|
||||
required this.clearnetStatusUrl,
|
||||
required this.onionStatusUrl,
|
||||
required this.status,
|
||||
this.fiatAmount,
|
||||
this.fiatEquiv,
|
||||
this.amountTo,
|
||||
required this.coinTo,
|
||||
required this.address,
|
||||
required this.createdAt,
|
||||
required this.walletId,
|
||||
required this.provider,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
|
||||
class AnonPayRequest {
|
||||
CryptoCurrency cryptoCurrency;
|
||||
String address;
|
||||
String name;
|
||||
String? amount;
|
||||
String email;
|
||||
String description;
|
||||
String? fiatEquivalent;
|
||||
|
||||
AnonPayRequest({
|
||||
required this.cryptoCurrency,
|
||||
required this.address,
|
||||
required this.name,
|
||||
required this.email,
|
||||
this.amount,
|
||||
required this.description,
|
||||
this.fiatEquivalent,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
class AnonpayStatusResponse {
|
||||
final String status;
|
||||
final double? fiatAmount;
|
||||
final String? fiatEquiv;
|
||||
final double? amountTo;
|
||||
final String coinTo;
|
||||
final String address;
|
||||
|
||||
const AnonpayStatusResponse({
|
||||
required this.status,
|
||||
this.fiatAmount,
|
||||
this.fiatEquiv,
|
||||
this.amountTo,
|
||||
required this.coinTo,
|
||||
required this.address,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user