diff --git a/web/database/database-module-provider.js b/web/database/database-module-provider.js --- a/web/database/database-module-provider.js +++ b/web/database/database-module-provider.js @@ -1,10 +1,17 @@ // @flow +import localforage from 'localforage'; + import { DATABASE_WORKER_PATH, DATABASE_MODULE_FILE_PATH, + SQLITE_ENCRYPTION_KEY, } from './utils/constants.js'; import { isDesktopSafari, isSQLiteSupported } from './utils/db-utils.js'; +import { + exportKeyToJWK, + generateDatabaseCryptoKey, +} from './utils/worker-crypto-utils.js'; import WorkerConnectionProxy from './utils/WorkerConnectionProxy.js'; import type { AppState } from '../redux/redux-setup.js'; import { @@ -72,10 +79,7 @@ })(); } - initDBForLoggedInUser( - currentLoggedInUserID: ?string, - encryptionKey?: ?SubtleCrypto$JsonWebKey, - ) { + async initDBForLoggedInUser(currentLoggedInUserID: ?string) { if (this.status === databaseStatuses.initSuccess) { return; } @@ -84,6 +88,11 @@ this.status === databaseStatuses.notSupported && isSQLiteSupported(currentLoggedInUserID) ) { + let encryptionKey = null; + if (isDesktopSafari) { + encryptionKey = await getSafariEncryptionKey(); + } + this.init(encryptionKey); } } @@ -121,6 +130,18 @@ } } +async function getSafariEncryptionKey(): Promise { + const encryptionKey = await localforage.getItem(SQLITE_ENCRYPTION_KEY); + if (encryptionKey) { + return await exportKeyToJWK(encryptionKey); + } + const newEncryptionKey = await generateDatabaseCryptoKey({ + extractable: true, + }); + await localforage.setItem(SQLITE_ENCRYPTION_KEY, newEncryptionKey); + return await exportKeyToJWK(newEncryptionKey); +} + let databaseModule: ?DatabaseModule = null; async function getDatabaseModule(): Promise { if (!databaseModule) { diff --git a/web/database/sqlite-data-handler.js b/web/database/sqlite-data-handler.js --- a/web/database/sqlite-data-handler.js +++ b/web/database/sqlite-data-handler.js @@ -8,27 +8,9 @@ import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js'; import { getDatabaseModule } from './database-module-provider.js'; -import { SQLITE_ENCRYPTION_KEY } from './utils/constants.js'; -import { isDesktopSafari } from './utils/db-utils.js'; -import { - exportKeyToJWK, - generateDatabaseCryptoKey, -} from './utils/worker-crypto-utils.js'; import { useSelector } from '../redux/redux-utils.js'; import { workerRequestMessageTypes } from '../types/worker-types.js'; -async function getSafariEncryptionKey(): Promise { - const encryptionKey = await localforage.getItem(SQLITE_ENCRYPTION_KEY); - if (encryptionKey) { - return await exportKeyToJWK(encryptionKey); - } - const newEncryptionKey = await generateDatabaseCryptoKey({ - extractable: true, - }); - await localforage.setItem(SQLITE_ENCRYPTION_KEY, newEncryptionKey); - return await exportKeyToJWK(newEncryptionKey); -} - function SQLiteDataHandler(): React.Node { const dispatch = useDispatch(); const rehydrateConcluded = useSelector( @@ -69,14 +51,7 @@ const databaseModule = await getDatabaseModule(); if (currentLoggedInUserID) { - let databaseEncryptionKeyJWK = null; - if (isDesktopSafari) { - databaseEncryptionKeyJWK = await getSafariEncryptionKey(); - } - await databaseModule.initDBForLoggedInUser( - currentLoggedInUserID, - databaseEncryptionKeyJWK, - ); + await databaseModule.initDBForLoggedInUser(currentLoggedInUserID); } if (!rehydrateConcluded) {