@@ -27,7 +27,12 @@ import Database from "./index.js";
2727import { SerializedKeyPair , SerializedKey } from "@notesnook/crypto" ;
2828import { logger } from "../logger.js" ;
2929import { 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
3237const 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