1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Make encryption asynchronous

We're going to need to send out a load of messages to distribute the megolm
keys; as a first step, deal with the asynchronicity this will require.
This commit is contained in:
Richard van der Hoff
2016-08-17 18:18:20 +01:00
parent e0bd05a8c4
commit 32fa51818b
8 changed files with 105 additions and 63 deletions

View File

@@ -39,35 +39,33 @@ var MEGOLM_ALGORITHM = "m.megolm.v1.aes-sha2";
*/
function MegolmEncryption(params) {
base.EncryptionAlgorithm.call(this, params);
this._prepPromise = null;
this._outboundSessionId = null;
}
utils.inherits(MegolmEncryption, base.EncryptionAlgorithm);
/**
* @inheritdoc
* @param {string[]} roomMembers list of currently-joined users in the room
* @return {module:client.Promise} Promise which resolves when setup is complete
*/
MegolmEncryption.prototype.initRoomEncryption = function(roomMembers) {
// nothing to do here.
return q();
};
/**
* @private
*
* @return {module:client.Promise} Promise which resolves when setup is
* complete.
*/
MegolmEncryption.prototype._ensureOutboundSession = function() {
if (this._outboundSessionId) {
return;
if (this._prepPromise) {
// prep already in progress
return this._prepPromise;
}
if (this._outboundSessionId) {
// prep already done
return q();
}
var session_id = this._olmDevice.createOutboundGroupSession();
this._outboundSessionId = session_id;
var key = this._olmDevice.getOutboundGroupSessionKey(session_id);
// TODO: initiate key-sharing
console.log(
'Created outbound session. Add with window.mxMatrixClientPeg.' +
'matrixClient._crypto._olmDevice.addInboundGroupSession("' +
@@ -82,8 +80,17 @@ MegolmEncryption.prototype._ensureOutboundSession = function() {
this._roomId, this._olmDevice.deviceCurve25519Key, session_id,
key.key, key.chain_index
);
var self = this;
// TODO: initiate key-sharing
this._prepPromise = q.delay(3000).then(function() {
console.log("woop woop, we totally shared the keys");
self._prepPromise = null;
});
return this._prepPromise;
};
/**
* @inheritdoc
*
@@ -91,30 +98,31 @@ MegolmEncryption.prototype._ensureOutboundSession = function() {
* @param {string} eventType
* @param {object} plaintext event content
*
* @return {object} new event body
* @return {module:client.Promise} Promise which resolves to the new event body
*/
MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
this._ensureOutboundSession();
var self = this;
return this._ensureOutboundSession().then(function() {
var payloadJson = {
room_id: self._roomId,
type: eventType,
content: content
};
var payloadJson = {
room_id: this._roomId,
type: eventType,
content: content
};
var ciphertext = self._olmDevice.encryptGroupMessage(
self._outboundSessionId, JSON.stringify(payloadJson)
);
var ciphertext = this._olmDevice.encryptGroupMessage(
this._outboundSessionId, JSON.stringify(payloadJson)
);
var encryptedContent = {
algorithm: MEGOLM_ALGORITHM,
sender_key: self._olmDevice.deviceCurve25519Key,
body: ciphertext,
session_id: self._outboundSessionId,
signature: "FIXME",
};
var encryptedContent = {
algorithm: MEGOLM_ALGORITHM,
sender_key: this._olmDevice.deviceCurve25519Key,
body: ciphertext,
session_id: this._outboundSessionId,
signature: "FIXME",
};
return encryptedContent;
return encryptedContent;
});
};
/**