Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / keys / missingKeysMemberProcess.ts
bloba5dce32e3458552aaf7a2729c763d79b77ea256a
1 import type { PrivateKeyReference } from '@proton/crypto';
3 import type { Address, Api, KeyGenConfig, KeyTransparencyVerify, Member } from '../interfaces';
4 import { getHasMemberMigratedAddressKeys } from './keyMigration';
5 import { createMemberAddressKeysLegacy, createMemberAddressKeysV2, getDecryptedMemberKey } from './memberKeys';
7 type OnUpdateCallback = (ID: string, update: { status: 'loading' | 'error' | 'ok'; result?: string }) => void;
9 interface MissingKeysMemberProcessArguments {
10     api: Api;
11     keyGenConfig: KeyGenConfig;
12     onUpdate: OnUpdateCallback;
13     organizationKey: PrivateKeyReference;
14     ownerAddresses: Address[];
15     member: Member;
16     memberAddresses: Address[];
17     memberAddressesToGenerate: Address[];
18     keyTransparencyVerify: KeyTransparencyVerify;
21 export const missingKeysMemberProcess = async ({
22     api,
23     keyGenConfig,
24     onUpdate,
25     ownerAddresses,
26     member,
27     memberAddresses,
28     memberAddressesToGenerate,
29     organizationKey,
30     keyTransparencyVerify,
31 }: MissingKeysMemberProcessArguments) => {
32     const PrimaryKey = member.Keys.find(({ Primary }) => Primary === 1);
34     if (!PrimaryKey) {
35         throw new Error('Member keys are not set up');
36     }
38     const hasMigratedAddressKeys = getHasMemberMigratedAddressKeys(memberAddresses, ownerAddresses);
40     const primaryMemberUserKey = await getDecryptedMemberKey(PrimaryKey, organizationKey);
42     return Promise.all(
43         memberAddressesToGenerate.map(async (memberAddress) => {
44             try {
45                 onUpdate(memberAddress.ID, { status: 'loading' });
47                 if (hasMigratedAddressKeys) {
48                     await createMemberAddressKeysV2({
49                         api,
50                         member,
51                         memberAddress,
52                         memberAddressKeys: [], // Assume no keys exist for this address since we are in this modal.
53                         keyGenConfig,
54                         memberUserKey: primaryMemberUserKey,
55                         organizationKey,
56                         keyTransparencyVerify,
57                     });
58                 } else {
59                     await createMemberAddressKeysLegacy({
60                         api,
61                         member,
62                         memberAddress,
63                         memberAddressKeys: [], // Assume no keys exist for this address since we are in this modal.
64                         keyGenConfig,
65                         memberUserKey: primaryMemberUserKey,
66                         organizationKey,
67                         keyTransparencyVerify,
68                     });
69                 }
71                 onUpdate(memberAddress.ID, { status: 'ok' });
72             } catch (e: any) {
73                 onUpdate(memberAddress.ID, { status: 'error', result: e.message });
74             }
75         })
76     );