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 { | import type { | ||||
Account as OlmAccount, | Account as OlmAccount, | ||||
Utility as OlmUtility, | Utility as OlmUtility, | ||||
Session as OlmSession, | |||||
} from '@commapp/olm'; | } from '@commapp/olm'; | ||||
import uuid from 'uuid'; | import uuid from 'uuid'; | ||||
import { olmEncryptedMessageTypes } from 'lib/types/crypto-types.js'; | |||||
type PickledOlmAccount = { | type PickledOlmAccount = { | ||||
+picklingKey: string, | +picklingKey: string, | ||||
+pickledAccount: string, | +pickledAccount: string, | ||||
}; | }; | ||||
const maxPublishedPrekeyAge = 30 * 24 * 60 * 60 * 1000; | const maxPublishedPrekeyAge = 30 * 24 * 60 * 60 * 1000; | ||||
const maxOldPrekeyAge = 24 * 60 * 60 * 1000; | const maxOldPrekeyAge = 24 * 60 * 60 * 1000; | ||||
Show All 19 Lines | ): Promise<OlmAccount> { | ||||
account.unpickle( | account.unpickle( | ||||
pickledOlmAccount.picklingKey, | pickledOlmAccount.picklingKey, | ||||
pickledOlmAccount.pickledAccount, | pickledOlmAccount.pickledAccount, | ||||
); | ); | ||||
return account; | return account; | ||||
} | } | ||||
async function createPickledOlmSession( | |||||
account: OlmAccount, | |||||
accountPicklingKey: string, | |||||
initialEncryptedMessage: string, | |||||
): Promise<string> { | |||||
await olm.init(); | |||||
const session = new olm.Session(); | |||||
session.create_inbound(account, initialEncryptedMessage); | |||||
account.remove_one_time_keys(session); | |||||
session.decrypt(olmEncryptedMessageTypes.PREKEY, initialEncryptedMessage); | |||||
return session.pickle(accountPicklingKey); | |||||
} | |||||
async function unpickleOlmSession( | |||||
pickledSession: string, | |||||
picklingKey: string, | |||||
): Promise<OlmSession> { | |||||
await olm.init(); | |||||
const session = new olm.Session(); | |||||
session.unpickle(picklingKey, pickledSession); | |||||
return session; | |||||
} | |||||
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; | ||||
} | } | ||||
Show All 17 Lines | if ( | ||||
currentDate - lastPrekeyPublishDate >= maxOldPrekeyAge | currentDate - lastPrekeyPublishDate >= maxOldPrekeyAge | ||||
) { | ) { | ||||
account.forget_old_prekey(); | account.forget_old_prekey(); | ||||
} | } | ||||
} | } | ||||
export { | export { | ||||
createPickledOlmAccount, | createPickledOlmAccount, | ||||
createPickledOlmSession, | |||||
getOlmUtility, | getOlmUtility, | ||||
unpickleOlmAccount, | unpickleOlmAccount, | ||||
unpickleOlmSession, | |||||
validateAccountPrekey, | validateAccountPrekey, | ||||
}; | }; |