1 import type { CreateSecureLinkData } from '@proton/pass/lib/crypto/processes';
8 ItemMoveSingleToShareRequest,
9 ItemRevisionContentsResponse,
12 NewUserInviteCreateRequest,
13 NewUserInvitePromoteRequest,
14 PublicLinkGetContentResponse,
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 = {
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> {
39 getDecryptedAddressKeys: (addressId: string) => Promise<DecryptedAddressKey[]>;
40 getContext: () => PassCryptoManagerContext;
44 snapshot?: SerializedCryptoContext<PassCryptoSnapshot>;
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>;
64 latestItemKey: ItemKeyResponse;
66 }) => Promise<ItemUpdateRequest>;
67 moveItem: (data: { destinationShareId: string; content: Uint8Array }) => Promise<ItemMoveSingleToShareRequest>;
68 createVaultInvite: (data: {
70 invitedPublicKey: string;
73 }) => Promise<InviteCreateRequest>;
74 promoteInvite: (data: { shareId: string; invitedPublicKey: string }) => Promise<NewUserInvitePromoteRequest>;
75 createNewUserVaultInvite: (data: {
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: {
93 latestItemKey: ItemLatestKeyResponse;
94 }) => Promise<CreateSecureLinkData>;
95 openSecureLink: (data: { linkKey: string; publicLinkContent: PublicLinkGetContentResponse }) => Promise<Uint8Array>;
97 encryptedLinkKey: string;
98 linkKeyShareKeyRotation: number;
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
131 : T extends Map<infer K, infer U>
132 ? (readonly [K, SerializedCryptoContext<U>])[]
133 : T extends (infer U)[]
134 ? SerializedCryptoContext<U>[]
136 ? { [K in keyof T as T[K] extends CryptoKey ? never : K]: SerializedCryptoContext<T[K]> }