Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / keys / getInactiveKeys.ts
blobb41801acfdbaf3e823464137ecc5add8d01935e4
1 import { CryptoProxy } from '@proton/crypto';
2 import getRandomString from '@proton/utils/getRandomString';
3 import isTruthy from '@proton/utils/isTruthy';
5 import type { Address, DecryptedKey, InactiveKey, Key, User } from '../interfaces';
6 import type {
7     KeyReactivationRequest,
8     KeyReactivationRequestState,
9     KeyReactivationRequestStateData,
10 } from './reactivation/interface';
12 export const getInactiveKeys = async (Keys: Key[], decryptedKeys: DecryptedKey[]): Promise<InactiveKey[]> => {
13     const decryptedKeysIDs = new Set<string>(decryptedKeys.map(({ ID }) => ID));
14     const inactiveKeys = Keys.filter(({ ID }) => !decryptedKeysIDs.has(ID));
15     return Promise.all(
16         inactiveKeys.map(async (Key) => {
17             const { fingerprint } = await CryptoProxy.getKeyInfo({ armoredKey: Key.PrivateKey }).catch(() => ({
18                 fingerprint: undefined,
19             }));
20             return {
21                 Key,
22                 fingerprint: fingerprint || Key.Fingerprint,
23             };
24         })
25     );
28 export const getAllKeysReactivationRequests = ({
29     addresses,
30     user,
31     inactiveKeys,
32 }: {
33     addresses: Address[] | undefined;
34     user: User | undefined;
35     inactiveKeys: { user: InactiveKey[]; addresses: { [key: string]: InactiveKey[] | undefined } };
36 }): KeyReactivationRequest[] => {
37     const allAddressesKeys = (addresses || []).map((address) => {
38         const inactiveAddressKeys = inactiveKeys.addresses[address.ID];
39         if (!inactiveAddressKeys?.length) {
40             return;
41         }
42         return {
43             address,
44             keysToReactivate: inactiveAddressKeys || [],
45         };
46     });
48     const userKeysReactivation =
49         user && inactiveKeys.user.length
50             ? {
51                   user: user,
52                   keysToReactivate: inactiveKeys.user,
53               }
54             : undefined;
56     return [userKeysReactivation, ...allAddressesKeys].filter(isTruthy);
59 export const getLikelyHasKeysToReactivate = (user: User, addresses?: Address[]) => {
60     return (
61         user?.Keys?.some((Key) => !Key.Active) || addresses?.some((address) => address.Keys?.some((Key) => !Key.Active))
62     );
65 export const getInitialStates = (initial: KeyReactivationRequest[]): KeyReactivationRequestState[] => {
66     if (initial.length === 0) {
67         throw new Error('Keys to reactivate needed');
68     }
70     return initial.map((record) => {
71         const keyStates = record.keysToReactivate.map((Key): KeyReactivationRequestStateData => {
72             return {
73                 id: getRandomString(12),
74                 Key: Key.Key,
75                 fingerprint: Key.fingerprint || '-',
76                 result: undefined,
77             };
78         });
79         return {
80             ...record,
81             keysToReactivate: keyStates,
82         };
83     });