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';
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));
16 inactiveKeys.map(async (Key) => {
17 const { fingerprint } = await CryptoProxy.getKeyInfo({ armoredKey: Key.PrivateKey }).catch(() => ({
18 fingerprint: undefined,
22 fingerprint: fingerprint || Key.Fingerprint,
28 export const getAllKeysReactivationRequests = ({
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) {
44 keysToReactivate: inactiveAddressKeys || [],
48 const userKeysReactivation =
49 user && inactiveKeys.user.length
52 keysToReactivate: inactiveKeys.user,
56 return [userKeysReactivation, ...allAddressesKeys].filter(isTruthy);
59 export const getLikelyHasKeysToReactivate = (user: User, addresses?: Address[]) => {
61 user?.Keys?.some((Key) => !Key.Active) || addresses?.some((address) => address.Keys?.some((Key) => !Key.Active))
65 export const getInitialStates = (initial: KeyReactivationRequest[]): KeyReactivationRequestState[] => {
66 if (initial.length === 0) {
67 throw new Error('Keys to reactivate needed');
70 return initial.map((record) => {
71 const keyStates = record.keysToReactivate.map((Key): KeyReactivationRequestStateData => {
73 id: getRandomString(12),
75 fingerprint: Key.fingerprint || '-',
81 keysToReactivate: keyStates,