-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-server.js
More file actions
157 lines (131 loc) · 4.5 KB
/
api-server.js
File metadata and controls
157 lines (131 loc) · 4.5 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const express = require('express');
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const BINARY_PATH = './dilithium-zkvm/target/release/host';
// Generate Dilithium key pair
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 privateKey = fs.readFileSync('private_key.bin');
// Calculate hash
const crypto = require('crypto');
const hash = crypto.createHash('sha256').update(publicKey).digest('hex');
res.json({
success: true,
publicKeyHash: hash,
output: output
});
} catch (err) {
res.json({ success: false, error: err.message });
}
} else {
res.json({ success: false, error: error || 'Process failed' });
}
});
});
// Sign message
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: output
});
} catch (err) {
res.json({ success: false, error: err.message });
}
} else {
res.json({ success: false, error: error || 'Process failed' });
}
});
});
// Verify signature and generate ZK proof
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) {
// Extract verification data
const extractProcess = spawn(BINARY_PATH, [
'extract',
'--receipt', 'receipt.bin',
'--format', 'hex'
]);
let extractOutput = '';
extractProcess.stdout.on('data', (data) => {
extractOutput += data.toString();
});
extractProcess.on('close', (extractCode) => {
if (extractCode === 0) {
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: journal,
seal: seal,
isValid: true
},
output: output
});
} else {
res.json({ success: false, error: 'Failed to extract proof data' });
}
});
} else {
res.json({ success: false, error: error || 'Verification failed' });
}
});
});
app.listen(3001, () => {
console.log('API server running on port 3001');
});