diff --git a/spec/unit/crypto/algorithms/megolm.spec.js b/spec/unit/crypto/algorithms/megolm.spec.js index ec7e65d41..18f4e1343 100644 --- a/spec/unit/crypto/algorithms/megolm.spec.js +++ b/spec/unit/crypto/algorithms/megolm.spec.js @@ -119,29 +119,32 @@ describe("MegolmDecryption", function() { }, }; - expect(megolmDecryption.hasKeysForKeyRequest(keyRequest)) - .toBe(true); + return megolmDecryption.hasKeysForKeyRequest( + keyRequest, + ).then((hasKeys) => { + expect(hasKeys).toBe(true); - // set up some pre-conditions for the share call - const deviceInfo = {}; - mockCrypto.getStoredDevice.andReturn(deviceInfo); + // set up some pre-conditions for the share call + const deviceInfo = {}; + mockCrypto.getStoredDevice.andReturn(deviceInfo); - const awaitEnsureSessions = new Promise((res, rej) => { - mockOlmLib.ensureOlmSessionsForDevices.andCall(() => { - res(); - return Promise.resolve({'@alice:foo': {'alidevice': { - sessionId: 'alisession', - }}}); + const awaitEnsureSessions = new Promise((res, rej) => { + mockOlmLib.ensureOlmSessionsForDevices.andCall(() => { + res(); + return Promise.resolve({'@alice:foo': {'alidevice': { + sessionId: 'alisession', + }}}); + }); }); - }); - mockBaseApis.sendToDevice = expect.createSpy(); + mockBaseApis.sendToDevice = expect.createSpy(); - // do the share - megolmDecryption.shareKeysWithDevice(keyRequest); + // do the share + megolmDecryption.shareKeysWithDevice(keyRequest); - // it's asynchronous, so we have to wait a bit - return awaitEnsureSessions.then(() => { + // it's asynchronous, so we have to wait a bit + return awaitEnsureSessions; + }).then(() => { // check that it called encryptMessageForDevice with // appropriate args. expect(mockOlmLib.encryptMessageForDevice.calls.length) diff --git a/src/crypto/algorithms/base.js b/src/crypto/algorithms/base.js index 661455070..15f8aed25 100644 --- a/src/crypto/algorithms/base.js +++ b/src/crypto/algorithms/base.js @@ -20,6 +20,8 @@ limitations under the License. * @module */ +import Promise from 'bluebird'; + /** * map of registered encryption algorithm classes. A map from string to {@link * module:crypto/algorithms/base.EncryptionAlgorithm|EncryptionAlgorithm} class @@ -143,11 +145,11 @@ class DecryptionAlgorithm { * Determine if we have the keys necessary to respond to a room key request * * @param {module:crypto~IncomingRoomKeyRequest} keyRequest - * @return {boolean} true if we have the keys and could (theoretically) share + * @return {Promise} true if we have the keys and could (theoretically) share * them; else false. */ hasKeysForKeyRequest(keyRequest) { - return false; + return Promise.resolve(false); } /** diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index eb5505e03..948f1f89b 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -719,7 +719,7 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) { /** * @inheritdoc */ -MegolmDecryption.prototype.hasKeysForKeyRequest = function(keyRequest) { +MegolmDecryption.prototype.hasKeysForKeyRequest = async function(keyRequest) { const body = keyRequest.requestBody; return this._olmDevice.hasInboundSessionKeys( diff --git a/src/crypto/index.js b/src/crypto/index.js index eb3723fe5..6c28bce3f 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -1131,7 +1131,7 @@ Crypto.prototype._processReceivedRoomKeyRequest = async function(req) { return; } - if (!decryptor.hasKeysForKeyRequest(req)) { + if (!await decryptor.hasKeysForKeyRequest(req)) { console.log( `room key request for unknown session ${roomId} / ` + body.session_id,