Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/utils/olm-utils.js
// @flow | // @flow | ||||
import olm from '@commapp/olm'; | import olm from '@commapp/olm'; | ||||
import type { Utility as OlmUtility } from '@commapp/olm'; | import type { | ||||
import invariant from 'invariant'; | Account as OlmAccount, | ||||
Utility as OlmUtility, | |||||
} from '@commapp/olm'; | |||||
import uuid from 'uuid'; | |||||
import { getCommConfig } from 'lib/utils/comm-config.js'; | type PickledOlmAccount = { | ||||
type OlmConfig = { | |||||
+picklingKey: string, | +picklingKey: string, | ||||
+pickledAccount: string, | +pickledAccount: string, | ||||
}; | }; | ||||
async function getOlmConfig(): Promise<OlmConfig> { | async function createPickledOlmAccount(): Promise<PickledOlmAccount> { | ||||
const olmConfig = await getCommConfig({ | await olm.init(); | ||||
folder: 'secrets', | const account = new olm.Account(); | ||||
name: 'olm_config', | account.create(); | ||||
}); | |||||
invariant(olmConfig, 'OLM config missing'); | const picklingKey = uuid.v4(); | ||||
return olmConfig; | const pickledAccount = account.pickle(picklingKey); | ||||
return { | |||||
picklingKey: picklingKey, | |||||
pickledAccount: pickledAccount, | |||||
}; | |||||
} | |||||
async function unpickleOlmAccount( | |||||
pickledOlmAccount: PickledOlmAccount, | |||||
): Promise<OlmAccount> { | |||||
await olm.init(); | |||||
const account = new olm.Account(); | |||||
account.unpickle( | |||||
pickledOlmAccount.picklingKey, | |||||
pickledOlmAccount.pickledAccount, | |||||
); | |||||
return account; | |||||
} | } | ||||
let cachedOLMUtility: OlmUtility; | let cachedOLMUtility: OlmUtility; | ||||
function getOlmUtility(): OlmUtility { | function getOlmUtility(): OlmUtility { | ||||
if (cachedOLMUtility) { | if (cachedOLMUtility) { | ||||
return cachedOLMUtility; | return cachedOLMUtility; | ||||
} | } | ||||
cachedOLMUtility = new olm.Utility(); | cachedOLMUtility = new olm.Utility(); | ||||
return cachedOLMUtility; | return cachedOLMUtility; | ||||
} | } | ||||
export { getOlmConfig, getOlmUtility }; | export { createPickledOlmAccount, getOlmUtility, unpickleOlmAccount }; |