Skip to content

Commit

Permalink
fix: add missing typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
helciofranco committed Dec 11, 2024
1 parent c1792f6 commit 99c6655
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
13 changes: 8 additions & 5 deletions packages/app/src/systems/Account/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/systems/Account/utils/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
Expand Down
34 changes: 17 additions & 17 deletions packages/app/src/systems/Core/services/chromeStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export class ChromeStorageTable {
import type { Account, NetworkData, Vault } from '@fuel-wallet/types';

interface ChromeStorageRow<T> {
key: string;
data: T;
}

export class ChromeStorageTable<T> {
constructor(private readonly tableName: string) {
this.tableName = tableName;
}
Expand All @@ -24,33 +31,26 @@ export class ChromeStorageTable {
};
}

async getAll() {
async getAll(): Promise<ChromeStorageRow<T>[]> {
const rowsMap = (await chrome?.storage?.local?.get(this.tableName)) || {};
const rows = rowsMap[this.tableName] || [];
return rows.map((row: any) => row.data);
const rows: ChromeStorageRow<T>[] = rowsMap[this.tableName] || [];
return rows;
}


async set({ key, data }: { key: string; data: any }) {
async set({ key, data }: ChromeStorageRow<T>) {
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,
});
}

Expand Down Expand Up @@ -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<Account>('accounts'),
networks: new ChromeStorageTable<NetworkData>('networks'),
vaults: new ChromeStorageTable<Vault>('vaults'),
};
8 changes: 4 additions & 4 deletions packages/app/src/systems/Core/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof DbEvents, 'close' | 'blocked'>;
export type FuelCachedAsset = AssetData &
Expand Down Expand Up @@ -55,23 +55,23 @@ 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,
data: account,
});
}
}
if (!!vaults.length) {
if (vaults.length) {
for (const vault of vaults) {
await chromeStorage.vaults.set({
key: vault.key,
data: vault,
});
}
}
if (!!networks.length) {
if (networks.length) {
for (const network of networks) {
await chromeStorage.networks.set({
key: network.id || '',
Expand Down

0 comments on commit 99c6655

Please sign in to comment.