From 843036f0cef73084977728f7f8494d7770589bff Mon Sep 17 00:00:00 2001 From: lilia Date: Sun, 1 May 2016 14:15:51 -0700 Subject: [PATCH] Remove getRegistrationId and encryptMessageFor from protocol_wrapper We can now use protocol classes like SessionCipher directly because it supports per-device read/write serialization internally. // FREEBIE --- js/libtextsecure.js | 31 +++++++++---------------------- libtextsecure/outgoing_message.js | 12 ++++++++---- libtextsecure/protocol_wrapper.js | 12 ------------ libtextsecure/storage/devices.js | 7 +------ 4 files changed, 18 insertions(+), 44 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 1fd14e9ed..317018de7 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -35505,13 +35505,6 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ return protocolInstance.closeOpenSessionForDevice(encodedNumber); }); }, - encryptMessageFor: function(deviceObject, pushMessageContent) { - return queueJobForNumber(deviceObject.encodedNumber, function() { - var address = libsignal.SignalProtocolAddress.fromString(deviceObject.encodedNumber); - var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); - return sessionCipher.encrypt(pushMessageContent); - }); - }, startWorker: function() { protocolInstance.startWorker('/js/libsignal-protocol-worker.js'); }, @@ -35526,11 +35519,6 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ return protocolInstance.hasOpenSession(encodedNumber); }); }, - getRegistrationId: function(encodedNumber) { - return queueJobForNumber(encodedNumber, function() { - return protocolInstance.getRegistrationId(encodedNumber); - }); - }, handlePreKeyWhisperMessage: function(from, blob) { console.log('prekey whisper message'); blob.mark(); @@ -35824,12 +35812,7 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { return Promise.all(deviceIds.map(function(deviceId) { var address = new libsignal.SignalProtocolAddress(number, deviceId).toString(); - return textsecure.protocol_wrapper.getRegistrationId(address).then(function(registrationId) { - return { - encodedNumber : address, - registrationId : registrationId - }; - }); + return { encodedNumber : address }; })); }); }); @@ -37619,17 +37602,21 @@ OutgoingMessage.prototype = { encryptToDevices: function(deviceObjectList) { var plaintext = this.message.toArrayBuffer(); return Promise.all(deviceObjectList.map(function(device) { - return textsecure.protocol_wrapper.encryptMessageFor(device, plaintext).then(function(encryptedMsg) { - return this.toJSON(device, encryptedMsg); + var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + return sessionCipher.encrypt(plaintext).then(function(encryptedMsg) { + return sessionCipher.getRemoteRegistrationId().then(function(registrationId) { + return this.toJSON(device, encryptedMsg, registrationId); + }.bind(this)); }.bind(this)); }.bind(this))); }, - toJSON: function(device, encryptedMsg) { + toJSON: function(device, encryptedMsg, registrationId) { var json = { type: encryptedMsg.type, destinationDeviceId: textsecure.utils.unencodeNumber(device.encodedNumber)[1], - destinationRegistrationId: device.registrationId + destinationRegistrationId: registrationId }; if (device.relay !== undefined) { diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 2458a6b1a..8f22089eb 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -130,17 +130,21 @@ OutgoingMessage.prototype = { encryptToDevices: function(deviceObjectList) { var plaintext = this.message.toArrayBuffer(); return Promise.all(deviceObjectList.map(function(device) { - return textsecure.protocol_wrapper.encryptMessageFor(device, plaintext).then(function(encryptedMsg) { - return this.toJSON(device, encryptedMsg); + var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + return sessionCipher.encrypt(plaintext).then(function(encryptedMsg) { + return sessionCipher.getRemoteRegistrationId().then(function(registrationId) { + return this.toJSON(device, encryptedMsg, registrationId); + }.bind(this)); }.bind(this)); }.bind(this))); }, - toJSON: function(device, encryptedMsg) { + toJSON: function(device, encryptedMsg, registrationId) { var json = { type: encryptedMsg.type, destinationDeviceId: textsecure.utils.unencodeNumber(device.encodedNumber)[1], - destinationRegistrationId: device.registrationId + destinationRegistrationId: registrationId }; if (device.relay !== undefined) { diff --git a/libtextsecure/protocol_wrapper.js b/libtextsecure/protocol_wrapper.js index 216447137..a10ac0f7f 100644 --- a/libtextsecure/protocol_wrapper.js +++ b/libtextsecure/protocol_wrapper.js @@ -40,13 +40,6 @@ return protocolInstance.closeOpenSessionForDevice(encodedNumber); }); }, - encryptMessageFor: function(deviceObject, pushMessageContent) { - return queueJobForNumber(deviceObject.encodedNumber, function() { - var address = libsignal.SignalProtocolAddress.fromString(deviceObject.encodedNumber); - var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); - return sessionCipher.encrypt(pushMessageContent); - }); - }, startWorker: function() { protocolInstance.startWorker('/js/libsignal-protocol-worker.js'); }, @@ -61,11 +54,6 @@ return protocolInstance.hasOpenSession(encodedNumber); }); }, - getRegistrationId: function(encodedNumber) { - return queueJobForNumber(encodedNumber, function() { - return protocolInstance.getRegistrationId(encodedNumber); - }); - }, handlePreKeyWhisperMessage: function(from, blob) { console.log('prekey whisper message'); blob.mark(); diff --git a/libtextsecure/storage/devices.js b/libtextsecure/storage/devices.js index f54afbe04..8050c4879 100644 --- a/libtextsecure/storage/devices.js +++ b/libtextsecure/storage/devices.js @@ -38,12 +38,7 @@ return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { return Promise.all(deviceIds.map(function(deviceId) { var address = new libsignal.SignalProtocolAddress(number, deviceId).toString(); - return textsecure.protocol_wrapper.getRegistrationId(address).then(function(registrationId) { - return { - encodedNumber : address, - registrationId : registrationId - }; - }); + return { encodedNumber : address }; })); }); });