2023-12-08 14:05:52 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-12-28 19:20:59 +00:00
|
|
|
import 'package:cake_wallet/buy/buy_provider.dart';
|
2023-12-08 14:05:52 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
|
|
|
import 'package:cake_wallet/routes.dart';
|
2024-05-05 01:44:50 +00:00
|
|
|
import 'package:cake_wallet/src/screens/connect_device/connect_device_page.dart';
|
2023-12-08 14:05:52 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
|
|
|
|
import 'package:cake_wallet/utils/device_info.dart';
|
|
|
|
import 'package:cake_wallet/utils/show_pop_up.dart';
|
2024-05-05 01:44:50 +00:00
|
|
|
import 'package:cake_wallet/view_model/hardware_wallet/ledger_view_model.dart';
|
2023-12-08 14:05:52 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
2023-12-28 19:20:59 +00:00
|
|
|
class DFXBuyProvider extends BuyProvider {
|
2024-05-05 01:44:50 +00:00
|
|
|
DFXBuyProvider({required WalletBase wallet, bool isTestEnvironment = false, LedgerViewModel? ledgerVM})
|
|
|
|
: super(wallet: wallet, isTestEnvironment: isTestEnvironment, ledgerVM: ledgerVM);
|
2023-12-08 14:05:52 +00:00
|
|
|
|
|
|
|
static const _baseUrl = 'api.dfx.swiss';
|
2024-04-25 11:06:11 +00:00
|
|
|
// static const _signMessagePath = '/v1/auth/signMessage';
|
|
|
|
static const _authPath = '/v1/auth';
|
2023-12-08 14:05:52 +00:00
|
|
|
static const walletName = 'CakeWallet';
|
|
|
|
|
2023-12-28 19:20:59 +00:00
|
|
|
@override
|
2024-04-25 10:52:24 +00:00
|
|
|
String get title => 'DFX.swiss';
|
2023-12-28 19:20:59 +00:00
|
|
|
|
|
|
|
@override
|
2024-01-01 13:05:37 +00:00
|
|
|
String get providerDescription => S.current.dfx_option_description;
|
2023-12-28 19:20:59 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get lightIcon => 'assets/images/dfx_light.png';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get darkIcon => 'assets/images/dfx_dark.png';
|
|
|
|
|
2023-12-08 14:05:52 +00:00
|
|
|
String get assetOut {
|
2023-12-28 19:20:59 +00:00
|
|
|
switch (wallet.type) {
|
2023-12-08 14:05:52 +00:00
|
|
|
case WalletType.bitcoin:
|
|
|
|
return 'BTC';
|
|
|
|
case WalletType.bitcoinCash:
|
|
|
|
return 'BCH';
|
|
|
|
case WalletType.litecoin:
|
|
|
|
return 'LTC';
|
|
|
|
case WalletType.monero:
|
|
|
|
return 'XMR';
|
|
|
|
case WalletType.ethereum:
|
|
|
|
return 'ETH';
|
2024-01-22 23:53:20 +00:00
|
|
|
case WalletType.polygon:
|
|
|
|
return 'MATIC';
|
2023-12-08 14:05:52 +00:00
|
|
|
default:
|
2023-12-28 19:20:59 +00:00
|
|
|
throw Exception("WalletType is not available for DFX ${wallet.type}");
|
2023-12-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String get blockchain {
|
2023-12-28 19:20:59 +00:00
|
|
|
switch (wallet.type) {
|
2023-12-08 14:05:52 +00:00
|
|
|
case WalletType.bitcoin:
|
|
|
|
case WalletType.bitcoinCash:
|
|
|
|
case WalletType.litecoin:
|
|
|
|
return 'Bitcoin';
|
|
|
|
case WalletType.monero:
|
|
|
|
return 'Monero';
|
|
|
|
case WalletType.ethereum:
|
|
|
|
return 'Ethereum';
|
2024-01-22 23:53:20 +00:00
|
|
|
case WalletType.polygon:
|
|
|
|
return 'Polygon';
|
2023-12-08 14:05:52 +00:00
|
|
|
default:
|
2023-12-28 19:20:59 +00:00
|
|
|
throw Exception("WalletType is not available for DFX ${wallet.type}");
|
2023-12-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 15:29:51 +00:00
|
|
|
String get walletAddress =>
|
|
|
|
wallet.walletAddresses.primaryAddress ?? wallet.walletAddresses.address;
|
|
|
|
|
2024-04-25 11:06:11 +00:00
|
|
|
Future<String> getSignMessage() async =>
|
|
|
|
"By_signing_this_message,_you_confirm_that_you_are_the_sole_owner_of_the_provided_Blockchain_address._Your_ID:_$walletAddress";
|
|
|
|
|
|
|
|
// // Lets keep this just in case, but we can avoid this API Call
|
|
|
|
// Future<String> getSignMessage() async {
|
|
|
|
// final uri = Uri.https(_baseUrl, _signMessagePath, {'address': walletAddress});
|
|
|
|
//
|
|
|
|
// final response = await http.get(uri, headers: {'accept': 'application/json'});
|
|
|
|
//
|
|
|
|
// if (response.statusCode == 200) {
|
|
|
|
// final responseBody = jsonDecode(response.body);
|
|
|
|
// return responseBody['message'] as String;
|
|
|
|
// } else {
|
|
|
|
// throw Exception(
|
|
|
|
// 'Failed to get sign message. Status: ${response.statusCode} ${response.body}');
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
Future<String> auth() async {
|
2024-05-05 01:44:50 +00:00
|
|
|
final signMessage = await getSignature(await getSignMessage());
|
2023-12-08 14:05:52 +00:00
|
|
|
|
|
|
|
final requestBody = jsonEncode({
|
|
|
|
'wallet': walletName,
|
|
|
|
'address': walletAddress,
|
|
|
|
'signature': signMessage,
|
|
|
|
});
|
|
|
|
|
2024-04-25 11:06:11 +00:00
|
|
|
final uri = Uri.https(_baseUrl, _authPath);
|
2024-01-11 15:29:51 +00:00
|
|
|
var response = await http.post(
|
|
|
|
uri,
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: requestBody,
|
|
|
|
);
|
2023-12-08 14:05:52 +00:00
|
|
|
|
|
|
|
if (response.statusCode == 201) {
|
|
|
|
final responseBody = jsonDecode(response.body);
|
|
|
|
return responseBody['accessToken'] as String;
|
2023-12-28 19:20:59 +00:00
|
|
|
} else if (response.statusCode == 403) {
|
|
|
|
final responseBody = jsonDecode(response.body);
|
|
|
|
final message = responseBody['message'] ?? 'Service unavailable in your country';
|
|
|
|
throw Exception(message);
|
2023-12-08 14:05:52 +00:00
|
|
|
} else {
|
2024-01-11 15:29:51 +00:00
|
|
|
throw Exception('Failed to sign up. Status: ${response.statusCode} ${response.body}');
|
2023-12-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 01:44:50 +00:00
|
|
|
Future<String> getSignature(String message) async {
|
2023-12-28 19:20:59 +00:00
|
|
|
switch (wallet.type) {
|
2023-12-08 14:05:52 +00:00
|
|
|
case WalletType.ethereum:
|
2024-01-22 23:53:20 +00:00
|
|
|
case WalletType.polygon:
|
2023-12-28 19:20:59 +00:00
|
|
|
return wallet.signMessage(message);
|
2023-12-08 14:05:52 +00:00
|
|
|
case WalletType.monero:
|
|
|
|
case WalletType.litecoin:
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
case WalletType.bitcoinCash:
|
2024-01-11 15:29:51 +00:00
|
|
|
return wallet.signMessage(message, address: walletAddress);
|
2023-12-08 14:05:52 +00:00
|
|
|
default:
|
2023-12-28 19:20:59 +00:00
|
|
|
throw Exception("WalletType is not available for DFX ${wallet.type}");
|
2023-12-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 13:05:37 +00:00
|
|
|
@override
|
2023-12-28 19:20:59 +00:00
|
|
|
Future<void> launchProvider(BuildContext context, bool? isBuyAction) async {
|
2024-05-05 01:44:50 +00:00
|
|
|
if (wallet.isHardwareWallet) {
|
|
|
|
if (!ledgerVM!.isConnected) {
|
|
|
|
await Navigator.of(context).pushNamed(Routes.connectDevices,
|
|
|
|
arguments: ConnectDevicePageParams(
|
|
|
|
walletType: wallet.walletInfo.type,
|
|
|
|
onConnectDevice: (BuildContext context, LedgerViewModel ledgerVM) {
|
|
|
|
ledgerVM.setLedger(wallet);
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
ledgerVM!.setLedger(wallet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 14:05:52 +00:00
|
|
|
try {
|
|
|
|
final assetOut = this.assetOut;
|
|
|
|
final blockchain = this.blockchain;
|
2023-12-28 19:20:59 +00:00
|
|
|
final actionType = isBuyAction == true ? '/buy' : '/sell';
|
2023-12-08 14:05:52 +00:00
|
|
|
|
2024-04-25 11:06:11 +00:00
|
|
|
final accessToken = await auth();
|
2023-12-08 14:05:52 +00:00
|
|
|
|
2023-12-28 19:20:59 +00:00
|
|
|
final uri = Uri.https('services.dfx.swiss', actionType, {
|
2023-12-08 14:05:52 +00:00
|
|
|
'session': accessToken,
|
|
|
|
'lang': 'en',
|
|
|
|
'asset-out': assetOut,
|
|
|
|
'blockchain': blockchain,
|
|
|
|
'asset-in': 'EUR',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (await canLaunchUrl(uri)) {
|
|
|
|
if (DeviceInfo.instance.isMobile) {
|
2024-01-11 15:29:51 +00:00
|
|
|
Navigator.of(context).pushNamed(Routes.webViewPage, arguments: [title, uri]);
|
2023-12-08 14:05:52 +00:00
|
|
|
} else {
|
|
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Exception('Could not launch URL');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
await showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
2024-04-25 10:52:24 +00:00
|
|
|
alertTitle: "DFX.swiss",
|
2023-12-08 14:05:52 +00:00
|
|
|
alertContent: S.of(context).buy_provider_unavailable + ': $e',
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|