Skip to content

Commit 0b4fcc1

Browse files
committed
core: fix types
1 parent adfb491 commit 0b4fcc1

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

packages/core/src/api/key-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ type WrappedKey =
5353
private: Cipher<"base64">;
5454
};
5555

56-
type UnwrapKeyReturnType<T extends WrappedKey> = T extends {
56+
export type UnwrapKeyReturnType<T extends WrappedKey> = T extends {
5757
public: string;
5858
private: Cipher<"base64">;
5959
}
6060
? SerializedKeyPair
6161
: SerializedKey;
6262

63-
type KeyTypeFromId<TId extends KeyId> =
63+
export type KeyTypeFromId<TId extends KeyId> =
6464
(typeof KEY_INFO)[TId]["type"] extends "symmetric"
6565
? Cipher<"base64">
6666
: {

packages/core/src/api/user-manager.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import Database from "./index.js";
2727
import { SerializedKeyPair, SerializedKey } from "@notesnook/crypto";
2828
import { logger } from "../logger.js";
2929
import { KEY_VERSION, KeyVersion } from "./sync/types.js";
30-
import { KeyId, KeyManager } from "./key-manager.js";
30+
import {
31+
KeyId,
32+
KeyManager,
33+
KeyTypeFromId,
34+
UnwrapKeyReturnType
35+
} from "./key-manager.js";
3136

3237
const ENDPOINTS = {
3338
signup: "/users",
@@ -471,13 +476,13 @@ class UserManager {
471476
return { key, salt: user.salt };
472477
}
473478

474-
private async getUserKey(
475-
id: KeyId,
479+
private async getUserKey<TId extends KeyId>(
480+
id: TId,
476481
config: {
477482
generateKey: () => Promise<SerializedKey | SerializedKeyPair>;
478483
errorContext: string;
479484
}
480-
): Promise<SerializedKey | SerializedKeyPair | undefined> {
485+
): Promise<UnwrapKeyReturnType<KeyTypeFromId<TId>> | undefined> {
481486
try {
482487
const masterKey = await this.getMasterKey();
483488
if (!masterKey) return;
@@ -489,10 +494,13 @@ class UserManager {
489494
await this.updateUser({
490495
[id]: await this.keyManager.wrapKey(key, masterKey)
491496
});
492-
return key;
497+
return key as UnwrapKeyReturnType<KeyTypeFromId<TId>>;
493498
}
494499

495-
return await this.keyManager.unwrapKey(wrappedKey, masterKey);
500+
return (await this.keyManager.unwrapKey(
501+
wrappedKey,
502+
masterKey
503+
)) as UnwrapKeyReturnType<KeyTypeFromId<TId>>;
496504
} catch (e) {
497505
logger.error(e, `Could not get ${config.errorContext}.`);
498506
if (e instanceof Error)

0 commit comments

Comments
 (0)