diff --git a/packages/app/src/systems/Account/services/account.ts b/packages/app/src/systems/Account/services/account.ts index bf3f44f31..3c34fd98d 100644 --- a/packages/app/src/systems/Account/services/account.ts +++ b/packages/app/src/systems/Account/services/account.ts @@ -262,8 +262,11 @@ export class AccountService { console.log('recovering accounts', backupAccounts); for (const account of backupAccounts) { // in case of recovery, the first account will be the current - if (account.key && account.address) { - await db.accounts.add({ ...account, isCurrent: isCurrentFlag }); + if (account.key && account.data.address) { + await db.accounts.add({ + ...account.data, + isCurrent: isCurrentFlag, + }); isCurrentFlag = false; } } @@ -272,15 +275,15 @@ export class AccountService { console.log('recovering vaults', backupVaults); for (const vault of backupVaults) { if (vault.key && vault.data) { - await db.vaults.add(vault); + await db.vaults.add(vault.data); } } } if (needsNetworkRecovery) { console.log('recovering networks', backupNetworks); for (const network of backupNetworks) { - if (network.key && network.id) { - await db.networks.add(network); + if (network.key && network.data.id) { + await db.networks.add(network.data); } } } diff --git a/packages/app/src/systems/Account/utils/storage.tsx b/packages/app/src/systems/Account/utils/storage.tsx index babf0d542..7c418c900 100644 --- a/packages/app/src/systems/Account/utils/storage.tsx +++ b/packages/app/src/systems/Account/utils/storage.tsx @@ -14,7 +14,7 @@ export class IndexedDBStorage implements StorageAbstract { } async setItem(key: string, data: string) { - await chromeStorage.vaults.set({ key, data: { data } }); + await chromeStorage.vaults.set({ key, data: { key, data } }); await db.transaction('rw', db.vaults, db.accounts, async () => { await db.vaults.put({ key, data }); }); diff --git a/packages/app/src/systems/Core/services/chromeStorage.ts b/packages/app/src/systems/Core/services/chromeStorage.ts index adbe6d974..797a0c4ab 100644 --- a/packages/app/src/systems/Core/services/chromeStorage.ts +++ b/packages/app/src/systems/Core/services/chromeStorage.ts @@ -1,4 +1,11 @@ -export class ChromeStorageTable { +import type { Account, NetworkData, Vault } from '@fuel-wallet/types'; + +interface ChromeStorageRow { + key: string; + data: T; +} + +export class ChromeStorageTable { constructor(private readonly tableName: string) { this.tableName = tableName; } @@ -24,33 +31,26 @@ export class ChromeStorageTable { }; } - async getAll() { + async getAll(): Promise[]> { const rowsMap = (await chrome?.storage?.local?.get(this.tableName)) || {}; - const rows = rowsMap[this.tableName] || []; - return rows.map((row: any) => row.data); + const rows: ChromeStorageRow[] = rowsMap[this.tableName] || []; + return rows; } - - async set({ key, data }: { key: string; data: any }) { + async set({ key, data }: ChromeStorageRow) { const { index, rows } = await this.get({ key }); // update if (index !== -1) { rows[index] = { key, - data: { - key, - ...data, - }, + data, }; } else { // create rows.push({ key, - data: { - key, - ...data, - }, + data, }); } @@ -78,7 +78,7 @@ export class ChromeStorageTable { } export const chromeStorage = { - accounts: new ChromeStorageTable('accounts'), - networks: new ChromeStorageTable('networks'), - vaults: new ChromeStorageTable('vaults'), + accounts: new ChromeStorageTable('accounts'), + networks: new ChromeStorageTable('networks'), + vaults: new ChromeStorageTable('vaults'), }; diff --git a/packages/app/src/systems/Core/utils/database.ts b/packages/app/src/systems/Core/utils/database.ts index 4807506e6..a89a63d94 100644 --- a/packages/app/src/systems/Core/utils/database.ts +++ b/packages/app/src/systems/Core/utils/database.ts @@ -12,8 +12,8 @@ import Dexie, { type DbEvents, type PromiseExtended, type Table } from 'dexie'; import 'dexie-observable'; import type { AssetFuel } from 'fuels'; import type { TransactionCursor } from '~/systems/Transaction'; -import { applyDbVersioning } from './databaseVersioning'; import { chromeStorage } from '../services/chromeStorage'; +import { applyDbVersioning } from './databaseVersioning'; type FailureEvents = Extract; export type FuelCachedAsset = AssetData & @@ -55,7 +55,7 @@ export class FuelDB extends Dexie { // @TODO: this is a temporary solution to avoid the storage accounts of being wrong and // users losing funds in case of no backup // if has account, save to chrome storage - if (!!accounts.length) { + if (accounts.length) { for (const account of accounts) { await chromeStorage.accounts.set({ key: account.address, @@ -63,7 +63,7 @@ export class FuelDB extends Dexie { }); } } - if (!!vaults.length) { + if (vaults.length) { for (const vault of vaults) { await chromeStorage.vaults.set({ key: vault.key, @@ -71,7 +71,7 @@ export class FuelDB extends Dexie { }); } } - if (!!networks.length) { + if (networks.length) { for (const network of networks) { await chromeStorage.networks.set({ key: network.id || '',