forked from ChristopherCobb9302JamesAvila/niftyzk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
271 lines (226 loc) · 10.1 KB
/
Copy pathmain.js
File metadata and controls
271 lines (226 loc) · 10.1 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const { Command } = require("commander");
const figlet = require("figlet");
const { setupWithCurrentDir, setupWithNewDir, checkIfCircomIsInstalled, checkPKGJsonForPlonk } = require("./lib/init/packages");
const { explainPtauFiles, selectPtauFileToDownload, isValidPtauFilename, downloadPtauFile, isValidPlonkSetupFileName, downloadUniversalSetupFiles, explainPlonkitFiles, selectPlonkSetupileToDownload } = require("./lib/loaders/ptauLoader")
const chalk = require("chalk");
const { circuitPrompts } = require("./lib/gencircuit/circuitprompt");
const { compileCircuits } = require("./lib/compile/runcompiler");
const { runServer } = require("./lib/phase2ceremony/server");
const { finalize } = require("./lib/compile/finalize");
const { verificationKey } = require("./lib/compile/verificationkey");
const { genContract } = require("./lib/compile/contract");
const { hotReload } = require("./lib/dev/hotreload");
const { getResponse } = require("./lib/ai/openai");
const { compileWithPlonkit } = require("./lib/compile/runPlonkCompiler");
const { prepareClient } = require("./lib/ai/credentials");
const path = require("path");
const fs = require("fs");
const { isInCwd } = require("./lib/ai/utils");
console.log(figlet.textSync("NiftyZK"))
const program = new Command();
program.version("0.3.0")
.description("Scaffold a new Circom project and circuits, compile it and run Powers of Tau Phase-2 ceremonies. Generate a cosmwasm verifier contract. Supports Groth-16 with a BN128 curve")
.name("niftyzk")
program
.command("init")
.description("Initialize and scaffold a new project")
.option("--plonk", "Use the plonk flag to generate tests for plonk")
.action(async (flags, options) => {
async function onSuccess() {
if (options.args.length === 0) {
await setupWithCurrentDir(flags.plonk).then(() => {
circuitPrompts(undefined, flags.plonk)
});
} else {
const dirname = options.args[0];
await setupWithNewDir(dirname, flags.plonk).then(() => {
circuitPrompts(dirname, flags.plonk)
})
}
}
checkIfCircomIsInstalled(onSuccess)
})
program
.command("ptaufiles")
.description("Display information and download ptau files")
.option("-f, --filename [filename]", "The file to download")
.action(async (option) => {
if (
typeof option.filename === "string" &&
isValidPtauFilename(option.filename)) {
await downloadPtauFile(option.filename)
} else {
explainPtauFiles()
selectPtauFileToDownload()
}
})
program.command("plonkfiles")
.description("Display informtion and download the plonk setup files")
.option("-f, --filename [filename]", "The file to download")
.action(async (option) => {
//TODO: only run this if plonk was set in package.json
if (typeof option.filename === "string" && isValidPlonkSetupFileName(option.filename)) {
await downloadUniversalSetupFiles(option.filename)
} else {
explainPlonkitFiles()
selectPlonkSetupileToDownload()
}
})
program.command("gencircuit")
.description("Generate circom circuits and javascript tests for the current directory")
.action(() => {
circuitPrompts(undefined)
})
program.command("dev")
.description("Hot reload for circuit development. input.js must contain a valid circuit input")
.option("--circuit [path]", "The circuit to test. Defaults to circuits/circuit.circom")
.option("--assertout", "Asserts the output of the circuit. The output must be exported from a getOutput() function from input.js")
.option("--verbose", "Show extra information for debugging")
.action(async (options) => {
await hotReload(options.circuit, options.assertout, options.verbose);
})
program.command("compile")
.description("Compile the circuits. Defaults to Groth16 with a BN254 curve. Use the setup plonk flag if you want to use plonk")
.option("--circuit [path]", "Specify the location for the circuit. Defaults to circuits/circuit.circom")
.action(async (options, command) => {
const hasPlonk = await checkPKGJsonForPlonk()
if (hasPlonk) {
if (options.circuit) {
compileWithPlonkit(options.circuit)
} else {
compileWithPlonkit("")
}
//Compile it with the rust wasm bindings!
} else {
if (options.circuit) {
//Path was specified. compile circuit with that path
compileCircuits(options.circuit)
} else {
//Use default path
compileCircuits("")
}
}
})
program
.command("ceremony")
.description("Only for Groth16. Runs a phase 2 ceremony server that accepts anonymized contributions via a website. Default port is 3000. Prefix the command with PORT=number to change the default port")
.action(() => {
//Run the the ceremony server
runServer()
})
program.command("finalize")
.description("Only for Groth16. Finalize the circuit after the phase2 ceremony is finished")
.option("-b, --beacon [string]", "A random beacon to use for finalizing the ceremony. For example a block hash or a hex number outputted by a Verifiable Delay Function (VDF)")
.option("-i, --iter [num]", "Number of iterations")
.option("-n, --name [string]", "The name of the final contribution")
.action(async (options) => {
if (!options.beacon) {
console.log(chalk.red("Missing beacon option"))
return;
}
console.log(options)
if (typeof options.beacon === "boolean") {
console.log("Missing beacon value")
return;
}
if (options.beacon.length < 10) {
console.log(chalk.red("Beacon too short"))
return;
}
//TODO: Check if the beacon is a valid hex string
await finalize(options.beacon, options.iter, options.name)
})
program
.command("vkey")
.description("Generate the verification key for this circuit.")
.option("--final", "Export the final verification key for PLONK or after the Phase2 ceremony")
.action(async (options) => {
await verificationKey(options.final ?? false).catch(err => {
console.log(err.message)
}).then(() => process.exit(0))
})
program.command("gencontract")
.description("Generate a cosmwasm verifier smart contract")
.option("--circuit", "The full name of the circuit file to use. Defaults to circuit.circom")
.option("--ark", "Use the Arkworks Groth-16 verifier implementation")
.option("--bellman", "Use the Bellman Groth-16 verifier implementation")
.option("--overwrite", "If a contract directory already exists, you are required use this option to overwrite it.")
.option("--folder [name]", "Specify the name of the generated contract's folder")
.action(async (options) => {
if (!options.folder) {
console.log(chalk.red("Use the --folder flag to specify the name of the contract directory"))
return;
}
if (typeof options.folder !== "string") {
console.log(chalk.red("Must specify the name of the folder to generate the contracts to."))
return;
}
const isPlonk = await checkPKGJsonForPlonk();
//Ask only when it's not plonk
if (!isPlonk) {
if (!options.ark && !options.bellman) {
console.log(chalk.red("You need to use either --ark or --bellman implementations!"))
return;
}
if (options.ark && options.bellman) {
console.log(chalk.red("Can't use --ark and --bellman at the same time. Select one."))
return;
}
}
await genContract(options, isPlonk)
})
program
.command("vibe")
.description("Generate code with an LLM. Supporting OpenAI")
.option("--file [name]", "The file to create or update.")
.option("--circom", "Generate circom code")
.option("--cosmwasm", "Generate cosmwasm contract files in rust")
.option("--preserve", "Do not update the file, just print information to the console")
.option("--prompt [message]", "The prompt to update the circom circuit")
.action(async (options) => {
console.log(chalk.blue("NIFTYZK AI IS IN PREVIEW"))
const hasPgkJson = fs.existsSync(path.join(process.cwd(), "package.json"));
if (!hasPgkJson) {
console.log(chalk.red("AI must be invoked in the root of the project"))
return;
}
const client = await prepareClient()
if (!options.circom && !options.cosmwasm) {
console.log(chalk.red("You need to use either --cosmwasm or --circom to chose which code to generate"))
return;
}
if (options.circom && options.cosmwasm) {
console.log(chalk.red("You can't use both circom and cosmwasm at the same time for now."))
return;
}
if (!options.file) {
console.log(chalk.red("You need to specify the file name to create or update"))
return;
}
if (!isInCwd(options.file)) {
console.log(chalk.red("File is not in the currently working directory"))
return;
}
const fileExists = fs.existsSync(options.file)
//Send the prompt
let content = ""
if (fileExists) {
content = fs.readFileSync(options.file)
}
const output_text = await getResponse(
client,
options.prompt,
options.preserve,
content,
fileExists,
options.circom ? "circom" : "cosmwasm"
)
//switch.. write back the file or similar
if (!options.preserve) {
if (output_text) {
//For now just write the file back to disk
fs.writeFileSync(options.file, output_text)
}
}
})
program.parse();