diff --git a/lib/shared/olm-session-creator-context.js b/lib/shared/olm-session-creator-context.js --- a/lib/shared/olm-session-creator-context.js +++ b/lib/shared/olm-session-creator-context.js @@ -12,6 +12,10 @@ notificationsInitializationInfo: OlmSessionInitializationInfo, keyserverID: string, ) => Promise, + +contentSessionCreator: ( + contentIdentityKeys: OLMIdentityKeys, + contentInitializationInfo: OlmSessionInitializationInfo, + ) => Promise, }; const OlmSessionCreatorContext: React.Context = diff --git a/native/account/account-hooks.js b/native/account/account-hooks.js --- a/native/account/account-hooks.js +++ b/native/account/account-hooks.js @@ -6,7 +6,10 @@ import type { OLMIdentityKeys } from 'lib/types/crypto-types.js'; import type { OlmSessionInitializationInfo } from 'lib/types/request-types.js'; -import { nativeNotificationsSessionCreator } from '../utils/crypto-utils.js'; +import { + nativeNotificationsSessionCreator, + nativeOutboundContentSessionCreator, +} from '../utils/crypto-utils.js'; type Props = { +children: React.Node, @@ -25,8 +28,20 @@ ); } +function contentSessionCreator( + contentIdentityKeys: OLMIdentityKeys, + contentInitializationInfo: OlmSessionInitializationInfo, +) { + return nativeOutboundContentSessionCreator( + contentIdentityKeys, + contentInitializationInfo, + contentIdentityKeys.ed25519, + ); +} + const contextValue = { notificationsSessionCreator, + contentSessionCreator, }; function OlmSessionCreatorProvider(props: Props): React.Node { diff --git a/native/utils/crypto-utils.js b/native/utils/crypto-utils.js --- a/native/utils/crypto-utils.js +++ b/native/utils/crypto-utils.js @@ -177,4 +177,5 @@ nativeNotificationsSessionCreator, nativeInboundContentSessionCreator, createOlmSessionsWithOwnDevices, + nativeOutboundContentSessionCreator, }; diff --git a/web/account/account-hooks.js b/web/account/account-hooks.js --- a/web/account/account-hooks.js +++ b/web/account/account-hooks.js @@ -260,6 +260,44 @@ [getOrCreateCryptoStore], ); + const createNewContentSession = React.useCallback( + async ( + contentIdentityKeys: OLMIdentityKeys, + contentInitializationInfo: OlmSessionInitializationInfo, + ) => { + const [{ primaryAccount }] = await Promise.all([ + getOrCreateCryptoStore(), + initOlm(), + ]); + + const account = new olm.Account(); + const { picklingKey, pickledAccount } = primaryAccount; + account.unpickle(picklingKey, pickledAccount); + + const contentPrekey = getPrekeyValueFromBlob( + contentInitializationInfo.prekey, + ); + const [contentOneTimeKey] = getOneTimeKeyValuesFromBlob( + contentInitializationInfo.oneTimeKey, + ); + + const session = new olm.Session(); + session.create_outbound( + account, + contentIdentityKeys.curve25519, + contentIdentityKeys.ed25519, + contentPrekey, + contentInitializationInfo.prekeySignature, + contentOneTimeKey, + ); + const { body: initialContentEncryptedMessage } = session.encrypt( + JSON.stringify(initialEncryptedMessageContent), + ); + return initialContentEncryptedMessage; + }, + [getOrCreateCryptoStore], + ); + const notificationsSessionPromise = React.useRef>(null); const createNotificationsSession = React.useCallback( async ( @@ -302,8 +340,9 @@ const contextValue = React.useMemo( () => ({ notificationsSessionCreator: createNotificationsSession, + contentSessionCreator: createNewContentSession, }), - [createNotificationsSession], + [createNewContentSession, createNotificationsSession], ); return (