Made session reset synchronous.

Clean up some code.
pull/843/head
Mikunj 5 years ago
parent ebfff824bd
commit 820ce5cdf0

@ -333,31 +333,25 @@
this.storage = storage; this.storage = storage;
this.address = address; this.address = address;
this.sessionCipher = new libsignal.SessionCipher(storage, address); this.sessionCipher = new libsignal.SessionCipher(storage, address);
this.TYPE = Object.freeze({
MESSAGE: 1,
PREKEY: 2,
});
} }
async decryptWhisperMessage(buffer, encoding) { decryptWhisperMessage(buffer, encoding) {
// Capture active session return this._decryptMessage(this.TYPE.MESSAGE, buffer, encoding);
const activeSessionBaseKey = await this._getCurrentSessionBaseKey(); }
const promise = this.sessionCipher.decryptWhisperMessage(
buffer,
encoding
);
// Handle session reset
// eslint-disable-next-line more/no-then
promise.then(() => {
this._handleSessionResetIfNeeded(activeSessionBaseKey);
});
return promise; decryptPreKeyWhisperMessage(buffer, encoding) {
return this._decryptMessage(this.TYPE.PREKEY, buffer, encoding);
} }
async decryptPreKeyWhisperMessage(buffer, encoding) { async _decryptMessage(type, buffer, encoding) {
// Capture active session // Capture active session
const activeSessionBaseKey = await this._getCurrentSessionBaseKey(); const activeSessionBaseKey = await this._getCurrentSessionBaseKey();
if (!activeSessionBaseKey) { if (type === this.TYPE.PREKEY && !activeSessionBaseKey) {
const wrapped = dcodeIO.ByteBuffer.wrap(buffer); const wrapped = dcodeIO.ByteBuffer.wrap(buffer);
await window.libloki.storage.verifyFriendRequestAcceptPreKey( await window.libloki.storage.verifyFriendRequestAcceptPreKey(
this.address.getName(), this.address.getName(),
@ -365,18 +359,19 @@
); );
} }
const promise = this.sessionCipher.decryptPreKeyWhisperMessage( const decryptFunction = type === this.TYPE.PREKEY ? this.sessionCipher.decryptPreKeyWhisperMessage : this.sessionCipher.decryptWhisperMessage;
buffer, const result = await decryptFunction(buffer, encoding);
encoding
);
// Handle session reset // Handle session reset
// eslint-disable-next-line more/no-then // This needs to be done synchronously so that the next time we decrypt a message,
promise.then(() => { // we have the correct session
this._handleSessionResetIfNeeded(activeSessionBaseKey); try {
}); await this._handleSessionResetIfNeeded(activeSessionBaseKey);
} catch (e) {
window.log.info('Failed to handle session reset: ', e);
}
return promise; return result;
} }
async _handleSessionResetIfNeeded(previousSessionBaseKey) { async _handleSessionResetIfNeeded(previousSessionBaseKey) {

Loading…
Cancel
Save