|
1 | | -import fs from 'fs'; |
2 | | -import util from 'util'; |
3 | | -import zokrates from '@eyblockchain/zokrates-zexe.js'; |
4 | | -import path from 'path'; |
5 | 1 | import rabbitmq from '../utils/rabbitmq.mjs'; |
6 | | -import { getProofByCircuitPath } from '../utils/filing.mjs'; |
7 | 2 | import logger from '../utils/logger.mjs'; |
8 | | - |
9 | | -const unlink = util.promisify(fs.unlink); |
| 3 | +import generateProof from '../services/generateProof.mjs'; |
10 | 4 |
|
11 | 5 | export default function receiveMessage() { |
12 | | - const outputPath = `./output/`; |
13 | | - |
14 | 6 | rabbitmq.receiveMessage('generate-proof', async message => { |
15 | 7 | const { replyTo, correlationId } = message.properties; |
16 | | - const { |
17 | | - folderpath, |
18 | | - inputs, |
19 | | - transactionInputs, |
20 | | - outputDirectoryPath, |
21 | | - proofFileName, |
22 | | - backend = 'zexe', |
23 | | - provingScheme = 'gm17', |
24 | | - } = JSON.parse(message.content.toString()); |
25 | 8 |
|
26 | 9 | const response = { |
27 | 10 | error: null, |
28 | 11 | data: null, |
29 | 12 | }; |
30 | 13 |
|
31 | | - const circuitName = path.basename(folderpath); |
32 | | - |
33 | | - // Delete previous witness/proof files if they exist. |
34 | | - // Prevents bad inputs from going through anyway. |
35 | | - try { |
36 | | - await unlink(`${outputPath}/${folderpath}/${circuitName}_witness`); |
37 | | - await unlink(`${outputPath}/${folderpath}/${circuitName}_proof.json`); |
38 | | - } catch { |
39 | | - // No files to delete. Do nothing. |
40 | | - } |
41 | | - |
42 | | - const opts = {}; |
43 | | - opts.createFile = true; |
44 | | - opts.directory = `./output/${folderpath}` || outputDirectoryPath; |
45 | | - opts.fileName = `${circuitName}_proof.json` || proofFileName; |
46 | | - |
47 | 14 | try { |
48 | | - logger.info('Compute witness...'); |
49 | | - await zokrates.computeWitness( |
50 | | - `${outputPath}/${folderpath}/${circuitName}_out`, |
51 | | - `${outputPath}/${folderpath}/`, |
52 | | - `${circuitName}_witness`, |
53 | | - inputs, |
54 | | - ); |
55 | | - |
56 | | - logger.info('Generate proof...'); |
57 | | - await zokrates.generateProof( |
58 | | - `${outputPath}/${folderpath}/${circuitName}_pk.key`, |
59 | | - `${outputPath}/${folderpath}/${circuitName}_out`, |
60 | | - `${outputPath}/${folderpath}/${circuitName}_witness`, |
61 | | - provingScheme, |
62 | | - backend, |
63 | | - opts, |
64 | | - ); |
65 | | - |
66 | | - const { proof, inputs: publicInputs } = await getProofByCircuitPath(folderpath); |
67 | | - |
68 | | - logger.info(`Complete`); |
69 | | - logger.debug(`Responding with proof and inputs:`); |
70 | | - logger.debug(proof); |
71 | | - logger.debug(publicInputs); |
72 | | - |
73 | | - response.data = { proof, inputs: publicInputs, transactionInputs }; |
| 15 | + response.data = await generateProof(JSON.parse( |
| 16 | + message.content.toString(), |
| 17 | + )); |
74 | 18 | } catch (err) { |
| 19 | + logger.error('Error in generate-proof', err); |
75 | 20 | response.error = 'Proof generation failed'; |
76 | 21 | } |
77 | 22 |
|
78 | | - rabbitmq.sendMessage(replyTo, response, { correlationId, type: folderpath }); |
| 23 | + |
| 24 | + rabbitmq.sendMessage(replyTo, response, { correlationId }); |
79 | 25 | rabbitmq.sendACK(message); |
80 | 26 | }); |
81 | 27 | } |
0 commit comments