replace isPreKeyMessage with const enum
This commit is contained in:
parent
347edb5988
commit
78e0bb1ff0
2 changed files with 11 additions and 9 deletions
|
@ -19,6 +19,7 @@ import {groupBy} from "../../../utils/groupBy";
|
||||||
import {MultiLock, ILock} from "../../../utils/Lock";
|
import {MultiLock, ILock} from "../../../utils/Lock";
|
||||||
import {Session} from "./Session";
|
import {Session} from "./Session";
|
||||||
import {DecryptionResult} from "../DecryptionResult";
|
import {DecryptionResult} from "../DecryptionResult";
|
||||||
|
import {OlmPayloadType} from "./types";
|
||||||
|
|
||||||
import type {OlmMessage, OlmPayload} from "./types";
|
import type {OlmMessage, OlmPayload} from "./types";
|
||||||
import type {Account} from "../Account";
|
import type {Account} from "../Account";
|
||||||
|
@ -42,10 +43,6 @@ type CreateAndDecryptResult = {
|
||||||
plaintext: string
|
plaintext: string
|
||||||
};
|
};
|
||||||
|
|
||||||
function isPreKeyMessage(message: OlmMessage): boolean {
|
|
||||||
return message.type === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function sortSessions(sessions: Session[]) {
|
function sortSessions(sessions: Session[]) {
|
||||||
sessions.sort((a, b) => {
|
sessions.sort((a, b) => {
|
||||||
return b.data.lastUsed - a.data.lastUsed;
|
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});
|
throw new DecryptionError("OLM_BAD_ENCRYPTED_MESSAGE", event, {senderKey, error: err.message});
|
||||||
}
|
}
|
||||||
// could not decrypt with any existing session
|
// could not decrypt with any existing session
|
||||||
if (typeof plaintext !== "string" && isPreKeyMessage(message)) {
|
if (typeof plaintext !== "string" && message.type === OlmPayloadType.PreKey) {
|
||||||
let createResult: CreateAndDecryptResult;
|
let createResult: CreateAndDecryptResult;
|
||||||
try {
|
try {
|
||||||
createResult = this._createSessionAndDecrypt(senderKey, message, timestamp);
|
createResult = this._createSessionAndDecrypt(senderKey, message, timestamp);
|
||||||
|
@ -282,16 +279,16 @@ class SenderKeyDecryption {
|
||||||
}
|
}
|
||||||
const olmSession = session.load();
|
const olmSession = session.load();
|
||||||
try {
|
try {
|
||||||
if (isPreKeyMessage(message) && !olmSession.matches_inbound(message.body)) {
|
if (message.type === OlmPayloadType.PreKey && !olmSession.matches_inbound(message.body)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const plaintext = olmSession.decrypt(message.type, message.body);
|
const plaintext = olmSession.decrypt(message.type as number, message.body!);
|
||||||
session.save(olmSession);
|
session.save(olmSession);
|
||||||
session.data.lastUsed = this.timestamp;
|
session.data.lastUsed = this.timestamp;
|
||||||
return plaintext;
|
return plaintext;
|
||||||
} catch (err) {
|
} 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}`);
|
throw new Error(`Error decrypting prekey message with existing session id ${session.id}: ${err.message}`);
|
||||||
}
|
}
|
||||||
// decryption failed, bail out
|
// decryption failed, bail out
|
||||||
|
|
|
@ -14,8 +14,13 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export const enum OlmPayloadType {
|
||||||
|
PreKey = 0,
|
||||||
|
Normal = 1
|
||||||
|
}
|
||||||
|
|
||||||
export type OlmMessage = {
|
export type OlmMessage = {
|
||||||
type?: 0 | 1,
|
type?: OlmPayloadType,
|
||||||
body?: string
|
body?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue