mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-23 19:49:56 +01:00
core: fix types
This commit is contained in:
@@ -53,14 +53,14 @@ type WrappedKey =
|
||||
private: Cipher<"base64">;
|
||||
};
|
||||
|
||||
type UnwrapKeyReturnType<T extends WrappedKey> = T extends {
|
||||
export type UnwrapKeyReturnType<T extends WrappedKey> = T extends {
|
||||
public: string;
|
||||
private: Cipher<"base64">;
|
||||
}
|
||||
? SerializedKeyPair
|
||||
: SerializedKey;
|
||||
|
||||
type KeyTypeFromId<TId extends KeyId> =
|
||||
export type KeyTypeFromId<TId extends KeyId> =
|
||||
(typeof KEY_INFO)[TId]["type"] extends "symmetric"
|
||||
? Cipher<"base64">
|
||||
: {
|
||||
|
||||
@@ -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<TId extends KeyId>(
|
||||
id: TId,
|
||||
config: {
|
||||
generateKey: () => Promise<SerializedKey | SerializedKeyPair>;
|
||||
errorContext: string;
|
||||
}
|
||||
): Promise<SerializedKey | SerializedKeyPair | undefined> {
|
||||
): Promise<UnwrapKeyReturnType<KeyTypeFromId<TId>> | 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<KeyTypeFromId<TId>>;
|
||||
}
|
||||
|
||||
return await this.keyManager.unwrapKey(wrappedKey, masterKey);
|
||||
return (await this.keyManager.unwrapKey(
|
||||
wrappedKey,
|
||||
masterKey
|
||||
)) as UnwrapKeyReturnType<KeyTypeFromId<TId>>;
|
||||
} catch (e) {
|
||||
logger.error(e, `Could not get ${config.errorContext}.`);
|
||||
if (e instanceof Error)
|
||||
|
||||
Reference in New Issue
Block a user