diff --git a/packages/core/src/api/key-manager.ts b/packages/core/src/api/key-manager.ts index 43068cf37..b5290b1ad 100644 --- a/packages/core/src/api/key-manager.ts +++ b/packages/core/src/api/key-manager.ts @@ -53,14 +53,14 @@ type WrappedKey = private: Cipher<"base64">; }; -type UnwrapKeyReturnType = T extends { +export type UnwrapKeyReturnType = T extends { public: string; private: Cipher<"base64">; } ? SerializedKeyPair : SerializedKey; -type KeyTypeFromId = +export type KeyTypeFromId = (typeof KEY_INFO)[TId]["type"] extends "symmetric" ? Cipher<"base64"> : { diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.ts index f9aa82d56..f4901ac62 100644 --- a/packages/core/src/api/user-manager.ts +++ b/packages/core/src/api/user-manager.ts @@ -27,7 +27,12 @@ import Database from "./index.js"; import { SerializedKeyPair, SerializedKey } from "@notesnook/crypto"; import { logger } from "../logger.js"; import { KEY_VERSION, KeyVersion } from "./sync/types.js"; -import { KeyId, KeyManager } from "./key-manager.js"; +import { + KeyId, + KeyManager, + KeyTypeFromId, + UnwrapKeyReturnType +} from "./key-manager.js"; const ENDPOINTS = { signup: "/users", @@ -471,13 +476,13 @@ class UserManager { return { key, salt: user.salt }; } - private async getUserKey( - id: KeyId, + private async getUserKey( + id: TId, config: { generateKey: () => Promise; errorContext: string; } - ): Promise { + ): Promise> | undefined> { try { const masterKey = await this.getMasterKey(); if (!masterKey) return; @@ -489,10 +494,13 @@ class UserManager { await this.updateUser({ [id]: await this.keyManager.wrapKey(key, masterKey) }); - return key; + return key as UnwrapKeyReturnType>; } - return await this.keyManager.unwrapKey(wrappedKey, masterKey); + return (await this.keyManager.unwrapKey( + wrappedKey, + masterKey + )) as UnwrapKeyReturnType>; } catch (e) { logger.error(e, `Could not get ${config.errorContext}.`); if (e instanceof Error)