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';
13 * Upload a fresh verified epoch to avoid failing self audit
14 * after a password reset.
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) {
24 const [userKeys, { EpochID }] = await Promise.all([
25 getDecryptedUserKeysHelper(user, keyPassword),
26 fetchLatestEpoch(api),
28 if (!userKeys.length) {
33 .filter((address) => !getIsAddressDisabled(address))
34 .map(async (address) => {
35 if (!address.SignedKeyList) {
38 const { Revision } = address.SignedKeyList;
42 const newVerifiedEpoch: VerifiedEpoch = {
47 await uploadVerifiedEpoch(newVerifiedEpoch, address.ID, userKeys[0].privateKey, api);
50 } catch (error: any) {
51 ktSentryReportError(error, { context: 'resetSelfAudit' });
55 return resetSelfAudit;
58 export default useResetSelfAudit;