Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / keys / import / importKeysProcessLegacy.ts
blobed125503c2dda1e8a4c34fdbda54c7e8b0fbfeb1
1 import { CryptoProxy } from '@proton/crypto';
2 import { getDefaultKeyFlags } from '@proton/shared/lib/keys';
4 import { createAddressKeyRoute } from '../../api/keys';
5 import type { Address, Api, DecryptedKey, KeyTransparencyVerify } from '../../interfaces';
6 import {
7     getActiveAddressKeys,
8     getActiveKeyObject,
9     getNormalizedActiveAddressKeys,
10     getPrimaryFlag,
11 } from '../getActiveKeys';
12 import { getInactiveKeys } from '../getInactiveKeys';
13 import reactivateKeysProcessLegacy from '../reactivation/reactivateKeysProcessLegacy';
14 import { getSignedKeyListWithDeferredPublish } from '../signedKeyList';
15 import { getFilteredImportRecords } from './helper';
16 import type { KeyImportData, OnKeyImportCallback } from './interface';
18 export interface ImportKeysProcessLegacyArguments {
19     api: Api;
20     keyImportRecords: KeyImportData[];
21     onImport: OnKeyImportCallback;
22     keyPassword: string;
23     address: Address;
24     addressKeys: DecryptedKey[];
25     keyTransparencyVerify: KeyTransparencyVerify;
28 // handles import with non-migrated keys
29 const importKeysProcessLegacy = async ({
30     api,
31     keyImportRecords,
32     keyPassword,
33     onImport,
34     address,
35     addressKeys,
36     keyTransparencyVerify,
37 }: ImportKeysProcessLegacyArguments) => {
38     const activeKeys = await getActiveAddressKeys(address, address.SignedKeyList, address.Keys, addressKeys);
39     const inactiveKeys = await getInactiveKeys(address.Keys, activeKeys.v4); // v6 keys not present for non-migrated users
41     const [keysToReactivate, keysToImport, existingKeys] = getFilteredImportRecords(
42         keyImportRecords,
43         activeKeys.v4,
44         inactiveKeys
45     );
47     existingKeys.forEach((keyImportRecord) => {
48         onImport(keyImportRecord.id, new Error('Key already active'));
49     });
51     let mutableActiveKeys = activeKeys;
53     for (const keyImportRecord of keysToImport) {
54         try {
55             const { privateKey } = keyImportRecord;
56             if (!privateKey.isPrivateKeyV4()) {
57                 throw new Error('v6 keys not supported with non-migrated keys');
58             }
59             const privateKeyArmored = await CryptoProxy.exportPrivateKey({
60                 privateKey,
61                 passphrase: keyPassword,
62             });
64             const newActiveKey = await getActiveKeyObject(privateKey, {
65                 ID: 'tmp',
66                 primary: getPrimaryFlag(mutableActiveKeys.v4),
67                 flags: getDefaultKeyFlags(address),
68             });
69             const updatedActiveKeys = getNormalizedActiveAddressKeys(address, {
70                 v4: [...mutableActiveKeys.v4, newActiveKey],
71                 v6: [],
72             });
73             const [SignedKeyList, onSKLPublishSuccess] = await getSignedKeyListWithDeferredPublish(
74                 updatedActiveKeys,
75                 address,
76                 keyTransparencyVerify
77             );
79             const { Key } = await api(
80                 createAddressKeyRoute({
81                     AddressID: address.ID,
82                     Primary: newActiveKey.primary,
83                     PrivateKey: privateKeyArmored,
84                     SignedKeyList,
85                 })
86             );
87             // Only once the SKL is successfully posted we add it to the KT commit state.
88             await onSKLPublishSuccess();
89             // Mutably update the key with the latest value from the real ID.
90             newActiveKey.ID = Key.ID;
92             mutableActiveKeys = updatedActiveKeys;
94             onImport(keyImportRecord.id, 'ok');
95         } catch (e: any) {
96             onImport(keyImportRecord.id, e);
97         }
98     }
100     await reactivateKeysProcessLegacy({
101         api,
102         keyPassword,
103         userKeys: [],
104         addressesKeys: [
105             {
106                 address,
107                 keys: mutableActiveKeys.v4,
108             },
109         ],
110         keyReactivationRecords: [
111             {
112                 address,
113                 keysToReactivate,
114             },
115         ],
116         onReactivation: onImport,
117         keyTransparencyVerify,
118     });
121 export default importKeysProcessLegacy;