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();
}
const ourPrimaryKey = window.storage.get('primaryDevicePubKey');
const ourDevices = await libloki.storage.getAllDevicePubKeysForPrimaryPubKey(
ourPrimaryKey
);
// TODO: We should probably just *not* send any secondary devices and
// 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);
if (ourPrimaryKey) {
const user = new libsession.Types.PubKey(ourPrimaryKey);
const secondaryDevices = await libsession.Protocols.MultiDeviceProtocol.getSecondaryDevices(
user
);
if (secondaryDevices.some(device => device.key === id)) {
await conversation.setSecondaryStatus(true, ourPrimaryKey);
}
}
const otherDevices = await libloki.storage.getPairedDevicesFor(id);
const devices = [id, ...otherDevices];
const user = new libsession.Types.PubKey(id);
const devices = await libsession.Protocols.MultiDeviceProtocol.getAllDevices(
user
);
const deviceConversations = await Promise.all(
devices.map(d =>
ConversationController.getOrCreateAndWait(d, 'private')
ConversationController.getOrCreateAndWait(d.key, 'private')
)
);
deviceConversations.forEach(device => {

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

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

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

@ -7,6 +7,8 @@ import {
} from '../../../js/modules/data';
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.
* @param authorisation The pairing authorisation.

Loading…
Cancel
Save