Fixes issue with loading old android wallets. Changed seed text field type to visual password.

M committed Nov 13, 2020 at 16:58 UTC ba2b5fb1fa11abf6fa9640d38cc05286d5ce384d
6 files changed +67 -21
lib/entities/pathForWallet.dart
+7
@@ -19,3 +19,10 @@ Future<String> pathForWalletDir({@required String name, @required WalletType ty
19 Future<String> pathForWallet({@required String name, @required WalletType type}) async =>
20 await pathForWalletDir(name: name, type: type)
21 .then((path) => path + '/$name');
22 +
23 +Future<String> outdatedAndroidPathForWalletDir({String name}) async {
24 + final directory = await getApplicationDocumentsDirectory();
25 + final pathDir = directory.path + '/$name';
26 +
27 + return pathDir;
28 +}
\ No newline at end of file
lib/monero/monero_account_list.dart
+1 -1
@@ -20,7 +20,7 @@ abstract class MoneroAccountListBase with Store {
20 bool _isRefreshing;
21 bool _isUpdating;
22
23 - Future update() async {
23 + void update() async {
24 if (_isUpdating) {
25 return;
26 }
lib/monero/monero_wallet.dart
+2 -2
@@ -121,8 +121,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
121 _onAccountChangeReaction?.reaction?.dispose();
122 }
123
124 - Future<bool> validate() async {
125 - await accountList.update();
124 + bool validate() {
125 + accountList.update();
126 final accountListLength = accountList.accounts?.length ?? 0;
127
128 if (accountListLength <= 0) {
lib/monero/monero_wallet_service.dart
+48 -9
@@ -27,7 +27,7 @@ class MoneroRestoreWalletFromSeedCredentials extends WalletCredentials {
27
28 class MoneroWalletLoadingException implements Exception {
29 @override
30 - String toString() => 'The wallet is damaged.';
30 + String toString() => 'Failure to load the wallet.';
31 }
32
33 class MoneroRestoreWalletFromKeysCredentials extends WalletCredentials {
@@ -93,6 +93,11 @@ class MoneroWalletService extends WalletService<
93 Future<MoneroWallet> openWallet(String name, String password) async {
94 try {
95 final path = await pathForWallet(name: name, type: WalletType.monero);
96 +
97 + if (!File(path).existsSync()) {
98 + await repairOldAndroidWallet(name);
99 + }
100 +
101 await monero_wallet_manager
102 .openWalletAsync({'path': path, 'password': password});
103 final walletInfo = walletInfoSource.values.firstWhere(
@@ -100,17 +105,17 @@ class MoneroWalletService extends WalletService<
105 orElse: () => null);
106 final wallet = MoneroWallet(
107 filename: monero_wallet.getFilename(), walletInfo: walletInfo);
103 - final isValid = await wallet.validate();
108 + final isValid = wallet.validate();
109
110 if (!isValid) {
111 // if (wallet.seed?.isNotEmpty ?? false) {
107 - // let restore from seed in this case;
108 - // final seed = wallet.seed;
109 - // final credentials = MoneroRestoreWalletFromSeedCredentials(
110 - // name: name, password: password, mnemonic: seed, height: 2000000)
111 - // ..walletInfo = walletInfo;
112 - // await remove(name);
113 - // return restoreFromSeed(credentials);
112 + // let restore from seed in this case;
113 + // final seed = wallet.seed;
114 + // final credentials = MoneroRestoreWalletFromSeedCredentials(
115 + // name: name, password: password, mnemonic: seed, height: 2000000)
116 + // ..walletInfo = walletInfo;
117 + // await remove(name);
118 + // return restoreFromSeed(credentials);
119 // }
120
121 throw MoneroWalletLoadingException();
@@ -187,4 +192,38 @@ class MoneroWalletService extends WalletService<
192 rethrow;
193 }
194 }
195 +
196 + Future<void> repairOldAndroidWallet(String name) async {
197 + try {
198 + if (!Platform.isAndroid) {
199 + return;
200 + }
201 +
202 + final oldAndroidWalletDirPath =
203 + await outdatedAndroidPathForWalletDir(name: name);
204 + final dir = Directory(oldAndroidWalletDirPath);
205 +
206 + if (!dir.existsSync()) {
207 + throw MoneroWalletLoadingException();
208 + }
209 +
210 + final newWalletDirPath =
211 + await pathForWalletDir(name: name, type: WalletType.monero);
212 +
213 + dir.listSync().forEach((f) {
214 + final file = File(f.path);
215 + final name = f.path.split('/').last;
216 + final newPath = newWalletDirPath + '/$name';
217 + final newFile = File(newPath);
218 + print(file.path);
219 + if (!newFile.existsSync()) {
220 + newFile.createSync();
221 + }
222 + newFile.writeAsBytesSync(file.readAsBytesSync());
223 + });
224 + } catch (e) {
225 + print(e.toString());
226 + throw MoneroWalletLoadingException();
227 + }
228 + }
229 }
lib/src/widgets/seed_widget.dart
+1 -1
@@ -77,7 +77,7 @@ class SeedWidgetState extends State<SeedWidget> {
77 fontSize: 16.0, color: Theme.of(context).hintColor))),
78 Padding(
79 padding: EdgeInsets.only(right: 40, top: 10),
80 - child: ValidableAnnotatedEditableText(
80 + child: ValidatableAnnotatedEditableText(
81 cursorColor: Colors.blue,
82 backgroundCursorColor: Colors.blue,
83 validStyle: TextStyle(
lib/src/widgets/validable_annotated_editable_text.dart
+8 -8
@@ -22,8 +22,8 @@ class TextAnnotation extends Comparable<TextAnnotation> {
22 int compareTo(TextAnnotation other) => text.compareTo(other.text);
23 }
24
25 -class ValidableAnnotatedEditableText extends EditableText {
26 - ValidableAnnotatedEditableText({
25 +class ValidatableAnnotatedEditableText extends EditableText {
26 + ValidatableAnnotatedEditableText({
27 Key key,
28 FocusNode focusNode,
29 TextEditingController controller,
@@ -49,7 +49,7 @@ class ValidableAnnotatedEditableText extends EditableText {
49 controller: controller,
50 cursorColor: cursorColor,
51 style: validStyle,
52 - keyboardType: TextInputType.text,
52 + keyboardType: TextInputType.visiblePassword,
53 autocorrect: false,
54 autofocus: false,
55 selectionColor: selectionColor,
@@ -73,14 +73,14 @@ class ValidableAnnotatedEditableText extends EditableText {
73 final TextStyle invalidStyle;
74
75 @override
76 - ValidableAnnotatedEditableTextState createState() =>
77 - ValidableAnnotatedEditableTextState();
76 + ValidatableAnnotatedEditableTextState createState() =>
77 + ValidatableAnnotatedEditableTextState();
78 }
79
80 -class ValidableAnnotatedEditableTextState extends EditableTextState {
80 +class ValidatableAnnotatedEditableTextState extends EditableTextState {
81 @override
82 - ValidableAnnotatedEditableText get widget =>
83 - super.widget as ValidableAnnotatedEditableText;
82 + ValidatableAnnotatedEditableText get widget =>
83 + super.widget as ValidatableAnnotatedEditableText;
84
85 List<Annotation> getRanges() {
86 final result = List<Annotation>();