mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-19 10:04:17 +01:00
Backup.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import 'package:cake_wallet/core/backup.dart';
|
||||
import 'package:cake_wallet/core/execution_state.dart';
|
||||
import 'package:cake_wallet/entities/secret_store_key.dart';
|
||||
import 'package:cake_wallet/store/secret_store.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
|
||||
part 'backup_view_model.g.dart';
|
||||
|
||||
class BackupExportFile {
|
||||
BackupExportFile(this.content, {@required this.name});
|
||||
|
||||
final String name;
|
||||
final List<int> content;
|
||||
}
|
||||
|
||||
class BackupViewModel = BackupViewModelBase with _$BackupViewModel;
|
||||
|
||||
abstract class BackupViewModelBase with Store {
|
||||
BackupViewModelBase(this.secureStorage, this.secretStore, this.backupService)
|
||||
: isBackupPasswordVisible = false {
|
||||
state = InitialExecutionState();
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
||||
secretStore.values.observe((change) {
|
||||
if (change.key == key) {
|
||||
backupPassword = secretStore.read(key);
|
||||
}
|
||||
}, fireImmediately: true);
|
||||
}
|
||||
|
||||
final FlutterSecureStorage secureStorage;
|
||||
final SecretStore secretStore;
|
||||
final BackupService backupService;
|
||||
|
||||
@observable
|
||||
ExecutionState state;
|
||||
|
||||
@observable
|
||||
bool isBackupPasswordVisible;
|
||||
|
||||
@observable
|
||||
String backupPassword;
|
||||
|
||||
@action
|
||||
Future<void> init() async {
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
||||
backupPassword = await secureStorage.read(key: key);
|
||||
}
|
||||
|
||||
@action
|
||||
Future<BackupExportFile> exportBackup() async {
|
||||
try {
|
||||
state = IsExecutingState();
|
||||
final backupContent = await backupService.exportBackup('', nonce: '');
|
||||
state = ExecutedSuccessfullyState();
|
||||
|
||||
return BackupExportFile(backupContent.toList(),
|
||||
name: 'backup_${DateTime.now().toString()}.zip');
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
state = FailureState(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
void showMasterPassword() => isBackupPasswordVisible = true;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:cake_wallet/entities/secret_store_key.dart';
|
||||
import 'package:cake_wallet/store/secret_store.dart';
|
||||
|
||||
part 'edit_backup_password_view_model.g.dart';
|
||||
|
||||
class EditBackupPasswordViewModel = EditBackupPasswordViewModelBase
|
||||
with _$EditBackupPasswordViewModel;
|
||||
|
||||
abstract class EditBackupPasswordViewModelBase with Store {
|
||||
EditBackupPasswordViewModelBase(this.secureStorage, this.secretStore) {
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
||||
backupPassword = secretStore.read(key);
|
||||
}
|
||||
|
||||
final FlutterSecureStorage secureStorage;
|
||||
final SecretStore secretStore;
|
||||
|
||||
@observable
|
||||
String backupPassword;
|
||||
|
||||
@computed
|
||||
bool get canSave {
|
||||
return !(_originalPassword == backupPassword);
|
||||
}
|
||||
|
||||
String _originalPassword;
|
||||
|
||||
@action
|
||||
Future<void> init() async {
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
||||
final password = await secureStorage.read(key: key);
|
||||
_originalPassword = password;
|
||||
backupPassword = password;
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> save() async {
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
||||
await secureStorage.write(key: key, value: backupPassword);
|
||||
secretStore.write(key: key, value: backupPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cake_wallet/core/backup.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
|
||||
part 'restore_from_backup_view_model.g.dart';
|
||||
|
||||
class RestoreFromBackupViewModel = RestoreFromBackupViewModelBase with _$RestoreFromBackupViewModel;
|
||||
|
||||
abstract class RestoreFromBackupViewModelBase with Store {
|
||||
RestoreFromBackupViewModelBase(this.backupService);
|
||||
|
||||
@observable
|
||||
String filePath;
|
||||
|
||||
final BackupService backupService;
|
||||
|
||||
Future<void> import(String password) async {
|
||||
if (filePath?.isEmpty ?? true) {
|
||||
// FIXME: throw exception;
|
||||
return;
|
||||
}
|
||||
|
||||
final file = File(filePath);
|
||||
final data = await file.readAsBytes();
|
||||
|
||||
await backupService.importBackup(data, password, nonce: null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user