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: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<WalletInfo> _walletInfoSource;
final KeyService _keyService;
List<WalletInfo> _correctWallets;
Future<void> 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<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 {
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, 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,
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(

View file

@ -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',

View file

@ -80,7 +80,7 @@ class WalletListBodyState extends State<WalletListBody> {
: 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<WalletListBody> {
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
_imageFor(type: wallet.type),
wallet.isEnabled
? _imageFor(type: wallet.type)
: nonWalletTypeIcon,
SizedBox(width: 10),
Text(
wallet.name,

View file

@ -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)

View file

@ -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());

View file

@ -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;
}

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: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))));
}
}

View file

@ -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';
}