Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / keys / missingKeysSelfProcess.ts
blobc7ab67723a6b7d847994185478a439e5b4458d3d
1 import type { Address, Api, DecryptedKey, KeyGenConfig, KeyTransparencyVerify } from '../interfaces';
2 import { createAddressKeyLegacy, createAddressKeyV2 } from './add';
3 import { getHasMigratedAddressKeys } from './keyMigration';
5 type OnUpdateCallback = (ID: string, update: { status: 'loading' | 'error' | 'ok'; result?: string }) => void;
7 interface MissingKeysSelfProcessArguments {
8     api: Api;
9     userKeys: DecryptedKey[];
10     keyGenConfig: KeyGenConfig;
11     addresses: Address[];
12     addressesToGenerate: Address[];
13     password: string;
14     onUpdate: OnUpdateCallback;
15     keyTransparencyVerify: KeyTransparencyVerify;
18 export const missingKeysSelfProcess = ({
19     api,
20     userKeys,
21     keyGenConfig,
22     addresses,
23     addressesToGenerate,
24     password,
25     onUpdate,
26     keyTransparencyVerify,
27 }: MissingKeysSelfProcessArguments) => {
28     const hasMigratedAddressKeys = getHasMigratedAddressKeys(addresses);
30     return Promise.all(
31         addressesToGenerate.map(async (address) => {
32             try {
33                 onUpdate(address.ID, { status: 'loading' });
35                 if (hasMigratedAddressKeys) {
36                     await createAddressKeyV2({
37                         api,
38                         address,
39                         keyGenConfig,
40                         userKeys,
41                         activeKeys: [],
42                         keyTransparencyVerify,
43                     });
44                 } else {
45                     await createAddressKeyLegacy({
46                         api,
47                         address,
48                         keyGenConfig,
49                         passphrase: password,
50                         activeKeys: [],
51                         keyTransparencyVerify,
52                     });
53                 }
55                 onUpdate(address.ID, { status: 'ok' });
56             } catch (e: any) {
57                 onUpdate(address.ID, { status: 'error', result: e.message });
58             }
59         })
60     );