Fixes for backups for monero.com app

This commit is contained in:
M 2022-02-02 14:40:40 +02:00
parent 66d4b959af
commit 05c4d26634
8 changed files with 65 additions and 16 deletions

View file

@ -14,6 +14,7 @@ import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cake_wallet/entities/secret_store_key.dart'; import 'package:cake_wallet/entities/secret_store_key.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets; import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/wallet_types.g.dart';
class BackupService { class BackupService {
BackupService(this._flutterSecureStorage, this._walletInfoSource, BackupService(this._flutterSecureStorage, this._walletInfoSource,
@ -29,6 +30,7 @@ class BackupService {
final SharedPreferences _sharedPreferences; final SharedPreferences _sharedPreferences;
final Box<WalletInfo> _walletInfoSource; final Box<WalletInfo> _walletInfoSource;
final KeyService _keyService; final KeyService _keyService;
List<WalletInfo> _correctWallets;
Future<void> importBackup(Uint8List data, String password, Future<void> importBackup(Uint8List data, String password,
{String nonce = secrets.backupSalt}) async { {String nonce = secrets.backupSalt}) async {
@ -118,10 +120,35 @@ class BackupService {
} }
}); });
await _verifyWallets();
await _importKeychainDump(password, nonce: nonce); await _importKeychainDump(password, nonce: nonce);
await _importPreferencesDump(); await _importPreferencesDump();
} }
Future<void> _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<Box<WalletInfo>> _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>(WalletInfo.boxName);
}
Future<void> _importPreferencesDump() async { Future<void> _importPreferencesDump() async {
final appDir = await getApplicationDocumentsDirectory(); final appDir = await getApplicationDocumentsDirectory();
final preferencesFile = File('${appDir.path}/~_preferences_dump'); final preferencesFile = File('${appDir.path}/~_preferences_dump');
@ -132,15 +159,26 @@ class BackupService {
final data = final data =
json.decode(preferencesFile.readAsStringSync()) as Map<String, Object>; json.decode(preferencesFile.readAsStringSync()) as Map<String, Object>;
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, await _sharedPreferences.setString(PreferencesKey.currentWalletName,
data[PreferencesKey.currentWalletName] as String); currentWalletName);
await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey,
data[PreferencesKey.currentNodeIdKey] as int); data[PreferencesKey.currentNodeIdKey] as int);
await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey, await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey,
data[PreferencesKey.currentBalanceDisplayModeKey] as int); data[PreferencesKey.currentBalanceDisplayModeKey] as int);
await _sharedPreferences.setInt(PreferencesKey.currentWalletType, await _sharedPreferences.setInt(PreferencesKey.currentWalletType,
data[PreferencesKey.currentWalletType] as int); currentWalletType);
await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey, await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey,
data[PreferencesKey.currentFiatCurrencyKey] as String); data[PreferencesKey.currentFiatCurrencyKey] as String);
await _sharedPreferences.setBool( await _sharedPreferences.setBool(

View file

@ -52,7 +52,6 @@ class WalletMenu {
image: Image.asset('assets/images/open_book_menu.png', image: Image.asset('assets/images/open_book_menu.png',
height: 16, width: 16), height: 16, width: 16),
handler: () => Navigator.of(context).pushNamed(Routes.addressBook)), handler: () => Navigator.of(context).pushNamed(Routes.addressBook)),
if(!isMoneroOnly)
WalletMenuItem( WalletMenuItem(
title: S.current.backup, title: S.current.backup,
image: Image.asset('assets/images/restore_wallet.png', image: Image.asset('assets/images/restore_wallet.png',

View file

@ -80,7 +80,7 @@ class WalletListBodyState extends State<WalletListBody> {
: Theme.of(context).backgroundColor; : Theme.of(context).backgroundColor;
final row = GestureDetector( final row = GestureDetector(
onTap: () async { onTap: () async {
if (wallet.isCurrent) { if (wallet.isCurrent || !wallet.isEnabled) {
return; return;
} }
@ -132,7 +132,9 @@ class WalletListBodyState extends State<WalletListBody> {
crossAxisAlignment: crossAxisAlignment:
CrossAxisAlignment.center, CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
_imageFor(type: wallet.type), wallet.isEnabled
? _imageFor(type: wallet.type)
: nonWalletTypeIcon,
SizedBox(width: 10), SizedBox(width: 10),
Text( Text(
wallet.name, wallet.name,

View file

@ -161,13 +161,8 @@ class WelcomePage extends BasePage {
padding: EdgeInsets.only(top: 10), padding: EdgeInsets.only(top: 10),
child: PrimaryImageButton( child: PrimaryImageButton(
onPressed: () { onPressed: () {
if (isMoneroOnly) { Navigator.pushNamed(context, Routes.restoreOptions);
Navigator.of(context).pushNamed(Routes.moneroRestoreWalletFromWelcome); },
} else {
Navigator.pushNamed(context,
Routes.restoreOptions);
}
},
image: restoreWalletImage, image: restoreWalletImage,
text: S text: S
.of(context) .of(context)

View file

@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:cake_wallet/wallet_type_utils.dart';
part 'backup_view_model.g.dart'; part 'backup_view_model.g.dart';
@ -58,9 +59,10 @@ abstract class BackupViewModelBase with Store {
state = ExecutedSuccessfullyState(); state = ExecutedSuccessfullyState();
final now = DateTime.now(); final now = DateTime.now();
final formatter = DateFormat('yyyy-MM-dd_Hm'); final formatter = DateFormat('yyyy-MM-dd_Hm');
final snakeAppName = approximatedAppName.replaceAll(' ', '_').toLowerCase();
final fileName = '${snakeAppName}_backup_${formatter.format(now)}';
return BackupExportFile(backupContent.toList(), return BackupExportFile(backupContent.toList(), name: fileName);
name: 'cake_wallet_backup_${formatter.format(now)}');
} catch (e) { } catch (e) {
print(e.toString()); print(e.toString());
state = FailureState(e.toString()); state = FailureState(e.toString());

View file

@ -3,10 +3,15 @@ import 'package:cw_core/wallet_type.dart';
class WalletListItem { class WalletListItem {
const 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 String name;
final WalletType type; final WalletType type;
final bool isCurrent; final bool isCurrent;
final dynamic key; final dynamic key;
final bool isEnabled;
} }

View file

@ -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:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/wallet_types.g.dart';
part 'wallet_list_view_model.g.dart'; part 'wallet_list_view_model.g.dart';
@ -55,6 +56,7 @@ abstract class WalletListViewModelBase with Store {
type: info.type, type: info.type,
key: info.key, key: info.key,
isCurrent: info.name == _appStore.wallet.name && isCurrent: info.name == _appStore.wallet.name &&
info.type == _appStore.wallet.type))); info.type == _appStore.wallet.type,
isEnabled: availableWalletTypes.contains(info.type))));
} }
} }

View file

@ -5,3 +5,9 @@ bool get isMoneroOnly {
return availableWalletTypes.length == 1 return availableWalletTypes.length == 1
&& availableWalletTypes.first == WalletType.monero; && availableWalletTypes.first == WalletType.monero;
} }
String get approximatedAppName {
return isMoneroOnly
? 'Monero.com'
: 'Cake Wallet';
}