Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / keys / add / addAddressKeyHelper.ts
blob16f645a5b7df0e2137677a91e5a399ba43ff4fcb
1 import { createAddressKeyRoute, createAddressKeyRouteV2 } from '../../api/keys';
2 import { DEFAULT_KEYGEN_TYPE, KEYGEN_CONFIGS } from '../../constants';
3 import type { ActiveKey, Address, Api, KeyGenConfig, KeyPair, KeyTransparencyVerify } from '../../interfaces';
4 import { generateAddressKey, getNewAddressKeyToken } from '../addressKeys';
5 import { getActiveKeyObject, getNormalizedActiveKeys } from '../getActiveKeys';
6 import { getDefaultKeyFlags } from '../keyFlags';
7 import { getSignedKeyListWithDeferredPublish } from '../signedKeyList';
9 interface CreateAddressKeyLegacyArguments {
10     api: Api;
11     keyGenConfig?: KeyGenConfig;
12     address: Address;
13     passphrase: string;
14     activeKeys: ActiveKey[];
15     keyTransparencyVerify: KeyTransparencyVerify;
16     addressForwardingID?: string;
19 export const removePrimary = (activeKey: ActiveKey): ActiveKey => {
20     if (activeKey.primary) {
21         return {
22             ...activeKey,
23             primary: 0,
24         };
25     }
26     return activeKey;
29 export const createAddressKeyLegacy = async ({
30     api,
31     address,
32     keyGenConfig = KEYGEN_CONFIGS[DEFAULT_KEYGEN_TYPE],
33     passphrase,
34     activeKeys,
35     keyTransparencyVerify,
36     addressForwardingID: AddressForwardingID,
37 }: CreateAddressKeyLegacyArguments) => {
38     const { privateKey, privateKeyArmored } = await generateAddressKey({
39         email: address.Email,
40         passphrase,
41         keyGenConfig,
42     });
43     const newActiveKey = await getActiveKeyObject(privateKey, {
44         ID: 'tmp',
45         primary: 1,
46         flags: getDefaultKeyFlags(address),
47     });
48     const updatedActiveKeys = getNormalizedActiveKeys(address, [newActiveKey, ...activeKeys.map(removePrimary)]);
49     const [SignedKeyList, onSKLPublishSuccess] = await getSignedKeyListWithDeferredPublish(
50         updatedActiveKeys,
51         address,
52         keyTransparencyVerify
53     );
54     const { Key } = await api(
55         createAddressKeyRoute({
56             AddressID: address.ID,
57             Primary: newActiveKey.primary,
58             PrivateKey: privateKeyArmored,
59             SignedKeyList,
60             AddressForwardingID,
61         })
62     );
63     await onSKLPublishSuccess();
64     newActiveKey.ID = Key.ID;
66     return [newActiveKey, updatedActiveKeys] as const;
69 interface CreateAddressKeyV2Arguments {
70     api: Api;
71     userKeys: KeyPair[];
72     keyGenConfig?: KeyGenConfig;
73     address: Address;
74     activeKeys: ActiveKey[];
75     keyTransparencyVerify: KeyTransparencyVerify;
78 export const createAddressKeyV2 = async ({
79     api,
80     userKeys,
81     keyGenConfig = KEYGEN_CONFIGS[DEFAULT_KEYGEN_TYPE],
82     address,
83     activeKeys,
84     keyTransparencyVerify,
85 }: CreateAddressKeyV2Arguments) => {
86     const { token, encryptedToken, signature } = await getNewAddressKeyToken({ address, userKeys });
87     const { privateKey, privateKeyArmored } = await generateAddressKey({
88         email: address.Email,
89         passphrase: token,
90         keyGenConfig,
91     });
92     const newActiveKey = await getActiveKeyObject(privateKey, {
93         ID: 'tmp',
94         primary: 1,
95         flags: getDefaultKeyFlags(address),
96     });
97     const updatedActiveKeys = getNormalizedActiveKeys(address, [newActiveKey, ...activeKeys.map(removePrimary)]);
98     const [SignedKeyList, onSKLPublishSuccess] = await getSignedKeyListWithDeferredPublish(
99         updatedActiveKeys,
100         address,
101         keyTransparencyVerify
102     );
103     const { Key } = await api(
104         createAddressKeyRouteV2({
105             AddressID: address.ID,
106             Primary: newActiveKey.primary,
107             PrivateKey: privateKeyArmored,
108             SignedKeyList,
109             Signature: signature,
110             Token: encryptedToken,
111         })
112     );
113     // Only once the SKL is successfully posted we add it to the KT commit state.
114     await onSKLPublishSuccess();
115     newActiveKey.ID = Key.ID;
117     return [newActiveKey, updatedActiveKeys] as const;