1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/app/scripts/controllers/backup.js

78 lines
2.3 KiB
JavaScript
Raw Normal View History

3box Replacement (#15243) * Backup user data Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Tests for prependZero (utils.js) Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix advancedtab test Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> backup controller tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Backup controller don't have a store. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore from file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Advanced Tab tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fix Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> e2e tests for backup unit tests for restore. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix comments on PR. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> restore style Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> We should move the exportAsFile to a utility file in the shared/ directory Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move export as file to shared folder Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Refactor create download folder methods Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move the backup/restore buttons closer to 3box Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Change descriptions Add to search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> refactor code to use if instead of && Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore button should change cursor to pointer. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix restore not uploading same file twice. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Do not backup these items in preferences identities lostIdentities selectedAddress Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Only update what is needed. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Fixed test for search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * remove txError as it currently does nothing. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove dispatch, not needed since we're not dispatching any actions. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Event should be title case. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Make backup/restore normal async functions rename event as per product suggestion. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Use success Actionable message for success message and danger for error message Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * change event name to match with backup Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix e2e Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-08-09 20:36:32 +02:00
import { exportAsFile } from '../../../shared/modules/export-utils';
import { prependZero } from '../../../shared/modules/string-utils';
export default class BackupController {
constructor(opts = {}) {
const {
preferencesController,
addressBookController,
trackMetaMetricsEvent,
} = opts;
this.preferencesController = preferencesController;
this.addressBookController = addressBookController;
this._trackMetaMetricsEvent = trackMetaMetricsEvent;
}
async restoreUserData(jsonString) {
const existingPreferences = this.preferencesController.store.getState();
const { preferences, addressBook } = 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 (preferences && addressBook) {
this._trackMetaMetricsEvent({
event: 'User Data Imported',
category: 'Backup',
});
}
}
async backupUserData() {
const userData = {
preferences: { ...this.preferencesController.store.getState() },
addressBook: { ...this.addressBookController.state },
};
/**
* 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`;
exportAsFile(userDataFileName, result);
this._trackMetaMetricsEvent({
event: 'User Data Exported',
category: 'Backup',
});
return result;
}
}