Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / keys / setupAddressKeys.ts
blob3a39fd43fb3f5f23141b6d5a472b3f7cba4bc878
1 import type { ProductParam } from '@proton/shared/lib/apps/product';
2 import type { DeviceSecretData } from '@proton/shared/lib/keys/device';
4 import { setupAddress as setupAddressRoute } from '../api/addresses';
5 import type { Api, PreAuthKTVerify, Address as tsAddress } from '../interfaces';
6 import { handleSetupKeys } from './setupKeys';
8 interface SetupAddressArgs {
9     api: Api;
10     username: string;
11     domain: string;
14 export const handleSetupAddress = async ({ api, username, domain }: SetupAddressArgs) => {
15     if (!domain) {
16         throw new Error('Missing domain');
17     }
18     const { Address } = await api<{ Address: tsAddress }>(
19         setupAddressRoute({
20             Domain: domain,
21             DisplayName: username,
22             Signature: '',
23         })
24     );
25     return [Address];
28 interface SetupAddressKeysArgs {
29     password: string;
30     api: Api;
31     username: string;
32     addresses: tsAddress[];
33     domains: string[];
34     preAuthKTVerify: PreAuthKTVerify;
35     productParam: ProductParam;
36     deviceSecretData?: DeviceSecretData;
39 export const handleSetupAddressKeys = async ({
40     username,
41     password,
42     api,
43     addresses,
44     domains,
45     preAuthKTVerify,
46     productParam,
47 }: SetupAddressKeysArgs) => {
48     const addressesToUse =
49         addresses?.length > 0 ? addresses : await handleSetupAddress({ api, domain: domains[0], username });
51     return handleSetupKeys({
52         api,
53         addresses: addressesToUse,
54         password,
55         preAuthKTVerify,
56         product: productParam,
57     });