2022-08-09 20:36:32 +02:00
|
|
|
import { prependZero } from '../../../shared/modules/string-utils';
|
|
|
|
|
2023-08-16 21:29:17 +02:00
|
|
|
export default class Backup {
|
2022-08-09 20:36:32 +02:00
|
|
|
constructor(opts = {}) {
|
|
|
|
const {
|
|
|
|
preferencesController,
|
|
|
|
addressBookController,
|
2023-03-09 22:00:28 +01:00
|
|
|
networkController,
|
2022-08-09 20:36:32 +02:00
|
|
|
trackMetaMetricsEvent,
|
|
|
|
} = opts;
|
|
|
|
|
|
|
|
this.preferencesController = preferencesController;
|
|
|
|
this.addressBookController = addressBookController;
|
2023-03-09 22:00:28 +01:00
|
|
|
this.networkController = networkController;
|
2022-08-09 20:36:32 +02:00
|
|
|
this._trackMetaMetricsEvent = trackMetaMetricsEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
async restoreUserData(jsonString) {
|
|
|
|
const existingPreferences = this.preferencesController.store.getState();
|
2023-03-09 22:00:28 +01:00
|
|
|
const { preferences, addressBook, network } = JSON.parse(jsonString);
|
2022-08-09 20:36:32 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:00:28 +01:00
|
|
|
if (network) {
|
2023-06-22 20:46:09 +02:00
|
|
|
this.networkController.loadBackup(network);
|
2023-03-09 22:00:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preferences || addressBook || network) {
|
2022-08-09 20:36:32 +02:00
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: 'User Data Imported',
|
|
|
|
category: 'Backup',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async backupUserData() {
|
|
|
|
const userData = {
|
|
|
|
preferences: { ...this.preferencesController.store.getState() },
|
|
|
|
addressBook: { ...this.addressBookController.state },
|
2023-03-09 22:00:28 +01:00
|
|
|
network: {
|
|
|
|
networkConfigurations:
|
2023-06-22 20:46:09 +02:00
|
|
|
this.networkController.state.networkConfigurations,
|
2023-03-09 22:00:28 +01:00
|
|
|
},
|
2022-08-09 20:36:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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`;
|
|
|
|
|
2022-09-29 05:34:20 +02:00
|
|
|
return { fileName: userDataFileName, data: result };
|
2022-08-09 20:36:32 +02:00
|
|
|
}
|
|
|
|
}
|