diff --git a/js/delivery_receipts.js b/js/delivery_receipts.js index c2fa998ef..1201cca60 100644 --- a/js/delivery_receipts.js +++ b/js/delivery_receipts.js @@ -31,18 +31,13 @@ this.remove(receipts); return receipts; }, - async getTargetMessage(source, messages) { + async getTargetMessage(originalSource, messages) { if (messages.length === 0) { return null; } - const authorisation = await libloki.storage.getGrantAuthorisationForSecondaryPubKey( - source - ); - if (authorisation) { - // eslint-disable-next-line no-param-reassign - source = authorisation.primaryDevicePubKey; - } + const primary = await window.libsession.Protocols.MultiDeviceProtocol.getPrimaryDevice(originalSource); + const source = primary.key; const message = messages.find( item => !item.isIncoming() && source === item.get('conversationId') diff --git a/js/models/conversations.js b/js/models/conversations.js index 48997b89c..e39e7f4c5 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -211,10 +211,9 @@ return true; } - const ourDevices = await window.libloki.storage.getPairedDevicesFor( - this.ourNumber - ); - return ourDevices.includes(this.id); + const ourDevices = await window.libsession.Protocols.MultiDeviceProtocol.getAllDevices(this.ourNumber); + + return ourDevices.some(device => device.key === this.id); }, isOurLocalDevice() { return this.id === this.ourNumber; diff --git a/js/views/create_group_dialog_view.js b/js/views/create_group_dialog_view.js index a74074e6d..c9000c868 100644 --- a/js/views/create_group_dialog_view.js +++ b/js/views/create_group_dialog_view.js @@ -197,7 +197,7 @@ // exists in group, but hasn't yet synced with its other devices. const getDevicesForRemoved = async () => { const promises = notPresentInNew.map(member => - libloki.storage.getPairedDevicesFor(member) + window.libsession.Protocols.MultiDeviceProtocol.getAllDevices(member) ); const devices = _.flatten(await Promise.all(promises)); diff --git a/libloki/api.js b/libloki/api.js index 179d0d426..eca6cfce4 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -74,15 +74,6 @@ } } - // Returns the primary device pubkey for this secondary device pubkey - // or the same pubkey if there is no other device - async function getPrimaryDevicePubkey(pubKey) { - const authorisation = await window.libloki.storage.getGrantAuthorisationForSecondaryPubKey( - pubKey - ); - return authorisation ? authorisation.primaryDevicePubKey : pubKey; - } - async function sendSessionEstablishedMessage(pubKey) { // This message shouldn't be routed through multi-device. // It needs to go directly to the pubKey specified. @@ -330,7 +321,6 @@ createContactSyncProtoMessage, createGroupSyncProtoMessage, createOpenGroupsSyncProtoMessage, - getPrimaryDevicePubkey, debug, }; })(); diff --git a/libloki/storage.js b/libloki/storage.js index 82965b2ab..12a881418 100644 --- a/libloki/storage.js +++ b/libloki/storage.js @@ -114,65 +114,6 @@ } } - // fetches device mappings from server. - async function getPrimaryDeviceMapping(pubKey) { - if (typeof lokiFileServerAPI === 'undefined') { - // If this is not defined then we are initiating a pairing - return []; - } - const deviceMapping = await lokiFileServerAPI.getUserDeviceMapping(pubKey); - if (!deviceMapping) { - return []; - } - let authorisations = deviceMapping.authorisations || []; - if (deviceMapping.isPrimary === '0') { - const { primaryDevicePubKey } = - authorisations.find( - authorisation => - authorisation && authorisation.secondaryDevicePubKey === pubKey - ) || {}; - if (primaryDevicePubKey) { - // do NOT call getprimaryDeviceMapping recursively - // in case both devices are out of sync and think they are - // each others' secondary pubkey. - const primaryDeviceMapping = await lokiFileServerAPI.getUserDeviceMapping( - primaryDevicePubKey - ); - if (!primaryDeviceMapping) { - return []; - } - ({ authorisations } = primaryDeviceMapping); - } - } - - // filter out any invalid authorisations - return authorisations.filter(a => a && typeof a === 'object') || []; - } - - // if the device is a secondary device, - // fetch the device mappings for its primary device - async function saveAllPairingAuthorisationsFor() { - throw new Error('Use MultiDeviceProtocol instead.'); - } - - async function savePairingAuthorisation() { - throw new Error('Use MultiDeviceProtocol instead.'); - } - - // Transforms signatures from base64 to ArrayBuffer! - async function getGrantAuthorisationForSecondaryPubKey() { - throw new Error('Use MultiDeviceProtocol instead.'); - } - - // Transforms signatures from base64 to ArrayBuffer! - async function getAuthorisationForSecondaryPubKey() { - throw new Error('Use MultiDeviceProtocol instead.'); - } - - function getSecondaryDevicesFor() { - throw new Error('Use MultiDeviceProtocol instead.'); - } - function getGuardNodes() { return window.Signal.Data.getGuardNodes(); } @@ -181,27 +122,11 @@ return window.Signal.Data.updateGuardNodes(nodes); } - async function getAllDevicePubKeysForPrimaryPubKey() { - throw new Error('Use MultiDeviceProtocol instead.'); - } - - function getPairedDevicesFor() { - throw new Error('use MultiDeviceProtocol instead'); - } - window.libloki.storage = { getPreKeyBundleForContact, saveContactPreKeyBundle, removeContactPreKeyBundle, verifyFriendRequestAcceptPreKey, - getAllDevicePubKeysForPrimaryPubKey, - getPairedDevicesFor, - getSecondaryDevicesFor, - getPrimaryDeviceMapping, - saveAllPairingAuthorisationsFor, - savePairingAuthorisation, - getGrantAuthorisationForSecondaryPubKey, - getAuthorisationForSecondaryPubKey, getGuardNodes, updateGuardNodes, }; diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 563a4baa5..95c73451a 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -574,9 +574,8 @@ secondaryDevicePubKey, libloki.crypto.PairingType.GRANT ); - const existingAuthorisation = await libloki.storage.getAuthorisationForSecondaryPubKey( - secondaryDevicePubKey - ); + const authorisations = await libsession.Protocols.MultiDeviceProtocol.getPairingAuthorisations(secondaryDevicePubKey); + const existingAuthorisation = authorisations.some(pairing => pairing.secondaryDevicePubKey === secondaryDevicePubKey); if (!existingAuthorisation) { throw new Error( 'authoriseSecondaryDevice: request signature missing from database!' @@ -589,8 +588,9 @@ requestSignature, grantSignature, }; + // Update authorisation in database with the new grant signature - await libloki.storage.savePairingAuthorisation(authorisation); + await libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(authorisation); // Try to upload to the file server and then send a message try { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ace178e00..9b8db68f3 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -968,7 +968,7 @@ MessageReceiver.prototype.extend({ if (valid) { // Pairing dialog is open and is listening if (Whisper.events.isListenedTo('devicePairingRequestReceived')) { - await window.libloki.storage.savePairingAuthorisation(pairingRequest); + await window.libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(pairingRequest); Whisper.events.trigger( 'devicePairingRequestReceived', pairingRequest.secondaryDevicePubKey @@ -1014,7 +1014,7 @@ MessageReceiver.prototype.extend({ window.storage.remove('secondaryDeviceStatus'); window.storage.put('isSecondaryDevice', true); window.storage.put('primaryDevicePubKey', primaryDevicePubKey); - await libloki.storage.savePairingAuthorisation(pairingAuthorisation); + await window.libsession.Protocols.MultiDeviceProtocol.savePairingAuthorisation(pairingAuthorisation); const primaryConversation = await ConversationController.getOrCreateAndWait( primaryDevicePubKey, 'private' diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 0a2cfcd18..1a5869f16 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -431,13 +431,13 @@ OutgoingMessage.prototype = { const ourPubKey = textsecure.storage.user.getNumber(); const ourPrimaryPubkey = window.storage.get('primaryDevicePubKey'); const secondaryPubKeys = - (await window.libloki.storage.getSecondaryDevicesFor(ourPubKey)) || []; + (await window.libsession.Protocols.MultiDeviceProtocol.getSecondaryDevices(ourPubKey)) || []; let aliasedPubkey = devicePubKey; if (devicePubKey === ourPubKey) { aliasedPubkey = 'OUR_PUBKEY'; // should not happen } else if (devicePubKey === ourPrimaryPubkey) { aliasedPubkey = 'OUR_PRIMARY_PUBKEY'; - } else if (secondaryPubKeys.includes(devicePubKey)) { + } else if (secondaryPubKeys.some(device => device.key === devicePubKey)) { aliasedPubkey = 'OUR SECONDARY PUBKEY'; } libloki.api.debug.logSessionMessageSending( diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 08774c4a0..5ccaca3c4 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -537,12 +537,12 @@ MessageSender.prototype = { window.storage.get('primaryDevicePubKey') || textsecure.storage.user.getNumber(); const allOurDevices = ( - await libloki.storage.getAllDevicePubKeysForPrimaryPubKey( + await window.libsession.Protocols.MultiDeviceProtocol.getAllDevices( primaryDeviceKey ) ) // Don't send to ourselves - .filter(pubKey => pubKey !== textsecure.storage.user.getNumber()); + .filter(pubKey => pubKey.key !== textsecure.storage.user.getNumber()); if (allOurDevices.length === 0) { return null; } diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index e6df8d2bb..abfee68f4 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -15,6 +15,8 @@ import { SignalService } from './../protobuf'; import { removeFromCache } from './cache'; import { toNumber } from 'lodash'; import { DataMessage } from '../session/messages/outgoing'; +import { MultiDeviceProtocol } from '../session/protocols'; +import { PubKey } from '../session/types'; export { handleEndSession, handleMediumGroupUpdate }; @@ -265,7 +267,7 @@ async function handleSecondaryDeviceFriendRequest(pubKey: string) { if (!c || !(await c.isFriendWithAnyDevice())) { return false; } - await window.libloki.storage.savePairingAuthorisation(authorisation); + await MultiDeviceProtocol.savePairingAuthorisation(authorisation); return true; } @@ -601,14 +603,11 @@ export async function handleDataMessage( const source = envelope.senderIdentity || senderPubKey; - const isOwnDevice = async (pubkey: string) => { - const primaryDevice = window.storage.get('primaryDevicePubKey'); - const secondaryDevices = await window.libloki.storage.getPairedDevicesFor( - primaryDevice - ); + const isOwnDevice = async (device: string) => { + const pubKey = new PubKey(device); + const allDevices = await MultiDeviceProtocol.getAllDevices(pubKey); - const allDevices = [primaryDevice, ...secondaryDevices]; - return allDevices.includes(pubkey); + return allDevices.some(device => PubKey.isEqual(device, pubKey)); }; const ownDevice = await isOwnDevice(source); @@ -773,13 +772,7 @@ export async function handleMessageEvent(event: any): Promise { // - group.id if it is a group message let conversationId = id; - const authorisation = await window.libloki.storage.getGrantAuthorisationForSecondaryPubKey( - source - ); - - const primarySource = - (authorisation && authorisation.primaryDevicePubKey) || source; - + const primarySource = await MultiDeviceProtocol.getPrimaryDevice(source); if (isGroupMessage) { /* handle one part of the group logic here: handle requesting info of a new group, @@ -789,7 +782,7 @@ export async function handleMessageEvent(event: any): Promise { const shouldReturn = await preprocessGroupMessage( source, message.group, - primarySource + primarySource.key ); // handleGroupMessage() can process fully a message in some cases @@ -800,9 +793,9 @@ export async function handleMessageEvent(event: any): Promise { } } - if (source !== ourNumber && authorisation) { + if (source !== ourNumber) { // Ignore auth from our devices - conversationId = authorisation.primaryDevicePubKey; + conversationId = primarySource.key; } // the conversation with the primary device of that source (can be the same as conversationOrigin)