mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 01:47:00 +01:00
3ab5c1bf88
The backup module has been renamed so that it isn't confused with a controller. The "backup controller" has never managed any state or extended the controller base class. It is a module for backing up data, but it's not a controller. The backup controller's current inclusion in the `store` and `memstore` `ComposableObservableStore`s was getting in the way of some other enhancements to that class (which will come in a later PR).
80 lines
2.4 KiB
JavaScript
80 lines
2.4 KiB
JavaScript
import { prependZero } from '../../../shared/modules/string-utils';
|
|
|
|
export default class Backup {
|
|
constructor(opts = {}) {
|
|
const {
|
|
preferencesController,
|
|
addressBookController,
|
|
networkController,
|
|
trackMetaMetricsEvent,
|
|
} = opts;
|
|
|
|
this.preferencesController = preferencesController;
|
|
this.addressBookController = addressBookController;
|
|
this.networkController = networkController;
|
|
this._trackMetaMetricsEvent = trackMetaMetricsEvent;
|
|
}
|
|
|
|
async restoreUserData(jsonString) {
|
|
const existingPreferences = this.preferencesController.store.getState();
|
|
const { preferences, addressBook, network } = JSON.parse(jsonString);
|
|
if (preferences) {
|
|
preferences.identities = existingPreferences.identities;
|
|
preferences.lostIdentities = existingPreferences.lostIdentities;
|
|
preferences.selectedAddress = existingPreferences.selectedAddress;
|
|
|
|
this.preferencesController.store.updateState(preferences);
|
|
}
|
|
|
|
if (addressBook) {
|
|
this.addressBookController.update(addressBook, true);
|
|
}
|
|
|
|
if (network) {
|
|
this.networkController.loadBackup(network);
|
|
}
|
|
|
|
if (preferences || addressBook || network) {
|
|
this._trackMetaMetricsEvent({
|
|
event: 'User Data Imported',
|
|
category: 'Backup',
|
|
});
|
|
}
|
|
}
|
|
|
|
async backupUserData() {
|
|
const userData = {
|
|
preferences: { ...this.preferencesController.store.getState() },
|
|
addressBook: { ...this.addressBookController.state },
|
|
network: {
|
|
networkConfigurations:
|
|
this.networkController.state.networkConfigurations,
|
|
},
|
|
};
|
|
|
|
/**
|
|
* We can remove these properties since we will won't be restoring identities from backup
|
|
*/
|
|
delete userData.preferences.identities;
|
|
delete userData.preferences.lostIdentities;
|
|
delete userData.preferences.selectedAddress;
|
|
|
|
const result = JSON.stringify(userData);
|
|
|
|
const date = new Date();
|
|
|
|
const prefixZero = (num) => prependZero(num, 2);
|
|
|
|
/*
|
|
* userData.YYYY_MM_DD_HH_mm_SS e.g userData.2022_01_13_13_45_56
|
|
* */
|
|
const userDataFileName = `MetaMaskUserData.${date.getFullYear()}_${prefixZero(
|
|
date.getMonth() + 1,
|
|
)}_${prefixZero(date.getDay())}_${prefixZero(date.getHours())}_${prefixZero(
|
|
date.getMinutes(),
|
|
)}_${prefixZero(date.getDay())}.json`;
|
|
|
|
return { fileName: userDataFileName, data: result };
|
|
}
|
|
}
|