1 import type { Address, ProcessedApiKey } from '@proton/shared/lib/interfaces';
3 import { ADDRESS_FLAGS, ADDRESS_TYPE, KEY_FLAG } from '../constants';
4 import { clearBit, hasBit, setBit } from '../helpers/bitset';
6 export const setExternalFlags = (flags: number) => {
7 flags = setBit(flags, KEY_FLAG.FLAG_EMAIL_NO_ENCRYPT);
8 flags = setBit(flags, KEY_FLAG.FLAG_EMAIL_NO_SIGN);
12 export const clearExternalFlags = (flags: number) => {
13 flags = clearBit(flags, KEY_FLAG.FLAG_EMAIL_NO_ENCRYPT);
14 flags = clearBit(flags, KEY_FLAG.FLAG_EMAIL_NO_SIGN);
18 export const getDefaultKeyFlags = (address: Address | undefined) => {
19 let flags = KEY_FLAG.FLAG_NOT_OBSOLETE + KEY_FLAG.FLAG_NOT_COMPROMISED;
20 if (address?.Type === ADDRESS_TYPE.TYPE_EXTERNAL) {
21 flags = setExternalFlags(flags);
23 if (hasBit(address?.Flags, ADDRESS_FLAGS.FLAG_DISABLE_E2EE)) {
24 flags = setBit(flags, KEY_FLAG.FLAG_EMAIL_NO_ENCRYPT);
26 if (hasBit(address?.Flags, ADDRESS_FLAGS.FLAG_DISABLE_EXPECTED_SIGNED)) {
27 flags = setBit(flags, KEY_FLAG.FLAG_EMAIL_NO_SIGN);
32 export const getKeyHasFlagsToVerify = (flags: KEY_FLAG) => {
33 return hasBit(flags, KEY_FLAG.FLAG_NOT_COMPROMISED);
36 export const getKeyHasFlagsToEncrypt = (flags: KEY_FLAG) => {
37 return hasBit(flags, KEY_FLAG.FLAG_NOT_OBSOLETE);
40 export const supportsMail = (flags: number): Boolean => {
41 return !hasBit(flags, KEY_FLAG.FLAG_EMAIL_NO_ENCRYPT);
44 export const getMailCapableKeys = (keys: ProcessedApiKey[]) => {
45 return keys.filter(({ flags }) => supportsMail(flags));