2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-10-29 03:30:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
;(function() {
|
|
|
|
/**********************
|
|
|
|
*** Device Storage ***
|
|
|
|
**********************/
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.storage = window.textsecure.storage || {};
|
|
|
|
|
2015-04-22 03:05:40 +00:00
|
|
|
var tempKeys = {};
|
2014-10-29 03:30:35 +00:00
|
|
|
|
2015-04-22 03:05:40 +00:00
|
|
|
window.textsecure.storage.devices = {
|
2014-10-29 03:30:35 +00:00
|
|
|
saveKeysToDeviceObject: function(deviceObject) {
|
2015-04-22 03:05:40 +00:00
|
|
|
var number = textsecure.utils.unencodeNumber(deviceObject.encodedNumber)[0];
|
2015-07-21 22:49:11 +00:00
|
|
|
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
|
|
|
if (identityKey !== undefined && deviceObject.identityKey !== undefined && getString(identityKey) != getString(deviceObject.identityKey)) {
|
|
|
|
var error = new Error("Identity key changed");
|
|
|
|
error.identityKey = deviceObject.identityKey;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return textsecure.storage.axolotl.putIdentityKey(number, deviceObject.identityKey).then(function() {
|
|
|
|
tempKeys[deviceObject.encodedNumber] = {
|
|
|
|
preKey: deviceObject.preKey,
|
|
|
|
preKeyId: deviceObject.preKeyId,
|
|
|
|
signedKey: deviceObject.signedKey,
|
|
|
|
signedKeyId: deviceObject.signedKeyId,
|
|
|
|
signedKeySignature: deviceObject.signedKeySignature,
|
|
|
|
registrationId: deviceObject.registrationId
|
|
|
|
};
|
|
|
|
});
|
2015-04-22 03:05:40 +00:00
|
|
|
});
|
2014-10-29 03:30:35 +00:00
|
|
|
},
|
|
|
|
|
2015-01-19 19:56:48 +00:00
|
|
|
removeTempKeysFromDevice: function(encodedNumber) {
|
2015-04-22 03:05:40 +00:00
|
|
|
delete tempKeys[encodedNumber];
|
|
|
|
return Promise.resolve();
|
2015-01-19 19:56:48 +00:00
|
|
|
},
|
2015-11-04 00:13:19 +00:00
|
|
|
|
|
|
|
getStaleDeviceIdsForNumber: function(number) {
|
|
|
|
return textsecure.storage.axolotl.getDeviceIds(number).then(function(deviceIds) {
|
|
|
|
if (deviceIds.length === 0) {
|
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
var updateDevices = [];
|
|
|
|
return Promise.all(deviceIds.map(function(deviceId) {
|
|
|
|
var encodedNumber = number + '.' + deviceId;
|
|
|
|
return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(hasSession) {
|
|
|
|
if (!hasSession && !tempKeys[encodedNumber]) {
|
|
|
|
updateDevices.push(deviceId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})).then(function() {
|
|
|
|
return updateDevices;
|
2015-11-02 19:25:38 +00:00
|
|
|
});
|
2015-11-04 00:13:19 +00:00
|
|
|
});
|
2015-11-02 19:25:38 +00:00
|
|
|
},
|
2014-10-29 03:30:35 +00:00
|
|
|
getDeviceObjectsForNumber: function(number) {
|
2015-04-21 22:54:25 +00:00
|
|
|
return textsecure.storage.axolotl.getIdentityKey(number).then(function(identityKey) {
|
2015-04-22 03:05:40 +00:00
|
|
|
if (identityKey === undefined) {
|
2015-04-14 00:16:09 +00:00
|
|
|
return [];
|
2015-04-22 03:05:40 +00:00
|
|
|
}
|
|
|
|
return textsecure.storage.axolotl.getDeviceIds(number).then(function(deviceIds) {
|
|
|
|
// Add pending devices from tempKeys
|
|
|
|
for (var encodedNumber in tempKeys) {
|
|
|
|
var deviceNumber = textsecure.utils.unencodeNumber(encodedNumber)[0];
|
2015-06-04 23:20:07 +00:00
|
|
|
var deviceId = parseInt(textsecure.utils.unencodeNumber(encodedNumber)[1]);
|
2015-04-22 03:05:40 +00:00
|
|
|
if (deviceNumber === number && deviceIds.indexOf(deviceId) < 0) {
|
|
|
|
deviceIds.push(deviceId);
|
|
|
|
}
|
|
|
|
}
|
2015-11-17 19:56:45 +00:00
|
|
|
return Promise.all(deviceIds.map(function(deviceId) {
|
2015-04-22 03:05:40 +00:00
|
|
|
var encodedNumber = number + '.' + deviceId;
|
|
|
|
var deviceObject = tempKeys[encodedNumber] || {};
|
|
|
|
deviceObject.encodedNumber = encodedNumber;
|
|
|
|
deviceObject.identityKey = identityKey;
|
2015-11-17 19:56:45 +00:00
|
|
|
return textsecure.protocol_wrapper.getRegistrationId(encodedNumber).then(function(registrationId) {
|
|
|
|
if (deviceObject.registrationId === undefined) {
|
|
|
|
deviceObject.registrationId = registrationId;
|
|
|
|
}
|
|
|
|
return deviceObject;
|
|
|
|
});
|
|
|
|
}));
|
2015-04-21 22:54:25 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-29 03:30:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeDeviceIdsForNumber: function(number, deviceIdsToRemove) {
|
2015-04-22 03:05:40 +00:00
|
|
|
var promise = Promise.resolve();
|
|
|
|
for (var j in deviceIdsToRemove) {
|
|
|
|
promise = promise.then(function() {
|
|
|
|
var encodedNumber = number + "." + deviceIdsToRemove[j];
|
|
|
|
delete tempKeys[encodedNumber];
|
|
|
|
return textsecure.storage.axolotl.removeSession(encodedNumber);
|
|
|
|
});
|
2015-04-14 00:16:09 +00:00
|
|
|
}
|
2015-04-22 03:05:40 +00:00
|
|
|
return promise;
|
|
|
|
}
|
2014-10-29 03:30:35 +00:00
|
|
|
};
|
|
|
|
})();
|