diff --git a/native/handlers/peer-to-peer-message-handler.js b/native/handlers/peer-to-peer-message-handler.js --- a/native/handlers/peer-to-peer-message-handler.js +++ b/native/handlers/peer-to-peer-message-handler.js @@ -5,11 +5,24 @@ peerToPeerMessageTypes, } from 'lib/types/tunnelbroker/peer-to-peer-message-types.js'; +import { nativeInboundContentSessionCreator } from '../utils/crypto-utils.js'; + async function peerToPeerMessageHandler( message: PeerToPeerMessage, ): Promise { if (message.type === peerToPeerMessageTypes.OUTBOUND_SESSION_CREATION) { - console.log('Received session creation request'); + try { + const result = await nativeInboundContentSessionCreator(message); + console.log( + 'Created inbound session with device ' + + `${message.senderInfo.deviceID}: ${result}`, + ); + } catch (e) { + console.log( + 'Error creating inbound session with device ' + + `${message.senderInfo.deviceID}: ${e.message}`, + ); + } } else if (message.type === peerToPeerMessageTypes.ENCRYPTED_MESSAGE) { console.log('Received encrypted message'); } 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 @@ -1,9 +1,14 @@ // @flow -import type { OLMIdentityKeys } from 'lib/types/crypto-types'; -import type { OlmSessionInitializationInfo } from 'lib/types/request-types'; +import type { + IdentityKeysBlob, + OLMIdentityKeys, +} from 'lib/types/crypto-types.js'; +import type { InboundKeyInfoResponse } from 'lib/types/identity-service-types.js'; +import type { OlmSessionInitializationInfo } from 'lib/types/request-types.js'; +import type { OutboundSessionCreation } from 'lib/types/tunnelbroker/peer-to-peer-message-types.js'; -import { commCoreModule } from '../native-modules.js'; +import { commCoreModule, commRustModule } from '../native-modules.js'; function nativeNotificationsSessionCreator( notificationsIdentityKeys: OLMIdentityKeys, @@ -27,4 +32,52 @@ return ed25519; } -export { getContentSigningKey, nativeNotificationsSessionCreator }; +async function nativeInboundContentSessionCreator( + message: OutboundSessionCreation, +): Promise { + const { senderInfo, encryptedContent } = message; + + const authMetadata = await commCoreModule.getCommServicesAuthMetadata(); + const { userID, deviceID, accessToken } = authMetadata; + if (!userID || !deviceID || !accessToken) { + throw new Error('CommServicesAuthMetadata is missing'); + } + + const keysResponse = await commRustModule.getInboundKeysForUser( + userID, + deviceID, + accessToken, + senderInfo.userID, + ); + + const inboundKeys: InboundKeyInfoResponse[] = JSON.parse(keysResponse); + const deviceKeys: ?InboundKeyInfoResponse = inboundKeys.find(keys => { + const keysPayload: IdentityKeysBlob = JSON.parse(keys.payload); + return ( + keysPayload.primaryIdentityPublicKeys.ed25519 === senderInfo.deviceID + ); + }); + + if (!deviceKeys) { + throw new Error( + 'No keys for the device that requested creating a session, ' + + `deviceID: ${senderInfo.deviceID}`, + ); + } + const keysPayload: IdentityKeysBlob = JSON.parse(deviceKeys.payload); + const identityKeys = JSON.stringify({ + curve25519: keysPayload.primaryIdentityPublicKeys.curve25519, + ed25519: keysPayload.primaryIdentityPublicKeys.ed25519, + }); + return commCoreModule.initializeContentInboundSession( + identityKeys, + encryptedContent, + keysPayload.primaryIdentityPublicKeys.ed25519, + ); +} + +export { + getContentSigningKey, + nativeNotificationsSessionCreator, + nativeInboundContentSessionCreator, +};