Merge branch 'CAKE-306-add-moonpay-option-for-btc-buying' of github.com:cake-tech/cake_wallet into CAKE-306-add-moonpay-option-for-btc-buying

This commit is contained in:
M
2021-06-07 17:01:33 +03:00
45 changed files with 368 additions and 109 deletions
@@ -1,5 +1,5 @@
class BitcoinMnemonicIsIncorrectException implements Exception {
@override
String toString() =>
'Bitcoin mnemonic has incorrect format. Mnemonic should contain 24 words separated by space.';
'Bitcoin mnemonic has incorrect format. Mnemonic should contain 12 or 24 words separated by space.';
}
@@ -35,7 +35,7 @@ abstract class ElectrumTransactionHistoryBase
@override
void addMany(Map<String, ElectrumTransactionInfo> transactions) =>
this.transactions.addAll(transactions);
transactions.forEach((_, tx) => _updateOrInsert(tx));
@override
Future<void> save() async {
+5 -13
View File
@@ -23,7 +23,7 @@ class MoonPayBuyProvider extends BuyProvider {
static const _currenciesSuffix = '/v3/currencies';
static const _quoteSuffix = '/buy_quote';
static const _transactionsSuffix = '/v1/transactions';
static const _countrySuffix = '/v3/countries';
static const _ipAddressSuffix = '/v4/ip_address';
static const _apiKey = secrets.moonPayApiKey;
static const _secretKey = secrets.moonPaySecretKey;
@@ -125,23 +125,15 @@ class MoonPayBuyProvider extends BuyProvider {
);
}
static Future<bool> onEnabled(String deviceCountryCode) async {
final url = _apiUrl + _countrySuffix;
static Future<bool> onEnabled() async {
final url = _apiUrl + _ipAddressSuffix + '?apiKey=' + _apiKey;
var isBuyEnable = false;
final response = await get(url);
try {
final responseJSON = json.decode(response.body) as List<dynamic>;
for (var element in responseJSON) {
final countryMap = element as Map<String, dynamic>;
final countryCode = countryMap['alpha2'] as String;
if (countryCode == deviceCountryCode) {
isBuyEnable = countryMap['isBuyAllowed'] as bool;
break;
}
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
isBuyEnable = responseJSON['isBuyAllowed'] as bool;
} catch (e) {
isBuyEnable = false;
print(e.toString());
+7 -16
View File
@@ -1,11 +1,9 @@
import 'package:cake_wallet/bitcoin/bitcoin_wallet_service.dart';
import 'package:cake_wallet/bitcoin/litecoin_wallet_service.dart';
import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart';
import 'package:cake_wallet/core/backup_service.dart';
import 'package:cake_wallet/core/wallet_service.dart';
import 'package:cake_wallet/entities/biometric_auth.dart';
import 'package:cake_wallet/entities/contact_record.dart';
import 'package:cake_wallet/entities/load_current_wallet.dart';
import 'package:cake_wallet/buy/order.dart';
import 'package:cake_wallet/entities/transaction_description.dart';
import 'package:cake_wallet/entities/transaction_info.dart';
@@ -92,7 +90,6 @@ import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
import 'package:cake_wallet/view_model/wallet_restore_view_model.dart';
import 'package:cake_wallet/view_model/wallet_seed_view_model.dart';
import 'package:cake_wallet/view_model/exchange/exchange_view_model.dart';
import 'package:devicelocale/devicelocale.dart';
import 'package:flutter/widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';
@@ -115,6 +112,7 @@ import 'package:cake_wallet/store/templates/exchange_template_store.dart';
import 'package:cake_wallet/entities/template.dart';
import 'package:cake_wallet/exchange/exchange_template.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/entities/push_notifications_service.dart';
final getIt = GetIt.instance;
@@ -155,19 +153,8 @@ Future setup(
(secrets.wyreApiKey?.isNotEmpty ?? false) &&
(secrets.wyreAccountId?.isNotEmpty ?? false);
/*var isMoonPayEnabled = false;
try {
final locale = await Devicelocale.currentLocale;
final deviceCountryCode = locale.split('_').last;
isMoonPayEnabled = await MoonPayBuyProvider.onEnabled(deviceCountryCode);
} catch (e) {
isMoonPayEnabled = false;
print(e.toString());
}*/
final settingsStore = await SettingsStoreBase.load(
nodeSource: _nodeSource, isBitcoinBuyEnabled: isBitcoinBuyEnabled,
isMoonPayEnabled: true);
nodeSource: _nodeSource, isBitcoinBuyEnabled: isBitcoinBuyEnabled);
if (_isSetupFinished) {
return;
@@ -246,6 +233,9 @@ Future setup(
settingsStore: getIt.get<SettingsStore>(),
fiatConvertationStore: getIt.get<FiatConversionStore>()));
getIt.registerFactory(() => PushNotificationsService());
getIt.registerFactory(() => DashboardViewModel(
balanceViewModel: getIt.get<BalanceViewModel>(),
appStore: getIt.get<AppStore>(),
@@ -253,7 +243,8 @@ Future setup(
tradeFilterStore: getIt.get<TradeFilterStore>(),
transactionFilterStore: getIt.get<TransactionFilterStore>(),
settingsStore: settingsStore,
ordersStore: getIt.get<OrdersStore>()));
ordersStore: getIt.get<OrdersStore>(),
pushNotificationsService: getIt.get<PushNotificationsService>()));
getIt.registerFactory<AuthService>(() => AuthService(
secureStorage: getIt.get<FlutterSecureStorage>(),
+1 -1
View File
@@ -7,7 +7,7 @@ class BiometricAuth {
Future<bool> isAuthenticated() async {
try {
return await _localAuth.authenticateWithBiometrics(
return await _localAuth.authenticate(
localizedReason: S.current.biometric_auth_reason,
useErrorDialogs: true,
stickyAuth: false);
@@ -0,0 +1,45 @@
import 'dart:io';
import 'package:cake_wallet/main.dart';
import 'package:cake_wallet/utils/show_bar.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
class PushNotificationsService {
PushNotificationsService._();
factory PushNotificationsService() => _instance;
static final PushNotificationsService _instance = PushNotificationsService._();
static Future<dynamic> _onBackgroundMessage(Map<String, dynamic> message) async {}
final _firebaseMessaging = FirebaseMessaging();
bool _initialized = false;
Future<void> init() async {
if (_initialized) {
return;
}
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.configure(
onMessage: (message) async {
Map<dynamic, dynamic> alert = <dynamic, dynamic>{};
String msg = '';
String title = '';
if (Platform.isIOS) {
alert = message['aps']['alert'] as Map<dynamic, dynamic> ?? <dynamic, dynamic>{};
msg = alert['body'] as String ?? '';
title = alert['title'] as String ?? '';
}
if (Platform.isAndroid) {
msg = message['notification']['body'] as String ?? '';
title = message['notification']['title'] as String ?? '';
}
await showBar<void>(navigatorKey.currentContext, msg, titleText: title, duration: null);
},
onBackgroundMessage: _onBackgroundMessage);
_initialized = true;
}
}
+1 -1
View File
@@ -71,7 +71,7 @@ String walletTypeToDisplayName(WalletType type) {
case WalletType.bitcoin:
return 'Bitcoin (Electrum)';
case WalletType.litecoin:
return 'Litecoin';
return 'Litecoin (Electrum)';
default:
return '';
}
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:cake_wallet/view_model/dashboard/dashboard_view_model.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:auto_size_text/auto_size_text.dart';
@@ -176,7 +176,7 @@ class ExchangeTemplatePage extends BasePage {
initialIsAddressEditable:
exchangeViewModel.isReceiveAddressEnabled,
isAmountEstimated: true,
currencies: CryptoCurrency.all,
currencies: exchangeViewModel.receiveCurrencies,
onCurrencySelected: (currency) =>
exchangeViewModel.changeReceiveCurrency(
currency: currency),
@@ -45,6 +45,7 @@ class CurrencyPickerState extends State<CurrencyPicker> {
color: Palette.darkBlueCraiola,
);
final int crossAxisCount = 3;
final int maxNumberItemsInAlert = 12;
final int itemsCount;
final double backgroundHeight = 280;
final double thumbHeight = 72;
@@ -148,6 +149,7 @@ class CurrencyPickerState extends State<CurrencyPicker> {
);
})
),
if (itemsCount > maxNumberItemsInAlert)
CakeScrollbar(
backgroundHeight: backgroundHeight,
thumbHeight: thumbHeight,
-18
View File
@@ -1,5 +1,4 @@
import 'package:cake_wallet/bitcoin/bitcoin_transaction_priority.dart';
import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart';
import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cake_wallet/entities/transaction_priority.dart';
import 'package:cake_wallet/themes/theme_base.dart';
@@ -9,7 +8,6 @@ import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:package_info/package_info.dart';
import 'package:devicelocale/devicelocale.dart';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -41,7 +39,6 @@ abstract class SettingsStoreBase with Store {
@required TransactionPriority initialBitcoinTransactionPriority,
@required TransactionPriority initialMoneroTransactionPriority,
@required this.isBitcoinBuyEnabled,
@required this.isMoonPayEnabled,
this.actionlistDisplayMode}) {
fiatCurrency = initialFiatCurrency;
balanceDisplayMode = initialBalanceDisplayMode;
@@ -150,12 +147,9 @@ abstract class SettingsStoreBase with Store {
bool isBitcoinBuyEnabled;
bool isMoonPayEnabled;
static Future<SettingsStore> load(
{@required Box<Node> nodeSource,
@required bool isBitcoinBuyEnabled,
@required bool isMoonPayEnabled,
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
MoneroTransactionPriority initialMoneroTransactionPriority =
MoneroTransactionPriority.slow,
@@ -227,7 +221,6 @@ abstract class SettingsStoreBase with Store {
},
appVersion: packageInfo.version,
isBitcoinBuyEnabled: isBitcoinBuyEnabled,
isMoonPayEnabled: isMoonPayEnabled,
initialFiatCurrency: currentFiatCurrency,
initialBalanceDisplayMode: currentBalanceDisplayMode,
initialSaveRecipientAddress: shouldSaveRecipientAddress,
@@ -253,20 +246,9 @@ abstract class SettingsStoreBase with Store {
(secrets.wyreApiKey?.isNotEmpty ?? false) &&
(secrets.wyreAccountId?.isNotEmpty ?? false);
/*var isMoonPayEnabled = false;
try {
final locale = await Devicelocale.currentLocale;
final deviceCountryCode = locale.split('_').last;
isMoonPayEnabled = await MoonPayBuyProvider.onEnabled(deviceCountryCode);
} catch (e) {
isMoonPayEnabled = false;
print(e.toString());
}*/
final settings = await SettingsStoreBase.load(
nodeSource: nodeSource,
isBitcoinBuyEnabled: isBitcoinBuyEnabled,
isMoonPayEnabled: true,
initialBalanceDisplayMode: initialBalanceDisplayMode,
initialFiatCurrency: initialFiatCurrency,
initialMoneroTransactionPriority: initialMoneroTransactionPriority,
+12 -4
View File
@@ -2,8 +2,11 @@ import 'package:flushbar/flushbar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Future<T> showBar<T>(BuildContext context, String text,
{bool isDark = false, Duration duration = const Duration(seconds: 1), bool isDismissible = true}) {
Future<T> showBar<T>(BuildContext context, String messageText,
{bool isDark = false,
Duration duration = const Duration(seconds: 1),
bool isDismissible = true,
String titleText}) {
final bar = Flushbar<T>(
boxShadows: [
BoxShadow(
@@ -14,9 +17,14 @@ Future<T> showBar<T>(BuildContext context, String text,
backgroundColor: isDark ? Colors.black : Colors.white,
borderRadius: 35,
margin: EdgeInsets.all(50),
messageText: Text(text,
titleText: titleText != null
? Text(titleText,
textAlign: TextAlign.center,
style: TextStyle(color: isDark ? Colors.white : Colors.black, fontWeight: FontWeight.bold, fontSize: 24.0))
: null,
messageText: Text(messageText,
textAlign: TextAlign.center,
style: TextStyle(color: isDark ? Colors.white : Colors.black)),
style: TextStyle(color: isDark ? Colors.white : Colors.black, fontSize: 16)),
duration: duration,
isDismissible: isDismissible,
flushbarPosition: FlushbarPosition.TOP,
+21 -10
View File
@@ -21,15 +21,9 @@ class BuyViewModel = BuyViewModelBase with _$BuyViewModel;
abstract class BuyViewModelBase with Store {
BuyViewModelBase(this.ordersSource, this.ordersStore, this.settingsStore,
this.buyAmountViewModel, {@required this.wallet}) {
providerList = [WyreBuyProvider(wallet: wallet)];
if (settingsStore.isMoonPayEnabled ?? false) {
providerList.add(MoonPayBuyProvider(wallet: wallet));
}
_fetchBuyItems();
items = providerList.map((provider) =>
BuyItem(provider: provider, buyAmountViewModel: buyAmountViewModel))
.toList();
isRunning = false;
isDisabled = true;
isShowProviderButtons = false;
@@ -41,9 +35,6 @@ abstract class BuyViewModelBase with Store {
final BuyAmountViewModel buyAmountViewModel;
final WalletBase wallet;
@observable
List<BuyProvider> providerList;
@observable
BuyProvider selectedProvider;
@@ -96,4 +87,24 @@ abstract class BuyViewModelBase with Store {
buyAmountViewModel.amount = '';
selectedProvider = null;
}
Future<void> _fetchBuyItems() async {
final List<BuyProvider> _providerList = [WyreBuyProvider(wallet: wallet)];
var isMoonPayEnabled = false;
try {
isMoonPayEnabled = await MoonPayBuyProvider.onEnabled();
} catch (e) {
isMoonPayEnabled = false;
print(e.toString());
}
if (isMoonPayEnabled) {
_providerList.add(MoonPayBuyProvider(wallet: wallet));
}
items = _providerList.map((provider) =>
BuyItem(provider: provider, buyAmountViewModel: buyAmountViewModel))
.toList();
}
}
@@ -1,5 +1,6 @@
import 'package:cake_wallet/core/transaction_history.dart';
import 'package:cake_wallet/entities/balance.dart';
import 'package:cake_wallet/entities/push_notifications_service.dart';
import 'package:cake_wallet/buy/order.dart';
import 'package:cake_wallet/entities/transaction_history.dart';
import 'package:cake_wallet/exchange/trade_state.dart';
@@ -46,7 +47,9 @@ abstract class DashboardViewModelBase with Store {
this.tradeFilterStore,
this.transactionFilterStore,
this.settingsStore,
this.ordersStore}) {
this.ordersStore,
PushNotificationsService pushNotificationsService}) {
_pushNotificationsService = pushNotificationsService;
filterItems = {
S.current.transactions: [
FilterItem(
@@ -133,6 +136,8 @@ abstract class DashboardViewModelBase with Store {
return true;
});
Future.delayed(Duration(seconds: 2), () => _pushNotificationsService.init());
}
@observable
@@ -220,18 +225,20 @@ abstract class DashboardViewModelBase with Store {
bool get isBuyEnabled => settingsStore.isBitcoinBuyEnabled;
PushNotificationsService _pushNotificationsService;
ReactionDisposer _onMoneroAccountChangeReaction;
ReactionDisposer _onMoneroBalanceChangeReaction;
@observable
bool isOutdatedElectrumWallet;
Future<void> reconnect() async {
final node = appStore.settingsStore.getCurrentNode(wallet.type);
await wallet.connectToNode(node: node);
}
@observable
bool isOutdatedElectrumWallet;
@action
void _onWalletChange(
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
+9
View File
@@ -24,6 +24,15 @@ abstract class SupportViewModelBase with Store {
title: 'Email',
linkTitle: 'support@cakewallet.com',
link: 'mailto:support@cakewallet.com'),
LinkListItem(
title: 'Website',
linkTitle: 'cakewallet.com',
link: 'https://cakewallet.com'),
LinkListItem(
title: 'GitHub',
icon: 'assets/images/github.png',
linkTitle: S.current.apk_update,
link: 'https://github.com/cake-tech/cake_wallet/releases'),
LinkListItem(
title: 'Telegram',
icon: 'assets/images/Telegram.png',