Added selection of new wallet type. Returned support of bitcoin wallet.

M committed Nov 12, 2020 at 18:31 UTC e8f53766a08016198ae4d29aad6a4d08dae06298
10 files changed +110 -57
lib/bitcoin/bitcoin_wallet.dart
+6 -2
@@ -58,6 +58,7 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
58 {@required String password,
59 @required String name,
60 @required String dirPath,
61 + @required WalletInfo walletInfo,
62 String jsonSource}) {
63 final data = json.decode(jsonSource) as Map;
64 final mnemonic = data['mnemonic'] as String;
@@ -83,7 +84,8 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
84 name: name,
85 accountIndex: accountIndex,
86 initialAddresses: addresses,
86 - initialBalance: balance);
87 + initialBalance: balance,
88 + walletInfo: walletInfo);
89 }
90
91 static BitcoinWallet build(
@@ -91,6 +93,7 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
93 @required String password,
94 @required String name,
95 @required String dirPath,
96 + @required WalletInfo walletInfo,
97 List<BitcoinAddressRecord> initialAddresses,
98 BitcoinBalance initialBalance,
99 int accountIndex = 0}) {
@@ -107,7 +110,8 @@ abstract class BitcoinWalletBase extends WalletBase<BitcoinBalance> with Store {
110 accountIndex: accountIndex,
111 initialAddresses: initialAddresses,
112 initialBalance: initialBalance,
110 - transactionHistory: history);
113 + transactionHistory: history,
114 + walletInfo: walletInfo);
115 }
116
117 @override
lib/bitcoin/bitcoin_wallet_creation_credentials.dart
+7 -5
@@ -1,21 +1,23 @@
1 import 'package:cake_wallet/core/wallet_credentials.dart';
2 +import 'package:cake_wallet/entities/wallet_info.dart';
3
4 class BitcoinNewWalletCredentials extends WalletCredentials {
4 - BitcoinNewWalletCredentials({String name}) : super(name: name);
5 + BitcoinNewWalletCredentials({String name, WalletInfo walletInfo})
6 + : super(name: name, walletInfo: walletInfo);
7 }
8
9 class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
10 BitcoinRestoreWalletFromSeedCredentials(
9 - {String name, String password, this.mnemonic})
10 - : super(name: name, password: password);
11 + {String name, String password, this.mnemonic, WalletInfo walletInfo})
12 + : super(name: name, password: password, walletInfo: walletInfo);
13
14 final String mnemonic;
15 }
16
17 class BitcoinRestoreWalletFromWIFCredentials extends WalletCredentials {
18 BitcoinRestoreWalletFromWIFCredentials(
17 - {String name, String password, this.wif})
18 - : super(name: name, password: password);
19 + {String name, String password, this.wif, WalletInfo walletInfo})
20 + : super(name: name, password: password, walletInfo: walletInfo);
21
22 final String wif;
23 }
lib/bitcoin/bitcoin_wallet_service.dart
+16 -3
@@ -2,15 +2,22 @@ import 'dart:io';
2 import 'package:bip39/bip39.dart' as bip39;
3 import 'package:cake_wallet/bitcoin/file.dart';
4 import 'package:cake_wallet/bitcoin/bitcoin_wallet_creation_credentials.dart';
5 +import 'package:cake_wallet/core/wallet_base.dart';
6 import 'package:cake_wallet/core/wallet_service.dart';
7 import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
8 import 'package:cake_wallet/entities/pathForWallet.dart';
9 +import 'package:cake_wallet/entities/wallet_info.dart';
10 import 'package:cake_wallet/entities/wallet_type.dart';
11 +import 'package:hive/hive.dart';
12
13 class BitcoinWalletService extends WalletService<
14 BitcoinNewWalletCredentials,
15 BitcoinRestoreWalletFromSeedCredentials,
16 BitcoinRestoreWalletFromWIFCredentials> {
17 + BitcoinWalletService(this.walletInfoSource);
18 +
19 + final Box<WalletInfo> walletInfoSource;
20 +
21 @override
22 Future<BitcoinWallet> create(BitcoinNewWalletCredentials credentials) async {
23 final dirPath = await pathForWalletDir(
@@ -19,7 +26,8 @@ class BitcoinWalletService extends WalletService<
26 dirPath: dirPath,
27 mnemonic: bip39.generateMnemonic(),
28 password: credentials.password,
22 - name: credentials.name);
29 + name: credentials.name,
30 + walletInfo: credentials.walletInfo);
31 await wallet.save();
32 await wallet.init();
33
@@ -37,11 +45,15 @@ class BitcoinWalletService extends WalletService<
45 await pathForWalletDir(name: name, type: WalletType.bitcoin);
46 final walletPath = '$walletDirPath/$name';
47 final walletJSONRaw = await read(path: walletPath, password: password);
48 + final walletInfo = walletInfoSource.values.firstWhere(
49 + (info) => info.id == WalletBase.idFor(name, WalletType.bitcoin),
50 + orElse: () => null);
51 final wallet = BitcoinWalletBase.fromJSON(
52 password: password,
53 name: name,
54 dirPath: walletDirPath,
44 - jsonSource: walletJSONRaw);
55 + jsonSource: walletJSONRaw,
56 + walletInfo: walletInfo);
57 await wallet.init();
58
59 return wallet;
@@ -68,7 +80,8 @@ class BitcoinWalletService extends WalletService<
80 dirPath: dirPath,
81 name: credentials.name,
82 password: credentials.password,
71 - mnemonic: credentials.mnemonic);
83 + mnemonic: credentials.mnemonic,
84 + walletInfo: credentials.walletInfo);
85 await wallet.save();
86 await wallet.init();
87
lib/core/wallet_credentials.dart
+1 -1
@@ -1,7 +1,7 @@
1 import 'package:cake_wallet/entities/wallet_info.dart';
2
3 abstract class WalletCredentials {
4 - WalletCredentials({this.name, this.password, this.height});
4 + WalletCredentials({this.name, this.password, this.height, this.walletInfo});
5
6 final String name;
7 final int height;
lib/di.dart
+7 -1
@@ -14,6 +14,7 @@ import 'package:cake_wallet/src/screens/contact/contact_page.dart';
14 import 'package:cake_wallet/src/screens/exchange_trade/exchange_confirm_page.dart';
15 import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_page.dart';
16 import 'package:cake_wallet/src/screens/faq/faq_page.dart';
17 +import 'package:cake_wallet/src/screens/new_wallet/new_wallet_type_page.dart';
18 import 'package:cake_wallet/src/screens/nodes/node_create_or_edit_page.dart';
19 import 'package:cake_wallet/src/screens/nodes/nodes_list_page.dart';
20 import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart';
@@ -353,7 +354,7 @@ Future setup(
354
355 getIt.registerFactory(() => MoneroWalletService(walletInfoSource));
356
356 - getIt.registerFactory(() => BitcoinWalletService());
357 + getIt.registerFactory(() => BitcoinWalletService(walletInfoSource));
358
359 getIt.registerFactoryParam<WalletService, WalletType, void>(
360 (WalletType param1, __) {
@@ -397,4 +398,9 @@ Future setup(
398 transactionInfo,
399 getIt.get<SettingsStore>().shouldSaveRecipientAddress,
400 transactionDescriptionBox));
401 +
402 + getIt.registerFactoryParam<NewWalletTypePage,
403 + void Function(BuildContext, WalletType), bool>(
404 + (para1, param2) => NewWalletTypePage(getIt.get<WalletNewVM>(),
405 + onTypeSelected: para1, isNewWallet: param2));
406 }
lib/router.dart
+15 -31
@@ -60,29 +60,18 @@ Route<dynamic> createRoute(RouteSettings settings) {
60
61 case Routes.newWalletFromWelcome:
62 return CupertinoPageRoute<void>(
63 - builder: (_) => getIt.get<SetupPinCodePage>(param1:
64 - (PinCodeState<PinCodeWidget> context, dynamic _) async {
65 - try {
66 - context.changeProcessText(
67 - 'Creating new wallet'); // FIXME: Unnamed constant
68 - final newWalletVM =
69 - getIt.get<WalletNewVM>(param1: WalletType.monero);
70 - await newWalletVM.create(
71 - options: 'English'); // FIXME: Unnamed constant
72 - context.hideProgressText();
73 - await Navigator.of(context.context)
74 - .pushNamed(Routes.seed, arguments: true);
75 - } catch (e) {
76 - context.changeProcessText('Error: ${e.toString()}');
77 - }
78 - }),
63 + builder: (_) => getIt.get<SetupPinCodePage>(
64 + param1: (PinCodeState<PinCodeWidget> context, dynamic _) =>
65 + Navigator.of(context.context)
66 + .pushNamed(Routes.newWalletType)),
67 fullscreenDialog: true);
68
69 case Routes.newWalletType:
70 return CupertinoPageRoute<void>(
83 - builder: (_) => NewWalletTypePage(
84 - onTypeSelected: (context, type) => Navigator.of(context)
85 - .pushNamed(Routes.newWallet, arguments: type)));
71 + builder: (_) => getIt.get<NewWalletTypePage>(
72 + param1: (BuildContext context, WalletType _) =>
73 + Navigator.of(context).pushNamed(Routes.seed, arguments: true),
74 + param2: true));
75
76 case Routes.newWallet:
77 final type = WalletType.monero; // settings.arguments as WalletType;
@@ -104,11 +93,11 @@ Route<dynamic> createRoute(RouteSettings settings) {
93
94 case Routes.restoreWalletType:
95 return CupertinoPageRoute<void>(
107 - builder: (_) => NewWalletTypePage(
108 - onTypeSelected: (context, type) => Navigator.of(context)
109 - .pushNamed(Routes.restoreWalletOptions, arguments: type),
110 - isNewWallet: false,
111 - ));
96 + builder: (_) => getIt.get<NewWalletTypePage>(
97 + param1: (BuildContext context, WalletType type) =>
98 + Navigator.of(context)
99 + .pushNamed(Routes.restoreWalletFromSeed, arguments: type),
100 + param2: false));
101
102 case Routes.restoreOptions:
103 final type = settings.arguments as WalletType;
@@ -146,7 +135,7 @@ Route<dynamic> createRoute(RouteSettings settings) {
135 return CupertinoPageRoute<void>(
136 builder: (_) => getIt.get<SetupPinCodePage>(
137 param1: (PinCodeState<PinCodeWidget> context, dynamic _) =>
149 - Navigator.pushNamed(context.context, Routes.restoreWallet)),
138 + Navigator.pushNamed(context.context, Routes.restoreWalletType)),
139 fullscreenDialog: true);
140
141 case Routes.seed:
@@ -160,12 +149,7 @@ Route<dynamic> createRoute(RouteSettings settings) {
149 getIt.get<WalletRestorePage>(param1: WalletType.monero));
150
151 case Routes.restoreWalletFromSeed:
163 - // final args = settings.arguments as List<dynamic>;
164 - final type = WalletType.monero; //args.first as WalletType;
165 - // final language = type == WalletType.monero
166 - // ? args[1] as String
167 - // : LanguageList.english;
168 -
152 + final type = settings.arguments as WalletType;
153 return CupertinoPageRoute<void>(
154 builder: (_) => RestoreWalletFromSeedPage(type: type));
155
lib/src/screens/new_wallet/new_wallet_type_page.dart
+51 -9
@@ -1,6 +1,11 @@
1 +import 'package:cake_wallet/core/execution_state.dart';
2 import 'package:cake_wallet/di.dart';
3 import 'package:cake_wallet/entities/wallet_type.dart';
4 +import 'package:cake_wallet/routes.dart';
5 import 'package:cake_wallet/store/settings_store.dart';
6 +import 'package:cake_wallet/utils/show_bar.dart';
7 +import 'package:cake_wallet/view_model/wallet_new_vm.dart';
8 +import 'package:flushbar/flushbar.dart';
9 import 'package:flutter/material.dart';
10 import 'package:flutter/cupertino.dart';
11 import 'package:cake_wallet/generated/i18n.dart';
@@ -10,25 +15,28 @@ import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
15 import 'package:cake_wallet/src/screens/new_wallet/widgets/select_button.dart';
16
17 class NewWalletTypePage extends BasePage {
13 - NewWalletTypePage({this.onTypeSelected, this.isNewWallet = true});
18 + NewWalletTypePage(this.walletNewVM,
19 + {this.onTypeSelected, this.isNewWallet});
20
21 final void Function(BuildContext, WalletType) onTypeSelected;
22 final bool isNewWallet;
23 + final WalletNewVM walletNewVM;
24
25 @override
19 - String get title => isNewWallet
20 - ? S.current.new_wallet
21 - : S.current.wallet_list_restore_wallet;
26 + String get title =>
27 + isNewWallet ? S.current.new_wallet : S.current.wallet_list_restore_wallet;
28
29 @override
30 Widget body(BuildContext context) =>
25 - WalletTypeForm(onTypeSelected: onTypeSelected);
31 + WalletTypeForm(walletNewVM, isNewWallet, onTypeSelected: onTypeSelected);
32 }
33
34 class WalletTypeForm extends StatefulWidget {
29 - WalletTypeForm({this.onTypeSelected});
35 + WalletTypeForm(this.walletNewVM, this.isNewWallet, {this.onTypeSelected});
36
37 final void Function(BuildContext, WalletType) onTypeSelected;
38 + final WalletNewVM walletNewVM;
39 + final bool isNewWallet;
40
41 @override
42 WalletTypeFormState createState() => WalletTypeFormState();
@@ -42,10 +50,12 @@ class WalletTypeFormState extends State<WalletTypeForm> {
50 final bitcoinIcon =
51 Image.asset('assets/images/bitcoin.png', height: 24, width: 24);
52 final walletTypeImage = Image.asset('assets/images/wallet_type.png');
45 - final walletTypeLightImage = Image.asset('assets/images/wallet_type_light.png');
53 + final walletTypeLightImage =
54 + Image.asset('assets/images/wallet_type_light.png');
55
56 WalletType selected;
57 List<WalletType> types;
58 + Flushbar<void> _progressBar;
59
60 @override
61 void initState() {
@@ -56,7 +66,8 @@ class WalletTypeFormState extends State<WalletTypeForm> {
66 @override
67 Widget build(BuildContext context) {
68 final walletImage = getIt.get<SettingsStore>().isDarkTheme
59 - ? walletTypeImage : walletTypeLightImage;
69 + ? walletTypeImage
70 + : walletTypeLightImage;
71
72 return Container(
73 padding: EdgeInsets.only(top: 24),
@@ -94,7 +105,7 @@ class WalletTypeFormState extends State<WalletTypeForm> {
105 ),
106 bottomSectionPadding: EdgeInsets.only(left: 24, right: 24, bottom: 24),
107 bottomSection: PrimaryButton(
97 - onPressed: () => widget.onTypeSelected(context, selected),
108 + onPressed: () => onTypeSelected(),
109 text: S.of(context).seed_language_next,
110 color: Colors.green,
111 textColor: Colors.white,
@@ -114,4 +125,35 @@ class WalletTypeFormState extends State<WalletTypeForm> {
125 return null;
126 }
127 }
128 +
129 + Future<void> onTypeSelected() async {
130 + if (!widget.isNewWallet) {
131 + widget.onTypeSelected(context, selected);
132 + return;
133 + }
134 +
135 + try {
136 + _changeProcessText(S.of(context).creating_new_wallet);
137 + widget.walletNewVM.type = selected;
138 + await widget.walletNewVM
139 + .create(options: 'English'); // FIXME: Unnamed constant
140 + await _progressBar?.dismiss();
141 + final state = widget.walletNewVM.state;
142 +
143 + if (state is ExecutedSuccessfullyState) {
144 + widget.onTypeSelected(context, selected);
145 + }
146 +
147 + if (state is FailureState) {
148 + _changeProcessText(
149 + S.of(context).creating_new_wallet_error(state.error));
150 + }
151 + } catch (e) {
152 + _changeProcessText(S.of(context).creating_new_wallet_error(e.toString()));
153 + }
154 + }
155 +
156 + void _changeProcessText(String text) {
157 + _progressBar = createBar<void>(text, duration: null)..show(context);
158 + }
159 }
lib/src/screens/wallet_list/wallet_list_page.dart
+2 -2
@@ -165,7 +165,7 @@ class WalletListBodyState extends State<WalletListBody> {
165 ),
166 bottomSection: Column(children: <Widget>[
167 PrimaryImageButton(
168 - onPressed: () => _generateNewWallet(),
168 + onPressed: () => Navigator.of(context).pushNamed(Routes.newWalletType),
169 image: newWalletImage,
170 text: S.of(context).wallet_list_create_new_wallet,
171 color: Theme.of(context).accentTextTheme.subtitle.decorationColor,
@@ -175,7 +175,7 @@ class WalletListBodyState extends State<WalletListBody> {
175 SizedBox(height: 10.0),
176 PrimaryImageButton(
177 onPressed: () =>
178 - Navigator.of(context).pushNamed(Routes.restoreWallet),
178 + Navigator.of(context).pushNamed(Routes.restoreWalletType),
179 image: restoreWalletImage,
180 text: S.of(context).wallet_list_restore_wallet,
181 color: Theme.of(context).accentTextTheme.caption.color,
lib/view_model/wallet_creation_vm.dart
+1 -1
@@ -27,7 +27,7 @@ abstract class WalletCreationVMBase with Store {
27 @observable
28 ExecutionState state;
29
30 - final WalletType type;
30 + WalletType type;
31 final bool isRecovery;
32 final Box<WalletInfo> _walletInfoSource;
33 final AppStore _appStore;
lib/view_model/wallet_new_vm.dart
+4 -2
@@ -43,6 +43,8 @@ abstract class WalletNewVMBase extends WalletCreationVM with Store {
43 }
44
45 @override
46 - Future<WalletBase> process(WalletCredentials credentials) async =>
47 - _walletCreationService.create(credentials);
46 + Future<WalletBase> process(WalletCredentials credentials) async {
47 + _walletCreationService.changeWalletType(type: type);
48 + return _walletCreationService.create(credentials);
49 + }
50 }