From 641a7dbc6d7665c68b005c55d945efe7b94f1fae Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 17 Nov 2015 11:54:45 -0800 Subject: [PATCH] Update libaxolotl encryptMessageFor now requires an array buffer instead of a protobuf. By converting the message to an array buffer outside the Promise.all, we avoid allocating a new copy for each device. // FREEBIE --- js/libtextsecure.js | 9 ++++++--- libtextsecure/libaxolotl.js | 6 ++++-- libtextsecure/sendmessage.js | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 05911bff154..bb4731445c7 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37510,7 +37510,10 @@ window.axolotl.protocol = function(storage_interface) { } // return Promise(encoded [PreKey]WhisperMessage) - self.encryptMessageFor = function(deviceObject, pushMessageContent) { + self.encryptMessageFor = function(deviceObject, plaintext) { + if (!(plaintext instanceof ArrayBuffer)) { + throw new Error("Expected plaintext to be an ArrayBuffer"); + } return storage_interface.getMyIdentityKey().then(function(ourIdentityKey) { return storage_interface.getMyRegistrationId().then(function(myRegistrationId) { return crypto_storage.getOpenSession(deviceObject.encodedNumber).then(function(session) { @@ -37518,7 +37521,6 @@ window.axolotl.protocol = function(storage_interface) { var doEncryptPushMessageContent = function() { var msg = new axolotlInternal.protobuf.WhisperMessage(); - var plaintext = axolotlInternal.utils.convertToArrayBuffer(pushMessageContent.encode()); var paddedPlaintext = new Uint8Array(getPaddedMessageLength(plaintext.byteLength)); paddedPlaintext.set(new Uint8Array(plaintext)); @@ -39864,6 +39866,7 @@ MessageSender.prototype = { // message == DataMessage or ContentMessage proto encryptToDevices: function(timestamp, number, deviceObjectList, message) { var legacy = (message instanceof textsecure.protobuf.DataMessage); + var plaintext = message.toArrayBuffer(); var relay = deviceObjectList[0].relay; for (var i=1; i < deviceObjectList.length; ++i) { if (deviceObjectList[i].relay !== relay) { @@ -39871,7 +39874,7 @@ MessageSender.prototype = { } } return Promise.all(deviceObjectList.map(function(device) { - return textsecure.protocol_wrapper.encryptMessageFor(device, message).then(function(encryptedMsg) { + return textsecure.protocol_wrapper.encryptMessageFor(device, plaintext).then(function(encryptedMsg) { return textsecure.protocol_wrapper.getRegistrationId(device.encodedNumber).then(function(registrationId) { return textsecure.storage.devices.removeTempKeysFromDevice(device.encodedNumber).then(function() { var json = { diff --git a/libtextsecure/libaxolotl.js b/libtextsecure/libaxolotl.js index 2f15bb0b441..63094080b18 100644 --- a/libtextsecure/libaxolotl.js +++ b/libtextsecure/libaxolotl.js @@ -37412,7 +37412,10 @@ window.axolotl.protocol = function(storage_interface) { } // return Promise(encoded [PreKey]WhisperMessage) - self.encryptMessageFor = function(deviceObject, pushMessageContent) { + self.encryptMessageFor = function(deviceObject, plaintext) { + if (!(plaintext instanceof ArrayBuffer)) { + throw new Error("Expected plaintext to be an ArrayBuffer"); + } return storage_interface.getMyIdentityKey().then(function(ourIdentityKey) { return storage_interface.getMyRegistrationId().then(function(myRegistrationId) { return crypto_storage.getOpenSession(deviceObject.encodedNumber).then(function(session) { @@ -37420,7 +37423,6 @@ window.axolotl.protocol = function(storage_interface) { var doEncryptPushMessageContent = function() { var msg = new axolotlInternal.protobuf.WhisperMessage(); - var plaintext = axolotlInternal.utils.convertToArrayBuffer(pushMessageContent.encode()); var paddedPlaintext = new Uint8Array(getPaddedMessageLength(plaintext.byteLength)); paddedPlaintext.set(new Uint8Array(plaintext)); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 088b8052e4d..528e15262ae 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -11,6 +11,7 @@ MessageSender.prototype = { // message == DataMessage or ContentMessage proto encryptToDevices: function(timestamp, number, deviceObjectList, message) { var legacy = (message instanceof textsecure.protobuf.DataMessage); + var plaintext = message.toArrayBuffer(); var relay = deviceObjectList[0].relay; for (var i=1; i < deviceObjectList.length; ++i) { if (deviceObjectList[i].relay !== relay) { @@ -18,7 +19,7 @@ MessageSender.prototype = { } } return Promise.all(deviceObjectList.map(function(device) { - return textsecure.protocol_wrapper.encryptMessageFor(device, message).then(function(encryptedMsg) { + return textsecure.protocol_wrapper.encryptMessageFor(device, plaintext).then(function(encryptedMsg) { return textsecure.protocol_wrapper.getRegistrationId(device.encodedNumber).then(function(registrationId) { return textsecure.storage.devices.removeTempKeysFromDevice(device.encodedNumber).then(function() { var json = {