// Encryption key management (desktop). Hidden where the platform has no key store yet. function pgpAvailable() { return Boolean(window.daybreakMail?.pgp); } function openEncryptionKeys() { const root = $('#modal-root'); const account = connectedMailAccount || {}; root.innerHTML = `
`; const close = () => { root.innerHTML = ''; }; root.querySelector('.modal-close').onclick = close; root.querySelector('.modal-overlay').onclick = event => { if (event.target.classList.contains('modal-overlay')) close(); }; const status = message => { $('#pgp-status').textContent = message; }; const passphraseField = () => $('#pgp-passphrase'); const needPassphrase = () => { const value = passphraseField().value; if (!value) { status('Enter the key passphrase in the Passphrase field first.'); passphraseField().focus(); } return value; }; async function render() { const list = $('#pgp-key-list'); let keys = []; try { keys = await window.daybreakMail.pgp.list(); } catch (error) { list.textContent = error.message || 'Could not load keys.'; return; } list.replaceChildren(); if (!keys.length) { list.textContent = 'No keys yet.'; return; } keys.forEach(key => { const row = document.createElement('div'); row.className = 'attachment-row'; const info = document.createElement('div'); info.className = 'attachment-row-info'; const title = document.createElement('strong'); title.textContent = key.email || 'OpenPGP key'; const meta = document.createElement('small'); meta.textContent = `${key.name ? key.name + ' · ' : ''}${key.algorithm} · ${new Date(key.createdAt).toLocaleDateString()}`; const fingerprint = document.createElement('small'); fingerprint.textContent = `Fingerprint: ${key.fingerprint.match(/.{1,4}/g).join(' ')}`; info.append(title, meta, fingerprint); row.append(info); const actions = [ ['Export public', () => window.daybreakMail.pgp.exportPublic(key.fingerprint), false], ['Export private', () => window.daybreakMail.pgp.exportPrivate(key.fingerprint, needPassphrase()), true] ]; if (key.hasRevocationCertificate) actions.push(['Revocation cert.', () => window.daybreakMail.pgp.exportRevocation(key.fingerprint, needPassphrase()), true]); actions.push(['Delete', async () => { if (!confirm('Delete this key? Encrypted mail that needs it can no longer be read unless you have a backup.')) return { canceled: true }; await window.daybreakMail.pgp.remove(key.fingerprint, needPassphrase()); await render(); return { deleted: true }; }, true]); actions.forEach(([label, run, secret]) => { const button = document.createElement('button'); button.className = 'discard'; button.textContent = label; button.onclick = async () => { if (secret && !passphraseField().value) { needPassphrase(); return; } try { const result = await run(); status(result?.saved ? 'File saved.' : result?.deleted ? 'Key deleted.' : ''); } catch (error) { status(error.message || 'That action failed.'); } }; row.append(button); }); list.append(row); }); } $('#pgp-generate').onclick = async () => { const button = $('#pgp-generate'); button.disabled = true; status('Creating key…'); try { await window.daybreakMail.pgp.generate({ name: $('#pgp-name').value, email: $('#pgp-email').value, passphrase: passphraseField().value, accountId: account.id || null }); status('Key created. Export your private key and revocation certificate now and keep them safe.'); await render(); } catch (error) { status(error.message || 'Could not create the key.'); } button.disabled = false; }; $('#pgp-import').onclick = async () => { const passphrase = needPassphrase(); if (!passphrase) return; try { const result = await window.daybreakMail.pgp.importKey({ passphrase, accountId: account.id || null }); status(result.canceled ? '' : 'Key imported.'); await render(); } catch (error) { status(error.message || 'Could not import the key.'); } }; void render(); } if (typeof openPreferences === 'function') { const baseOpenPreferences = openPreferences; openPreferences = function () { baseOpenPreferences(); const fields = $('#modal-root .compose-fields'); if (!fields || !pgpAvailable()) return; const button = document.createElement('button'); button.type = 'button'; button.className = 'discard'; button.id = 'open-encryption-keys'; button.textContent = 'Encryption keys…'; button.onclick = openEncryptionKeys; fields.append(button); }; }