replace isPreKeyMessage with const enum

This commit is contained in:
Bruno Windels 2022-02-18 17:00:56 +01:00
parent 347edb5988
commit 78e0bb1ff0
2 changed files with 11 additions and 9 deletions

View File

@ -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

View File

@ -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
}