1 import { getAllPublicKeys } from '@proton/shared/lib/api/keys';
2 import type { KEY_FLAG } from '@proton/shared/lib/constants';
3 import { API_CUSTOM_ERROR_CODES } from '@proton/shared/lib/errors';
4 import type { Api } from '@proton/shared/lib/interfaces';
12 const cache = new Map<string, string[]>();
14 * Retrieve all public keys for a specific user email.
15 * We silence errors for address missing and external domain as we allow addition of exernal users.
17 export const getPublicKeysForEmail = async (api: Api, email: string) => {
18 const cached = cache.get(email);
22 return api<{ Address: { Keys: Key[] } }>({
27 silence: [API_CUSTOM_ERROR_CODES.KEY_GET_ADDRESS_MISSING, API_CUSTOM_ERROR_CODES.KEY_GET_DOMAIN_EXTERNAL],
29 .then(({ Address }) => {
30 const keysMap = Address.Keys.map((key) => key.PublicKey);
31 cache.set(email, keysMap);
36 error?.data?.Code === API_CUSTOM_ERROR_CODES.KEY_GET_ADDRESS_MISSING ||
37 error?.data?.Code === API_CUSTOM_ERROR_CODES.KEY_GET_DOMAIN_EXTERNAL
44 export const getPrimaryPublicKeyForEmail = async (api: Api, email: string) =>
45 getPublicKeysForEmail(api, email).then((PublicKeys) => PublicKeys?.[0]);