Remove payments API routing initialization
[ProtonMail-WebClient.git] / packages / components / containers / keyTransparency / useResetSelfAudit.ts
blobdb542c249cc3033e52fe91e0e6822cdf52029f1b
1 import useKTActivation from '@proton/components/containers/keyTransparency/useKTActivation';
2 import useApi from '@proton/components/hooks/useApi';
3 import type { VerifiedEpoch } from '@proton/key-transparency/lib';
4 import { fetchLatestEpoch, ktSentryReportError, uploadVerifiedEpoch } from '@proton/key-transparency/lib';
5 import { getSilentApi } from '@proton/shared/lib/api/helpers/customConfig';
6 import { getIsAddressDisabled } from '@proton/shared/lib/helpers/address';
7 import type { Address, ResetSelfAudit, User } from '@proton/shared/lib/interfaces';
8 import { KeyTransparencyActivation } from '@proton/shared/lib/interfaces';
9 import { getDecryptedUserKeysHelper } from '@proton/shared/lib/keys';
11 /**
12  *
13  * Upload a fresh verified epoch to avoid failing self audit
14  * after a password reset.
15  */
16 const useResetSelfAudit = () => {
17     const api = getSilentApi(useApi());
18     const ktActivation = useKTActivation();
19     const resetSelfAudit: ResetSelfAudit = async (user: User, keyPassword: string, addressesBeforeReset: Address[]) => {
20         if (ktActivation === KeyTransparencyActivation.DISABLED) {
21             return;
22         }
23         try {
24             const [userKeys, { EpochID }] = await Promise.all([
25                 getDecryptedUserKeysHelper(user, keyPassword),
26                 fetchLatestEpoch(api),
27             ]);
28             if (!userKeys.length) {
29                 return;
30             }
31             await Promise.all(
32                 addressesBeforeReset
33                     .filter((address) => !getIsAddressDisabled(address))
34                     .map(async (address) => {
35                         if (!address.SignedKeyList) {
36                             return;
37                         }
38                         const { Revision } = address.SignedKeyList;
39                         if (!Revision) {
40                             return;
41                         }
42                         const newVerifiedEpoch: VerifiedEpoch = {
43                             EpochID,
44                             Revision: Revision,
45                             SKLCreationTime: 0,
46                         };
47                         await uploadVerifiedEpoch(newVerifiedEpoch, address.ID, userKeys[0].privateKey, api);
48                     })
49             );
50         } catch (error: any) {
51             ktSentryReportError(error, { context: 'resetSelfAudit' });
52         }
53     };
55     return resetSelfAudit;
58 export default useResetSelfAudit;