Complete restauration from BackUp
This commit is contained in:
parent
5bd3e4553c
commit
54c9e0b403
@ -143,8 +143,8 @@ export async function init(): Promise<void> {
|
||||
} else {
|
||||
services.restoreDevice(device);
|
||||
}
|
||||
await services.restoreProcesses();
|
||||
await services.restoreSecrets();
|
||||
await services.restoreProcessesFromDB();
|
||||
await services.restoreSecretsFromDB();
|
||||
|
||||
if (services.isPaired()) {
|
||||
await navigate('process');
|
||||
|
@ -252,6 +252,21 @@ class Database {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async clearStore(storeName: string): Promise<void> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const clearRequest = store.clear();
|
||||
clearRequest.onsuccess = () => resolve(clearRequest.result);
|
||||
clearRequest.onerror = () => reject(clearRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Database;
|
||||
|
@ -279,9 +279,13 @@ export default class Services {
|
||||
async resetDevice() {
|
||||
this.sdkClient.reset_device();
|
||||
|
||||
// Remove the device from db
|
||||
// Clear all stores
|
||||
const db = await Database.getInstance();
|
||||
await db.deleteObject('wallet', '1');
|
||||
await db.clearStore('wallet');
|
||||
await db.clearStore('shared_secrets');
|
||||
await db.clearStore('unconfirmed_secrets');
|
||||
await db.clearStore('processes');
|
||||
await db.clearStore('diffs');
|
||||
}
|
||||
|
||||
async sendNewTxMessage(message: string) {
|
||||
@ -434,7 +438,6 @@ export default class Services {
|
||||
|
||||
|
||||
if (updatedProcess.modified_state) {
|
||||
console.log('modified state:', updatedProcess.modified_state);
|
||||
const responsePrdReturn = this.sdkClient.create_response_prd(processId, updatedProcess.modified_state);
|
||||
await this.handleApiReturn(responsePrdReturn);
|
||||
}
|
||||
@ -627,8 +630,17 @@ export default class Services {
|
||||
return processes;
|
||||
}
|
||||
|
||||
public async restoreProcessesFromBackUp(processes: Record<string, Process>) {
|
||||
const db = await Database.getInstance();
|
||||
for (const [commitedIn, process] of Object.entries(processes)) {
|
||||
await db.addObject({ storeName: 'processes', object: process, key: commitedIn});
|
||||
}
|
||||
|
||||
await this.restoreProcessesFromDB();
|
||||
}
|
||||
|
||||
// Restore process in wasm with persistent storage
|
||||
public async restoreProcesses() {
|
||||
public async restoreProcessesFromDB() {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
const processes: Record<string, Process> = await db.dumpStore('processes');
|
||||
@ -643,7 +655,40 @@ export default class Services {
|
||||
}
|
||||
}
|
||||
|
||||
public async restoreSecrets() {
|
||||
public async clearSecretsFromDB() {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
await db.clearStore('shared_secrets');
|
||||
await db.clearStore('unconfirmed_secrets');
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public async restoreSecretsFromBackUp(secretsStore: SecretsStore) {
|
||||
const db = await Database.getInstance();
|
||||
|
||||
for (const secret of secretsStore.unconfirmed_secrets) {
|
||||
await db.addObject({
|
||||
storeName: 'unconfirmed_secrets',
|
||||
object: secret,
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
const entries = Object.entries(secretsStore.shared_secrets).map(([key, value]) => ({ key, value }));
|
||||
for (const entry of entries) {
|
||||
await db.addObject({
|
||||
storeName: 'shared_secrets',
|
||||
object: entry.value,
|
||||
key: entry.key,
|
||||
});
|
||||
}
|
||||
|
||||
// Now we can transfer them to memory
|
||||
await this.restoreSecretsFromDB();
|
||||
}
|
||||
|
||||
public async restoreSecretsFromDB() {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
const sharedSecrets: Record<string, string> = await db.dumpStore('shared_secrets');
|
||||
@ -700,6 +745,11 @@ export default class Services {
|
||||
this.restoreDevice(device);
|
||||
|
||||
// TODO restore secrets and processes from file
|
||||
const secretsStore = backup.secrets;
|
||||
await this.restoreSecretsFromBackUp(secretsStore);
|
||||
|
||||
const processes = backup.processes;
|
||||
await this.restoreProcessesFromBackUp(processes);
|
||||
}
|
||||
|
||||
public async createBackUp(): Promise<BackUp | null> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user