const http = require('http'); const fs = require('fs'); const config = JSON.parse(fs.readFileSync('config.json', 'utf8')); const PORT = config.zhtpPort || 4200; const PEERS = config.peers || []; function loadKeys() { const dir = './users'; if (!fs.existsSync(dir)) return []; return fs .readdirSync(dir) .filter(name => name.endsWith('.key')) .map(name => fs.readFileSync(`${dir}/${name}`, 'utf8').trim()); } function handleRequest(req, res) { let body = ''; req.on('data', chunk => (body += chunk)); req.on('end', () => { try { const json = JSON.parse(body || '{}'); if (json.method === 'ping') { return res.end(JSON.stringify({ jsonrpc: '2.0', id: json.id, result: 'pong' })); } if (json.method === 'getCitizens') { return res.end(JSON.stringify({ jsonrpc: '2.0', id: json.id, result: loadKeys() })); } return res.end(JSON.stringify({ jsonrpc: '2.0', id: json.id, error: 'Unknown method' })); } catch (err) { return res.end(JSON.stringify({ error: 'Invalid JSON-RPC input' })); } }); } http.createServer(handleRequest).listen(PORT, () => { console.log(`✅ Sovereign ZHTP node listening on port ${PORT}`); if (PEERS.length > 0) { console.log(`🔗 Peers: ${PEERS.join(', ')}`); } });