feat(INDA-383): daily stats.
[ProtonMail-WebClient.git] / packages / shared / lib / keyTransparency / createKeyMigrationKTVerifier.ts
blob72fb32bf9d153cf0acb0955c41b9e537f7c08413
1 import {
2     KeyTransparencyError,
3     fetchLatestEpoch,
4     ktSentryReport,
5     ktSentryReportError,
6     verifyAddressIsAbsent,
7     verifyAddressIsObsolete,
8 } from '@proton/key-transparency/lib';
10 import type { KeyMigrationKTVerifier } from '../interfaces';
11 import { KeyTransparencyActivation } from '../interfaces';
13 const createKeyMigrationKTVerifier = (ktActivation: KeyTransparencyActivation): KeyMigrationKTVerifier => {
14     return async ({ email, signedKeyList, api }) => {
15         if (ktActivation === KeyTransparencyActivation.DISABLED) {
16             return;
17         }
18         try {
19             const epoch = await fetchLatestEpoch(api);
20             if (signedKeyList?.ObsolescenceToken) {
21                 await verifyAddressIsObsolete(epoch, email, signedKeyList, api);
22             } else {
23                 await verifyAddressIsAbsent(epoch, email, api);
24             }
25         } catch (error: any) {
26             if (error instanceof KeyTransparencyError) {
27                 ktSentryReport('KT error during key migration', { error: error.message });
28             } else {
29                 ktSentryReportError(error, { context: 'KeyMigrationKTVerifier' });
30             }
31         }
32     };
35 export default createKeyMigrationKTVerifier;