Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / store / _crypto / driveCrypto.ts
blob1286f82cc0a90fc9218011f3b67710fca46de05f
1 import type { PrivateKeyReference, PublicKeyReference } from '@proton/crypto';
2 import { toPublicKeyReference } from '@proton/crypto';
3 import { ADDRESS_STATUS } from '@proton/shared/lib/constants';
4 import { canonicalizeInternalEmail } from '@proton/shared/lib/helpers/email';
5 import type { Address } from '@proton/shared/lib/interfaces/Address';
6 import type { GetAddressKeys } from '@proton/shared/lib/interfaces/hooks/GetAddressKeys';
7 import { getPrimaryKey } from '@proton/shared/lib/keys';
8 import { decryptPassphrase } from '@proton/shared/lib/keys/drivePassphrase';
9 import { splitKeys } from '@proton/shared/lib/keys/keys';
11 import type { ShareWithKey } from '../_shares';
13 // Special case for drive to allow users with just an external address
14 export const getActiveAddresses = (addresses: Address[]): Address[] => {
15     return addresses.filter(({ Status }) => Status === ADDRESS_STATUS.STATUS_ENABLED);
18 export const getPrimaryAddressAsync = async (getAddresses: () => Promise<Address[]>) => {
19     const addresses = await getAddresses();
20     const [activeAddress] = getActiveAddresses(addresses);
22     if (!activeAddress) {
23         throw new Error('User has no active address');
24     }
26     return activeAddress;
29 export const getPrimaryAddressKeyAsync = async (
30     getPrimaryAddress: () => Promise<Address>,
31     getAddressKeys: GetAddressKeys
32 ) => {
33     const activeAddress = await getPrimaryAddress();
34     const addressKeys = await getAddressKeys(activeAddress.ID);
35     const { privateKey, publicKey: maybePublicKey, ID: addressKeyID } = getPrimaryKey(addressKeys) || {};
37     if (!privateKey || !addressKeyID) {
38         // Should never happen
39         throw new Error('Primary private key is not available');
40     }
42     const publicKey = maybePublicKey || (await toPublicKeyReference(privateKey));
44     return { addressKeyID, privateKey, publicKey, address: activeAddress };
47 const getOwnAddressWithEmail = async (email: string, getAddresses: () => Promise<Address[]>) => {
48     // Some characters can be changed but still be the same email.
49     return (await getAddresses()).find(
50         ({ Email }) => canonicalizeInternalEmail(Email) === canonicalizeInternalEmail(email)
51     );
54 const getOwnAddress = async (addressId: string, getAddresses: () => Promise<Address[]>) => {
55     return (await getAddresses()).find(({ ID }) => ID === addressId);
58 const getOwnAddressAndKeys = async (
59     addressId: string,
60     getAddresses: () => Promise<Address[]>,
61     getAddressKeys: GetAddressKeys
62 ) => {
63     const address = await getOwnAddress(addressId, getAddresses);
64     if (!address) {
65         return {};
66     }
67     const addressKeys = await getAddressKeys(address.ID);
69     return { address, addressKeys };
72 export const getOwnAddressAndPrimaryKeysAsync = async (
73     addressId: string,
74     getAddresses: () => Promise<Address[]>,
75     getAddressKeys: GetAddressKeys
76 ) => {
77     const { address, addressKeys } = await getOwnAddressAndKeys(addressId, getAddresses, getAddressKeys);
78     const { privateKey, publicKey, ID: addressKeyID } = getPrimaryKey(addressKeys) || {};
80     if (!privateKey || !addressKeyID) {
81         // Should never happen
82         throw new Error('Primary private key is not available');
83     }
84     if (!address) {
85         // Should never happen
86         throw new Error('Address is not available');
87     }
89     return { addressKeyID, address, privateKey, publicKey: publicKey || (await toPublicKeyReference(privateKey)) };
92 const getOwnAddressAndKeysWithEmail = async (
93     email: string,
94     getAddresses: () => Promise<Address[]>,
95     getAddressKeys: GetAddressKeys
96 ) => {
97     const address = await getOwnAddressWithEmail(email, getAddresses);
98     if (!address) {
99         return {};
100     }
101     const addressKeys = await getAddressKeys(address.ID);
103     return { address, addressKeys };
106 export const getOwnAddressKeysAsync = async (
107     addressId: string,
108     getAddresses: () => Promise<Address[]>,
109     getAddressKeys: GetAddressKeys
110 ) => {
111     const { addressKeys } = await getOwnAddressAndKeys(addressId, getAddresses, getAddressKeys);
112     return addressKeys ? splitKeys(addressKeys) : undefined;
115 export const getOwnAddressKeysWithEmailAsync = async (
116     email: string,
117     getAddresses: () => Promise<Address[]>,
118     getAddressKeys: GetAddressKeys
119 ) => {
120     const { addressKeys } = await getOwnAddressAndKeysWithEmail(email, getAddresses, getAddressKeys);
121     return addressKeys ? splitKeys(addressKeys) : undefined;
124 export const decryptSharePassphraseAsync = async (
125     meta: ShareWithKey,
126     privateKeys: PrivateKeyReference[],
127     getVerificationKey: (email: string) => Promise<PublicKeyReference[]>
128 ) => {
129     const publicKeys = await getVerificationKey(meta.creator);
130     return decryptPassphrase({
131         armoredPassphrase: meta.passphrase,
132         armoredSignature: meta.passphraseSignature,
133         privateKeys,
134         publicKeys,
135     });