2020-09-03 15:36:17 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-09-10 12:09:17 +02:00
|
|
|
import {MEGOLM_ALGORITHM, DecryptionSource} from "./common.js";
|
2020-09-03 15:36:17 +02:00
|
|
|
import {groupBy} from "../../utils/groupBy.js";
|
2020-09-10 12:09:17 +02:00
|
|
|
import {mergeMap} from "../../utils/mergeMap.js";
|
2020-09-03 15:36:17 +02:00
|
|
|
import {makeTxnId} from "../common.js";
|
|
|
|
|
2020-09-03 17:50:28 +02:00
|
|
|
const ENCRYPTED_TYPE = "m.room.encrypted";
|
2020-09-03 15:36:17 +02:00
|
|
|
|
2020-09-18 13:10:41 +02:00
|
|
|
function encodeMissingSessionKey(senderKey, sessionId) {
|
|
|
|
return `${senderKey}|${sessionId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeMissingSessionKey(key) {
|
|
|
|
const [senderKey, sessionId] = key.split("|");
|
|
|
|
return {senderKey, sessionId};
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:36:17 +02:00
|
|
|
export class RoomEncryption {
|
2020-09-18 13:10:41 +02:00
|
|
|
constructor({room, deviceTracker, olmEncryption, megolmEncryption, megolmDecryption, encryptionParams, storage, sessionBackup, notifyMissingMegolmSession, clock}) {
|
2020-09-03 15:36:17 +02:00
|
|
|
this._room = room;
|
|
|
|
this._deviceTracker = deviceTracker;
|
|
|
|
this._olmEncryption = olmEncryption;
|
2020-09-03 17:50:28 +02:00
|
|
|
this._megolmEncryption = megolmEncryption;
|
2020-09-04 16:27:14 +02:00
|
|
|
this._megolmDecryption = megolmDecryption;
|
2020-09-03 15:36:17 +02:00
|
|
|
// content of the m.room.encryption event
|
2020-09-03 17:50:28 +02:00
|
|
|
this._encryptionParams = encryptionParams;
|
2020-09-04 12:09:19 +02:00
|
|
|
|
|
|
|
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
|
|
|
|
this._megolmSyncCache = this._megolmDecryption.createSessionCache();
|
2020-09-04 12:10:12 +02:00
|
|
|
// not `event_id`, but an internal event id passed in to the decrypt methods
|
|
|
|
this._eventIdsByMissingSession = new Map();
|
2020-09-08 10:50:39 +02:00
|
|
|
this._senderDeviceCache = new Map();
|
2020-09-08 14:24:48 +02:00
|
|
|
this._storage = storage;
|
2020-09-17 15:58:46 +02:00
|
|
|
this._sessionBackup = sessionBackup;
|
2020-09-17 18:00:00 +02:00
|
|
|
this._notifyMissingMegolmSession = notifyMissingMegolmSession;
|
2020-09-18 13:10:41 +02:00
|
|
|
this._clock = clock;
|
2020-09-18 13:08:18 +02:00
|
|
|
this._disposed = false;
|
2020-09-17 14:20:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 18:00:00 +02:00
|
|
|
async enableSessionBackup(sessionBackup) {
|
2020-09-17 15:58:46 +02:00
|
|
|
if (this._sessionBackup) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-17 14:20:15 +02:00
|
|
|
this._sessionBackup = sessionBackup;
|
2020-09-17 18:00:00 +02:00
|
|
|
for(const key of this._eventIdsByMissingSession.keys()) {
|
2020-09-18 13:10:41 +02:00
|
|
|
const {senderKey, sessionId} = decodeMissingSessionKey(key);
|
|
|
|
await this._requestMissingSessionFromBackup(senderKey, sessionId, null);
|
2020-09-17 18:00:00 +02:00
|
|
|
}
|
2020-09-04 12:09:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
notifyTimelineClosed() {
|
|
|
|
// empty the backfill cache when closing the timeline
|
|
|
|
this._megolmBackfillCache.dispose();
|
|
|
|
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
|
2020-09-08 10:50:39 +02:00
|
|
|
this._senderDeviceCache = new Map(); // purge the sender device cache
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async writeMemberChanges(memberChanges, txn) {
|
2020-09-11 14:41:12 +02:00
|
|
|
const memberChangesArray = Array.from(memberChanges.values());
|
|
|
|
if (memberChangesArray.some(m => m.hasLeft)) {
|
|
|
|
this._megolmEncryption.discardOutboundSession(this._room.id, txn);
|
2020-09-08 11:09:09 +02:00
|
|
|
}
|
2020-09-11 14:41:12 +02:00
|
|
|
if (memberChangesArray.some(m => m.hasJoined)) {
|
|
|
|
await this._addShareRoomKeyOperationForNewMembers(memberChangesArray, txn);
|
|
|
|
}
|
|
|
|
await this._deviceTracker.writeMemberChanges(this._room, memberChanges, txn);
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 12:09:17 +02:00
|
|
|
// this happens before entries exists, as they are created by the syncwriter
|
|
|
|
// but we want to be able to map it back to something in the timeline easily
|
|
|
|
// when retrying decryption.
|
|
|
|
async prepareDecryptAll(events, source, isTimelineOpen, txn) {
|
|
|
|
const errors = [];
|
|
|
|
const validEvents = [];
|
|
|
|
for (const event of events) {
|
|
|
|
if (event.redacted_because || event.unsigned?.redacted_because) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (event.content?.algorithm !== MEGOLM_ALGORITHM) {
|
|
|
|
errors.set(event.event_id, new Error("Unsupported algorithm: " + event.content?.algorithm));
|
|
|
|
}
|
|
|
|
validEvents.push(event);
|
2020-09-08 17:16:01 +02:00
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
let customCache;
|
|
|
|
let sessionCache;
|
|
|
|
if (source === DecryptionSource.Sync) {
|
|
|
|
sessionCache = this._megolmSyncCache;
|
|
|
|
} else if (source === DecryptionSource.Timeline) {
|
|
|
|
sessionCache = this._megolmBackfillCache;
|
|
|
|
} else if (source === DecryptionSource.Retry) {
|
|
|
|
// when retrying, we could have mixed events from at the bottom of the timeline (sync)
|
|
|
|
// and somewhere else, so create a custom cache we use just for this operation.
|
2020-09-18 13:10:41 +02:00
|
|
|
customCache = this._megolmDecryption.createSessionCache();
|
2020-09-10 12:09:17 +02:00
|
|
|
sessionCache = customCache;
|
|
|
|
} else {
|
|
|
|
throw new Error("Unknown source: " + source);
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
const preparation = await this._megolmDecryption.prepareDecryptAll(
|
|
|
|
this._room.id, validEvents, sessionCache, txn);
|
|
|
|
if (customCache) {
|
|
|
|
customCache.dispose();
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
2020-09-18 13:10:41 +02:00
|
|
|
return new DecryptionPreparation(preparation, errors, {isTimelineOpen, source}, this);
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async _processDecryptionResults(results, errors, flags, txn) {
|
|
|
|
for (const error of errors.values()) {
|
|
|
|
if (error.code === "MEGOLM_NO_SESSION") {
|
2020-09-18 13:10:41 +02:00
|
|
|
this._addMissingSessionEvent(error.event, flags.source);
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags.isTimelineOpen) {
|
|
|
|
for (const result of results.values()) {
|
|
|
|
await this._verifyDecryptionResult(result, txn);
|
|
|
|
}
|
2020-09-08 10:50:39 +02:00
|
|
|
}
|
2020-09-04 12:09:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:50:39 +02:00
|
|
|
async _verifyDecryptionResult(result, txn) {
|
|
|
|
let device = this._senderDeviceCache.get(result.senderCurve25519Key);
|
|
|
|
if (!device) {
|
|
|
|
device = await this._deviceTracker.getDeviceByCurve25519Key(result.senderCurve25519Key, txn);
|
|
|
|
this._senderDeviceCache.set(result.senderCurve25519Key, device);
|
|
|
|
}
|
|
|
|
if (device) {
|
|
|
|
result.setDevice(device);
|
|
|
|
} else if (!this._room.isTrackingMembers) {
|
|
|
|
result.setRoomNotTrackedYet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:10:41 +02:00
|
|
|
_addMissingSessionEvent(event, source) {
|
2020-09-04 12:10:12 +02:00
|
|
|
const senderKey = event.content?.["sender_key"];
|
|
|
|
const sessionId = event.content?.["session_id"];
|
2020-09-18 13:10:41 +02:00
|
|
|
const key = encodeMissingSessionKey(senderKey, sessionId);
|
2020-09-04 12:10:12 +02:00
|
|
|
let eventIds = this._eventIdsByMissingSession.get(key);
|
2020-09-17 14:20:15 +02:00
|
|
|
// new missing session
|
2020-09-04 12:10:12 +02:00
|
|
|
if (!eventIds) {
|
2020-09-18 13:10:41 +02:00
|
|
|
this._requestMissingSessionFromBackup(senderKey, sessionId, source);
|
2020-09-10 12:09:17 +02:00
|
|
|
eventIds = new Set();
|
2020-09-04 12:10:12 +02:00
|
|
|
this._eventIdsByMissingSession.set(key, eventIds);
|
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
eventIds.add(event.event_id);
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:10:41 +02:00
|
|
|
async _requestMissingSessionFromBackup(senderKey, sessionId, source) {
|
|
|
|
// if the message came from sync, wait 10s to see if the room key arrives,
|
|
|
|
// and only after that proceed to request from backup
|
|
|
|
if (source === DecryptionSource.Sync) {
|
|
|
|
await this._clock.createTimeout(10000).elapsed();
|
|
|
|
if (this._disposed || !this._eventIdsByMissingSession.has(encodeMissingSessionKey(senderKey, sessionId))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 09:30:25 +02:00
|
|
|
// show prompt to enable secret storage
|
|
|
|
if (!this._sessionBackup) {
|
|
|
|
this._notifyMissingMegolmSession();
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 13:10:41 +02:00
|
|
|
|
2020-09-17 17:57:52 +02:00
|
|
|
try {
|
|
|
|
const session = await this._sessionBackup.getSession(this._room.id, sessionId);
|
|
|
|
if (session?.algorithm === MEGOLM_ALGORITHM) {
|
|
|
|
if (session["sender_key"] !== senderKey) {
|
|
|
|
console.warn("Got session key back from backup with different sender key, ignoring", {session, senderKey});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const txn = await this._storage.readWriteTxn([this._storage.storeNames.inboundGroupSessions]);
|
|
|
|
let roomKey;
|
|
|
|
try {
|
|
|
|
roomKey = await this._megolmDecryption.addRoomKeyFromBackup(
|
|
|
|
this._room.id, sessionId, session, txn);
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await txn.complete();
|
|
|
|
|
|
|
|
if (roomKey) {
|
|
|
|
// this will call into applyRoomKeys below
|
|
|
|
await this._room.notifyRoomKeys([roomKey]);
|
|
|
|
}
|
|
|
|
} else if (session?.algorithm) {
|
|
|
|
console.info(`Backed-up session of unknown algorithm: ${session.algorithm}`);
|
2020-09-17 14:20:15 +02:00
|
|
|
}
|
2020-09-17 17:57:52 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Could not get session ${sessionId} from backup`, err);
|
2020-09-17 14:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {RoomKeyDescription}
|
|
|
|
* @property {RoomKeyDescription} senderKey the curve25519 key of the sender
|
|
|
|
* @property {RoomKeyDescription} sessionId
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {Array<RoomKeyDescription>} roomKeys
|
|
|
|
* @return {Array<string>} the event ids that should be retried to decrypt
|
|
|
|
*/
|
2020-09-04 12:10:12 +02:00
|
|
|
applyRoomKeys(roomKeys) {
|
|
|
|
// retry decryption with the new sessions
|
2020-09-10 12:09:17 +02:00
|
|
|
const retryEventIds = [];
|
2020-09-04 12:10:12 +02:00
|
|
|
for (const roomKey of roomKeys) {
|
2020-09-18 13:10:41 +02:00
|
|
|
const key = encodeMissingSessionKey(roomKey.senderKey, roomKey.sessionId);
|
2020-09-04 15:28:22 +02:00
|
|
|
const entriesForSession = this._eventIdsByMissingSession.get(key);
|
|
|
|
if (entriesForSession) {
|
2020-09-04 12:10:12 +02:00
|
|
|
this._eventIdsByMissingSession.delete(key);
|
2020-09-10 12:09:17 +02:00
|
|
|
retryEventIds.push(...entriesForSession);
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
return retryEventIds;
|
2020-09-04 12:10:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 15:36:17 +02:00
|
|
|
async encrypt(type, content, hsApi) {
|
2020-09-11 14:41:12 +02:00
|
|
|
await this._deviceTracker.trackRoom(this._room);
|
2020-09-03 17:50:28 +02:00
|
|
|
const megolmResult = await this._megolmEncryption.encrypt(this._room.id, type, content, this._encryptionParams);
|
|
|
|
if (megolmResult.roomKeyMessage) {
|
2020-09-11 14:41:12 +02:00
|
|
|
this._shareNewRoomKey(megolmResult.roomKeyMessage, hsApi);
|
2020-09-03 17:50:28 +02:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: ENCRYPTED_TYPE,
|
|
|
|
content: megolmResult.content
|
|
|
|
};
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:38:27 +02:00
|
|
|
needsToShareKeys(memberChanges) {
|
|
|
|
for (const m of memberChanges.values()) {
|
2020-09-11 14:41:12 +02:00
|
|
|
if (m.hasJoined) {
|
2020-09-08 14:38:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
async _shareNewRoomKey(roomKeyMessage, hsApi) {
|
|
|
|
const devices = await this._deviceTracker.devicesForTrackedRoom(this._room.id, hsApi);
|
|
|
|
const userIds = Array.from(devices.reduce((set, device) => set.add(device.userId), new Set()));
|
2020-09-08 14:38:27 +02:00
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
// store operation for room key share, in case we don't finish here
|
|
|
|
const writeOpTxn = await this._storage.readWriteTxn([this._storage.storeNames.operations]);
|
|
|
|
let operationId;
|
|
|
|
try {
|
|
|
|
operationId = this._writeRoomKeyShareOperation(roomKeyMessage, userIds, writeOpTxn);
|
|
|
|
} catch (err) {
|
|
|
|
writeOpTxn.abort();
|
|
|
|
throw err;
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
2020-09-11 14:41:12 +02:00
|
|
|
await writeOpTxn.complete();
|
|
|
|
// TODO: at this point we have the room key stored, and the rest is sort of optional
|
|
|
|
// it would be nice if we could signal SendQueue that any error from here on is non-fatal and
|
|
|
|
// return the encrypted payload.
|
2020-09-08 14:24:48 +02:00
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
// send the room key
|
|
|
|
await this._sendRoomKey(roomKeyMessage, devices, hsApi);
|
|
|
|
|
|
|
|
// remove the operation
|
|
|
|
const removeOpTxn = await this._storage.readWriteTxn([this._storage.storeNames.operations]);
|
|
|
|
try {
|
|
|
|
removeOpTxn.operations.remove(operationId);
|
|
|
|
} catch (err) {
|
|
|
|
removeOpTxn.abort();
|
|
|
|
throw err;
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
2020-09-11 14:41:12 +02:00
|
|
|
await removeOpTxn.complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _addShareRoomKeyOperationForNewMembers(memberChangesArray, txn) {
|
|
|
|
const userIds = memberChangesArray.filter(m => m.hasJoined).map(m => m.userId);
|
|
|
|
const roomKeyMessage = await this._megolmEncryption.createRoomKeyMessage(
|
|
|
|
this._room.id, txn);
|
2020-09-08 14:24:48 +02:00
|
|
|
if (roomKeyMessage) {
|
2020-09-11 14:41:12 +02:00
|
|
|
this._writeRoomKeyShareOperation(roomKeyMessage, userIds, txn);
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:41:12 +02:00
|
|
|
_writeRoomKeyShareOperation(roomKeyMessage, userIds, txn) {
|
|
|
|
const id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString();
|
|
|
|
txn.operations.add({
|
|
|
|
id,
|
|
|
|
type: "share_room_key",
|
|
|
|
scope: this._room.id,
|
|
|
|
userIds,
|
|
|
|
roomKeyMessage,
|
|
|
|
});
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
async flushPendingRoomKeyShares(hsApi, operations = null) {
|
|
|
|
if (!operations) {
|
|
|
|
const txn = await this._storage.readTxn([this._storage.storeNames.operations]);
|
|
|
|
operations = await txn.operations.getAllByTypeAndScope("share_room_key", this._room.id);
|
|
|
|
}
|
|
|
|
for (const operation of operations) {
|
|
|
|
// just to be sure
|
|
|
|
if (operation.type !== "share_room_key") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const devices = await this._deviceTracker.devicesForRoomMembers(this._room.id, operation.userIds, hsApi);
|
|
|
|
await this._sendRoomKey(operation.roomKeyMessage, devices, hsApi);
|
|
|
|
const removeTxn = await this._storage.readWriteTxn([this._storage.storeNames.operations]);
|
|
|
|
try {
|
|
|
|
removeTxn.operations.remove(operation.id);
|
|
|
|
} catch (err) {
|
|
|
|
removeTxn.abort();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
await removeTxn.complete();
|
2020-09-08 14:24:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _sendRoomKey(roomKeyMessage, devices, hsApi) {
|
|
|
|
const messages = await this._olmEncryption.encrypt(
|
|
|
|
"m.room_key", roomKeyMessage, devices, hsApi);
|
|
|
|
await this._sendMessagesToDevices(ENCRYPTED_TYPE, messages, hsApi);
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:36:17 +02:00
|
|
|
async _sendMessagesToDevices(type, messages, hsApi) {
|
|
|
|
const messagesByUser = groupBy(messages, message => message.device.userId);
|
|
|
|
const payload = {
|
|
|
|
messages: Array.from(messagesByUser.entries()).reduce((userMap, [userId, messages]) => {
|
|
|
|
userMap[userId] = messages.reduce((deviceMap, message) => {
|
|
|
|
deviceMap[message.device.deviceId] = message.content;
|
|
|
|
return deviceMap;
|
|
|
|
}, {});
|
|
|
|
return userMap;
|
|
|
|
}, {})
|
|
|
|
};
|
|
|
|
const txnId = makeTxnId();
|
|
|
|
await hsApi.sendToDevice(type, payload, txnId).response();
|
|
|
|
}
|
2020-09-18 13:08:18 +02:00
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._disposed = true;
|
|
|
|
}
|
2020-09-03 15:36:17 +02:00
|
|
|
}
|
2020-09-10 12:09:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wrappers around megolm decryption classes to be able to post-process
|
|
|
|
* the decryption results before turning them
|
|
|
|
*/
|
|
|
|
class DecryptionPreparation {
|
|
|
|
constructor(megolmDecryptionPreparation, extraErrors, flags, roomEncryption) {
|
|
|
|
this._megolmDecryptionPreparation = megolmDecryptionPreparation;
|
|
|
|
this._extraErrors = extraErrors;
|
|
|
|
this._flags = flags;
|
|
|
|
this._roomEncryption = roomEncryption;
|
|
|
|
}
|
|
|
|
|
|
|
|
async decrypt() {
|
|
|
|
return new DecryptionChanges(
|
|
|
|
await this._megolmDecryptionPreparation.decrypt(),
|
|
|
|
this._extraErrors,
|
|
|
|
this._flags,
|
|
|
|
this._roomEncryption);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2020-09-10 16:40:30 +02:00
|
|
|
this._megolmDecryptionPreparation.dispose();
|
2020-09-10 12:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DecryptionChanges {
|
|
|
|
constructor(megolmDecryptionChanges, extraErrors, flags, roomEncryption) {
|
|
|
|
this._megolmDecryptionChanges = megolmDecryptionChanges;
|
|
|
|
this._extraErrors = extraErrors;
|
|
|
|
this._flags = flags;
|
|
|
|
this._roomEncryption = roomEncryption;
|
|
|
|
}
|
|
|
|
|
|
|
|
async write(txn) {
|
|
|
|
const {results, errors} = await this._megolmDecryptionChanges.write(txn);
|
|
|
|
mergeMap(this._extraErrors, errors);
|
|
|
|
await this._roomEncryption._processDecryptionResults(results, errors, this._flags, txn);
|
|
|
|
return new BatchDecryptionResult(results, errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BatchDecryptionResult {
|
|
|
|
constructor(results, errors) {
|
|
|
|
this.results = results;
|
|
|
|
this.errors = errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyToEntries(entries) {
|
|
|
|
for (const entry of entries) {
|
|
|
|
const result = this.results.get(entry.id);
|
|
|
|
if (result) {
|
|
|
|
entry.setDecryptionResult(result);
|
|
|
|
} else {
|
|
|
|
const error = this.errors.get(entry.id);
|
|
|
|
if (error) {
|
|
|
|
entry.setDecryptionError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|