From d32a352d8a3d1747ddb0a105d92b68dd7abff52a Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 3 May 2016 23:02:35 -0700 Subject: [PATCH] Use deviceIds/addresses instead of encodedNumbers The getDeviceObjectsForNumber method returns device objects that contain nothing but the encodedNumber property. Instead, just deal in deviceIds until a SignalProtocolAddress is constructed to manage both the number and deviceId. // FREEBIE --- js/libtextsecure.js | 31 +++++++++++++++---------------- libtextsecure/outgoing_message.js | 22 +++++++++++----------- libtextsecure/sendmessage.js | 9 ++++----- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 322e233a1..3e92cf8b0 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38568,11 +38568,11 @@ OutgoingMessage.prototype = { }, reloadDevicesAndSend: function(number, recurse) { return function() { - return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) { - if (devicesForNumber.length == 0) { + return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { + if (deviceIds.length == 0) { return this.registerError(number, "Got empty device list when loading device keys", null); } - return this.doSendMessage(number, devicesForNumber, recurse); + return this.doSendMessage(number, deviceIds, recurse); }.bind(this)); }.bind(this); }, @@ -38623,14 +38623,14 @@ OutgoingMessage.prototype = { }); }, - doSendMessage: function(number, devicesForNumber, recurse) { + doSendMessage: function(number, deviceIds, recurse) { var ciphers = {}; var plaintext = this.message.toArrayBuffer(); - return Promise.all(devicesForNumber.map(function(device) { - var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + return Promise.all(deviceIds.map(function(deviceId) { + var address = new libsignal.SignalProtocolAddress(number, deviceId); var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); ciphers[address.getDeviceId()] = sessionCipher; - return this.encryptToDevice(device, plaintext, sessionCipher); + return this.encryptToDevice(address, plaintext, sessionCipher); }.bind(this))).then(function(jsonData) { return this.transmitMessage(number, jsonData, this.timestamp).then(function() { this.successfulNumbers[this.successfulNumbers.length] = number; @@ -38664,19 +38664,19 @@ OutgoingMessage.prototype = { }.bind(this)); }, - encryptToDevice: function(device, plaintext, sessionCipher) { + encryptToDevice: function(address, plaintext, sessionCipher) { return Promise.all([ sessionCipher.encrypt(plaintext), sessionCipher.getRemoteRegistrationId() ]).then(function(result) { - return this.toJSON(device, result[0], result[1]); + return this.toJSON(address, result[0], result[1]); }.bind(this)); }, - toJSON: function(device, encryptedMsg, registrationId) { + toJSON: function(address, encryptedMsg, registrationId) { var json = { type: encryptedMsg.type, - destinationDeviceId: textsecure.utils.unencodeNumber(device.encodedNumber)[1], + destinationDeviceId: address.getDeviceId(), destinationRegistrationId: registrationId }; @@ -38998,11 +38998,10 @@ MessageSender.prototype = { proto.body = "TERMINATE"; proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION; return this.sendIndividualProto(number, proto, timestamp).then(function(res) { - return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { - return Promise.all(devices.map(function(device) { - console.log('closing session for', device.encodedNumber); - - var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { + return Promise.all(deviceIds.map(function(deviceId) { + var address = new libsignal.SignalProtocolAddress(number, deviceId); + console.log('closing session for', address.toString()); var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); return sessionCipher.closeOpenSessionForDevice(); })).then(function() { diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 9691d684e..979832ac1 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -34,11 +34,11 @@ OutgoingMessage.prototype = { }, reloadDevicesAndSend: function(number, recurse) { return function() { - return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) { - if (devicesForNumber.length == 0) { + return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { + if (deviceIds.length == 0) { return this.registerError(number, "Got empty device list when loading device keys", null); } - return this.doSendMessage(number, devicesForNumber, recurse); + return this.doSendMessage(number, deviceIds, recurse); }.bind(this)); }.bind(this); }, @@ -89,14 +89,14 @@ OutgoingMessage.prototype = { }); }, - doSendMessage: function(number, devicesForNumber, recurse) { + doSendMessage: function(number, deviceIds, recurse) { var ciphers = {}; var plaintext = this.message.toArrayBuffer(); - return Promise.all(devicesForNumber.map(function(device) { - var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + return Promise.all(deviceIds.map(function(deviceId) { + var address = new libsignal.SignalProtocolAddress(number, deviceId); var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); ciphers[address.getDeviceId()] = sessionCipher; - return this.encryptToDevice(device, plaintext, sessionCipher); + return this.encryptToDevice(address, plaintext, sessionCipher); }.bind(this))).then(function(jsonData) { return this.transmitMessage(number, jsonData, this.timestamp).then(function() { this.successfulNumbers[this.successfulNumbers.length] = number; @@ -130,19 +130,19 @@ OutgoingMessage.prototype = { }.bind(this)); }, - encryptToDevice: function(device, plaintext, sessionCipher) { + encryptToDevice: function(address, plaintext, sessionCipher) { return Promise.all([ sessionCipher.encrypt(plaintext), sessionCipher.getRemoteRegistrationId() ]).then(function(result) { - return this.toJSON(device, result[0], result[1]); + return this.toJSON(address, result[0], result[1]); }.bind(this)); }, - toJSON: function(device, encryptedMsg, registrationId) { + toJSON: function(address, encryptedMsg, registrationId) { var json = { type: encryptedMsg.type, - destinationDeviceId: textsecure.utils.unencodeNumber(device.encodedNumber)[1], + destinationDeviceId: address.getDeviceId(), destinationRegistrationId: registrationId }; diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index babc0cbd4..2344b3f98 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -295,11 +295,10 @@ MessageSender.prototype = { proto.body = "TERMINATE"; proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION; return this.sendIndividualProto(number, proto, timestamp).then(function(res) { - return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { - return Promise.all(devices.map(function(device) { - console.log('closing session for', device.encodedNumber); - - var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { + return Promise.all(deviceIds.map(function(deviceId) { + var address = new libsignal.SignalProtocolAddress(number, deviceId); + console.log('closing session for', address.toString()); var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); return sessionCipher.closeOpenSessionForDevice(); })).then(function() {