diff --git a/keyserver/src/database/migration-config.js b/keyserver/src/database/migration-config.js --- a/keyserver/src/database/migration-config.js +++ b/keyserver/src/database/migration-config.js @@ -7,6 +7,7 @@ import { dbQuery, SQL } from '../database/database.js'; import { processMessagesInDBForSearch } from '../database/search-utils.js'; import { updateRolesAndPermissionsForAllThreads } from '../updaters/thread-permission-updaters.js'; +import { createPickledOlmAccount } from '../utils/olm-utils.js'; const migrations: $ReadOnlyMap Promise> = new Map([ [ @@ -385,6 +386,25 @@ ); }, ], + [ + 34, + async () => { + const [pickledContentAccount, pickledNotificationsAccount] = + await Promise.all([ + createPickledOlmAccount(), + createPickledOlmAccount(), + ]); + + await dbQuery( + SQL` + INSERT INTO olm_accounts (is_content, pickling_key, pickled_olm_account) + VALUES + (TRUE, ${pickledContentAccount.picklingKey}, ${pickledContentAccount.pickledAccount}), + (FALSE, ${pickledNotificationsAccount.picklingKey}, ${pickledNotificationsAccount.pickledAccount}); + `, + ); + }, + ], ]); const newDatabaseVersion: number = Math.max(...migrations.keys()); diff --git a/keyserver/src/database/setup-db.js b/keyserver/src/database/setup-db.js --- a/keyserver/src/database/setup-db.js +++ b/keyserver/src/database/setup-db.js @@ -13,12 +13,14 @@ import { updateDBVersion } from '../database/db-version.js'; import { newDatabaseVersion } from '../database/migration-config.js'; import { createScriptViewer } from '../session/scripts.js'; +import { createPickledOlmAccount } from '../utils/olm-utils.js'; async function setupDB() { await createTables(); await createUsers(); await createThreads(); await setUpMetadataTable(); + await createOlmAccounts(); } async function createTables() { @@ -466,4 +468,18 @@ await updateDBVersion(newDatabaseVersion); } +async function createOlmAccounts() { + const [pickledContentAccount, pickledNotificationsAccount] = + await Promise.all([createPickledOlmAccount(), createPickledOlmAccount()]); + + await dbQuery( + SQL` + INSERT INTO olm_accounts (is_content, pickling_key, pickled_olm_account) + VALUES + (TRUE, ${pickledContentAccount.picklingKey}, ${pickledContentAccount.pickledAccount}), + (FALSE, ${pickledNotificationsAccount.picklingKey}, ${pickledNotificationsAccount.pickledAccount}); + `, + ); +} + export { setupDB }; diff --git a/keyserver/src/utils/olm-utils.js b/keyserver/src/utils/olm-utils.js --- a/keyserver/src/utils/olm-utils.js +++ b/keyserver/src/utils/olm-utils.js @@ -1,23 +1,42 @@ // @flow import olm from '@commapp/olm'; -import type { Utility as OlmUtility } from '@commapp/olm'; -import invariant from 'invariant'; +import type { + Account as OlmAccount, + Utility as OlmUtility, +} from '@commapp/olm'; +import uuid from 'uuid'; -import { getCommConfig } from 'lib/utils/comm-config.js'; - -type OlmConfig = { +type PickledOlmAccount = { +picklingKey: string, +pickledAccount: string, }; -async function getOlmConfig(): Promise { - const olmConfig = await getCommConfig({ - folder: 'secrets', - name: 'olm_config', - }); - invariant(olmConfig, 'OLM config missing'); - return olmConfig; +async function createPickledOlmAccount(): Promise { + await olm.init(); + const account = new olm.Account(); + account.create(); + + const picklingKey = uuid.v4(); + const pickledAccount = account.pickle(picklingKey); + + return { + picklingKey: picklingKey, + pickledAccount: pickledAccount, + }; +} + +async function unpickleOlmAccount( + pickledOlmAccount: PickledOlmAccount, +): Promise { + await olm.init(); + const account = new olm.Account(); + + account.unpickle( + pickledOlmAccount.picklingKey, + pickledOlmAccount.pickledAccount, + ); + return account; } let cachedOLMUtility: OlmUtility; @@ -29,4 +48,4 @@ return cachedOLMUtility; } -export { getOlmConfig, getOlmUtility }; +export { createPickledOlmAccount, getOlmUtility, unpickleOlmAccount };