Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / keys / import / importKeysProcess.ts
blob288505c2d15bec093dc1369deb1126716a9f2d0c
1 import type { Address, DecryptedKey, KeyTransparencyVerify } from '../../interfaces';
2 import { getPrimaryKey } from '../getPrimaryKey';
3 import { getHasMigratedAddressKeys } from '../keyMigration';
4 import type { ImportKeysProcessLegacyArguments } from './importKeysProcessLegacy';
5 import importKeysProcessLegacy from './importKeysProcessLegacy';
6 import type { ImportKeysProcessV2Arguments } from './importKeysProcessV2';
7 import importKeysProcessV2 from './importKeysProcessV2';
9 interface Arguments extends Omit<ImportKeysProcessV2Arguments, 'userKey'>, ImportKeysProcessLegacyArguments {
10     addresses: Address[];
11     userKeys: DecryptedKey[];
12     keyTransparencyVerify: KeyTransparencyVerify;
15 export const importKeysProcess = async ({
16     api,
17     userKeys,
18     address,
19     addressKeys,
20     addresses,
21     keyImportRecords,
22     keyPassword,
23     onImport,
24     keyTransparencyVerify,
25 }: Arguments) => {
26     const hasMigratedAddressKeys = getHasMigratedAddressKeys(addresses);
28     if (hasMigratedAddressKeys) {
29         const primaryPrivateUserKey = getPrimaryKey(userKeys)?.privateKey;
30         if (!primaryPrivateUserKey) {
31             throw new Error('Missing primary private user key');
32         }
33         return importKeysProcessV2({
34             api,
35             keyImportRecords,
36             keyPassword,
37             address,
38             addressKeys,
39             onImport,
40             userKey: primaryPrivateUserKey,
41             keyTransparencyVerify,
42         });
43     }
45     return importKeysProcessLegacy({
46         api,
47         keyImportRecords,
48         keyPassword,
49         address,
50         addressKeys,
51         onImport,
52         keyTransparencyVerify,
53     });