From 78e0bb1ff0feac3f545ad0921ac8cd57fadd3f0e Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 18 Feb 2022 17:00:56 +0100 Subject: [PATCH] replace isPreKeyMessage with const enum --- src/matrix/e2ee/olm/Decryption.ts | 13 +++++-------- src/matrix/e2ee/olm/types.ts | 7 ++++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/matrix/e2ee/olm/Decryption.ts b/src/matrix/e2ee/olm/Decryption.ts index 77990586..9698add9 100644 --- a/src/matrix/e2ee/olm/Decryption.ts +++ b/src/matrix/e2ee/olm/Decryption.ts @@ -19,6 +19,7 @@ import {groupBy} from "../../../utils/groupBy"; import {MultiLock, ILock} from "../../../utils/Lock"; import {Session} from "./Session"; import {DecryptionResult} from "../DecryptionResult"; +import {OlmPayloadType} from "./types"; import type {OlmMessage, OlmPayload} from "./types"; import type {Account} from "../Account"; @@ -42,10 +43,6 @@ type CreateAndDecryptResult = { plaintext: string }; -function isPreKeyMessage(message: OlmMessage): boolean { - return message.type === 0; -} - function sortSessions(sessions: Session[]) { sessions.sort((a, b) => { return b.data.lastUsed - a.data.lastUsed; @@ -151,7 +148,7 @@ export class Decryption { throw new DecryptionError("OLM_BAD_ENCRYPTED_MESSAGE", event, {senderKey, error: err.message}); } // could not decrypt with any existing session - if (typeof plaintext !== "string" && isPreKeyMessage(message)) { + if (typeof plaintext !== "string" && message.type === OlmPayloadType.PreKey) { let createResult: CreateAndDecryptResult; try { createResult = this._createSessionAndDecrypt(senderKey, message, timestamp); @@ -282,16 +279,16 @@ class SenderKeyDecryption { } const olmSession = session.load(); try { - if (isPreKeyMessage(message) && !olmSession.matches_inbound(message.body)) { + if (message.type === OlmPayloadType.PreKey && !olmSession.matches_inbound(message.body)) { return; } try { - const plaintext = olmSession.decrypt(message.type, message.body); + const plaintext = olmSession.decrypt(message.type as number, message.body!); session.save(olmSession); session.data.lastUsed = this.timestamp; return plaintext; } catch (err) { - if (isPreKeyMessage(message)) { + if (message.type === OlmPayloadType.PreKey) { throw new Error(`Error decrypting prekey message with existing session id ${session.id}: ${err.message}`); } // decryption failed, bail out diff --git a/src/matrix/e2ee/olm/types.ts b/src/matrix/e2ee/olm/types.ts index b9e394d5..5302dad8 100644 --- a/src/matrix/e2ee/olm/types.ts +++ b/src/matrix/e2ee/olm/types.ts @@ -14,8 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ +export const enum OlmPayloadType { + PreKey = 0, + Normal = 1 +} + export type OlmMessage = { - type?: 0 | 1, + type?: OlmPayloadType, body?: string }