forked from AztecProtocol/noir-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoirServer.ts
58 lines (46 loc) · 1.98 KB
/
noirServer.ts
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
import initNoirWasm, { acir_read_bytes, compile } from '@noir-lang/noir_wasm';
// @ts-ignore
import { initialiseResolver } from '@noir-lang/noir-source-resolver';
import initialiseAztecBackend from '@noir-lang/aztec_backend';
// @ts-ignore
import { setup_generic_prover_and_verifier } from '@noir-lang/barretenberg';
import path from 'path';
import fs from "fs"
import { Noir } from './noir';
export class NoirServer extends Noir {
async compile() {
// I'm running on the server so I can use the file system
initialiseResolver((id: any) => {
try {
const code = fs.readFileSync(`circuits/src/${id}`, { encoding: 'utf8' }) as string;
return code
} catch (err) {
console.error(err);
throw err;
}
});
const compiled_noir = compile({
entry_point: 'file1.nr',
});
this.compiled = compiled_noir;
this.acir = acir_read_bytes(this.compiled.circuit);
[this.prover, this.verifier] = await setup_generic_prover_and_verifier(this.acir);
};
getSmartContract() {
const sc = this.verifier.SmartContract();
// The user must have a folder called 'contract' in the root directory. If not, we create it.
if (!fs.existsSync(path.join(__dirname, '../../contract'))) {
console.log('Contract folder does not exist. Creating...');
fs.mkdirSync(path.join(__dirname, '../../contract'));
}
// If the user already has a file called 'plonk_vk.sol' in the 'contract' folder, we delete it.
if (fs.existsSync(path.join(__dirname, '../../contract/plonk_vk.sol'))) {
fs.unlinkSync(path.join(__dirname, '../../contract/plonk_vk.sol'));
}
// We write the contract to a file called 'plonk_vk.sol' in the 'contract' folder.
fs.writeFileSync(path.join(__dirname, '../../contract/plonk_vk.sol'), sc, {
flag: 'w',
});
return sc;
}
}