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 {
11 keyGenConfig: KeyGenConfig;
12 onUpdate: OnUpdateCallback;
13 organizationKey: PrivateKeyReference;
14 ownerAddresses: Address[];
16 memberAddresses: Address[];
17 memberAddressesToGenerate: Address[];
18 keyTransparencyVerify: KeyTransparencyVerify;
21 export const missingKeysMemberProcess = async ({
28 memberAddressesToGenerate,
30 keyTransparencyVerify,
31 }: MissingKeysMemberProcessArguments) => {
32 const PrimaryKey = member.Keys.find(({ Primary }) => Primary === 1);
35 throw new Error('Member keys are not set up');
38 const hasMigratedAddressKeys = getHasMemberMigratedAddressKeys(memberAddresses, ownerAddresses);
40 const primaryMemberUserKey = await getDecryptedMemberKey(PrimaryKey, organizationKey);
43 memberAddressesToGenerate.map(async (memberAddress) => {
45 onUpdate(memberAddress.ID, { status: 'loading' });
47 if (hasMigratedAddressKeys) {
48 await createMemberAddressKeysV2({
52 memberAddressKeys: [], // Assume no keys exist for this address since we are in this modal.
54 memberUserKey: primaryMemberUserKey,
56 keyTransparencyVerify,
59 await createMemberAddressKeysLegacy({
63 memberAddressKeys: [], // Assume no keys exist for this address since we are in this modal.
65 memberUserKey: primaryMemberUserKey,
67 keyTransparencyVerify,
71 onUpdate(memberAddress.ID, { status: 'ok' });
73 onUpdate(memberAddress.ID, { status: 'error', result: e.message });