-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal-api-3003.js
More file actions
92 lines (79 loc) · 3.45 KB
/
real-api-3003.js
File metadata and controls
92 lines (79 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const express = require('express');
const { spawn } = require('child_process');
const fs = require('fs');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const BINARY_PATH = './dilithium-zkvm/target/release/host';
app.post('/api/generate-keypair', (req, res) => {
const process = spawn(BINARY_PATH, ['generate-keypair']);
let output = '';
let error = '';
process.stdout.on('data', (data) => output += data.toString());
process.stderr.on('data', (data) => error += data.toString());
process.on('close', (code) => {
if (code === 0) {
try {
const publicKey = fs.readFileSync('public_key.bin');
const crypto = require('crypto');
const hash = crypto.createHash('sha256').update(publicKey).digest('hex');
res.json({ success: true, publicKeyHash: hash, output });
} catch (err) {
res.json({ success: false, error: err.message });
}
} else {
res.json({ success: false, error: error || 'Key generation failed' });
}
});
});
app.post('/api/sign', (req, res) => {
const { message } = req.body;
const process = spawn(BINARY_PATH, ['sign', '--private-key', 'private_key.bin', '--message', message, '--output', 'signature.bin']);
let output = '';
let error = '';
process.stdout.on('data', (data) => output += data.toString());
process.stderr.on('data', (data) => error += data.toString());
process.on('close', (code) => {
if (code === 0) {
try {
const signature = fs.readFileSync('signature.bin');
res.json({ success: true, signature: signature.toString('hex'), output });
} catch (err) {
res.json({ success: false, error: err.message });
}
} else {
res.json({ success: false, error: error || 'Signing failed' });
}
});
});
app.post('/api/verify', (req, res) => {
const { message } = req.body;
const process = spawn(BINARY_PATH, ['verify', '--public-key', 'public_key.bin', '--signature', 'signature.bin', '--message', message, '--output', 'receipt.bin']);
let output = '';
let error = '';
process.stdout.on('data', (data) => output += data.toString());
process.stderr.on('data', (data) => error += data.toString());
process.on('close', (code) => {
if (code === 0) {
const extractProcess = spawn(BINARY_PATH, ['extract', '--receipt', 'receipt.bin', '--format', 'hex']);
let extractOutput = '';
extractProcess.stdout.on('data', (data) => extractOutput += data.toString());
extractProcess.on('close', () => {
try {
const lines = extractOutput.trim().split('\n');
const journal = lines.find(l => l.startsWith('journal:')).split(': ')[1];
const seal = lines.find(l => l.startsWith('seal:')).split(': ')[1];
res.json({ success: true, proof: { journal, seal, isValid: true }, output });
} catch (err) {
res.json({ success: false, error: 'Failed to extract proof data' });
}
});
} else {
res.json({ success: false, error: error || 'Verification failed' });
}
});
});
app.listen(3003, () => {
console.log('REAL API server running on port 3003');
});