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,28 @@ +// @flow + +import type { Account as OlmAccount } from '@commapp/olm'; + +import { dbQuery, SQL } from '../database/database.js'; +import { unpicklePickledOlmAccount } from '../utils/olm-utils.js'; + +async function fetchKeyserverOlmAccount( + isPrimary: boolean, +): Promise<{ account: OlmAccount, picklingKey: string }> { + const olmAccountResult = await dbQuery( + SQL` + SELECT pickling_key, pickled_olm_account FROM keyserver_olm_accounts WHERE is_primary=${isPrimary}; + `, + ); + + const picklingKey = olmAccountResult[0][0].pickling_key; + const pickledAccount = olmAccountResult[0][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,20 @@ +// @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 };