Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / keys / import / importKeysProcessV2.ts
blob8169757e44bf3fa035fa3910201b1bf798067a2d
1 import type { PrivateKeyReference } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import { getDefaultKeyFlags } from '@proton/shared/lib/keys';
5 import { createAddressKeyRouteV2 } from '../../api/keys';
6 import type { Address, Api, DecryptedKey, KeyTransparencyVerify } from '../../interfaces';
7 import { generateAddressKeyTokens } from '../addressKeys';
8 import { getActiveKeyObject, getActiveKeys, getNormalizedActiveKeys, getPrimaryFlag } from '../getActiveKeys';
9 import { getInactiveKeys } from '../getInactiveKeys';
10 import { reactivateAddressKeysV2 } from '../reactivation/reactivateKeysProcessV2';
11 import { getSignedKeyListWithDeferredPublish } from '../signedKeyList';
12 import { getFilteredImportRecords } from './helper';
13 import type { KeyImportData, OnKeyImportCallback } from './interface';
15 export interface ImportKeysProcessV2Arguments {
16     api: Api;
17     keyImportRecords: KeyImportData[];
18     onImport: OnKeyImportCallback;
19     keyPassword: string;
20     address: Address;
21     addressKeys: DecryptedKey[];
22     userKey: PrivateKeyReference;
23     keyTransparencyVerify: KeyTransparencyVerify;
26 const importKeysProcessV2 = async ({
27     api,
28     keyImportRecords,
29     onImport,
30     address,
31     addressKeys,
32     userKey,
33     keyTransparencyVerify,
34 }: ImportKeysProcessV2Arguments) => {
35     const activeKeys = await getActiveKeys(address, address.SignedKeyList, address.Keys, addressKeys);
36     const inactiveKeys = await getInactiveKeys(address.Keys, activeKeys);
38     const [keysToReactivate, keysToImport, existingKeys] = getFilteredImportRecords(
39         keyImportRecords,
40         activeKeys,
41         inactiveKeys
42     );
44     existingKeys.forEach((keyImportRecord) => {
45         onImport(keyImportRecord.id, new Error('Key already active'));
46     });
48     let mutableActiveKeys = activeKeys;
50     for (const keyImportRecord of keysToImport) {
51         try {
52             const { privateKey } = keyImportRecord;
54             const { token, encryptedToken, signature } = await generateAddressKeyTokens(userKey);
55             const privateKeyArmored = await CryptoProxy.exportPrivateKey({
56                 privateKey,
57                 passphrase: token,
58             });
60             const newActiveKey = await getActiveKeyObject(privateKey, {
61                 ID: 'tmp',
62                 primary: getPrimaryFlag(mutableActiveKeys),
63                 flags: getDefaultKeyFlags(address),
64             });
65             const updatedActiveKeys = getNormalizedActiveKeys(address, [...mutableActiveKeys, newActiveKey]);
66             const [SignedKeyList, onSKLPublishSuccess] = await getSignedKeyListWithDeferredPublish(
67                 updatedActiveKeys,
68                 address,
69                 keyTransparencyVerify
70             );
72             const { Key } = await api(
73                 createAddressKeyRouteV2({
74                     AddressID: address.ID,
75                     Primary: newActiveKey.primary,
76                     PrivateKey: privateKeyArmored,
77                     SignedKeyList,
78                     Signature: signature,
79                     Token: encryptedToken,
80                 })
81             );
82             // Only once the SKL is successfully posted we add it to the KT commit state.
83             await onSKLPublishSuccess();
85             // Mutably update the key with the latest value from the real ID.
86             newActiveKey.ID = Key.ID;
88             mutableActiveKeys = updatedActiveKeys;
90             onImport(keyImportRecord.id, 'ok');
91         } catch (e: any) {
92             onImport(keyImportRecord.id, e);
93         }
94     }
96     await reactivateAddressKeysV2({
97         api,
98         address,
99         activeKeys: mutableActiveKeys,
100         userKey,
101         keysToReactivate,
102         onReactivation: onImport,
103         keyTransparencyVerify,
104     });
107 export default importKeysProcessV2;