Skip to content

Commit

Permalink
[Issue-2276] Backup and restore indexedDB
Browse files Browse the repository at this point in the history
  • Loading branch information
saltict committed Nov 28, 2023
1 parent f6bbdda commit 3637528
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/extension-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"dexie": "^3.2.2",
"dexie-export-import": "^4.0.7",
"eth-simple-keyring": "^4.2.0",
"ethereumjs-tx": "^2.1.2",
"ethereumjs-util": "^7.1.5",
Expand Down
7 changes: 7 additions & 0 deletions packages/extension-base/src/background/KoniTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { KeyringPair$Json, KeyringPair$Meta } from '@subwallet/keyring/types';
import { KeyringOptions } from '@subwallet/ui-keyring/options/types';
import { KeyringAddress, KeyringPairs$Json } from '@subwallet/ui-keyring/types';
import { SessionTypes } from '@walletconnect/types/dist/types/sign-client/session';
import { DexieExportJsonMeta } from 'dexie-export-import';
import Web3 from 'web3';
import { RequestArguments, TransactionConfig } from 'web3-core';
import { JsonRpcPayload, JsonRpcResponse } from 'web3-core-helpers';
Expand Down Expand Up @@ -2397,6 +2398,12 @@ export interface KoniRequestSignatures {
'pri(buyService.tokens.subscribe)': [null, Record<string, BuyTokenInfo>, Record<string, BuyTokenInfo>];
'pri(buyService.services.subscribe)': [null, Record<string, BuyServiceInfo>, Record<string, BuyServiceInfo>];
/* Buy Service */

/* Database Service */
'pri(database.export)': [null, string];
'pri(database.import)': [string, boolean];
'pri(database.checkMetadata)': [string, DexieExportJsonMeta];
/* Database Service */
}

export interface ApplicationMetadataType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4424,6 +4424,15 @@ export default class KoniExtension {
case 'pri(buyService.services.subscribe)':
return this.subscribeBuyServices(id, port);
/* Buy service */

/* Database */
case 'pri(database.export)':
return this.#koniState.dbService.exportDB();
case 'pri(database.import)':
return this.#koniState.dbService.importDB(request as string);
case 'pri(database.checkMetadata)':
return this.#koniState.dbService.checkImportMetadata(request as string);
/* Database */
// Default
default:
throw new Error(`Unable to handle message of type ${type}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import NominatorMetadataStore from '@subwallet/extension-base/services/storage-s
import { HistoryQuery } from '@subwallet/extension-base/services/storage-service/db-stores/Transaction';
import { reformatAddress } from '@subwallet/extension-base/utils';
import { Subscription } from 'dexie';
import { exportDB, peakImportFile } from 'dexie-export-import';

import { logger as createLogger } from '@polkadot/util';
import { Logger } from '@polkadot/util/types';

const EXPORT_EXCLUDE_TABLES = ['metadata'];

export default class DatabaseService {
private _db: KoniDatabase;
public stores;
Expand Down Expand Up @@ -343,4 +346,48 @@ export default class DatabaseService {
public upsertCampaign (campaign: ICampaign) {
return this.stores.campaign.upsertCampaign(campaign);
}

async exportDB () {
const blob = await exportDB(this._db, {
filter: (table, value, key) => {
if (EXPORT_EXCLUDE_TABLES.indexOf(table) >= 0) {
return false;
}

return true;
}
});

return await blob.text();
}

async importDB (data: string) {
try {
const blob = new Blob([data], { type: 'application/json' });

await this._db.import(blob, {
overwriteValues: true,
acceptMissingTables: true,
acceptVersionDiff: true
});

return true;
} catch (e) {
this.logger.error(e);

return false;
}
}

async checkImportMetadata (data: string) {
try {
const blob = new Blob([data], { type: 'application/json' });

return await peakImportFile(blob);
} catch (e) {
this.logger.error(e);

return null;
}
}
}
16 changes: 16 additions & 0 deletions packages/extension-koni-ui/src/messaging/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { sendMessage } from '@subwallet/extension-koni-ui/messaging';

export async function exportIndexedDB (): Promise<string> {
return sendMessage('pri(database.export)', null);
}

export async function importIndexedDB (request: string): Promise<boolean> {
return sendMessage('pri(database.import)', request);
}

export async function checkIndexedMetadata (request: string): Promise<object> {
return sendMessage('pri(database.checkMetadata)', request);
}
1 change: 1 addition & 0 deletions packages/extension-koni-ui/src/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export * from './qr-signer';
export * from './settings';
export * from './transaction';
export * from './WalletConnect';
export * from './database';
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5917,6 +5917,7 @@ __metadata:
buffer: ^6.0.3
cross-fetch: ^3.1.5
dexie: ^3.2.2
dexie-export-import: ^4.0.7
eth-simple-keyring: ^4.2.0
ethereumjs-tx: ^2.1.2
ethereumjs-util: ^7.1.5
Expand Down Expand Up @@ -6053,6 +6054,7 @@ __metadata:
bowser: ^2.11.0
classnames: ^2.3.2
copy-to-clipboard: ^3.3.3
dexie-export-import: ^4.0.7
enzyme: ^3.11.0
enzyme-adapter-react-16: ^1.15.7
file-saver: ^2.0.5
Expand Down Expand Up @@ -11611,6 +11613,15 @@ __metadata:
languageName: node
linkType: hard

"dexie-export-import@npm:^4.0.7":
version: 4.0.7
resolution: "dexie-export-import@npm:4.0.7"
peerDependencies:
dexie: ^2.0.4 || ^3.0.0 || ^4.0.1-alpha.5
checksum: e0a80b09b5805da5d3daa90e5d8de397e4f3f16650b84e2aea46af4f44f1e2259babb094fca4c2944345696068ca593197781a5627569e875b60ab8d32b31db3
languageName: node
linkType: hard

"dexie@npm:^3.2.2":
version: 3.2.2
resolution: "dexie@npm:3.2.2"
Expand Down

0 comments on commit 3637528

Please sign in to comment.