diff --git a/keyserver/src/fetchers/olm-account-fetcher.js b/keyserver/src/fetchers/olm-account-fetcher.js new file mode 100644 --- /dev/null +++ b/keyserver/src/fetchers/olm-account-fetcher.js @@ -0,0 +1,39 @@ +// @flow + +import type { Account as OlmAccount } from '@commapp/olm'; + +import { ServerError } from 'lib/utils/errors.js'; + +import { dbQuery, SQL } from '../database/database.js'; +import { unpicklePickledOlmAccount } from '../utils/olm-utils.js'; + +async function fetchKeyserverOlmAccount( + olmAccountType: 'primary' | 'notifications', +): Promise<{ account: OlmAccount, picklingKey: string }> { + const isPrimary = olmAccountType === 'primary'; + const [olmAccountResult] = await dbQuery( + SQL` + SELECT pickling_key, pickled_olm_account + FROM keyserver_olm_accounts + WHERE is_primary = ${isPrimary} + `, + ); + + if (olmAccountResult.length === 0) { + throw new ServerError( + 'olm_accounts was not correctly populated during migrations', + ); + } + + const picklingKey = olmAccountResult[0].pickling_key; + const pickledAccount = olmAccountResult[0].pickled_olm_account; + + const account = await unpicklePickledOlmAccount({ + picklingKey, + pickledAccount, + }); + + return { account, picklingKey }; +} + +export { fetchKeyserverOlmAccount }; diff --git a/keyserver/src/updaters/olm-account-updater.js b/keyserver/src/updaters/olm-account-updater.js new file mode 100644 --- /dev/null +++ b/keyserver/src/updaters/olm-account-updater.js @@ -0,0 +1,22 @@ +// @flow + +import type { Account as OlmAccount } from '@commapp/olm'; + +import { dbQuery, SQL } from '../database/database.js'; + +async function updateOlmAccount( + account: OlmAccount, + picklingKey: string, + isPrimary: boolean, +): Promise { + const pickledAccount = account.pickle(picklingKey); + await dbQuery( + SQL` + UPDATE keyserver_olm_accounts + SET pickled_olm_account = ${pickledAccount} + WHERE is_primary = ${isPrimary} + `, + ); +} + +export { updateOlmAccount };