import { ipcMain } from 'electron'; import * as crypto from 'crypto'; import * as fs from 'fs'; import * as path from 'path'; import os from 'os'; ipcMain.handle('save-keys', async (event, { epk, passphrase, userId }) => { try { // Attempt to decipher the epk to verify the passphrase const [ivHex, encryptedKey] = epk.split(':'); const iv = Buffer.from(ivHex, 'hex'); // Create a decipher using the passphrase and IV const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(passphrase.padEnd(32, ' ')), iv); // Decrypt the private key let decryptedPrivateKey = decipher.update(encryptedKey, 'hex', 'utf8'); decryptedPrivateKey += decipher.final('utf8'); // Determine base directory based on OS let baseDir; if (process.platform === 'win32') { baseDir = path.join(os.homedir(), 'AppData', 'Roaming', 'ultima'); } else if (process.platform === 'linux') { baseDir = path.join(os.homedir(), '.ultima'); } else if (process.platform === 'darwin') { baseDir = path.join(os.homedir(), 'Library', 'Application Support', 'Ultima'); } // Ensure the directory exists const userDir = path.join(baseDir, 'Sessions', userId); if (!fs.existsSync(userDir)) { fs.mkdirSync(userDir, { recursive: true }); } // Define file paths const epkFilePath = path.join(userDir, 'epk.txt'); const passphraseFilePath = path.join(userDir, 'passphrase.txt'); // Write epk and derivedKey to files fs.writeFileSync(epkFilePath, epk, { encoding: 'utf8', flag: 'w' }); fs.writeFileSync(passphraseFilePath, passphrase, { encoding: 'utf8', flag: 'w' }); return { success: true }; } catch (error) { return { success: false, error: error.message }; } });