Skip to content

Commit 5789791

Browse files
committed
Remove Axolotl.createSessionFromPreKeyWhisperMessage
There is never a time when a call to decryptPreKeyWhisperMessage wouldn't be proceeded by a call to createSessionFromPreKeyWhisperMessage so it makes sense to do this for the client in decryptPreKeyWhisperMessage.
1 parent 9650240 commit 5789791

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

dist/axolotl.js

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,41 @@
292292
}, $__20, this);
293293
}));
294294
this.createSessionFromPreKeyBundle = sessionFactory.createSessionFromPreKeyBundle;
295-
this.createSessionFromPreKeyWhisperMessage = sessionFactory.createSessionFromPreKeyWhisperMessage;
296295
this.encryptMessage = sessionCipher.encryptMessage;
297296
this.decryptWhisperMessage = sessionCipher.decryptWhisperMessage;
298-
this.decryptPreKeyWhisperMessage = sessionCipher.decryptPreKeyWhisperMessage;
297+
this.decryptPreKeyWhisperMessage = co.wrap($traceurRuntime.initGeneratorFunction(function $__21(session, preKeyWhisperMessageBytes) {
298+
var $__22, $__23, $__24;
299+
return $traceurRuntime.createGeneratorInstance(function ($ctx) {
300+
while (true)
301+
switch ($ctx.state) {
302+
case 0:
303+
$ctx.state = 2;
304+
return sessionFactory.createSessionFromPreKeyWhisperMessage(session, preKeyWhisperMessageBytes);
305+
case 2:
306+
session = $ctx.sent;
307+
$ctx.state = 4;
308+
break;
309+
case 4:
310+
$__22 = sessionCipher.decryptPreKeyWhisperMessage;
311+
$__23 = $__22.call(sessionCipher, session, preKeyWhisperMessageBytes);
312+
$ctx.state = 10;
313+
break;
314+
case 10:
315+
$ctx.state = 6;
316+
return $__23;
317+
case 6:
318+
$__24 = $ctx.sent;
319+
$ctx.state = 8;
320+
break;
321+
case 8:
322+
$ctx.returnValue = $__24;
323+
$ctx.state = -2;
324+
break;
325+
default:
326+
return $ctx.end();
327+
}
328+
}, $__21, this);
329+
}));
299330
Object.freeze(self);
300331
}
301332
var $__default = Axolotl;
@@ -959,37 +990,33 @@
959990
var ProtocolConstants = ($__ProtocolConstants__ = _require(9), $__ProtocolConstants__ && $__ProtocolConstants__.__esModule && $__ProtocolConstants__ || { default: $__ProtocolConstants__ }).default;
960991
var ArrayBufferUtils = ($__ArrayBufferUtils__ = _require(1), $__ArrayBufferUtils__ && $__ArrayBufferUtils__.__esModule && $__ArrayBufferUtils__ || { default: $__ArrayBufferUtils__ }).default;
961992
var SessionState = ($__SessionState__ = _require(14), $__SessionState__ && $__SessionState__.__esModule && $__SessionState__ || { default: $__SessionState__ }).default;
962-
function Session(session) {
963-
var self = this;
964-
var states = [];
993+
var Session = function Session(session) {
994+
this.states = [];
965995
if (session) {
966-
for (var $__3 = session.states[$traceurRuntime.toProperty(Symbol.iterator)](), $__4 = void 0; !($__4 = $__3.next()).done;) {
967-
var state = $__4.value;
996+
for (var $__4 = session.states[$traceurRuntime.toProperty(Symbol.iterator)](), $__5 = void 0; !($__5 = $__4.next()).done;) {
997+
var state = $__5.value;
968998
{
969-
states.push(new SessionState(state));
999+
this.states.push(new SessionState(state));
9701000
}
9711001
}
9721002
}
973-
Object.defineProperty(self, 'states', {
974-
get: function () {
975-
return states;
976-
}
977-
});
978-
self.mostRecentState = function () {
979-
return states[0];
980-
};
981-
self.addState = function (state) {
982-
states.unshift(state);
983-
if (states.length > ProtocolConstants.maximumSessionStatesPerIdentity) {
984-
states.pop();
1003+
Object.seal(this);
1004+
};
1005+
$traceurRuntime.createClass(Session, {
1006+
mostRecentState: function () {
1007+
return this.states[0];
1008+
},
1009+
addState: function (state) {
1010+
this.states.unshift(state);
1011+
if (this.states.length > ProtocolConstants.maximumSessionStatesPerIdentity) {
1012+
this.states.pop();
9851013
}
986-
};
987-
self.removeState = function (state) {
988-
var index = states.indexOf(state);
989-
states.splice(index, 1);
990-
};
991-
Object.freeze(this);
992-
}
1014+
},
1015+
removeState: function (state) {
1016+
var index = this.states.indexOf(state);
1017+
this.states.splice(index, 1);
1018+
}
1019+
}, {});
9931020
var $__default = Session;
9941021
},
9951022
function (module, exports) {

src/Axolotl.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,6 @@ function Axolotl(crypto, store) {
196196
*/
197197
this.createSessionFromPreKeyBundle = sessionFactory.createSessionFromPreKeyBundle;
198198

199-
/**
200-
* Create a session from a PreKeyWhisperMessage.
201-
* @method
202-
* @type {Session} session - a session, if one exists, or null otherwise.
203-
* @type {ArrayBuffer} preKeyWhisperMessageBytes - the bytes of a PreKeyWhisperMessage.
204-
* @returns {Promise.<Session, Error>}
205-
*/
206-
this.createSessionFromPreKeyWhisperMessage = sessionFactory.createSessionFromPreKeyWhisperMessage;
207-
208199
/**
209200
* Encrypt a message using the session.
210201
* <p>
@@ -232,14 +223,18 @@ function Axolotl(crypto, store) {
232223
this.decryptWhisperMessage = sessionCipher.decryptWhisperMessage;
233224

234225
/**
235-
* Unwrap the WhisperMessage from a PreKeyWhisperMessage and attempt to decrypt it using session.
226+
* Unwrap the WhisperMessage from a PreKeyWhisperMessage and attempt to decrypt it using session. If a session does
227+
* not already exist, it will be created.
236228
*
237229
* @method
238-
* @param {Session} session
230+
* @param {Session} session - a session, if one exists, or null otherwise.
239231
* @param {ArrayBuffer} preKeyWhisperMessageBytes - the encrypted message bytes
240232
* @returns {Promise.<Object, InvalidMessageException>} an object containing the decrypted message and a new session
241233
*/
242-
this.decryptPreKeyWhisperMessage = sessionCipher.decryptPreKeyWhisperMessage;
234+
this.decryptPreKeyWhisperMessage = co.wrap(function*(session, preKeyWhisperMessageBytes) {
235+
session = yield sessionFactory.createSessionFromPreKeyWhisperMessage(session, preKeyWhisperMessageBytes);
236+
return yield sessionCipher.decryptPreKeyWhisperMessage(session, preKeyWhisperMessageBytes);
237+
});
243238

244239
Object.freeze(self);
245240
}

test/unit/Axolotl.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ describe("Axolotl", () => {
188188
var decryptMessage = co.wrap(function*(receivingAxolotl, sendingSession, message) {
189189
var result;
190190
if (message.ciphertext.isPreKeyWhisperMessage) {
191-
sendingSession.session = yield receivingAxolotl.createSessionFromPreKeyWhisperMessage(
192-
sendingSession.session, message.ciphertext.body);
193191
result = yield receivingAxolotl.decryptPreKeyWhisperMessage(sendingSession.session,
194192
message.ciphertext.body);
195193
} else {
@@ -409,7 +407,7 @@ describe("Axolotl", () => {
409407
},
410408
message: {}
411409
});
412-
yield assert.isRejected(bobAxolotl.createSessionFromPreKeyWhisperMessage(aliceSession, message),
410+
yield assert.isRejected(bobAxolotl.decryptPreKeyWhisperMessage(aliceSession, message),
413411
UnsupportedProtocolVersionException);
414412
}));
415413
it("accepts out of order message delivery (main ratchet)", co.wrap(function*() {

0 commit comments

Comments
 (0)