Convert index.js to index.ts

This commit is contained in:
RMidhunSuresh 2021-11-29 13:53:06 +05:30
parent f9f59fec39
commit 9b8ab9fd8d
2 changed files with 27 additions and 16 deletions

View File

@ -16,7 +16,7 @@ limitations under the License.
import type {Platform} from "../../platform/web/Platform.js"; import type {Platform} from "../../platform/web/Platform.js";
interface IKeyDescription { export interface IKeyDescription {
algorithm: string; algorithm: string;
passphrase: { passphrase: {
algorithm: string; algorithm: string;

View File

@ -18,13 +18,24 @@ import {KeyDescription, Key} from "./common";
import {keyFromPassphrase} from "./passphrase.js"; import {keyFromPassphrase} from "./passphrase.js";
import {keyFromRecoveryKey} from "./recoveryKey.js"; import {keyFromRecoveryKey} from "./recoveryKey.js";
import {SESSION_E2EE_KEY_PREFIX} from "../e2ee/common.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`; 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<KeyDescription | undefined> {
const txn = await storage.readTxn([ const txn = await storage.readTxn([
storage.storeNames.accountData storage.storeNames.accountData
]); ]);
@ -37,30 +48,30 @@ async function readDefaultKeyDescription(storage) {
if (!keyAccountData) { if (!keyAccountData) {
return; 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<void> {
txn.session.set(SSSS_KEY, {id: key.id, binaryKey: key.binaryKey}); txn.session.set(SSSS_KEY, {id: key.id, binaryKey: key.binaryKey});
} }
export async function readKey(txn) { export async function readKey(txn: Transaction): Promise<Key | undefined> {
const keyData = await txn.session.get(SSSS_KEY); const keyData = await txn.session.get(SSSS_KEY);
if (!keyData) { if (!keyData) {
return; return;
} }
const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${keyData.id}`); const keyAccountData = await txn.accountData.get(`m.secret_storage.key.${keyData.id}`);
if (keyAccountData) { 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) { export async function removeKey(txn: Transaction): Promise<void> {
await txn.session.remove(SSSS_KEY); 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<Key> {
const keyDescription = await readDefaultKeyDescription(storage); const keyDescription = await readDefaultKeyDescription(storage);
if (!keyDescription) { if (!keyDescription) {
throw new Error("Could not find a default secret storage key in account data"); 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); return await keyFromCredentialAndDescription(type, credential, keyDescription, platform, olm);
} }
export async function keyFromCredentialAndDescription(type, credential, keyDescription, platform, olm) { export async function keyFromCredentialAndDescription(type: KeyType, credential: string, keyDescription: KeyDescription, platform: Platform, olm: Olm): Promise<Key> {
let key; let key: Key;
if (type === KeyType.Passphrase) { if (type === KeyType.Passphrase) {
key = await keyFromPassphrase(keyDescription, credential, platform); key = await keyFromPassphrase(keyDescription, credential, platform);
} else if (type === KeyType.RecoveryKey) { } else if (type === KeyType.RecoveryKey) {
@ -80,9 +91,9 @@ export async function keyFromCredentialAndDescription(type, credential, keyDescr
return key; return key;
} }
export async function keyFromDehydratedDeviceKey(key, storage, platform) { export async function keyFromDehydratedDeviceKey(key: Key, storage: Storage, platform: Platform): Promise<Key | undefined> {
const keyDescription = await readDefaultKeyDescription(storage); const keyDescription = await readDefaultKeyDescription(storage);
if (await keyDescription.isCompatible(key, platform)) { if (await keyDescription?.isCompatible(key, platform)) {
return key.withDescription(keyDescription); return key.withDescription(keyDescription!);
} }
} }