1 import { useGetAddresses } from '@proton/account/addresses/hooks';
2 import { useProtonDomains } from '@proton/account/protonDomains/hooks';
3 import { useUser } from '@proton/account/user/hooks';
4 import { useGetUserKeys } from '@proton/account/userKeys/hooks';
5 import useKTVerifier from '@proton/components/containers/keyTransparency/useKTVerifier';
6 import { orderAddress, setupAddress } from '@proton/shared/lib/api/addresses';
7 import { DEFAULT_KEYGEN_TYPE, KEYGEN_CONFIGS } from '@proton/shared/lib/constants';
8 import { type Address, type ApiResponse } from '@proton/shared/lib/interfaces';
9 import { missingKeysSelfProcess } from '@proton/shared/lib/keys';
10 import noop from '@proton/utils/noop';
12 import useApi from '../useApi';
13 import useAuthentication from '../useAuthentication';
14 import useEventManager from '../useEventManager';
16 const useShortDomainAddress = () => {
18 const [user, loadingUser] = useUser();
19 const shortDomain = `${user.Name}@pm.me`;
20 const [{ premiumDomains }, loadingProtonDomains] = useProtonDomains();
21 const getAddresses = useGetAddresses();
22 const authentication = useAuthentication();
23 const { call } = useEventManager();
24 const getUserKeys = useGetUserKeys();
25 const { keyTransparencyVerify, keyTransparencyCommit } = useKTVerifier(api, async () => user);
28 loadingDependencies: loadingProtonDomains || loadingUser,
29 shortDomainAddress: shortDomain,
30 hasShortDomain: (addresses: Address[]) => addresses.some(({ Email }) => Email === shortDomain),
31 createShortDomainAddress: async ({
35 /** Set short domain as default address after creation */
37 addressSignature?: string;
39 const [Domain = ''] = premiumDomains;
40 const addresses = await getAddresses();
42 // Early return if the address already exists
43 if (addresses.some(({ Email }) => Email === shortDomain)) {
48 const [{ DisplayName = '', Signature = '' } = {}] = addresses || [];
49 const { Address } = await api<ApiResponse & { Address: Address }>(
52 DisplayName: DisplayName || '', // DisplayName can be null
53 Signature: addressSignature ?? Signature ?? '', // Signature can be null
56 const userKeys = await getUserKeys();
57 await missingKeysSelfProcess({
61 addressesToGenerate: [Address],
62 password: authentication.getPassword(),
63 keyGenConfig: KEYGEN_CONFIGS[DEFAULT_KEYGEN_TYPE],
65 keyTransparencyVerify,
67 await keyTransparencyCommit(userKeys);
70 // Default address is the first one in the list so we need to reorder the addresses
71 await api(orderAddress([Address.ID, ...addresses.map(({ ID }) => ID)]));
74 // Call event manager to ensure all the UI is up to date
77 // Return newly created address
83 export default useShortDomainAddress;