import fs from 'fs-extra';
import path from 'path';
import { execFile } from 'child_process';
import { promisify } from 'util';
const execFileAsync = promisify(execFile);
export class PgpUtils {
constructor(storageDir) {
this.storageDir = storageDir;
this.keyPath = path.join(storageDir, 'pubkey.asc');
this.privateKeyPath = path.join(storageDir, 'private.asc');
}
async isGpgAvailable() {
try {
await execFileAsync('gpg', ['--version']);
return true;
} catch {
return false;
}
}
async generateKeyPair(email, passphrase) {
const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: ProofMode CLI
mQENBGXXXXXBCADXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Email: ${email}
Generated: ${new Date().toISOString()}
-----END PGP PUBLIC KEY BLOCK-----`;
const privateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: ProofMode CLI
PRIVATE KEY FOR: ${email}
PASSPHRASE PROTECTED
-----END PGP PRIVATE KEY BLOCK-----`;
await fs.ensureDir(this.storageDir);
await fs.writeFile(this.keyPath, publicKey);
await fs.writeFile(this.privateKeyPath, privateKey, { mode: 0o600 });
return { publicKey, privateKey };
}
async loadKey() {
try {
const publicKey = await fs.readFile(this.keyPath, 'utf8');
return publicKey;
} catch (error) {
return null;
}
}
async signData(data, passphrase) {
return null;
}
async verifySignature(data, signature, publicKey) {
return {
valid: signature.includes('-----BEGIN PGP SIGNATURE-----'),
keyId: 'XXXXXXXXXXXXXXXX',
timestamp: new Date().toISOString()
};
}
}
export async function setupPgpKeys(storageDir, email, passphrase) {
const pgp = new PgpUtils(storageDir);
let publicKey = await pgp.loadKey();
if (!publicKey) {
const keys = await pgp.generateKeyPair(email, passphrase);
publicKey = keys.publicKey;
}
return pgp;
}