From af48ca9e4b9fdce9d9b100d3207b40a60c13bea1 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 20 Jul 2015 12:22:33 -0700 Subject: [PATCH] Update libaxolotl, remove one string conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously we would convert a bytebuffer to a string, pass it to libaxolotl where it would be parsed back into a bytebuffer. Ideally we would just pass the bytebuffer, but it turns out that libaxolotl's bytebyffer class is identical but separate from libtextsecure's bytebuffer class. ¯\_(ツ)_/¯ So instead we pass the underlying array buffer, which is handled more or less the same way as a bytebuffer, and most importantly, does not involve any copying. // FREEBIE --- js/libtextsecure.js | 25 ++++++++++--------------- libtextsecure/axolotl_wrapper.js | 23 +++++++++-------------- libtextsecure/libaxolotl.js | 2 +- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index e56c84ad3f..715d9df7cd 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37442,7 +37442,7 @@ window.axolotl.protocol = function(storage_interface) { // Inits a session (maybe) and then decrypts the message self.handlePreKeyWhisperMessage = function(from, encodedMessage) { - var preKeyProto = axolotlInternal.protobuf.PreKeyWhisperMessage.decode(encodedMessage, 'binary'); + var preKeyProto = axolotlInternal.protobuf.PreKeyWhisperMessage.decode(encodedMessage); return initSessionFromPreKeyWhisperMessage(from, preKeyProto).then(function(sessions) { return doDecryptWhisperMessage(from, axolotlInternal.utils.convertToString(preKeyProto.message), sessions[0], preKeyProto.registrationId).then(function(result) { if (sessions[1] !== undefined) @@ -37784,19 +37784,6 @@ axolotlInternal.RecipientRecord = function() { textsecure.storage.axolotl = new AxolotlStore(); var axolotlInstance = axolotl.protocol(textsecure.storage.axolotl); - var handlePreKeyWhisperMessage = function(from, message) { - try { - return axolotlInstance.handlePreKeyWhisperMessage(from, message); - } catch(e) { - if (e.message === 'Unknown identity key') { - // create an error that the UI will pick up and ask the - // user if they want to re-negotiate - throw new textsecure.IncomingIdentityKeyError(from, message); - } - throw e; - } - }; - window.textsecure = window.textsecure || {}; window.textsecure.protocol_wrapper = { decrypt: function(source, sourceDevice, type, blob) { @@ -37808,7 +37795,15 @@ axolotlInternal.RecipientRecord = function() { case textsecure.protobuf.Envelope.Type.PREKEY_BUNDLE: if (blob.readUint8() != ((3 << 4) | 3)) throw new Error("Bad version byte"); - return handlePreKeyWhisperMessage(fromAddress, getString(blob)); + var blob = blob.toArrayBuffer(); + return axolotlInstance.handlePreKeyWhisperMessage(fromAddress, blob).catch(function(e) { + if (e.message === 'Unknown identity key') { + // create an error that the UI will pick up and ask the + // user if they want to re-negotiate + throw new textsecure.IncomingIdentityKeyError(fromAddress, blob); + } + throw e; + }); default: return new Promise.reject(new Error("Unknown message type")); } diff --git a/libtextsecure/axolotl_wrapper.js b/libtextsecure/axolotl_wrapper.js index cafb69eb0b..63116b132f 100644 --- a/libtextsecure/axolotl_wrapper.js +++ b/libtextsecure/axolotl_wrapper.js @@ -5,19 +5,6 @@ textsecure.storage.axolotl = new AxolotlStore(); var axolotlInstance = axolotl.protocol(textsecure.storage.axolotl); - var handlePreKeyWhisperMessage = function(from, message) { - try { - return axolotlInstance.handlePreKeyWhisperMessage(from, message); - } catch(e) { - if (e.message === 'Unknown identity key') { - // create an error that the UI will pick up and ask the - // user if they want to re-negotiate - throw new textsecure.IncomingIdentityKeyError(from, message); - } - throw e; - } - }; - window.textsecure = window.textsecure || {}; window.textsecure.protocol_wrapper = { decrypt: function(source, sourceDevice, type, blob) { @@ -29,7 +16,15 @@ case textsecure.protobuf.Envelope.Type.PREKEY_BUNDLE: if (blob.readUint8() != ((3 << 4) | 3)) throw new Error("Bad version byte"); - return handlePreKeyWhisperMessage(fromAddress, getString(blob)); + var blob = blob.toArrayBuffer(); + return axolotlInstance.handlePreKeyWhisperMessage(fromAddress, blob).catch(function(e) { + if (e.message === 'Unknown identity key') { + // create an error that the UI will pick up and ask the + // user if they want to re-negotiate + throw new textsecure.IncomingIdentityKeyError(fromAddress, blob); + } + throw e; + }); default: return new Promise.reject(new Error("Unknown message type")); } diff --git a/libtextsecure/libaxolotl.js b/libtextsecure/libaxolotl.js index 73d5775989..0049b03673 100644 --- a/libtextsecure/libaxolotl.js +++ b/libtextsecure/libaxolotl.js @@ -37365,7 +37365,7 @@ window.axolotl.protocol = function(storage_interface) { // Inits a session (maybe) and then decrypts the message self.handlePreKeyWhisperMessage = function(from, encodedMessage) { - var preKeyProto = axolotlInternal.protobuf.PreKeyWhisperMessage.decode(encodedMessage, 'binary'); + var preKeyProto = axolotlInternal.protobuf.PreKeyWhisperMessage.decode(encodedMessage); return initSessionFromPreKeyWhisperMessage(from, preKeyProto).then(function(sessions) { return doDecryptWhisperMessage(from, axolotlInternal.utils.convertToString(preKeyProto.message), sessions[0], preKeyProto.registrationId).then(function(result) { if (sessions[1] !== undefined)