Rewrite most of the device storage stuff
This commit is contained in:
parent
f1f5914879
commit
1806210b26
6 changed files with 132 additions and 45 deletions
|
@ -21,10 +21,32 @@
|
|||
|
||||
sessions: {
|
||||
get: function(identifier) {
|
||||
return textsecure.storage.devices.getDeviceObject(identifier);
|
||||
var device = textsecure.storage.devices.getDeviceObject(identifier, true);
|
||||
if (device === undefined)
|
||||
return undefined;
|
||||
var record = new axolotl.sessions.RecipientRecord();
|
||||
if (device.sessions !== undefined)
|
||||
record.deserialize(device.sessions);
|
||||
else
|
||||
// TODO: (even for numbers, not devices) We MUST return an identityKey if we have one available
|
||||
record.identityKey = device.identityKey;
|
||||
if (getString(device.identityKey) !== getString(record.identityKey))
|
||||
throw new Error("Got mismatched identity key on sessions load");
|
||||
return record;
|
||||
},
|
||||
put: function(object) {
|
||||
return textsecure.storage.devices.saveDeviceObject(object);
|
||||
put: function(identifier, record) {
|
||||
var device = textsecure.storage.devices.getDeviceObject(identifier);
|
||||
if (device === undefined) {
|
||||
device = { encodedNumber: identifier,
|
||||
//TODO: Remove this duplication (esp registrationId?)
|
||||
identityKey: record.identityKey,
|
||||
registrationId: record.registrationId
|
||||
};
|
||||
}
|
||||
if (getString(device.identityKey) !== getString(record.identityKey))
|
||||
throw new Error("Tried to put session for device with changed identity key");
|
||||
device.sessions = record.serialize();
|
||||
return textsecure.storage.devices.saveDeviceObject(device);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -67,6 +67,7 @@ window.textsecure.messaging = function() {
|
|||
}
|
||||
|
||||
return axolotl.protocol.encryptMessageFor(deviceObjectList[i], message).then(function(encryptedMsg) {
|
||||
textsecure.storage.devices.removeTempKeysFromDevice(deviceObjectList[i].encodedNumber);
|
||||
jsonData[i] = {
|
||||
type: encryptedMsg.type,
|
||||
destinationDeviceId: textsecure.utils.unencodeNumber(deviceObjectList[i].encodedNumber)[1],
|
||||
|
|
|
@ -32,21 +32,42 @@
|
|||
return internalSaveDeviceObject(deviceObject, true);
|
||||
},
|
||||
|
||||
removeTempKeysFromDevice: function(encodedNumber) {
|
||||
var deviceObject = textsecure.storage.devices.getDeviceObject(encodedNumber);
|
||||
try {
|
||||
delete deviceObject['signedKey'];
|
||||
delete deviceObject['signedKeyId'];
|
||||
delete deviceObject['signedKeySignature'];
|
||||
delete deviceObject['preKey'];
|
||||
delete deviceObject['preKeyId'];
|
||||
} catch(_) {}
|
||||
return internalSaveDeviceObject(deviceObject, false);
|
||||
},
|
||||
|
||||
getDeviceObjectsForNumber: function(number) {
|
||||
var map = textsecure.storage.getEncrypted("devices" + number);
|
||||
return map === undefined ? [] : map.devices;
|
||||
},
|
||||
|
||||
getDeviceObject: function(encodedNumber) {
|
||||
var number = textsecure.utils.unencodeNumber(encodedNumber);
|
||||
var devices = textsecure.storage.devices.getDeviceObjectsForNumber(number[0]);
|
||||
if (devices === undefined)
|
||||
getDeviceObject: function(encodedNumber, returnIdentityKey) {
|
||||
var number = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
||||
var devices = textsecure.storage.devices.getDeviceObjectsForNumber(number);
|
||||
if (devices.length == 0) {
|
||||
if (returnIdentityKey) {
|
||||
var identityKey = textsecure.storage.devices.getIdentityKeyForNumber(number);
|
||||
if (identityKey !== undefined)
|
||||
return {identityKey: identityKey};
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
for (var i in devices)
|
||||
if (devices[i].encodedNumber == encodedNumber)
|
||||
return devices[i];
|
||||
|
||||
if (returnIdentityKey)
|
||||
return {identityKey: devices[0].identityKey};
|
||||
|
||||
return undefined;
|
||||
},
|
||||
|
||||
|
@ -96,6 +117,7 @@
|
|||
map.devices[i].preKeyId = deviceObject.preKeyId;
|
||||
map.devices[i].signedKey = deviceObject.signedKey;
|
||||
map.devices[i].signedKeyId = deviceObject.signedKeyId;
|
||||
map.devices[i].signedKeySignature = deviceObject.signedKeySignature;
|
||||
map.devices[i].registrationId = deviceObject.registrationId;
|
||||
}
|
||||
updated = true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue