diff --git a/native/handlers/peer-to-peer-message-handler.js b/native/handlers/peer-to-peer-message-handler.js index 7e3ab52fe..32653925b 100644 --- a/native/handlers/peer-to-peer-message-handler.js +++ b/native/handlers/peer-to-peer-message-handler.js @@ -1,18 +1,31 @@ // @flow import { type PeerToPeerMessage, 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'); } } export { peerToPeerMessageHandler }; diff --git a/native/utils/crypto-utils.js b/native/utils/crypto-utils.js index 7babf295a..1d0727ac7 100644 --- a/native/utils/crypto-utils.js +++ b/native/utils/crypto-utils.js @@ -1,30 +1,83 @@ // @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, notificationsInitializationInfo: OlmSessionInitializationInfo, ): Promise { const { prekey, prekeySignature, oneTimeKey } = notificationsInitializationInfo; return commCoreModule.initializeNotificationsSession( JSON.stringify(notificationsIdentityKeys), prekey, prekeySignature, oneTimeKey, ); } async function getContentSigningKey(): Promise { await commCoreModule.initializeCryptoAccount(); const { primaryIdentityPublicKeys: { ed25519 }, } = await commCoreModule.getUserPublicKey(); 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, +};