Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/utils/olm-utils.js
// @flow | // @flow | ||||
import olm from '@matrix-org/olm'; | import olm from '@matrix-org/olm'; | ||||
import type { Utility as OlmUtility } from '@matrix-org/olm'; | |||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import { importJSON } from './import-json.js'; | import { importJSON } from './import-json.js'; | ||||
type OlmConfig = { | type OlmConfig = { | ||||
+picklingKey: string, | +picklingKey: string, | ||||
+pickledAccount: string, | +pickledAccount: string, | ||||
}; | }; | ||||
async function getOlmConfig(): Promise<OlmConfig> { | async function getOlmConfig(): Promise<OlmConfig> { | ||||
const olmConfig = await importJSON({ folder: 'secrets', name: 'olm_config' }); | const olmConfig = await importJSON({ folder: 'secrets', name: 'olm_config' }); | ||||
invariant(olmConfig, 'OLM config missing'); | invariant(olmConfig, 'OLM config missing'); | ||||
return olmConfig; | return olmConfig; | ||||
} | } | ||||
export type OLMUtility = { | let cachedOLMUtility: OlmUtility; | ||||
+free: () => void, | function getOlmUtility(): OlmUtility { | ||||
+sha256: (input: string | Uint8Array) => string, | |||||
+ed25519_verify: ( | |||||
key: string, | |||||
message: string | Uint8Array, | |||||
signature: string, | |||||
) => void, | |||||
}; | |||||
let cachedOLMUtility: 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 { getOlmConfig, getOlmUtility }; |