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, | Session as OlmSession, | ||||
} from '@commapp/olm'; | } from '@commapp/olm'; | ||||
import { getRustAPI } from 'rust-node-addon'; | |||||
import uuid from 'uuid'; | import uuid from 'uuid'; | ||||
import { | import { | ||||
olmEncryptedMessageTypes, | olmEncryptedMessageTypes, | ||||
type OLMOneTimeKeys, | type OLMOneTimeKeys, | ||||
} from 'lib/types/crypto-types.js'; | } from 'lib/types/crypto-types.js'; | ||||
import { ServerError } from 'lib/utils/errors.js'; | |||||
import { values } from 'lib/utils/objects.js'; | import { values } from 'lib/utils/objects.js'; | ||||
import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js'; | |||||
import { fetchIdentityInfo } from '../user/identity.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 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
function getOneTimeKeyValues(keyBlob: string): $ReadOnlyArray<string> { | function getOneTimeKeyValues(keyBlob: string): $ReadOnlyArray<string> { | ||||
const content: OLMOneTimeKeys = JSON.parse(keyBlob); | const content: OLMOneTimeKeys = JSON.parse(keyBlob); | ||||
const keys: $ReadOnlyArray<string> = values(content.curve25519); | const keys: $ReadOnlyArray<string> = values(content.curve25519); | ||||
return keys; | return keys; | ||||
} | } | ||||
async function uploadNewOneTimeKeys(numberOfKeys: number) { | |||||
const [rustAPI, identityInfo] = await Promise.all([ | |||||
getRustAPI(), | |||||
fetchIdentityInfo(), | |||||
]); | |||||
if (!identityInfo) { | |||||
throw new ServerError('missing_identity_info'); | |||||
} | |||||
await fetchCallUpdateOlmAccount('content', (contentAccount: OlmAccount) => { | |||||
contentAccount.generate_one_time_keys(numberOfKeys); | |||||
const contentOneTimeKeys = getOneTimeKeyValues( | |||||
contentAccount.one_time_keys(), | |||||
); | |||||
const deviceID = JSON.parse(contentAccount.identity_keys()).curve25519; | |||||
return fetchCallUpdateOlmAccount( | |||||
'notifications', | |||||
async (notifAccount: OlmAccount) => { | |||||
notifAccount.generate_one_time_keys(numberOfKeys); | |||||
const notifOneTimeKeys = getOneTimeKeyValues( | |||||
notifAccount.one_time_keys(), | |||||
); | |||||
await rustAPI.uploadOneTimeKeys( | |||||
identityInfo.userId, | |||||
deviceID, | |||||
identityInfo.accessToken, | |||||
contentOneTimeKeys, | |||||
notifOneTimeKeys, | |||||
); | |||||
notifAccount.mark_keys_as_published(); | |||||
contentAccount.mark_keys_as_published(); | |||||
}, | |||||
); | |||||
}); | |||||
} | |||||
export { | export { | ||||
createPickledOlmAccount, | createPickledOlmAccount, | ||||
createPickledOlmSession, | createPickledOlmSession, | ||||
getOlmUtility, | getOlmUtility, | ||||
unpickleOlmAccount, | unpickleOlmAccount, | ||||
unpickleOlmSession, | unpickleOlmSession, | ||||
validateAccountPrekey, | validateAccountPrekey, | ||||
getOneTimeKeyValues, | getOneTimeKeyValues, | ||||
uploadNewOneTimeKeys, | |||||
}; | }; |