diff --git a/lib/bitcoin/bitcoin_wallet.dart b/lib/bitcoin/bitcoin_wallet.dart index e43c0aeaa..33e69daf7 100644 --- a/lib/bitcoin/bitcoin_wallet.dart +++ b/lib/bitcoin/bitcoin_wallet.dart @@ -58,6 +58,7 @@ abstract class BitcoinWalletBase extends WalletBase with Store { {@required String password, @required String name, @required String dirPath, + @required WalletInfo walletInfo, String jsonSource}) { final data = json.decode(jsonSource) as Map; final mnemonic = data['mnemonic'] as String; @@ -83,7 +84,8 @@ abstract class BitcoinWalletBase extends WalletBase with Store { name: name, accountIndex: accountIndex, initialAddresses: addresses, - initialBalance: balance); + initialBalance: balance, + walletInfo: walletInfo); } static BitcoinWallet build( @@ -91,6 +93,7 @@ abstract class BitcoinWalletBase extends WalletBase with Store { @required String password, @required String name, @required String dirPath, + @required WalletInfo walletInfo, List initialAddresses, BitcoinBalance initialBalance, int accountIndex = 0}) { @@ -107,7 +110,8 @@ abstract class BitcoinWalletBase extends WalletBase with Store { accountIndex: accountIndex, initialAddresses: initialAddresses, initialBalance: initialBalance, - transactionHistory: history); + transactionHistory: history, + walletInfo: walletInfo); } @override diff --git a/lib/bitcoin/bitcoin_wallet_creation_credentials.dart b/lib/bitcoin/bitcoin_wallet_creation_credentials.dart index 051a5700e..f8df42df0 100644 --- a/lib/bitcoin/bitcoin_wallet_creation_credentials.dart +++ b/lib/bitcoin/bitcoin_wallet_creation_credentials.dart @@ -1,21 +1,23 @@ import 'package:cake_wallet/core/wallet_credentials.dart'; +import 'package:cake_wallet/entities/wallet_info.dart'; class BitcoinNewWalletCredentials extends WalletCredentials { - BitcoinNewWalletCredentials({String name}) : super(name: name); + BitcoinNewWalletCredentials({String name, WalletInfo walletInfo}) + : super(name: name, walletInfo: walletInfo); } class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials { BitcoinRestoreWalletFromSeedCredentials( - {String name, String password, this.mnemonic}) - : super(name: name, password: password); + {String name, String password, this.mnemonic, WalletInfo walletInfo}) + : super(name: name, password: password, walletInfo: walletInfo); final String mnemonic; } class BitcoinRestoreWalletFromWIFCredentials extends WalletCredentials { BitcoinRestoreWalletFromWIFCredentials( - {String name, String password, this.wif}) - : super(name: name, password: password); + {String name, String password, this.wif, WalletInfo walletInfo}) + : super(name: name, password: password, walletInfo: walletInfo); final String wif; } diff --git a/lib/bitcoin/bitcoin_wallet_service.dart b/lib/bitcoin/bitcoin_wallet_service.dart index 2e9a1c305..9272f0cb1 100644 --- a/lib/bitcoin/bitcoin_wallet_service.dart +++ b/lib/bitcoin/bitcoin_wallet_service.dart @@ -2,15 +2,22 @@ import 'dart:io'; import 'package:bip39/bip39.dart' as bip39; import 'package:cake_wallet/bitcoin/file.dart'; import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart'; +import 'package:cake_wallet/core/wallet_base.dart'; import 'package:cake_wallet/core/wallet_service.dart'; import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart'; import 'package:cake_wallet/entities/pathForWallet.dart'; +import 'package:cake_wallet/entities/wallet_info.dart'; import 'package:cake_wallet/entities/wallet_type.dart'; +import 'package:hive/hive.dart'; class BitcoinWalletService extends WalletService< BitcoinNewWalletCredentials, BitcoinRestoreWalletFromSeedCredentials, BitcoinRestoreWalletFromWIFCredentials> { + BitcoinWalletService(this.walletInfoSource); + + final Box walletInfoSource; + @override Future create(BitcoinNewWalletCredentials credentials) async { final dirPath = await pathForWalletDir( @@ -19,7 +26,8 @@ class BitcoinWalletService extends WalletService< dirPath: dirPath, mnemonic: bip39.generateMnemonic(), password: credentials.password, - name: credentials.name); + name: credentials.name, + walletInfo: credentials.walletInfo); await wallet.save(); await wallet.init(); @@ -37,11 +45,15 @@ class BitcoinWalletService extends WalletService< await pathForWalletDir(name: name, type: WalletType.bitcoin); final walletPath = '$walletDirPath/$name'; final walletJSONRaw = await read(path: walletPath, password: password); + final walletInfo = walletInfoSource.values.firstWhere( + (info) => info.id == WalletBase.idFor(name, WalletType.bitcoin), + orElse: () => null); final wallet = BitcoinWalletBase.fromJSON( password: password, name: name, dirPath: walletDirPath, - jsonSource: walletJSONRaw); + jsonSource: walletJSONRaw, + walletInfo: walletInfo); await wallet.init(); return wallet; @@ -68,7 +80,8 @@ class BitcoinWalletService extends WalletService< dirPath: dirPath, name: credentials.name, password: credentials.password, - mnemonic: credentials.mnemonic); + mnemonic: credentials.mnemonic, + walletInfo: credentials.walletInfo); await wallet.save(); await wallet.init(); diff --git a/lib/core/wallet_credentials.dart b/lib/core/wallet_credentials.dart index 3998bb005..ddfafab1a 100644 --- a/lib/core/wallet_credentials.dart +++ b/lib/core/wallet_credentials.dart @@ -1,7 +1,7 @@ import 'package:cake_wallet/entities/wallet_info.dart'; abstract class WalletCredentials { - WalletCredentials({this.name, this.password, this.height}); + WalletCredentials({this.name, this.password, this.height, this.walletInfo}); final String name; final int height; diff --git a/lib/di.dart b/lib/di.dart index 9dad3efb3..b3c8f1d6b 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -14,6 +14,7 @@ import 'package:cake_wallet/src/screens/contact/contact_page.dart'; import 'package:cake_wallet/src/screens/exchange_trade/exchange_confirm_page.dart'; import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_page.dart'; import 'package:cake_wallet/src/screens/faq/faq_page.dart'; +import 'package:cake_wallet/src/screens/new_wallet/new_wallet_type_page.dart'; import 'package:cake_wallet/src/screens/nodes/node_create_or_edit_page.dart'; import 'package:cake_wallet/src/screens/nodes/nodes_list_page.dart'; import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart'; @@ -353,7 +354,7 @@ Future setup( getIt.registerFactory(() => MoneroWalletService(walletInfoSource)); - getIt.registerFactory(() => BitcoinWalletService()); + getIt.registerFactory(() => BitcoinWalletService(walletInfoSource)); getIt.registerFactoryParam( (WalletType param1, __) { @@ -397,4 +398,9 @@ Future setup( transactionInfo, getIt.get().shouldSaveRecipientAddress, transactionDescriptionBox)); + + getIt.registerFactoryParam( + (para1, param2) => NewWalletTypePage(getIt.get(), + onTypeSelected: para1, isNewWallet: param2)); } diff --git a/lib/router.dart b/lib/router.dart index acad61d04..aa3f2cf71 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -60,29 +60,18 @@ Route createRoute(RouteSettings settings) { case Routes.newWalletFromWelcome: return CupertinoPageRoute( - builder: (_) => getIt.get(param1: - (PinCodeState context, dynamic _) async { - try { - context.changeProcessText( - 'Creating new wallet'); // FIXME: Unnamed constant - final newWalletVM = - getIt.get(param1: WalletType.monero); - await newWalletVM.create( - options: 'English'); // FIXME: Unnamed constant - context.hideProgressText(); - await Navigator.of(context.context) - .pushNamed(Routes.seed, arguments: true); - } catch (e) { - context.changeProcessText('Error: ${e.toString()}'); - } - }), + builder: (_) => getIt.get( + param1: (PinCodeState context, dynamic _) => + Navigator.of(context.context) + .pushNamed(Routes.newWalletType)), fullscreenDialog: true); case Routes.newWalletType: return CupertinoPageRoute( - builder: (_) => NewWalletTypePage( - onTypeSelected: (context, type) => Navigator.of(context) - .pushNamed(Routes.newWallet, arguments: type))); + builder: (_) => getIt.get( + param1: (BuildContext context, WalletType _) => + Navigator.of(context).pushNamed(Routes.seed, arguments: true), + param2: true)); case Routes.newWallet: final type = WalletType.monero; // settings.arguments as WalletType; @@ -104,11 +93,11 @@ Route createRoute(RouteSettings settings) { case Routes.restoreWalletType: return CupertinoPageRoute( - builder: (_) => NewWalletTypePage( - onTypeSelected: (context, type) => Navigator.of(context) - .pushNamed(Routes.restoreWalletOptions, arguments: type), - isNewWallet: false, - )); + builder: (_) => getIt.get( + param1: (BuildContext context, WalletType type) => + Navigator.of(context) + .pushNamed(Routes.restoreWalletFromSeed, arguments: type), + param2: false)); case Routes.restoreOptions: final type = settings.arguments as WalletType; @@ -146,7 +135,7 @@ Route createRoute(RouteSettings settings) { return CupertinoPageRoute( builder: (_) => getIt.get( param1: (PinCodeState context, dynamic _) => - Navigator.pushNamed(context.context, Routes.restoreWallet)), + Navigator.pushNamed(context.context, Routes.restoreWalletType)), fullscreenDialog: true); case Routes.seed: @@ -160,12 +149,7 @@ Route createRoute(RouteSettings settings) { getIt.get(param1: WalletType.monero)); case Routes.restoreWalletFromSeed: - // final args = settings.arguments as List; - final type = WalletType.monero; //args.first as WalletType; - // final language = type == WalletType.monero - // ? args[1] as String - // : LanguageList.english; - + final type = settings.arguments as WalletType; return CupertinoPageRoute( builder: (_) => RestoreWalletFromSeedPage(type: type)); diff --git a/lib/src/screens/new_wallet/new_wallet_type_page.dart b/lib/src/screens/new_wallet/new_wallet_type_page.dart index cf3b3b896..5751b11bd 100644 --- a/lib/src/screens/new_wallet/new_wallet_type_page.dart +++ b/lib/src/screens/new_wallet/new_wallet_type_page.dart @@ -1,6 +1,11 @@ +import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/entities/wallet_type.dart'; +import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/utils/show_bar.dart'; +import 'package:cake_wallet/view_model/wallet_new_vm.dart'; +import 'package:flushbar/flushbar.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -10,25 +15,28 @@ import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart'; import 'package:cake_wallet/src/screens/new_wallet/widgets/select_button.dart'; class NewWalletTypePage extends BasePage { - NewWalletTypePage({this.onTypeSelected, this.isNewWallet = true}); + NewWalletTypePage(this.walletNewVM, + {this.onTypeSelected, this.isNewWallet}); final void Function(BuildContext, WalletType) onTypeSelected; final bool isNewWallet; + final WalletNewVM walletNewVM; @override - String get title => isNewWallet - ? S.current.new_wallet - : S.current.wallet_list_restore_wallet; + String get title => + isNewWallet ? S.current.new_wallet : S.current.wallet_list_restore_wallet; @override Widget body(BuildContext context) => - WalletTypeForm(onTypeSelected: onTypeSelected); + WalletTypeForm(walletNewVM, isNewWallet, onTypeSelected: onTypeSelected); } class WalletTypeForm extends StatefulWidget { - WalletTypeForm({this.onTypeSelected}); + WalletTypeForm(this.walletNewVM, this.isNewWallet, {this.onTypeSelected}); final void Function(BuildContext, WalletType) onTypeSelected; + final WalletNewVM walletNewVM; + final bool isNewWallet; @override WalletTypeFormState createState() => WalletTypeFormState(); @@ -42,10 +50,12 @@ class WalletTypeFormState extends State { final bitcoinIcon = Image.asset('assets/images/bitcoin.png', height: 24, width: 24); final walletTypeImage = Image.asset('assets/images/wallet_type.png'); - final walletTypeLightImage = Image.asset('assets/images/wallet_type_light.png'); + final walletTypeLightImage = + Image.asset('assets/images/wallet_type_light.png'); WalletType selected; List types; + Flushbar _progressBar; @override void initState() { @@ -56,7 +66,8 @@ class WalletTypeFormState extends State { @override Widget build(BuildContext context) { final walletImage = getIt.get().isDarkTheme - ? walletTypeImage : walletTypeLightImage; + ? walletTypeImage + : walletTypeLightImage; return Container( padding: EdgeInsets.only(top: 24), @@ -94,7 +105,7 @@ class WalletTypeFormState extends State { ), bottomSectionPadding: EdgeInsets.only(left: 24, right: 24, bottom: 24), bottomSection: PrimaryButton( - onPressed: () => widget.onTypeSelected(context, selected), + onPressed: () => onTypeSelected(), text: S.of(context).seed_language_next, color: Colors.green, textColor: Colors.white, @@ -114,4 +125,35 @@ class WalletTypeFormState extends State { return null; } } + + Future onTypeSelected() async { + if (!widget.isNewWallet) { + widget.onTypeSelected(context, selected); + return; + } + + try { + _changeProcessText(S.of(context).creating_new_wallet); + widget.walletNewVM.type = selected; + await widget.walletNewVM + .create(options: 'English'); // FIXME: Unnamed constant + await _progressBar?.dismiss(); + final state = widget.walletNewVM.state; + + if (state is ExecutedSuccessfullyState) { + widget.onTypeSelected(context, selected); + } + + if (state is FailureState) { + _changeProcessText( + S.of(context).creating_new_wallet_error(state.error)); + } + } catch (e) { + _changeProcessText(S.of(context).creating_new_wallet_error(e.toString())); + } + } + + void _changeProcessText(String text) { + _progressBar = createBar(text, duration: null)..show(context); + } } diff --git a/lib/src/screens/wallet_list/wallet_list_page.dart b/lib/src/screens/wallet_list/wallet_list_page.dart index 5162b1c5b..37745cdec 100644 --- a/lib/src/screens/wallet_list/wallet_list_page.dart +++ b/lib/src/screens/wallet_list/wallet_list_page.dart @@ -165,7 +165,7 @@ class WalletListBodyState extends State { ), bottomSection: Column(children: [ PrimaryImageButton( - onPressed: () => _generateNewWallet(), + onPressed: () => Navigator.of(context).pushNamed(Routes.newWalletType), image: newWalletImage, text: S.of(context).wallet_list_create_new_wallet, color: Theme.of(context).accentTextTheme.subtitle.decorationColor, @@ -175,7 +175,7 @@ class WalletListBodyState extends State { SizedBox(height: 10.0), PrimaryImageButton( onPressed: () => - Navigator.of(context).pushNamed(Routes.restoreWallet), + Navigator.of(context).pushNamed(Routes.restoreWalletType), image: restoreWalletImage, text: S.of(context).wallet_list_restore_wallet, color: Theme.of(context).accentTextTheme.caption.color, diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index d29002f2b..3509f8c07 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -27,7 +27,7 @@ abstract class WalletCreationVMBase with Store { @observable ExecutionState state; - final WalletType type; + WalletType type; final bool isRecovery; final Box _walletInfoSource; final AppStore _appStore; diff --git a/lib/view_model/wallet_new_vm.dart b/lib/view_model/wallet_new_vm.dart index 5ebf05f1b..c21f1b8c1 100644 --- a/lib/view_model/wallet_new_vm.dart +++ b/lib/view_model/wallet_new_vm.dart @@ -43,6 +43,8 @@ abstract class WalletNewVMBase extends WalletCreationVM with Store { } @override - Future process(WalletCredentials credentials) async => - _walletCreationService.create(credentials); + Future process(WalletCredentials credentials) async { + _walletCreationService.changeWalletType(type: type); + return _walletCreationService.create(credentials); + } }