-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (71 loc) · 2.54 KB
/
app.js
File metadata and controls
83 lines (71 loc) · 2.54 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
import express from 'express';
import { writeFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import hardhat from 'hardhat';
import { exec } from 'child_process';
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){
console.log("Writing file")
// Save contract source code
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!');
}
})
// Compile contract
console.log("Compiling contract")
await hardhat.run('compile', {
sources: [filePath]
});
console.log("Contract compiled")
}
async function deployCompiledContract() {
await hardhat.run("deploy")
}
app.post("/compile-circuit-and-generate-proof", async (req, res) => {
let noirSourceCode = req.get("noirSourceCode");
let inputs = req.get("inputs");
let response = { object: undefined, errors: [] };
try {
// Write noir source code to file
console.log("Writing file")
const filePath = path.join(__dirname, '/artifacts/main.nr');
writeFileSync(filePath, noirSourceCode, (err) => {
if (err) {console.error('Error writing to file:', err)} else console.log('File written successfully!')})
// The command should write the compiled circuit to a file (to use it later) and return the file and the proof
// The compiled circuit is binary + abi
let { proof, compiledCircuit } = await hardhat.run("generate-proof", {
noirSourceCode,
inputs
});
response.object = { proof };
} catch (e) {
console.log(e);
response.errors.push(e.message);
}
});
app.post("/compile-and-deploy-contract", async (req, res) => {
let response = { object: undefined, errors: [] }
try {
let { contractSourceCode } = req.body;
await compileContract(contractSourceCode)
await deployCompiledContract()
let deployment = await import("./artifacts/deployment_with_address.json", { assert: { type: 'json' } });
exec("wagmi generate")
response.object = { contractAddress: deployment.default.address }
} 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}!`));