Slowly replace old multi device functions

pull/1176/head
Mikunj 5 years ago
parent c8414fdce6
commit b36b3e7725

@ -1817,25 +1817,23 @@
activeAt = activeAt || Date.now(); activeAt = activeAt || Date.now();
} }
const ourPrimaryKey = window.storage.get('primaryDevicePubKey'); const ourPrimaryKey = window.storage.get('primaryDevicePubKey');
const ourDevices = await libloki.storage.getAllDevicePubKeysForPrimaryPubKey( if (ourPrimaryKey) {
ourPrimaryKey const user = new libsession.Types.PubKey(ourPrimaryKey);
const secondaryDevices = await libsession.Protocols.MultiDeviceProtocol.getSecondaryDevices(
user
); );
// TODO: We should probably just *not* send any secondary devices and if (secondaryDevices.some(device => device.key === id)) {
// just load them all and send FRs when we get the mapping
const isOurSecondaryDevice =
id !== ourPrimaryKey &&
ourDevices &&
ourDevices.some(devicePubKey => devicePubKey === id);
if (isOurSecondaryDevice) {
await conversation.setSecondaryStatus(true, ourPrimaryKey); await conversation.setSecondaryStatus(true, ourPrimaryKey);
} }
}
const otherDevices = await libloki.storage.getPairedDevicesFor(id); const user = new libsession.Types.PubKey(id);
const devices = [id, ...otherDevices]; const devices = await libsession.Protocols.MultiDeviceProtocol.getAllDevices(
user
);
const deviceConversations = await Promise.all( const deviceConversations = await Promise.all(
devices.map(d => devices.map(d =>
ConversationController.getOrCreateAndWait(d, 'private') ConversationController.getOrCreateAndWait(d.key, 'private')
) )
); );
deviceConversations.forEach(device => { deviceConversations.forEach(device => {

@ -877,7 +877,7 @@
return this; return this;
} }
const device = window.libsession.Type.PubKey.from(this.id); const device = window.libsession.Types.PubKey.from(this.id);
if (device) { if (device) {
return ConversationController.getOrCreateAndWait(device.key, 'private'); return ConversationController.getOrCreateAndWait(device.key, 'private');
} }

@ -1,13 +1,10 @@
/* global window, libsignal, textsecure, Signal, /* global window, libsignal, textsecure,
lokiFileServerAPI, ConversationController */ lokiFileServerAPI, */
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
(function() { (function() {
window.libloki = window.libloki || {}; window.libloki = window.libloki || {};
const timers = {};
const REFRESH_DELAY = 60 * 1000;
async function getPreKeyBundleForContact(pubKey) { async function getPreKeyBundleForContact(pubKey) {
const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair(); const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair();
const identityKey = myKeyPair.pubKey; const identityKey = myKeyPair.pubKey;
@ -154,84 +151,26 @@
// if the device is a secondary device, // if the device is a secondary device,
// fetch the device mappings for its primary device // fetch the device mappings for its primary device
async function saveAllPairingAuthorisationsFor(pubKey) { async function saveAllPairingAuthorisationsFor() {
// Will be false if there is no timer throw new Error('Use MultiDeviceProtocol instead.');
const cacheValid = timers[pubKey] > Date.now();
if (cacheValid) {
return;
}
timers[pubKey] = Date.now() + REFRESH_DELAY;
const authorisations = await getPrimaryDeviceMapping(pubKey);
await Promise.all(
authorisations.map(authorisation =>
savePairingAuthorisation(authorisation)
)
);
}
async function savePairingAuthorisation(authorisation) {
if (!authorisation) {
return;
} }
// Ensure that we have a conversation for all the devices async function savePairingAuthorisation() {
const conversation = await ConversationController.getOrCreateAndWait( throw new Error('Use MultiDeviceProtocol instead.');
authorisation.secondaryDevicePubKey,
'private'
);
await conversation.setSecondaryStatus(
true,
authorisation.primaryDevicePubKey
);
await window.Signal.Data.createOrUpdatePairingAuthorisation(authorisation);
} }
// Transforms signatures from base64 to ArrayBuffer! // Transforms signatures from base64 to ArrayBuffer!
async function getGrantAuthorisationForSecondaryPubKey(secondaryPubKey) { async function getGrantAuthorisationForSecondaryPubKey() {
const conversation = ConversationController.get(secondaryPubKey); throw new Error('Use MultiDeviceProtocol instead.');
if (!conversation || conversation.isPublic() || conversation.isRss()) {
return null;
}
await saveAllPairingAuthorisationsFor(secondaryPubKey);
const authorisation = await window.Signal.Data.getGrantAuthorisationForSecondaryPubKey(
secondaryPubKey
);
if (!authorisation) {
return null;
}
return {
...authorisation,
requestSignature: Signal.Crypto.base64ToArrayBuffer(
authorisation.requestSignature
),
grantSignature: Signal.Crypto.base64ToArrayBuffer(
authorisation.grantSignature
),
};
} }
// Transforms signatures from base64 to ArrayBuffer! // Transforms signatures from base64 to ArrayBuffer!
async function getAuthorisationForSecondaryPubKey(secondaryPubKey) { async function getAuthorisationForSecondaryPubKey() {
await saveAllPairingAuthorisationsFor(secondaryPubKey); throw new Error('Use MultiDeviceProtocol instead.');
const authorisation = await window.Signal.Data.getAuthorisationForSecondaryPubKey(
secondaryPubKey
);
if (!authorisation) {
return null;
}
return {
...authorisation,
requestSignature: Signal.Crypto.base64ToArrayBuffer(
authorisation.requestSignature
),
grantSignature: authorisation.grantSignature
? Signal.Crypto.base64ToArrayBuffer(authorisation.grantSignature)
: null,
};
} }
function getSecondaryDevicesFor(primaryDevicePubKey) { function getSecondaryDevicesFor() {
return window.Signal.Data.getSecondaryDevicesFor(primaryDevicePubKey); throw new Error('Use MultiDeviceProtocol instead.');
} }
function getGuardNodes() { function getGuardNodes() {
@ -242,15 +181,12 @@
return window.Signal.Data.updateGuardNodes(nodes); return window.Signal.Data.updateGuardNodes(nodes);
} }
async function getAllDevicePubKeysForPrimaryPubKey(primaryDevicePubKey) { async function getAllDevicePubKeysForPrimaryPubKey() {
await saveAllPairingAuthorisationsFor(primaryDevicePubKey); throw new Error('Use MultiDeviceProtocol instead.');
const secondaryPubKeys =
(await getSecondaryDevicesFor(primaryDevicePubKey)) || [];
return secondaryPubKeys.concat(primaryDevicePubKey);
} }
function getPairedDevicesFor(pubkey) { function getPairedDevicesFor() {
return window.Signal.Data.getPairedDevicesFor(pubkey); throw new Error('use MultiDeviceProtocol instead');
} }
window.libloki.storage = { window.libloki.storage = {
@ -258,6 +194,14 @@
saveContactPreKeyBundle, saveContactPreKeyBundle,
removeContactPreKeyBundle, removeContactPreKeyBundle,
verifyFriendRequestAcceptPreKey, verifyFriendRequestAcceptPreKey,
getAllDevicePubKeysForPrimaryPubKey,
getPairedDevicesFor,
getSecondaryDevicesFor,
getPrimaryDeviceMapping,
saveAllPairingAuthorisationsFor,
savePairingAuthorisation,
getGrantAuthorisationForSecondaryPubKey,
getAuthorisationForSecondaryPubKey,
getGuardNodes, getGuardNodes,
updateGuardNodes, updateGuardNodes,
}; };

@ -1720,16 +1720,13 @@ MessageReceiver.prototype.extend({
async handleSyncMessage(envelope, syncMessage) { async handleSyncMessage(envelope, syncMessage) {
// We should only accept sync messages from our devices // We should only accept sync messages from our devices
const ourNumber = textsecure.storage.user.getNumber(); const ourNumber = textsecure.storage.user.getNumber();
const ourPrimaryNumber = window.storage.get('primaryDevicePubKey'); const user = new libsession.Types.PubKey(ourNumber);
const ourOtherDevices = await libloki.storage.getAllDevicePubKeysForPrimaryPubKey( const ourDevices = await libsession.Protocols.MultiDeviceProtocol.getAllDevices(
ourPrimaryNumber user
); );
const ourDevices = new Set([ const validSyncSender = ourDevices.some(
ourNumber, device => device.key === envelope.source
ourPrimaryNumber, );
...ourOtherDevices,
]);
const validSyncSender = ourDevices.has(envelope.source);
if (!validSyncSender) { if (!validSyncSender) {
throw new Error( throw new Error(
"Received sync message from a device we aren't paired with" "Received sync message from a device we aren't paired with"

@ -7,6 +7,8 @@ import {
} from '../../../js/modules/data'; } from '../../../js/modules/data';
import { PrimaryPubKey, PubKey, SecondaryPubKey } from '../types'; import { PrimaryPubKey, PubKey, SecondaryPubKey } from '../types';
// TODO: We should fetch mappings when we can and only fetch them once every 5 minutes or something
/** /**
* Save pairing authorisation to the database. * Save pairing authorisation to the database.
* @param authorisation The pairing authorisation. * @param authorisation The pairing authorisation.

Loading…
Cancel
Save