1 import type { useGetAddressKeys } from '@proton/components';
2 import { CryptoProxy } from '@proton/crypto';
4 import { getAllCalendarKeys, getPassphrases, queryMembers, reactivateCalendarKey } from '../../../api/calendars';
5 import type { Address, Api } from '../../../interfaces';
12 } from '../../../interfaces/calendar';
13 import { splitKeys } from '../../../keys';
14 import { getMemberAddressWithAdminPermissions } from '../../getMemberWithAdmin';
15 import { decryptPassphrase } from './calendarKeys';
17 interface ReactivateCalendarsKeysArgumentsShared {
19 getAddressKeys: ReturnType<typeof useGetAddressKeys>;
23 interface ReactivateCalendarsKeysArguments extends ReactivateCalendarsKeysArgumentsShared {
24 calendars: Calendar[];
27 interface ReactivateCalendarKeysArguments extends ReactivateCalendarsKeysArgumentsShared {
31 const reactivateCalendarKeys = async ({
36 }: ReactivateCalendarKeysArguments) => {
37 const [{ Keys = [] }, { Passphrases = [] }, { Members = [] }] = await Promise.all([
38 api<{ Keys: CalendarKey[] }>(getAllCalendarKeys(CalendarID)),
39 api<{ Passphrases: Passphrase[] }>(getPassphrases(CalendarID)),
40 api<{ Members: CalendarMember[] }>(queryMembers(CalendarID)),
43 const { Member: selfMember, Address: selfAddress } = getMemberAddressWithAdminPermissions(Members, addresses);
44 const addressKeys = await getAddressKeys(selfAddress.ID);
45 const { privateKeys, publicKeys } = splitKeys(addressKeys);
47 const decryptedPrimaryPassphrase = await (() => {
48 const targetPassphrase = Passphrases.find(({ Flags }) => Flags === 1);
49 if (!targetPassphrase) {
50 throw new Error('Passphrase not found');
52 const { MemberPassphrases = [] } = targetPassphrase;
53 const memberPassphrase = MemberPassphrases.find(({ MemberID }) => MemberID === selfMember.ID);
54 if (!memberPassphrase) {
55 throw new Error('Member passphrase not found');
57 const { Passphrase, Signature } = memberPassphrase;
58 return decryptPassphrase({
59 armoredPassphrase: Passphrase,
60 armoredSignature: Signature,
67 Keys.filter(({ Flags }) => Flags === 0).map(async ({ PassphraseID, PrivateKey, ID: KeyID }) => {
69 const targetPassphrase = Passphrases.find(
70 ({ ID: otherPassphraseID }) => otherPassphraseID === PassphraseID
72 if (!targetPassphrase) {
73 throw new Error('Passphrase not found');
75 const { MemberPassphrases = [] } = targetPassphrase;
76 const memberPassphrase = MemberPassphrases.find(({ MemberID }) => MemberID === selfMember.ID);
77 if (!memberPassphrase) {
78 throw new Error('Member passphrase not found');
80 const { Passphrase, Signature } = memberPassphrase;
81 const decryptedPassphrase = await decryptPassphrase({
82 armoredPassphrase: Passphrase,
83 armoredSignature: Signature,
87 const privateKey = await CryptoProxy.importPrivateKey({
88 armoredKey: PrivateKey,
89 passphrase: decryptedPassphrase,
91 const armoredEncryptedKey = await CryptoProxy.exportPrivateKey({
92 privateKey: privateKey,
93 passphrase: decryptedPrimaryPassphrase,
95 return await api<ReenableKeyResponse>(
96 reactivateCalendarKey(CalendarID, KeyID, { PrivateKey: armoredEncryptedKey })
105 export const reactivateCalendarsKeys = async ({
110 }: ReactivateCalendarsKeysArguments) => {
112 calendars.map(async (calendar) => {
113 return reactivateCalendarKeys({