-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (40 loc) · 1.37 KB
/
app.js
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
import express from 'express';
import { writeFileSync, readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import hardhat from 'hardhat';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
async function compileContract(contractSourceCode){
const filePath = path.join(__dirname, '/artifacts/SolidityVerifier.sol');
writeFileSync(filePath, contractSourceCode, (err) => {
if (err) {
console.error('Error writing to file:', err)
} else {
console.log('File written successfully!');
}
})
await hardhat.run('compile');
}
app.post("/compile-contract", async (req,res) => {
let response = { object: undefined, errors: [] }
try {
let { contractSourceCode } = req.body;
await compileContract(contractSourceCode)
let file = readFileSync("./artifacts/hardhat/artifacts/SolidityVerifier.sol/UltraVerifier.json", "utf8")
let json_file = JSON.parse(file)
let contractBytecode = json_file.bytecode
response.object = {
contractBytecode
}
} catch (e) {
console.log(e)
response.errors.push(e.message)
}
res.send(response);
})
const PORT = 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}!`));