1 import { CryptoProxy, VERIFICATION_STATUS } from '@proton/crypto';
3 import { getContact, updateContact } from '../api/contacts';
4 import { CONTACT_CARD_TYPE } from '../constants';
5 import type { Api, DecryptedKey, Key } from '../interfaces';
6 import type { Contact } from '../interfaces/contacts';
7 import { splitKeys } from '../keys/keys';
8 import { getKeyUsedForContact } from './keyVerifications';
9 import { resignCards } from './resign';
12 * Process all contacts and update each of them without the content encrypted with the given key
14 export const dropDataEncryptedWithAKey = async (
17 userKeys: DecryptedKey[],
19 progressionCallback: (progress: number, updated: number) => void,
20 exitRef: { current: boolean }
23 const { privateKeys } = splitKeys(userKeys);
25 for (let i = 0; i < contacts.length && !exitRef.current; i++) {
26 const contactID = contacts[i].ID;
27 const { Contact } = await api<{ Contact: Contact }>(getContact(contactID));
28 const match = await getKeyUsedForContact(Contact, [referenceKey], true);
31 const Cards = await Promise.all(
34 card.Type !== CONTACT_CARD_TYPE.ENCRYPTED &&
35 card.Type !== CONTACT_CARD_TYPE.ENCRYPTED_AND_SIGNED
36 ).map(async (card) => {
37 let { Signature } = card;
38 if (card.Type === CONTACT_CARD_TYPE.SIGNED) {
39 const signature = await CryptoProxy.signMessage({
41 stripTrailingSpaces: true,
42 signingKeys: [privateKeys[0]],
45 Signature = signature;
47 return { ...card, Signature };
50 await api<{ Contact: Contact }>(updateContact(contactID, { Cards }));
51 // console.log('dropDataEncryptedWithAKey', updateContact(contactID, { Cards }));
53 progressionCallback(i + 1, updated);
58 * Process all contacts and resign each of them with the given key
60 export const resignAllContacts = async (
62 userKeys: DecryptedKey[],
64 progressionCallback: (progress: number, updated: number) => void,
65 exitRef: { current: boolean }
68 const { publicKeys, privateKeys } = splitKeys(userKeys);
70 for (let i = 0; i < contacts.length && !exitRef.current; i++) {
71 const contactID = contacts[i].ID;
72 const { Contact } = await api<{ Contact: Contact }>(getContact(contactID));
74 // Should only be one signed card
75 const signedCard = Contact.Cards.find((card) => card.Type === CONTACT_CARD_TYPE.SIGNED);
77 if (!signedCard || !signedCard.Signature) {
78 progressionCallback(i + 1, updated);
82 const { verified } = await CryptoProxy.verifyMessage({
83 textData: signedCard.Data,
84 stripTrailingSpaces: true,
85 verificationKeys: publicKeys,
86 armoredSignature: signedCard.Signature,
89 if (verified !== VERIFICATION_STATUS.SIGNED_AND_VALID) {
91 const Cards = await resignCards({
92 contactCards: Contact.Cards,
93 privateKeys: [privateKeys[0]],
95 await api<{ Contact: Contact }>(updateContact(contactID, { Cards }));
96 // console.log('resignAllContacts', updateContact(contactID, { Cards }));
98 progressionCallback(i + 1, updated);