From f9f59fec39133f91aa2aeda33288516f5622c3a9 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 25 Nov 2021 17:25:00 +0530 Subject: [PATCH 01/11] Convert common.js to ts --- src/matrix/e2ee/Dehydration.js | 2 +- src/matrix/ssss/{common.js => common.ts} | 43 +++++++++++++++++------- src/matrix/ssss/index.js | 2 +- src/matrix/ssss/passphrase.js | 2 +- src/matrix/ssss/recoveryKey.js | 2 +- 5 files changed, 35 insertions(+), 16 deletions(-) rename src/matrix/ssss/{common.js => common.ts} (71%) diff --git a/src/matrix/e2ee/Dehydration.js b/src/matrix/e2ee/Dehydration.js index 461e6c42..1f51c054 100644 --- a/src/matrix/e2ee/Dehydration.js +++ b/src/matrix/e2ee/Dehydration.js @@ -15,7 +15,7 @@ limitations under the License. */ const DEHYDRATION_LIBOLM_PICKLE_ALGORITHM = "org.matrix.msc2697.v1.olm.libolm_pickle"; -import {KeyDescription} from "../ssss/common.js"; +import {KeyDescription} from "../ssss/common"; import {keyFromCredentialAndDescription} from "../ssss/index.js"; export async function getDehydratedDevice(hsApi, olm, platform, log) { diff --git a/src/matrix/ssss/common.js b/src/matrix/ssss/common.ts similarity index 71% rename from src/matrix/ssss/common.js rename to src/matrix/ssss/common.ts index 406e8558..97653673 100644 --- a/src/matrix/ssss/common.js +++ b/src/matrix/ssss/common.ts @@ -14,25 +14,41 @@ See the License for the specific language governing permissions and limitations under the License. */ +import type {Platform} from "../../platform/web/Platform.js"; + +interface IKeyDescription { + algorithm: string; + passphrase: { + algorithm: string; + iterations: number; + salt: string; + }; + mac: string; + iv: string; +} + export class KeyDescription { - constructor(id, keyDescription) { + private readonly _id: string; + private readonly _keyDescription: IKeyDescription; + + constructor(id: string, keyDescription: IKeyDescription) { this._id = id; this._keyDescription = keyDescription; } - get id() { + get id(): string { return this._id; } - get passphraseParams() { + get passphraseParams(): IKeyDescription["passphrase"] { return this._keyDescription?.passphrase; } - get algorithm() { + get algorithm(): string { return this._keyDescription?.algorithm; } - async isCompatible(key, platform) { + async isCompatible(key: Key, platform: Platform): Promise { if (this.algorithm === "m.secret_storage.v1.aes-hmac-sha2") { const kd = this._keyDescription; if (kd.mac) { @@ -53,33 +69,36 @@ export class KeyDescription { } export class Key { - constructor(keyDescription, binaryKey) { + private readonly _keyDescription: KeyDescription; + private readonly _binaryKey: Uint8Array; + + constructor(keyDescription: KeyDescription, binaryKey: Uint8Array) { this._keyDescription = keyDescription; this._binaryKey = binaryKey; } - withDescription(description) { + withDescription(description: KeyDescription): Key { return new Key(description, this._binaryKey); } - get description() { + get description(): KeyDescription { return this._keyDescription; } - get id() { + get id(): string { return this._keyDescription.id; } - get binaryKey() { + get binaryKey(): Uint8Array { return this._binaryKey; } - get algorithm() { + get algorithm(): string { return this._keyDescription.algorithm; } } -async function calculateKeyMac(key, ivStr, platform) { +async function calculateKeyMac(key: BufferSource, ivStr: string, platform: Platform): Promise { const {crypto, encoding} = platform; const {utf8, base64} = encoding; const {derive, aes, hmac} = crypto; diff --git a/src/matrix/ssss/index.js b/src/matrix/ssss/index.js index b063ab0b..0a4783b1 100644 --- a/src/matrix/ssss/index.js +++ b/src/matrix/ssss/index.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {KeyDescription, Key} from "./common.js"; +import {KeyDescription, Key} from "./common"; import {keyFromPassphrase} from "./passphrase.js"; import {keyFromRecoveryKey} from "./recoveryKey.js"; import {SESSION_E2EE_KEY_PREFIX} from "../e2ee/common.js"; diff --git a/src/matrix/ssss/passphrase.js b/src/matrix/ssss/passphrase.js index 681e4548..9460e4db 100644 --- a/src/matrix/ssss/passphrase.js +++ b/src/matrix/ssss/passphrase.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {Key} from "./common.js"; +import {Key} from "./common"; const DEFAULT_ITERATIONS = 500000; const DEFAULT_BITSIZE = 256; diff --git a/src/matrix/ssss/recoveryKey.js b/src/matrix/ssss/recoveryKey.js index bfe132a4..b1a746bc 100644 --- a/src/matrix/ssss/recoveryKey.js +++ b/src/matrix/ssss/recoveryKey.js @@ -13,7 +13,7 @@ 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. */ -import {Key} from "./common.js"; +import {Key} from "./common"; const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01]; From 9b8ab9fd8da2049a7e584802c5bc0a9a39c05b7c Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 29 Nov 2021 13:53:06 +0530 Subject: [PATCH 02/11] Convert index.js to index.ts --- src/matrix/ssss/common.ts | 2 +- src/matrix/ssss/{index.js => index.ts} | 41 ++++++++++++++++---------- 2 files changed, 27 insertions(+), 16 deletions(-) rename src/matrix/ssss/{index.js => index.ts} (62%) diff --git a/src/matrix/ssss/common.ts b/src/matrix/ssss/common.ts index 97653673..c4d0adeb 100644 --- a/src/matrix/ssss/common.ts +++ b/src/matrix/ssss/common.ts @@ -16,7 +16,7 @@ limitations under the License. import type {Platform} from "../../platform/web/Platform.js"; -interface IKeyDescription { +export interface IKeyDescription { algorithm: string; passphrase: { algorithm: string; diff --git a/src/matrix/ssss/index.js b/src/matrix/ssss/index.ts similarity index 62% rename from src/matrix/ssss/index.js rename to src/matrix/ssss/index.ts index 0a4783b1..f0c72e0f 100644 --- a/src/matrix/ssss/index.js +++ b/src/matrix/ssss/index.ts @@ -18,13 +18,24 @@ import {KeyDescription, Key} from "./common"; import {keyFromPassphrase} from "./passphrase.js"; import {keyFromRecoveryKey} from "./recoveryKey.js"; import {SESSION_E2EE_KEY_PREFIX} from "../e2ee/common.js"; -import {createEnum} from "../../utils/enum"; +import type {Storage} from "../storage/idb/Storage"; +import type {Transaction} from "../storage/idb/Transaction"; +import type {IKeyDescription} from "./common"; +import type {Platform} from "../../platform/web/Platform.js"; + +type Olm = { + PRIVATE_KEY_LENGTH: number; + [key: string]: any +}; const SSSS_KEY = `${SESSION_E2EE_KEY_PREFIX}ssssKey`; -export const KeyType = createEnum("RecoveryKey", "Passphrase"); +export enum KeyType { + "RecoveryKey", + "Passphrase" +} -async function readDefaultKeyDescription(storage) { +async function readDefaultKeyDescription(storage: Storage): Promise { const txn = await storage.readTxn([ storage.storeNames.accountData ]); @@ -37,30 +48,30 @@ async function readDefaultKeyDescription(storage) { if (!keyAccountData) { return; } - return new KeyDescription(id, keyAccountData.content); + return new KeyDescription(id, keyAccountData.content as IKeyDescription); } -export async function writeKey(key, txn) { +export async function writeKey(key: Key, txn: Transaction): Promise { txn.session.set(SSSS_KEY, {id: key.id, binaryKey: key.binaryKey}); } -export async function readKey(txn) { +export async function readKey(txn: Transaction): Promise { const keyData = await txn.session.get(SSSS_KEY); if (!keyData) { return; } const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${keyData.id}`); if (keyAccountData) { - return new Key(new KeyDescription(keyData.id, keyAccountData.content), keyData.binaryKey); + return new Key(new KeyDescription(keyData.id, keyAccountData.content as IKeyDescription), keyData.binaryKey); } } -export async function removeKey(txn) { - await txn.session.remove(SSSS_KEY); +export async function removeKey(txn: Transaction): Promise { + txn.session.remove(SSSS_KEY); } -export async function keyFromCredential(type, credential, storage, platform, olm) { +export async function keyFromCredential(type: KeyType, credential: string, storage: Storage, platform: Platform, olm: Olm): Promise { const keyDescription = await readDefaultKeyDescription(storage); if (!keyDescription) { throw new Error("Could not find a default secret storage key in account data"); @@ -68,8 +79,8 @@ export async function keyFromCredential(type, credential, storage, platform, olm return await keyFromCredentialAndDescription(type, credential, keyDescription, platform, olm); } -export async function keyFromCredentialAndDescription(type, credential, keyDescription, platform, olm) { - let key; +export async function keyFromCredentialAndDescription(type: KeyType, credential: string, keyDescription: KeyDescription, platform: Platform, olm: Olm): Promise { + let key: Key; if (type === KeyType.Passphrase) { key = await keyFromPassphrase(keyDescription, credential, platform); } else if (type === KeyType.RecoveryKey) { @@ -80,9 +91,9 @@ export async function keyFromCredentialAndDescription(type, credential, keyDescr return key; } -export async function keyFromDehydratedDeviceKey(key, storage, platform) { +export async function keyFromDehydratedDeviceKey(key: Key, storage: Storage, platform: Platform): Promise { const keyDescription = await readDefaultKeyDescription(storage); - if (await keyDescription.isCompatible(key, platform)) { - return key.withDescription(keyDescription); + if (await keyDescription?.isCompatible(key, platform)) { + return key.withDescription(keyDescription!); } } From dd4704b818894857af62b67606706a4766bc72af Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 29 Nov 2021 13:54:44 +0530 Subject: [PATCH 03/11] Fix imports --- src/domain/AccountSetupViewModel.js | 2 +- src/domain/session/settings/SessionBackupViewModel.js | 2 +- src/matrix/Session.js | 2 +- src/matrix/e2ee/Dehydration.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/domain/AccountSetupViewModel.js b/src/domain/AccountSetupViewModel.js index 7930b87d..b2ce808a 100644 --- a/src/domain/AccountSetupViewModel.js +++ b/src/domain/AccountSetupViewModel.js @@ -15,7 +15,7 @@ limitations under the License. */ import {ViewModel} from "./ViewModel.js"; -import {KeyType} from "../matrix/ssss/index.js"; +import {KeyType} from "../matrix/ssss/index"; import {Status} from "./session/settings/SessionBackupViewModel.js"; export class AccountSetupViewModel extends ViewModel { diff --git a/src/domain/session/settings/SessionBackupViewModel.js b/src/domain/session/settings/SessionBackupViewModel.js index 52be43b4..5a127904 100644 --- a/src/domain/session/settings/SessionBackupViewModel.js +++ b/src/domain/session/settings/SessionBackupViewModel.js @@ -15,7 +15,7 @@ limitations under the License. */ import {ViewModel} from "../../ViewModel.js"; -import {KeyType} from "../../../matrix/ssss/index.js"; +import {KeyType} from "../../../matrix/ssss/index"; import {createEnum} from "../../../utils/enum"; export const Status = createEnum("Enabled", "SetupKey", "SetupPhrase", "Pending"); diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 17c0cf94..e3f3cda3 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -42,7 +42,7 @@ import { writeKey as ssssWriteKey, removeKey as ssssRemoveKey, keyFromDehydratedDeviceKey as createSSSSKeyFromDehydratedDeviceKey -} from "./ssss/index.js"; +} from "./ssss/index"; import {SecretStorage} from "./ssss/SecretStorage.js"; import {ObservableValue, RetainedObservableValue} from "../observable/ObservableValue"; diff --git a/src/matrix/e2ee/Dehydration.js b/src/matrix/e2ee/Dehydration.js index 1f51c054..a14c40ce 100644 --- a/src/matrix/e2ee/Dehydration.js +++ b/src/matrix/e2ee/Dehydration.js @@ -16,7 +16,7 @@ limitations under the License. const DEHYDRATION_LIBOLM_PICKLE_ALGORITHM = "org.matrix.msc2697.v1.olm.libolm_pickle"; import {KeyDescription} from "../ssss/common"; -import {keyFromCredentialAndDescription} from "../ssss/index.js"; +import {keyFromCredentialAndDescription} from "../ssss/index"; export async function getDehydratedDevice(hsApi, olm, platform, log) { try { From e45f66a199d8b4f2948c5829adf28061abb6e2af Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 29 Nov 2021 15:18:04 +0530 Subject: [PATCH 04/11] Convert passphrase.js to ts --- src/matrix/ssss/common.ts | 1 + src/matrix/ssss/index.ts | 2 +- src/matrix/ssss/{passphrase.js => passphrase.ts} | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) rename src/matrix/ssss/{passphrase.js => passphrase.ts} (86%) diff --git a/src/matrix/ssss/common.ts b/src/matrix/ssss/common.ts index c4d0adeb..b7c06192 100644 --- a/src/matrix/ssss/common.ts +++ b/src/matrix/ssss/common.ts @@ -22,6 +22,7 @@ export interface IKeyDescription { algorithm: string; iterations: number; salt: string; + bits?: number; }; mac: string; iv: string; diff --git a/src/matrix/ssss/index.ts b/src/matrix/ssss/index.ts index f0c72e0f..15e95200 100644 --- a/src/matrix/ssss/index.ts +++ b/src/matrix/ssss/index.ts @@ -15,7 +15,7 @@ limitations under the License. */ import {KeyDescription, Key} from "./common"; -import {keyFromPassphrase} from "./passphrase.js"; +import {keyFromPassphrase} from "./passphrase"; import {keyFromRecoveryKey} from "./recoveryKey.js"; import {SESSION_E2EE_KEY_PREFIX} from "../e2ee/common.js"; import type {Storage} from "../storage/idb/Storage"; diff --git a/src/matrix/ssss/passphrase.js b/src/matrix/ssss/passphrase.ts similarity index 86% rename from src/matrix/ssss/passphrase.js rename to src/matrix/ssss/passphrase.ts index 9460e4db..00e4801e 100644 --- a/src/matrix/ssss/passphrase.js +++ b/src/matrix/ssss/passphrase.ts @@ -15,6 +15,8 @@ limitations under the License. */ import {Key} from "./common"; +import type {KeyDescription} from "./common"; +import type {Platform} from "../../platform/web/Platform.js"; const DEFAULT_ITERATIONS = 500000; const DEFAULT_BITSIZE = 256; @@ -25,7 +27,7 @@ const DEFAULT_BITSIZE = 256; * @param {Platform} platform * @return {Key} */ -export async function keyFromPassphrase(keyDescription, passphrase, platform) { +export async function keyFromPassphrase(keyDescription: KeyDescription, passphrase: string, platform: Platform): Promise { const {passphraseParams} = keyDescription; if (!passphraseParams) { throw new Error("not a passphrase key"); From 814c0bed2e0ca4f47aab56834444d62d29077fdd Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 29 Nov 2021 15:54:38 +0530 Subject: [PATCH 05/11] Convert recoveryKey.js to ts --- src/matrix/ssss/index.ts | 8 ++------ src/matrix/ssss/{recoveryKey.js => recoveryKey.ts} | 11 +++++++++-- 2 files changed, 11 insertions(+), 8 deletions(-) rename src/matrix/ssss/{recoveryKey.js => recoveryKey.ts} (80%) diff --git a/src/matrix/ssss/index.ts b/src/matrix/ssss/index.ts index 15e95200..fa09228d 100644 --- a/src/matrix/ssss/index.ts +++ b/src/matrix/ssss/index.ts @@ -16,17 +16,13 @@ limitations under the License. import {KeyDescription, Key} from "./common"; import {keyFromPassphrase} from "./passphrase"; -import {keyFromRecoveryKey} from "./recoveryKey.js"; +import {keyFromRecoveryKey} from "./recoveryKey"; import {SESSION_E2EE_KEY_PREFIX} from "../e2ee/common.js"; import type {Storage} from "../storage/idb/Storage"; import type {Transaction} from "../storage/idb/Transaction"; import type {IKeyDescription} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; - -type Olm = { - PRIVATE_KEY_LENGTH: number; - [key: string]: any -}; +import type { Olm } from "./recoveryKey"; const SSSS_KEY = `${SESSION_E2EE_KEY_PREFIX}ssssKey`; diff --git a/src/matrix/ssss/recoveryKey.js b/src/matrix/ssss/recoveryKey.ts similarity index 80% rename from src/matrix/ssss/recoveryKey.js rename to src/matrix/ssss/recoveryKey.ts index b1a746bc..1271a814 100644 --- a/src/matrix/ssss/recoveryKey.js +++ b/src/matrix/ssss/recoveryKey.ts @@ -14,8 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ import {Key} from "./common"; +import {KeyDescription} from "./common"; +import type {Platform} from "../../platform/web/Platform.js"; -const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01]; +export type Olm = { + PRIVATE_KEY_LENGTH: number; + [key: string]: any; +}; + +const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01] as const; /** * @param {Olm} olm @@ -23,7 +30,7 @@ const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01]; * @param {string} recoveryKey * @return {Key} */ -export function keyFromRecoveryKey(keyDescription, recoveryKey, olm, platform) { +export function keyFromRecoveryKey(keyDescription: KeyDescription, recoveryKey: string, olm: Olm, platform: Platform): Key { const result = platform.encoding.base58.decode(recoveryKey.replace(/ /g, '')); let parity = 0; From b2efcb9515699a00ed36ef0f412421e38478daef Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 29 Nov 2021 17:05:15 +0530 Subject: [PATCH 06/11] Convert SecretStorage.js to ts --- src/matrix/Session.js | 2 +- .../ssss/{SecretStorage.js => SecretStorage.ts} | 12 +++++++++--- src/matrix/ssss/index.ts | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) rename src/matrix/ssss/{SecretStorage.js => SecretStorage.ts} (83%) diff --git a/src/matrix/Session.js b/src/matrix/Session.js index e3f3cda3..49151252 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -43,7 +43,7 @@ import { removeKey as ssssRemoveKey, keyFromDehydratedDeviceKey as createSSSSKeyFromDehydratedDeviceKey } from "./ssss/index"; -import {SecretStorage} from "./ssss/SecretStorage.js"; +import {SecretStorage} from "./ssss/SecretStorage"; import {ObservableValue, RetainedObservableValue} from "../observable/ObservableValue"; const PICKLE_KEY = "DEFAULT_KEY"; diff --git a/src/matrix/ssss/SecretStorage.js b/src/matrix/ssss/SecretStorage.ts similarity index 83% rename from src/matrix/ssss/SecretStorage.js rename to src/matrix/ssss/SecretStorage.ts index 04597084..3bbbc070 100644 --- a/src/matrix/ssss/SecretStorage.js +++ b/src/matrix/ssss/SecretStorage.ts @@ -13,14 +13,20 @@ 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. */ +import type {Key} from "./common"; +import type {Platform} from "../../platform/web/Platform.js"; +import type {Transaction} from "../storage/idb/Transaction"; export class SecretStorage { - constructor({key, platform}) { + private readonly _key: Key; + private readonly _platform: Platform; + + constructor({key, platform}: {key: Key, platform: Platform}) { this._key = key; this._platform = platform; } - async readSecret(name, txn) { + async readSecret(name: string, txn: Transaction): Promise { const accountData = await txn.accountData.get(name); if (!accountData) { return; @@ -37,7 +43,7 @@ export class SecretStorage { } } - async _decryptAESSecret(type, encryptedData) { + async _decryptAESSecret(type: string, encryptedData: any): Promise { const {base64, utf8} = this._platform.encoding; // now derive the aes and mac key from the 4s key const hkdfKey = await this._platform.crypto.derive.hkdf( diff --git a/src/matrix/ssss/index.ts b/src/matrix/ssss/index.ts index fa09228d..c82729f2 100644 --- a/src/matrix/ssss/index.ts +++ b/src/matrix/ssss/index.ts @@ -22,7 +22,7 @@ import type {Storage} from "../storage/idb/Storage"; import type {Transaction} from "../storage/idb/Transaction"; import type {IKeyDescription} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; -import type { Olm } from "./recoveryKey"; +import type {Olm} from "./recoveryKey"; const SSSS_KEY = `${SESSION_E2EE_KEY_PREFIX}ssssKey`; From d2c09933c7910588aeba6f5379b984010fbef12e Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 2 Dec 2021 11:57:20 +0530 Subject: [PATCH 07/11] Type encrypted data --- src/matrix/ssss/SecretStorage.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/matrix/ssss/SecretStorage.ts b/src/matrix/ssss/SecretStorage.ts index 3bbbc070..c026b453 100644 --- a/src/matrix/ssss/SecretStorage.ts +++ b/src/matrix/ssss/SecretStorage.ts @@ -17,6 +17,12 @@ import type {Key} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; import type {Transaction} from "../storage/idb/Transaction"; +type EncryptedData = { + iv: string; + ciphertext: string; + mac: string; +} + export class SecretStorage { private readonly _key: Key; private readonly _platform: Platform; @@ -31,7 +37,7 @@ export class SecretStorage { if (!accountData) { return; } - const encryptedData = accountData?.content?.encrypted?.[this._key.id]; + const encryptedData = accountData?.content?.encrypted?.[this._key.id] as EncryptedData; if (!encryptedData) { throw new Error(`Secret ${accountData.type} is not encrypted for key ${this._key.id}`); } @@ -43,7 +49,7 @@ export class SecretStorage { } } - async _decryptAESSecret(type: string, encryptedData: any): Promise { + async _decryptAESSecret(type: string, encryptedData: EncryptedData): Promise { const {base64, utf8} = this._platform.encoding; // now derive the aes and mac key from the 4s key const hkdfKey = await this._platform.crypto.derive.hkdf( From 7362e38413359bbd2398dadceeeacb906c65a409 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 2 Dec 2021 12:01:03 +0530 Subject: [PATCH 08/11] Convert interface to type --- src/matrix/ssss/common.ts | 8 ++++---- src/matrix/ssss/index.ts | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/matrix/ssss/common.ts b/src/matrix/ssss/common.ts index b7c06192..2bb5e8b5 100644 --- a/src/matrix/ssss/common.ts +++ b/src/matrix/ssss/common.ts @@ -16,7 +16,7 @@ limitations under the License. import type {Platform} from "../../platform/web/Platform.js"; -export interface IKeyDescription { +export type KeyDescriptionData = { algorithm: string; passphrase: { algorithm: string; @@ -30,9 +30,9 @@ export interface IKeyDescription { export class KeyDescription { private readonly _id: string; - private readonly _keyDescription: IKeyDescription; + private readonly _keyDescription: KeyDescriptionData; - constructor(id: string, keyDescription: IKeyDescription) { + constructor(id: string, keyDescription: KeyDescriptionData) { this._id = id; this._keyDescription = keyDescription; } @@ -41,7 +41,7 @@ export class KeyDescription { return this._id; } - get passphraseParams(): IKeyDescription["passphrase"] { + get passphraseParams(): KeyDescriptionData["passphrase"] { return this._keyDescription?.passphrase; } diff --git a/src/matrix/ssss/index.ts b/src/matrix/ssss/index.ts index c82729f2..8c2d126b 100644 --- a/src/matrix/ssss/index.ts +++ b/src/matrix/ssss/index.ts @@ -20,7 +20,7 @@ import {keyFromRecoveryKey} from "./recoveryKey"; import {SESSION_E2EE_KEY_PREFIX} from "../e2ee/common.js"; import type {Storage} from "../storage/idb/Storage"; import type {Transaction} from "../storage/idb/Transaction"; -import type {IKeyDescription} from "./common"; +import type {KeyDescriptionData} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; import type {Olm} from "./recoveryKey"; @@ -44,7 +44,7 @@ async function readDefaultKeyDescription(storage: Storage): Promise { @@ -58,7 +58,7 @@ export async function readKey(txn: Transaction): Promise { } const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${keyData.id}`); if (keyAccountData) { - return new Key(new KeyDescription(keyData.id, keyAccountData.content as IKeyDescription), keyData.binaryKey); + return new Key(new KeyDescription(keyData.id, keyAccountData.content as KeyDescriptionData), keyData.binaryKey); } } From e06a0e9e5a1261cfa548f45ad43e68b66318658a Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 2 Dec 2021 12:23:22 +0530 Subject: [PATCH 09/11] Use olm type from dependency --- src/matrix/ssss/index.ts | 6 +++--- src/matrix/ssss/recoveryKey.ts | 8 ++------ tsconfig.json | 3 ++- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/matrix/ssss/index.ts b/src/matrix/ssss/index.ts index 8c2d126b..6443d70a 100644 --- a/src/matrix/ssss/index.ts +++ b/src/matrix/ssss/index.ts @@ -22,7 +22,7 @@ import type {Storage} from "../storage/idb/Storage"; import type {Transaction} from "../storage/idb/Transaction"; import type {KeyDescriptionData} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; -import type {Olm} from "./recoveryKey"; +import type * as Olm from "@matrix-org/olm" const SSSS_KEY = `${SESSION_E2EE_KEY_PREFIX}ssssKey`; @@ -67,7 +67,7 @@ export async function removeKey(txn: Transaction): Promise { txn.session.remove(SSSS_KEY); } -export async function keyFromCredential(type: KeyType, credential: string, storage: Storage, platform: Platform, olm: Olm): Promise { +export async function keyFromCredential(type: KeyType, credential: string, storage: Storage, platform: Platform, olm: typeof Olm): Promise { const keyDescription = await readDefaultKeyDescription(storage); if (!keyDescription) { throw new Error("Could not find a default secret storage key in account data"); @@ -75,7 +75,7 @@ export async function keyFromCredential(type: KeyType, credential: string, stora return await keyFromCredentialAndDescription(type, credential, keyDescription, platform, olm); } -export async function keyFromCredentialAndDescription(type: KeyType, credential: string, keyDescription: KeyDescription, platform: Platform, olm: Olm): Promise { +export async function keyFromCredentialAndDescription(type: KeyType, credential: string, keyDescription: KeyDescription, platform: Platform, olm: typeof Olm): Promise { let key: Key; if (type === KeyType.Passphrase) { key = await keyFromPassphrase(keyDescription, credential, platform); diff --git a/src/matrix/ssss/recoveryKey.ts b/src/matrix/ssss/recoveryKey.ts index 1271a814..e194ae52 100644 --- a/src/matrix/ssss/recoveryKey.ts +++ b/src/matrix/ssss/recoveryKey.ts @@ -16,11 +16,7 @@ limitations under the License. import {Key} from "./common"; import {KeyDescription} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; - -export type Olm = { - PRIVATE_KEY_LENGTH: number; - [key: string]: any; -}; +import type * as Olm from "@matrix-org/olm" const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01] as const; @@ -30,7 +26,7 @@ const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01] as const; * @param {string} recoveryKey * @return {Key} */ -export function keyFromRecoveryKey(keyDescription: KeyDescription, recoveryKey: string, olm: Olm, platform: Platform): Key { +export function keyFromRecoveryKey(keyDescription: KeyDescription, recoveryKey: string, olm: typeof Olm, platform: Platform): Key { const result = platform.encoding.base58.decode(recoveryKey.replace(/ /g, '')); let parity = 0; diff --git a/tsconfig.json b/tsconfig.json index e09e7cc5..e3fae938 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,8 @@ "compilerOptions": { "strictNullChecks": true, "noEmit": true, - "target": "ES2020" + "target": "ES2020", + "moduleResolution": "node" }, "exclude": [ "src/sdk/paths/*" From 6dcebde69d23285ae11852124d5405b847f418df Mon Sep 17 00:00:00 2001 From: R Midhun Suresh Date: Fri, 3 Dec 2021 11:29:07 +0530 Subject: [PATCH 10/11] Alias namespace as Olm Co-authored-by: Bruno Windels --- src/matrix/ssss/recoveryKey.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/matrix/ssss/recoveryKey.ts b/src/matrix/ssss/recoveryKey.ts index e194ae52..739f19a9 100644 --- a/src/matrix/ssss/recoveryKey.ts +++ b/src/matrix/ssss/recoveryKey.ts @@ -16,7 +16,8 @@ limitations under the License. import {Key} from "./common"; import {KeyDescription} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; -import type * as Olm from "@matrix-org/olm" +import type * as OlmNamespace from "@matrix-org/olm"; +type Olm = typeof OlmNamespace; const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01] as const; From 9bffd31ee3f3464f9737a33cf24451d5d66f166b Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 3 Dec 2021 11:30:57 +0530 Subject: [PATCH 11/11] Remove typeof --- src/matrix/ssss/index.ts | 8 +++++--- src/matrix/ssss/recoveryKey.ts | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/matrix/ssss/index.ts b/src/matrix/ssss/index.ts index 6443d70a..37f47963 100644 --- a/src/matrix/ssss/index.ts +++ b/src/matrix/ssss/index.ts @@ -22,7 +22,9 @@ import type {Storage} from "../storage/idb/Storage"; import type {Transaction} from "../storage/idb/Transaction"; import type {KeyDescriptionData} from "./common"; import type {Platform} from "../../platform/web/Platform.js"; -import type * as Olm from "@matrix-org/olm" +import type * as OlmNamespace from "@matrix-org/olm" + +type Olm = typeof OlmNamespace; const SSSS_KEY = `${SESSION_E2EE_KEY_PREFIX}ssssKey`; @@ -67,7 +69,7 @@ export async function removeKey(txn: Transaction): Promise { txn.session.remove(SSSS_KEY); } -export async function keyFromCredential(type: KeyType, credential: string, storage: Storage, platform: Platform, olm: typeof Olm): Promise { +export async function keyFromCredential(type: KeyType, credential: string, storage: Storage, platform: Platform, olm: Olm): Promise { const keyDescription = await readDefaultKeyDescription(storage); if (!keyDescription) { throw new Error("Could not find a default secret storage key in account data"); @@ -75,7 +77,7 @@ export async function keyFromCredential(type: KeyType, credential: string, stora return await keyFromCredentialAndDescription(type, credential, keyDescription, platform, olm); } -export async function keyFromCredentialAndDescription(type: KeyType, credential: string, keyDescription: KeyDescription, platform: Platform, olm: typeof Olm): Promise { +export async function keyFromCredentialAndDescription(type: KeyType, credential: string, keyDescription: KeyDescription, platform: Platform, olm: Olm): Promise { let key: Key; if (type === KeyType.Passphrase) { key = await keyFromPassphrase(keyDescription, credential, platform); diff --git a/src/matrix/ssss/recoveryKey.ts b/src/matrix/ssss/recoveryKey.ts index 739f19a9..c619ed37 100644 --- a/src/matrix/ssss/recoveryKey.ts +++ b/src/matrix/ssss/recoveryKey.ts @@ -27,7 +27,7 @@ const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01] as const; * @param {string} recoveryKey * @return {Key} */ -export function keyFromRecoveryKey(keyDescription: KeyDescription, recoveryKey: string, olm: typeof Olm, platform: Platform): Key { +export function keyFromRecoveryKey(keyDescription: KeyDescription, recoveryKey: string, olm: Olm, platform: Platform): Key { const result = platform.encoding.base58.decode(recoveryKey.replace(/ /g, '')); let parity = 0;