Add return type to methods

This commit is contained in:
RMidhunSuresh 2023-06-13 15:38:33 +05:30
parent ba0b0300b4
commit 2922c1691a

View File

@ -77,16 +77,14 @@ export class SecretSharing {
this.crossSigning = options.crossSigning; this.crossSigning = options.crossSigning;
this.logger = options.logger; this.logger = options.logger;
this.aesEncryption = new AESEncryption(this.storage, options.crypto, this.encoding); this.aesEncryption = new AESEncryption(this.storage, options.crypto, this.encoding);
(window as any).foo = this;
this.init();
} }
private async init() { async load(): Promise<void> {
this.deviceMessageHandler.on("message", async ({ encrypted }) => { this.deviceMessageHandler.on("message", async ({ encrypted }) => {
const type: EVENT_TYPE = encrypted?.event.type; const type: EVENT_TYPE = encrypted?.event.type;
switch (type) { switch (type) {
case EVENT_TYPE.REQUEST: { case EVENT_TYPE.REQUEST: {
this._respondToRequest(encrypted); await this._respondToRequest(encrypted);
break; break;
} }
case EVENT_TYPE.SEND: { case EVENT_TYPE.SEND: {
@ -102,9 +100,9 @@ export class SecretSharing {
await this.aesEncryption.load(); await this.aesEncryption.load();
} }
private async _respondToRequest(request) { private async _respondToRequest(request): Promise<void> {
await this.logger.run("SharedSecret.respondToRequest", async (log) => { await this.logger.run("SharedSecret.respondToRequest", async (log) => {
if (!this.shouldRespondToRequest(request, log)) { if (!await this.shouldRespondToRequest(request, log)) {
return; return;
} }
const requestContent = request.event.content; const requestContent = request.event.content;
@ -127,9 +125,7 @@ export class SecretSharing {
} }
const messages = await log.wrap("olm encrypt", log => this.olmEncryption.encrypt( const messages = await log.wrap("olm encrypt", log => this.olmEncryption.encrypt(
EVENT_TYPE.SEND, content, [device], this.hsApi, log)); EVENT_TYPE.SEND, content, [device], this.hsApi, log));
console.log("messages", messages);
const payload = formatToDeviceMessagesPayload(messages); const payload = formatToDeviceMessagesPayload(messages);
console.log("payload", payload);
await this.hsApi.sendToDevice("m.room.encrypted", payload, makeTxnId(), {log}).response(); await this.hsApi.sendToDevice("m.room.encrypted", payload, makeTxnId(), {log}).response();
}); });
} }
@ -176,8 +172,7 @@ export class SecretSharing {
return false; return false;
} }
return true; return true;
}) });
} }
/** /**
@ -185,7 +180,7 @@ export class SecretSharing {
* Returns undefined otherwise. * Returns undefined otherwise.
* @param decryptionResult Encrypted to-device event that contains the secret * @param decryptionResult Encrypted to-device event that contains the secret
*/ */
async shouldAcceptSecret(decryptionResult: DecryptionResult): Promise<string | undefined> { private async shouldAcceptSecret(decryptionResult: DecryptionResult): Promise<string | undefined> {
// 1. Check if we can trust this response // 1. Check if we can trust this response
const crossSigning = this.crossSigning.get(); const crossSigning = this.crossSigning.get();
if (!crossSigning) { if (!crossSigning) {
@ -221,7 +216,7 @@ export class SecretSharing {
} }
} }
async removeStoredRequestId(requestId: string): Promise<void> { private async removeStoredRequestId(requestId: string): Promise<void> {
const txn = await this.storage.readWriteTxn([this.storage.storeNames.session]); const txn = await this.storage.readWriteTxn([this.storage.storeNames.session]);
const storedIds = await txn.session.get(STORAGE_KEY); const storedIds = await txn.session.get(STORAGE_KEY);
if (storedIds) { if (storedIds) {
@ -280,7 +275,7 @@ export class SecretSharing {
return deferred.promise; return deferred.promise;
} }
private async sendRequestForSecret(name: string, request_id: string, log: ILogItem) { private async sendRequestForSecret(name: string, request_id: string, log: ILogItem): Promise<void> {
const content = { const content = {
action: "request", action: "request",
name, name,
@ -291,9 +286,7 @@ export class SecretSharing {
devices = devices.filter(d => d.device_id !== this.deviceTracker.ownDeviceId); devices = devices.filter(d => d.device_id !== this.deviceTracker.ownDeviceId);
const messages = await log.wrap("olm encrypt", log => this.olmEncryption.encrypt( const messages = await log.wrap("olm encrypt", log => this.olmEncryption.encrypt(
EVENT_TYPE.REQUEST, content, devices, this.hsApi, log)); EVENT_TYPE.REQUEST, content, devices, this.hsApi, log));
console.log("messages", messages);
const payload = formatToDeviceMessagesPayload(messages); const payload = formatToDeviceMessagesPayload(messages);
console.log("payload", payload);
await this.hsApi.sendToDevice("m.room.encrypted", payload, makeTxnId(), {log}).response(); await this.hsApi.sendToDevice("m.room.encrypted", payload, makeTxnId(), {log}).response();
} }
@ -333,7 +326,7 @@ class AESEncryption {
constructor(private storage: Storage, private crypto: Crypto, private encoding: Encoding) { }; constructor(private storage: Storage, private crypto: Crypto, private encoding: Encoding) { };
async load() { async load(): Promise<void> {
const storageKey = `${SESSION_E2EE_KEY_PREFIX}localAESKey`; const storageKey = `${SESSION_E2EE_KEY_PREFIX}localAESKey`;
// 1. Check if we're already storing the AES key // 1. Check if we're already storing the AES key
const txn = await this.storage.readTxn([StoreNames.session]); const txn = await this.storage.readTxn([StoreNames.session]);
@ -343,8 +336,8 @@ class AESEncryption {
if (!key) { if (!key) {
/** /**
* Element creates the key as "non-extractable", meaning that it cannot * Element creates the key as "non-extractable", meaning that it cannot
* be exported through the crypto API. But since it is going * be exported through the crypto DOM API. But since it's going
* to end up in local-storage anyway, I don't see a reason to do that. * to end up in indexeddb anyway, it really doesn't matter.
*/ */
key = await this.crypto.aes.generateKey("jwk"); key = await this.crypto.aes.generateKey("jwk");
iv = await this.crypto.aes.generateIV(); iv = await this.crypto.aes.generateIV();