Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / pass / types / crypto / pass-crypto.ts
blobc088840585cee4305d7b59b5c25e0004f9cdcd4e
1 import type { CreateSecureLinkData } from '@proton/pass/lib/crypto/processes';
2 import type {
3     InviteAcceptRequest,
4     InviteCreateRequest,
5     ItemCreateRequest,
6     ItemKeyResponse,
7     ItemLatestKeyResponse,
8     ItemMoveSingleToShareRequest,
9     ItemRevisionContentsResponse,
10     ItemUpdateRequest,
11     KeyRotationKeyPair,
12     NewUserInviteCreateRequest,
13     NewUserInvitePromoteRequest,
14     PublicLinkGetContentResponse,
15     ShareGetResponse,
16     ShareKeyResponse,
17     VaultCreateRequest,
18     VaultUpdateRequest,
19 } from '@proton/pass/types/api';
20 import type { ShareRole, ShareType } from '@proton/pass/types/data/shares';
21 import type { MaybeNull } from '@proton/pass/types/utils';
22 import type { Address, DecryptedAddressKey, DecryptedKey, User } from '@proton/shared/lib/interfaces';
24 import type { ItemKey, OpenedItem, Rotation, ShareId, TypedOpenedShare, VaultKey } from './pass-types';
26 export type PassCryptoManagerContext = {
27     user?: User;
28     userKeys?: DecryptedKey[];
29     addresses?: Address[];
30     primaryUserKey?: DecryptedKey;
31     primaryAddress?: Address;
32     shareManagers: Map<ShareId, ShareManager>;
35 export type PassCryptoSnapshot = Pick<PassCryptoManagerContext, 'shareManagers'>;
37 export interface PassCryptoWorker extends SerializableCryptoContext<PassCryptoSnapshot> {
38     ready: boolean;
39     getDecryptedAddressKeys: (addressId: string) => Promise<DecryptedAddressKey[]>;
40     getContext: () => PassCryptoManagerContext;
41     hydrate: (options: {
42         addresses: Address[];
43         keyPassword: string;
44         snapshot?: SerializedCryptoContext<PassCryptoSnapshot>;
45         user: User;
46         clear?: boolean;
47     }) => Promise<void>;
48     clear: () => void;
49     getShareManager: (shareId: string) => ShareManager;
50     createVault: (content: Uint8Array) => Promise<VaultCreateRequest>;
51     updateVault: (data: { shareId: string; content: Uint8Array }) => Promise<VaultUpdateRequest>;
52     canOpenShare: (shareId: string) => boolean;
53     openShare: <T extends ShareType = ShareType>(data: {
54         encryptedShare: ShareGetResponse;
55         shareKeys: ShareKeyResponse[];
56     }) => Promise<MaybeNull<TypedOpenedShare<T>>>;
57     updateShareKeys: (data: { shareId: string; shareKeys: ShareKeyResponse[] }) => Promise<void>;
58     removeShare: (shareId: string) => void;
59     openItem: (data: { shareId: string; encryptedItem: ItemRevisionContentsResponse }) => Promise<OpenedItem>;
60     createItem: (data: { shareId: string; content: Uint8Array }) => Promise<ItemCreateRequest>;
61     updateItem: (data: {
62         content: Uint8Array;
63         lastRevision: number;
64         latestItemKey: ItemKeyResponse;
65         shareId: string;
66     }) => Promise<ItemUpdateRequest>;
67     moveItem: (data: { destinationShareId: string; content: Uint8Array }) => Promise<ItemMoveSingleToShareRequest>;
68     createVaultInvite: (data: {
69         email: string;
70         invitedPublicKey: string;
71         role: ShareRole;
72         shareId: string;
73     }) => Promise<InviteCreateRequest>;
74     promoteInvite: (data: { shareId: string; invitedPublicKey: string }) => Promise<NewUserInvitePromoteRequest>;
75     createNewUserVaultInvite: (data: {
76         email: string;
77         role: ShareRole;
78         shareId: string;
79     }) => Promise<NewUserInviteCreateRequest>;
80     acceptVaultInvite: (data: {
81         invitedAddressId: string;
82         inviteKeys: KeyRotationKeyPair[];
83         inviterPublicKeys: string[];
84     }) => Promise<InviteAcceptRequest>;
85     readVaultInvite: (data: {
86         encryptedVaultContent: string;
87         invitedAddressId: string;
88         inviteKey: KeyRotationKeyPair;
89         inviterPublicKeys: string[];
90     }) => Promise<Uint8Array>;
91     createSecureLink: (data: {
92         shareId: string;
93         latestItemKey: ItemLatestKeyResponse;
94     }) => Promise<CreateSecureLinkData>;
95     openSecureLink: (data: { linkKey: string; publicLinkContent: PublicLinkGetContentResponse }) => Promise<Uint8Array>;
96     openLinkKey: (data: {
97         encryptedLinkKey: string;
98         linkKeyShareKeyRotation: number;
99         shareId: string;
100     }) => Promise<Uint8Array>;
103 export type ShareContext<T extends ShareType = ShareType> = {
104     share: TypedOpenedShare<T>;
105     latestRotation: Rotation;
106     vaultKeys: Map<Rotation, VaultKey>;
107     itemKeys: Map<Rotation, ItemKey>;
110 export interface ShareManager<T extends ShareType = ShareType> extends SerializableCryptoContext<ShareContext> {
111     getShare: () => TypedOpenedShare<T>;
112     setShare: (share: TypedOpenedShare<T>) => void;
113     getLatestRotation: () => Rotation;
114     setLatestRotation: (rotation: Rotation) => void;
115     hasVaultKey: (rotation: Rotation) => boolean;
116     getVaultKey: (rotation: Rotation) => VaultKey;
117     getVaultKeys: () => VaultKey[];
118     addVaultKey: (vaultKey: VaultKey) => void;
119     isActive: (userKeys?: DecryptedKey[]) => boolean;
122 export interface SerializableCryptoContext<S> {
123     serialize: () => SerializedCryptoContext<S>;
126 export type SerializedCryptoContext<T> =
127     T extends SerializableCryptoContext<infer U>
128         ? SerializedCryptoContext<U>
129         : T extends Uint8Array
130           ? string
131           : T extends Map<infer K, infer U>
132             ? (readonly [K, SerializedCryptoContext<U>])[]
133             : T extends (infer U)[]
134               ? SerializedCryptoContext<U>[]
135               : T extends {}
136                 ? { [K in keyof T as T[K] extends CryptoKey ? never : K]: SerializedCryptoContext<T[K]> }
137                 : T;