From b790f828491681c8a50bf217124fc77ca1fb135c Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 19 Feb 2015 14:53:08 -0800 Subject: [PATCH] Remove libaxolotl dependency on textsecure/errors.js Let libaxolotl throw a generic error instead of a replayable error, and add an helper function in libtextsecure's axolotl_wrapper to catch and convert from the generic error to the replayable one. This allows the ReplayableError to remain a libtextsecure-level concept only. Somewhat unrelatedly, but nearby, fix some whitespace and add missing semicolon. --- js/libtextsecure.js | 22 +++++++++++++++++----- libaxolotl/protocol.js | 3 +-- libtextsecure/axolotl_wrapper.js | 19 ++++++++++++++++--- libtextsecure/libaxolotl_concat.js | 3 +-- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index c0a76753..b8501ba5 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -140,11 +140,24 @@ if ((finalMessage.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION) == textsecure.protobuf.PushMessageContent.Flags.END_SESSION && - finalMessage.sync !== null) + finalMessage.sync !== null) res[1](); return finalMessage; - } + }; + + var handlePreKeyWhisperMessage = function(from, message) { + try { + return axolotl.protocol.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 = { @@ -159,7 +172,7 @@ if (proto.message.readUint8() != ((3 << 4) | 3)) throw new Error("Bad version byte"); var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); - return axolotl.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); + return handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); case textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT: return Promise.resolve(null); default: @@ -37187,8 +37200,7 @@ window.axolotl.protocol = function() { if (open_session !== undefined) closeSession(open_session); // To be returned and saved later } else { - // ...otherwise create an error that the UI will pick up and ask the user if they want to re-negotiate - throw new textsecure.IncomingIdentityKeyError(encodedNumber, getString(message.encode())); + throw new Error('Unknown identity key'); } } return initSession(false, preKeyPair, signedPreKeyPair, encodedNumber, toArrayBuffer(message.identityKey), toArrayBuffer(message.baseKey), undefined) diff --git a/libaxolotl/protocol.js b/libaxolotl/protocol.js index e63f5403..faba1bd0 100644 --- a/libaxolotl/protocol.js +++ b/libaxolotl/protocol.js @@ -386,8 +386,7 @@ window.axolotl.protocol = function() { if (open_session !== undefined) closeSession(open_session); // To be returned and saved later } else { - // ...otherwise create an error that the UI will pick up and ask the user if they want to re-negotiate - throw new textsecure.IncomingIdentityKeyError(encodedNumber, getString(message.encode())); + throw new Error('Unknown identity key'); } } return initSession(false, preKeyPair, signedPreKeyPair, encodedNumber, toArrayBuffer(message.identityKey), toArrayBuffer(message.baseKey), undefined) diff --git a/libtextsecure/axolotl_wrapper.js b/libtextsecure/axolotl_wrapper.js index b3440bfb..b283b5b0 100644 --- a/libtextsecure/axolotl_wrapper.js +++ b/libtextsecure/axolotl_wrapper.js @@ -63,11 +63,24 @@ if ((finalMessage.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION) == textsecure.protobuf.PushMessageContent.Flags.END_SESSION && - finalMessage.sync !== null) + finalMessage.sync !== null) res[1](); return finalMessage; - } + }; + + var handlePreKeyWhisperMessage = function(from, message) { + try { + return axolotl.protocol.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 = { @@ -82,7 +95,7 @@ if (proto.message.readUint8() != ((3 << 4) | 3)) throw new Error("Bad version byte"); var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); - return axolotl.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); + return handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); case textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT: return Promise.resolve(null); default: diff --git a/libtextsecure/libaxolotl_concat.js b/libtextsecure/libaxolotl_concat.js index 23a4708d..3858c5bd 100644 --- a/libtextsecure/libaxolotl_concat.js +++ b/libtextsecure/libaxolotl_concat.js @@ -37001,8 +37001,7 @@ window.axolotl.protocol = function() { if (open_session !== undefined) closeSession(open_session); // To be returned and saved later } else { - // ...otherwise create an error that the UI will pick up and ask the user if they want to re-negotiate - throw new textsecure.IncomingIdentityKeyError(encodedNumber, getString(message.encode())); + throw new Error('Unknown identity key'); } } return initSession(false, preKeyPair, signedPreKeyPair, encodedNumber, toArrayBuffer(message.identityKey), toArrayBuffer(message.baseKey), undefined)