1 import { CryptoProxy } from '@proton/crypto';
2 import { decryptAddressKeyToken } from '@proton/shared/lib/keys/addressKeys';
3 import { splitKeys } from '@proton/shared/lib/keys/keys';
4 import { ORGANIZATION_SIGNATURE_CONTEXT, getIsPasswordless } from '@proton/shared/lib/keys/organizationKeys';
6 import type { CachedOrganizationKey, DecryptedKey, KeyPair, OrganizationKey } from '../interfaces';
8 export const getDecryptedOrganizationKey = async (armoredKey: string, passphrase: string) => {
9 const privateKey = await CryptoProxy.importPrivateKey({
13 const publicKey = await CryptoProxy.importPublicKey({
14 binaryKey: await CryptoProxy.exportPublicKey({ key: privateKey, format: 'binary' }),
22 export const getOrganizationKeyToken = async ({
28 Key?: OrganizationKey;
31 if (getIsPasswordless(Key)) {
32 const { privateKeys, publicKeys } = splitKeys(userKeys);
33 return decryptAddressKeyToken({
37 Signature: Key.Signature,
38 context: { value: ORGANIZATION_SIGNATURE_CONTEXT.SHARE_ORGANIZATION_KEY_TOKEN, required: true },
44 export const getDecryptedOrganizationKeyHelper = async ({
53 if (!Key.PrivateKey) {
54 throw new Error('Missing key');
56 if (Key.LegacyPrivateKey) {
57 return getDecryptedOrganizationKey(Key.LegacyPrivateKey, keyPassword);
59 return getDecryptedOrganizationKey(Key.PrivateKey, await getOrganizationKeyToken({ userKeys, Key, keyPassword }));
62 export const getCachedOrganizationKey = async ({
67 userKeys: DecryptedKey[];
70 }): Promise<CachedOrganizationKey> => {
71 if (!Key.PrivateKey) {
77 const { privateKey, publicKey } = await getDecryptedOrganizationKeyHelper({