Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / keys / generatePrivateMemberKeys.ts
blob14e9203d5bf1c56ff5ebc9dfb3aa8c374e9bd515
1 import { ADDRESS_STATUS, MEMBER_PRIVATE } from '../constants';
2 import type { Address, Api, DecryptedKey, KeyTransparencyVerify, UserModel as tsUserModel } from '../interfaces';
3 import { createAddressKeyLegacy, createAddressKeyV2 } from './add';
4 import { getHasMigratedAddressKeys } from './keyMigration';
6 export const getAddressesWithKeysToGenerate = (user: tsUserModel, addresses: Address[]) => {
7     // If signed in as subuser, or not a private user
8     if (!user || !addresses || user.OrganizationPrivateKey || user.Private !== MEMBER_PRIVATE.UNREADABLE) {
9         return [];
10     }
11     // Any enabled address without keys
12     return addresses.filter(({ Status, Keys = [] }) => {
13         return Status === ADDRESS_STATUS.STATUS_ENABLED && !Keys.length;
14     });
17 interface GenerateAllPrivateMemberKeys {
18     addresses: Address[];
19     addressesToGenerate: Address[];
20     userKeys: DecryptedKey[];
21     api: Api;
22     keyPassword: string;
23     keyTransparencyVerify: KeyTransparencyVerify;
26 export const generateAllPrivateMemberKeys = async ({
27     addresses,
28     addressesToGenerate,
29     userKeys,
30     keyPassword,
31     api,
32     keyTransparencyVerify,
33 }: GenerateAllPrivateMemberKeys) => {
34     if (!keyPassword) {
35         throw new Error('Password required to generate keys');
36     }
38     if (getHasMigratedAddressKeys(addresses)) {
39         return Promise.all(
40             addressesToGenerate.map((address) => {
41                 return createAddressKeyV2({
42                     api,
43                     userKeys,
44                     address,
45                     activeKeys: [],
46                     keyTransparencyVerify,
47                 });
48             })
49         );
50     }
52     return Promise.all(
53         addressesToGenerate.map((address) => {
54             return createAddressKeyLegacy({
55                 api,
56                 address,
57                 passphrase: keyPassword,
58                 activeKeys: [],
59                 keyTransparencyVerify,
60             });
61         })
62     );