2020-08-29 10:19:27 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:cake_wallet/core/key_service.dart';
|
2020-09-14 12:08:33 +00:00
|
|
|
import 'package:cake_wallet/router.dart';
|
|
|
|
import 'package:cake_wallet/routes.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:cake_wallet/src/domain/common/sync_status.dart';
|
2020-09-14 12:08:33 +00:00
|
|
|
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/dashboard/dashboard_page.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cake_wallet/di.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_wallet_service.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/monero/monero_wallet_service.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_base.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_service.dart';
|
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2020-07-24 16:29:16 +00:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:cake_wallet/store/authentication_store.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/secret_store_key.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/encrypt.dart';
|
2020-07-24 16:29:16 +00:00
|
|
|
import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart';
|
|
|
|
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
|
|
|
|
import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
// FIXME: move me
|
|
|
|
Future<void> loadCurrentWallet() async {
|
|
|
|
final appStore = getIt.get<AppStore>();
|
2020-09-14 12:08:33 +00:00
|
|
|
final name = getIt.get<SharedPreferences>().getString('current_wallet_name');
|
2020-07-06 20:09:03 +00:00
|
|
|
final typeRaw =
|
|
|
|
getIt.get<SharedPreferences>().getInt('current_wallet_type') ?? 0;
|
|
|
|
final type = deserializeFromInt(typeRaw);
|
|
|
|
final password =
|
|
|
|
await getIt.get<KeyService>().getWalletPassword(walletName: name);
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
WalletService _service;
|
|
|
|
switch (type) {
|
|
|
|
case WalletType.monero:
|
|
|
|
_service = MoneroWalletService();
|
|
|
|
break;
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
_service = BitcoinWalletService();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
final wallet = await _service.openWallet(name, password);
|
|
|
|
appStore.wallet = wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactionDisposer _initialAuthReaction;
|
2020-07-06 20:09:03 +00:00
|
|
|
ReactionDisposer _onCurrentWalletChangeReaction;
|
|
|
|
ReactionDisposer _onWalletSyncStatusChangeReaction;
|
2020-07-24 16:29:16 +00:00
|
|
|
ReactionDisposer _onCurrentFiatCurrencyChangeDisposer;
|
2020-08-29 10:19:27 +00:00
|
|
|
Timer _reconnectionTimer;
|
2020-06-20 07:10:00 +00:00
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
Future<void> bootstrap(
|
2020-09-14 12:08:33 +00:00
|
|
|
{FiatConvertationService fiatConvertationService,
|
|
|
|
GlobalKey<NavigatorState> navigatorKey}) async {
|
2020-06-20 07:10:00 +00:00
|
|
|
final authenticationStore = getIt.get<AuthenticationStore>();
|
2020-07-24 16:29:16 +00:00
|
|
|
final settingsStore = getIt.get<SettingsStore>();
|
|
|
|
final fiatConvertationStore = getIt.get<FiatConvertationStore>();
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
if (authenticationStore.state == AuthenticationState.uninitialized) {
|
|
|
|
authenticationStore.state =
|
|
|
|
getIt.get<SharedPreferences>().getString('current_wallet_name') == null
|
|
|
|
? AuthenticationState.denied
|
|
|
|
: AuthenticationState.installed;
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialAuthReaction ??= autorun((_) async {
|
|
|
|
final state = authenticationStore.state;
|
2020-09-14 12:08:33 +00:00
|
|
|
print(state);
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
if (state == AuthenticationState.installed) {
|
|
|
|
await loadCurrentWallet();
|
|
|
|
}
|
2020-09-14 12:08:33 +00:00
|
|
|
|
|
|
|
if (state == AuthenticationState.installed) {
|
|
|
|
await navigatorKey.currentState
|
|
|
|
.pushAndRemoveUntil(createLoginRoute(), (_) => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == AuthenticationState.allowed) {
|
|
|
|
await navigatorKey.currentState
|
|
|
|
.pushAndRemoveUntil(createDashboardRoute(), (_) => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == AuthenticationState.denied) {
|
|
|
|
await navigatorKey.currentState
|
|
|
|
.pushAndRemoveUntil(createWelcomeRoute(), (_) => false);
|
|
|
|
}
|
2020-06-20 07:10:00 +00:00
|
|
|
});
|
2020-07-06 20:09:03 +00:00
|
|
|
|
|
|
|
_onCurrentWalletChangeReaction ??=
|
|
|
|
reaction((_) => getIt.get<AppStore>().wallet, (WalletBase wallet) async {
|
|
|
|
_onWalletSyncStatusChangeReaction?.reaction?.dispose();
|
2020-08-29 10:19:27 +00:00
|
|
|
_reconnectionTimer?.cancel();
|
2020-08-27 16:54:34 +00:00
|
|
|
_onWalletSyncStatusChangeReaction = reaction(
|
2020-07-06 20:09:03 +00:00
|
|
|
(_) => wallet.syncStatus is ConnectedSyncStatus,
|
2020-08-31 08:44:58 +00:00
|
|
|
(Object _) async => await wallet.startSync());
|
2020-07-06 20:09:03 +00:00
|
|
|
|
2020-08-29 10:19:27 +00:00
|
|
|
_reconnectionTimer = Timer.periodic(Duration(seconds: 5), (_) async {
|
|
|
|
if (wallet.syncStatus is LostConnectionSyncStatus ||
|
|
|
|
wallet.syncStatus is FailedSyncStatus) {
|
|
|
|
try {
|
|
|
|
await wallet.connectToNode(
|
|
|
|
node: settingsStore.getCurrentNode(wallet.type));
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-06 20:09:03 +00:00
|
|
|
await getIt
|
|
|
|
.get<SharedPreferences>()
|
|
|
|
.setString('current_wallet_name', wallet.name);
|
|
|
|
|
|
|
|
await getIt
|
|
|
|
.get<SharedPreferences>()
|
|
|
|
.setInt('current_wallet_type', serializeToInt(wallet.type));
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
final node = settingsStore.getCurrentNode(wallet.type);
|
2020-07-24 16:29:16 +00:00
|
|
|
final cryptoCurrency = wallet.currency;
|
|
|
|
final fiatCurrency = settingsStore.fiatCurrency;
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
await wallet.connectToNode(node: node);
|
|
|
|
|
2020-07-24 16:29:16 +00:00
|
|
|
final price = await fiatConvertationService.getPrice(
|
2020-08-27 16:54:34 +00:00
|
|
|
crypto: cryptoCurrency, fiat: fiatCurrency);
|
2020-07-24 16:29:16 +00:00
|
|
|
|
|
|
|
fiatConvertationStore.setPrice(price);
|
|
|
|
});
|
|
|
|
|
2020-08-27 16:54:34 +00:00
|
|
|
_onCurrentFiatCurrencyChangeDisposer ??= reaction(
|
|
|
|
(_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async {
|
2020-07-24 16:29:16 +00:00
|
|
|
final cryptoCurrency = getIt.get<AppStore>().wallet.currency;
|
|
|
|
|
|
|
|
final price = await fiatConvertationService.getPrice(
|
2020-08-27 16:54:34 +00:00
|
|
|
crypto: cryptoCurrency, fiat: fiatCurrency);
|
2020-07-24 16:29:16 +00:00
|
|
|
|
|
|
|
fiatConvertationStore.setPrice(price);
|
2020-07-06 20:09:03 +00:00
|
|
|
});
|
2020-06-20 07:10:00 +00:00
|
|
|
}
|