From ba2b5fb1fa11abf6fa9640d38cc05286d5ce384d Mon Sep 17 00:00:00 2001 From: M Date: Fri, 13 Nov 2020 16:58:28 +0200 Subject: [PATCH] Fixes issue with loading old android wallets. Changed seed text field type to visual password. --- lib/entities/pathForWallet.dart | 7 +++ lib/monero/monero_account_list.dart | 2 +- lib/monero/monero_wallet.dart | 4 +- lib/monero/monero_wallet_service.dart | 57 ++++++++++++++++--- lib/src/widgets/seed_widget.dart | 2 +- .../validable_annotated_editable_text.dart | 16 +++--- 6 files changed, 67 insertions(+), 21 deletions(-) diff --git a/lib/entities/pathForWallet.dart b/lib/entities/pathForWallet.dart index 7e21ac178..2d55c6dfa 100644 --- a/lib/entities/pathForWallet.dart +++ b/lib/entities/pathForWallet.dart @@ -19,3 +19,10 @@ Future pathForWalletDir({@required String name, @required WalletType ty Future pathForWallet({@required String name, @required WalletType type}) async => await pathForWalletDir(name: name, type: type) .then((path) => path + '/$name'); + +Future outdatedAndroidPathForWalletDir({String name}) async { + final directory = await getApplicationDocumentsDirectory(); + final pathDir = directory.path + '/$name'; + + return pathDir; +} \ No newline at end of file diff --git a/lib/monero/monero_account_list.dart b/lib/monero/monero_account_list.dart index c8522b59b..9792d3c1d 100644 --- a/lib/monero/monero_account_list.dart +++ b/lib/monero/monero_account_list.dart @@ -20,7 +20,7 @@ abstract class MoneroAccountListBase with Store { bool _isRefreshing; bool _isUpdating; - Future update() async { + void update() async { if (_isUpdating) { return; } diff --git a/lib/monero/monero_wallet.dart b/lib/monero/monero_wallet.dart index 4b9dd1cfc..2d4878b4c 100644 --- a/lib/monero/monero_wallet.dart +++ b/lib/monero/monero_wallet.dart @@ -121,8 +121,8 @@ abstract class MoneroWalletBase extends WalletBase with Store { _onAccountChangeReaction?.reaction?.dispose(); } - Future validate() async { - await accountList.update(); + bool validate() { + accountList.update(); final accountListLength = accountList.accounts?.length ?? 0; if (accountListLength <= 0) { diff --git a/lib/monero/monero_wallet_service.dart b/lib/monero/monero_wallet_service.dart index 91b9c184d..191b340e4 100644 --- a/lib/monero/monero_wallet_service.dart +++ b/lib/monero/monero_wallet_service.dart @@ -27,7 +27,7 @@ class MoneroRestoreWalletFromSeedCredentials extends WalletCredentials { class MoneroWalletLoadingException implements Exception { @override - String toString() => 'The wallet is damaged.'; + String toString() => 'Failure to load the wallet.'; } class MoneroRestoreWalletFromKeysCredentials extends WalletCredentials { @@ -93,6 +93,11 @@ class MoneroWalletService extends WalletService< Future openWallet(String name, String password) async { try { final path = await pathForWallet(name: name, type: WalletType.monero); + + if (!File(path).existsSync()) { + await repairOldAndroidWallet(name); + } + await monero_wallet_manager .openWalletAsync({'path': path, 'password': password}); final walletInfo = walletInfoSource.values.firstWhere( @@ -100,17 +105,17 @@ class MoneroWalletService extends WalletService< orElse: () => null); final wallet = MoneroWallet( filename: monero_wallet.getFilename(), walletInfo: walletInfo); - final isValid = await wallet.validate(); + final isValid = wallet.validate(); if (!isValid) { // if (wallet.seed?.isNotEmpty ?? false) { - // let restore from seed in this case; - // final seed = wallet.seed; - // final credentials = MoneroRestoreWalletFromSeedCredentials( - // name: name, password: password, mnemonic: seed, height: 2000000) - // ..walletInfo = walletInfo; - // await remove(name); - // return restoreFromSeed(credentials); + // let restore from seed in this case; + // final seed = wallet.seed; + // final credentials = MoneroRestoreWalletFromSeedCredentials( + // name: name, password: password, mnemonic: seed, height: 2000000) + // ..walletInfo = walletInfo; + // await remove(name); + // return restoreFromSeed(credentials); // } throw MoneroWalletLoadingException(); @@ -187,4 +192,38 @@ class MoneroWalletService extends WalletService< rethrow; } } + + Future repairOldAndroidWallet(String name) async { + try { + if (!Platform.isAndroid) { + return; + } + + final oldAndroidWalletDirPath = + await outdatedAndroidPathForWalletDir(name: name); + final dir = Directory(oldAndroidWalletDirPath); + + if (!dir.existsSync()) { + throw MoneroWalletLoadingException(); + } + + final newWalletDirPath = + await pathForWalletDir(name: name, type: WalletType.monero); + + dir.listSync().forEach((f) { + final file = File(f.path); + final name = f.path.split('/').last; + final newPath = newWalletDirPath + '/$name'; + final newFile = File(newPath); + print(file.path); + if (!newFile.existsSync()) { + newFile.createSync(); + } + newFile.writeAsBytesSync(file.readAsBytesSync()); + }); + } catch (e) { + print(e.toString()); + throw MoneroWalletLoadingException(); + } + } } diff --git a/lib/src/widgets/seed_widget.dart b/lib/src/widgets/seed_widget.dart index e444bc687..ddebbffa0 100644 --- a/lib/src/widgets/seed_widget.dart +++ b/lib/src/widgets/seed_widget.dart @@ -77,7 +77,7 @@ class SeedWidgetState extends State { fontSize: 16.0, color: Theme.of(context).hintColor))), Padding( padding: EdgeInsets.only(right: 40, top: 10), - child: ValidableAnnotatedEditableText( + child: ValidatableAnnotatedEditableText( cursorColor: Colors.blue, backgroundCursorColor: Colors.blue, validStyle: TextStyle( diff --git a/lib/src/widgets/validable_annotated_editable_text.dart b/lib/src/widgets/validable_annotated_editable_text.dart index e603c0327..37bde13a2 100644 --- a/lib/src/widgets/validable_annotated_editable_text.dart +++ b/lib/src/widgets/validable_annotated_editable_text.dart @@ -22,8 +22,8 @@ class TextAnnotation extends Comparable { int compareTo(TextAnnotation other) => text.compareTo(other.text); } -class ValidableAnnotatedEditableText extends EditableText { - ValidableAnnotatedEditableText({ +class ValidatableAnnotatedEditableText extends EditableText { + ValidatableAnnotatedEditableText({ Key key, FocusNode focusNode, TextEditingController controller, @@ -49,7 +49,7 @@ class ValidableAnnotatedEditableText extends EditableText { controller: controller, cursorColor: cursorColor, style: validStyle, - keyboardType: TextInputType.text, + keyboardType: TextInputType.visiblePassword, autocorrect: false, autofocus: false, selectionColor: selectionColor, @@ -73,14 +73,14 @@ class ValidableAnnotatedEditableText extends EditableText { final TextStyle invalidStyle; @override - ValidableAnnotatedEditableTextState createState() => - ValidableAnnotatedEditableTextState(); + ValidatableAnnotatedEditableTextState createState() => + ValidatableAnnotatedEditableTextState(); } -class ValidableAnnotatedEditableTextState extends EditableTextState { +class ValidatableAnnotatedEditableTextState extends EditableTextState { @override - ValidableAnnotatedEditableText get widget => - super.widget as ValidableAnnotatedEditableText; + ValidatableAnnotatedEditableText get widget => + super.widget as ValidatableAnnotatedEditableText; List getRanges() { final result = List();