diff --git a/keyserver/src/fetchers/olm-account-fetchers.js b/keyserver/src/fetchers/olm-account-fetchers.js new file mode 100644 --- /dev/null +++ b/keyserver/src/fetchers/olm-account-fetchers.js @@ -0,0 +1,28 @@ +// @flow + +import { ServerError } from 'lib/utils/errors.js'; + +import { SQL, dbQuery } from '../database/database.js'; +import { type PickledOlmAccount } from '../utils/olm-objects.js'; + +async function fetchPickledOlmAccount( + olmAccountType: 'content' | 'notifications', +): Promise { + const isContent = olmAccountType === 'content'; + const [olmAccountResult] = await dbQuery( + SQL` + SELECT pickling_key, pickled_olm_account + FROM olm_accounts + WHERE is_content = ${isContent} + `, + ); + if (olmAccountResult.length === 0) { + throw new ServerError('missing_olm_account'); + } + const picklingKey = olmAccountResult[0].pickling_key; + const pickledAccount = olmAccountResult[0].pickled_olm_account; + + return { pickledAccount, picklingKey }; +} + +export { fetchPickledOlmAccount }; 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 @@ -21,10 +21,9 @@ import { publishPrekeys, uploadOneTimeKeys } from './identity-utils.js'; import type { PickledOlmAccount } from './olm-objects.js'; -import { - fetchCallUpdateOlmAccount, - fetchOlmAccount, -} from '../updaters/olm-account-updater.js'; +import { unpickleAccountAndUseCallback } from './olm-objects.js'; +import { fetchPickledOlmAccount } from '../fetchers/olm-account-fetchers.js'; +import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js'; import { verifyUserLoggedIn } from '../user/login.js'; async function createPickledOlmAccount(): Promise { @@ -230,8 +229,16 @@ } async function getContentSigningKey(): Promise { - const accountInfo = await fetchOlmAccount('content'); - return JSON.parse(accountInfo.account.identity_keys()).ed25519; + const pickledOlmAccount = await fetchPickledOlmAccount('content'); + const getAccountEd25519Key: (account: OlmAccount) => string = ( + account: OlmAccount, + ) => JSON.parse(account.identity_keys()).ed25519; + + const { result } = await unpickleAccountAndUseCallback( + pickledOlmAccount, + getAccountEd25519Key, + ); + return result; } function validateAndUploadAccountPrekeys(