From 05c4d26634a780339486a28472dd4eda976d1a17 Mon Sep 17 00:00:00 2001 From: M Date: Wed, 2 Feb 2022 14:40:40 +0200 Subject: [PATCH] Fixes for backups for monero.com app --- lib/core/backup_service.dart | 42 ++++++++++++++++++- lib/src/screens/dashboard/wallet_menu.dart | 1 - .../screens/wallet_list/wallet_list_page.dart | 6 ++- lib/src/screens/welcome/welcome_page.dart | 9 +--- lib/view_model/backup_view_model.dart | 6 ++- .../wallet_list/wallet_list_item.dart | 7 +++- .../wallet_list/wallet_list_view_model.dart | 4 +- lib/wallet_type_utils.dart | 6 +++ 8 files changed, 65 insertions(+), 16 deletions(-) diff --git a/lib/core/backup_service.dart b/lib/core/backup_service.dart index fcd8b2d0b..4f34777ae 100644 --- a/lib/core/backup_service.dart +++ b/lib/core/backup_service.dart @@ -14,6 +14,7 @@ import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/entities/secret_store_key.dart'; import 'package:cw_core/wallet_info.dart'; import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:cake_wallet/wallet_types.g.dart'; class BackupService { BackupService(this._flutterSecureStorage, this._walletInfoSource, @@ -29,6 +30,7 @@ class BackupService { final SharedPreferences _sharedPreferences; final Box _walletInfoSource; final KeyService _keyService; + List _correctWallets; Future importBackup(Uint8List data, String password, {String nonce = secrets.backupSalt}) async { @@ -118,10 +120,35 @@ class BackupService { } }); + await _verifyWallets(); await _importKeychainDump(password, nonce: nonce); await _importPreferencesDump(); } + Future _verifyWallets() async { + final walletInfoSource = await _reloadHiveWalletInfoBox(); + _correctWallets = walletInfoSource + .values + .where((info) => availableWalletTypes.contains(info.type)) + .toList(); + + if (_correctWallets.isEmpty) { + throw Exception('Correct wallets not detected'); + } + } + + Future> _reloadHiveWalletInfoBox() async { + final appDir = await getApplicationDocumentsDirectory(); + await Hive.close(); + Hive.init(appDir.path); + + if (!Hive.isAdapterRegistered(WalletInfo.typeId)) { + Hive.registerAdapter(WalletInfoAdapter()); + } + + return await Hive.openBox(WalletInfo.boxName); + } + Future _importPreferencesDump() async { final appDir = await getApplicationDocumentsDirectory(); final preferencesFile = File('${appDir.path}/~_preferences_dump'); @@ -132,15 +159,26 @@ class BackupService { final data = json.decode(preferencesFile.readAsStringSync()) as Map; + String currentWalletName = data[PreferencesKey.currentWalletName] as String; + int currentWalletType = data[PreferencesKey.currentWalletType] as int; + + final isCorrentCurrentWallet = _correctWallets + .any((info) => info.name == currentWalletName && + info.type.index == currentWalletType); + + if (!isCorrentCurrentWallet) { + currentWalletName = _correctWallets.first.name; + currentWalletType = _correctWallets.first.type.index; + } await _sharedPreferences.setString(PreferencesKey.currentWalletName, - data[PreferencesKey.currentWalletName] as String); + currentWalletName); await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, data[PreferencesKey.currentNodeIdKey] as int); await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey, data[PreferencesKey.currentBalanceDisplayModeKey] as int); await _sharedPreferences.setInt(PreferencesKey.currentWalletType, - data[PreferencesKey.currentWalletType] as int); + currentWalletType); await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey, data[PreferencesKey.currentFiatCurrencyKey] as String); await _sharedPreferences.setBool( diff --git a/lib/src/screens/dashboard/wallet_menu.dart b/lib/src/screens/dashboard/wallet_menu.dart index fdcb34bf7..704ddb2a6 100644 --- a/lib/src/screens/dashboard/wallet_menu.dart +++ b/lib/src/screens/dashboard/wallet_menu.dart @@ -52,7 +52,6 @@ class WalletMenu { image: Image.asset('assets/images/open_book_menu.png', height: 16, width: 16), handler: () => Navigator.of(context).pushNamed(Routes.addressBook)), - if(!isMoneroOnly) WalletMenuItem( title: S.current.backup, image: Image.asset('assets/images/restore_wallet.png', diff --git a/lib/src/screens/wallet_list/wallet_list_page.dart b/lib/src/screens/wallet_list/wallet_list_page.dart index bb45d6562..7194a4135 100644 --- a/lib/src/screens/wallet_list/wallet_list_page.dart +++ b/lib/src/screens/wallet_list/wallet_list_page.dart @@ -80,7 +80,7 @@ class WalletListBodyState extends State { : Theme.of(context).backgroundColor; final row = GestureDetector( onTap: () async { - if (wallet.isCurrent) { + if (wallet.isCurrent || !wallet.isEnabled) { return; } @@ -132,7 +132,9 @@ class WalletListBodyState extends State { crossAxisAlignment: CrossAxisAlignment.center, children: [ - _imageFor(type: wallet.type), + wallet.isEnabled + ? _imageFor(type: wallet.type) + : nonWalletTypeIcon, SizedBox(width: 10), Text( wallet.name, diff --git a/lib/src/screens/welcome/welcome_page.dart b/lib/src/screens/welcome/welcome_page.dart index 4621ac6e8..a8a185e05 100644 --- a/lib/src/screens/welcome/welcome_page.dart +++ b/lib/src/screens/welcome/welcome_page.dart @@ -161,13 +161,8 @@ class WelcomePage extends BasePage { padding: EdgeInsets.only(top: 10), child: PrimaryImageButton( onPressed: () { - if (isMoneroOnly) { - Navigator.of(context).pushNamed(Routes.moneroRestoreWalletFromWelcome); - } else { - Navigator.pushNamed(context, - Routes.restoreOptions); - } - }, + Navigator.pushNamed(context, Routes.restoreOptions); + }, image: restoreWalletImage, text: S .of(context) diff --git a/lib/view_model/backup_view_model.dart b/lib/view_model/backup_view_model.dart index 1a2e6da05..b66c5701f 100644 --- a/lib/view_model/backup_view_model.dart +++ b/lib/view_model/backup_view_model.dart @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:mobx/mobx.dart'; import 'package:intl/intl.dart'; +import 'package:cake_wallet/wallet_type_utils.dart'; part 'backup_view_model.g.dart'; @@ -58,9 +59,10 @@ abstract class BackupViewModelBase with Store { state = ExecutedSuccessfullyState(); final now = DateTime.now(); final formatter = DateFormat('yyyy-MM-dd_Hm'); + final snakeAppName = approximatedAppName.replaceAll(' ', '_').toLowerCase(); + final fileName = '${snakeAppName}_backup_${formatter.format(now)}'; - return BackupExportFile(backupContent.toList(), - name: 'cake_wallet_backup_${formatter.format(now)}'); + return BackupExportFile(backupContent.toList(), name: fileName); } catch (e) { print(e.toString()); state = FailureState(e.toString()); diff --git a/lib/view_model/wallet_list/wallet_list_item.dart b/lib/view_model/wallet_list/wallet_list_item.dart index d615f964d..c334daa35 100644 --- a/lib/view_model/wallet_list/wallet_list_item.dart +++ b/lib/view_model/wallet_list/wallet_list_item.dart @@ -3,10 +3,15 @@ import 'package:cw_core/wallet_type.dart'; class WalletListItem { const WalletListItem( - {@required this.name, @required this.type, @required this.key, this.isCurrent = false}); + {@required this.name, + @required this.type, + @required this.key, + this.isCurrent = false, + this.isEnabled = true}); final String name; final WalletType type; final bool isCurrent; final dynamic key; + final bool isEnabled; } diff --git a/lib/view_model/wallet_list/wallet_list_view_model.dart b/lib/view_model/wallet_list/wallet_list_view_model.dart index ee44a78ea..70bf1b30a 100644 --- a/lib/view_model/wallet_list/wallet_list_view_model.dart +++ b/lib/view_model/wallet_list/wallet_list_view_model.dart @@ -8,6 +8,7 @@ import 'package:cw_core/wallet_service.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart'; import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_type.dart'; +import 'package:cake_wallet/wallet_types.g.dart'; part 'wallet_list_view_model.g.dart'; @@ -55,6 +56,7 @@ abstract class WalletListViewModelBase with Store { type: info.type, key: info.key, isCurrent: info.name == _appStore.wallet.name && - info.type == _appStore.wallet.type))); + info.type == _appStore.wallet.type, + isEnabled: availableWalletTypes.contains(info.type)))); } } diff --git a/lib/wallet_type_utils.dart b/lib/wallet_type_utils.dart index 19edffd94..78666d67c 100644 --- a/lib/wallet_type_utils.dart +++ b/lib/wallet_type_utils.dart @@ -4,4 +4,10 @@ import 'package:cake_wallet/wallet_types.g.dart'; bool get isMoneroOnly { return availableWalletTypes.length == 1 && availableWalletTypes.first == WalletType.monero; +} + +String get approximatedAppName { + return isMoneroOnly + ? 'Monero.com' + : 'Cake Wallet'; } \ No newline at end of file