-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b06197
commit c020437
Showing
10 changed files
with
208 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,27 @@ | ||
import fs from 'fs'; | ||
import util from 'util'; | ||
import zokrates from '@eyblockchain/zokrates-zexe.js'; | ||
import path from 'path'; | ||
import rabbitmq from '../utils/rabbitmq.mjs'; | ||
import { getProofFromFile } from '../utils/filing.mjs'; | ||
import logger from '../utils/logger.mjs'; | ||
|
||
const unlink = util.promisify(fs.unlink); | ||
import generateProof from '../services/generateProof.mjs'; | ||
|
||
export default function receiveMessage() { | ||
const outputPath = `./output`; | ||
|
||
rabbitmq.receiveMessage('generate-proof', async message => { | ||
const { replyTo, correlationId } = message.properties; | ||
const { | ||
folderpath, | ||
inputs, | ||
transactionInputs, | ||
outputDirectoryPath, | ||
proofFileName, | ||
commitmentHash, | ||
backend = 'zexe', | ||
provingScheme = 'gm17', | ||
} = JSON.parse(message.content.toString()); | ||
|
||
const response = { | ||
error: null, | ||
data: null, | ||
}; | ||
|
||
const circuitName = path.basename(folderpath); | ||
|
||
const witnessFile = commitmentHash | ||
? `${circuitName}_${commitmentHash}_witness` | ||
: `${circuitName}_witness`; | ||
|
||
const proofJsonFile = commitmentHash | ||
? `${circuitName}_${commitmentHash}_proof.json` | ||
: `${circuitName}_proof.json`; | ||
|
||
const opts = {}; | ||
opts.createFile = true; | ||
opts.directory = outputDirectoryPath || `./output/${folderpath}`; | ||
opts.fileName = proofFileName || `${proofJsonFile}`; | ||
|
||
try { | ||
logger.info('Compute witness...'); | ||
await zokrates.computeWitness( | ||
`${outputPath}/${folderpath}/${circuitName}_out`, | ||
`${outputPath}/${folderpath}/`, | ||
`${witnessFile}`, | ||
inputs, | ||
); | ||
|
||
logger.info('Generate proof...'); | ||
await zokrates.generateProof( | ||
`${outputPath}/${folderpath}/${circuitName}_pk.key`, | ||
`${outputPath}/${folderpath}/${circuitName}_out`, | ||
`${outputPath}/${folderpath}/${witnessFile}`, | ||
provingScheme, | ||
backend, | ||
opts, | ||
); | ||
|
||
const { proof, inputs: publicInputs } = await getProofFromFile(`${folderpath}/${proofJsonFile}`); | ||
|
||
logger.info(`Complete`); | ||
logger.debug(`Responding with proof and inputs:`); | ||
logger.debug(proof); | ||
logger.debug(publicInputs); | ||
|
||
response.data = { proof, inputs: publicInputs, transactionInputs }; | ||
response.data = await generateProof(JSON.parse( | ||
message.content.toString(), | ||
)); | ||
} catch (err) { | ||
logger.error('Error in generate-proof', err); | ||
response.error = 'Proof generation failed'; | ||
} | ||
|
||
// Delete previous witness/proof files if they exist. | ||
// Prevents bad inputs from going through anyway. | ||
try { | ||
await unlink(`${outputPath}/${folderpath}/${witnessFile}`); | ||
await unlink(`${outputPath}/${folderpath}/${proofJsonFile}`); | ||
} catch { | ||
// No files to delete. Do nothing. | ||
} | ||
|
||
rabbitmq.sendMessage(replyTo, response, { correlationId, type: folderpath }); | ||
rabbitmq.sendMessage(replyTo, response, { correlationId }); | ||
rabbitmq.sendACK(message); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,17 @@ | ||
import fs from 'fs'; | ||
import util from 'util'; | ||
import express from 'express'; | ||
import zokrates from '@eyblockchain/zokrates-zexe.js'; | ||
import path from 'path'; | ||
import { getProofFromFile } from '../utils/filing.mjs'; | ||
import logger from '../utils/logger.mjs'; | ||
|
||
const unlink = util.promisify(fs.unlink); | ||
import generateProof from '../services/generateProof.mjs'; | ||
|
||
const router = express.Router(); | ||
|
||
const outputPath = `./output`; | ||
|
||
router.post('/', async (req, res, next) => { | ||
req.setTimeout(3600000); // 1 hour | ||
const { | ||
folderpath, | ||
inputs, | ||
transactionInputs, | ||
outputDirectoryPath, | ||
proofFileName, | ||
commitmentHash, | ||
backend = 'zexe', | ||
provingScheme = 'gm17', | ||
} = req.body; | ||
|
||
logger.info(`Received request to /generate-proof`); | ||
logger.debug(JSON.stringify(req.body, null, 2)); | ||
|
||
const circuitName = path.basename(folderpath); | ||
|
||
const witnessFile = commitmentHash | ||
? `${circuitName}_${commitmentHash}_witness` | ||
: `${circuitName}_witness`; | ||
|
||
const proofJsonFile = commitmentHash | ||
? `${circuitName}_${commitmentHash}_proof.json` | ||
: `${circuitName}_proof.json`; | ||
|
||
|
||
const opts = {}; | ||
opts.createFile = true; | ||
opts.directory = outputDirectoryPath || `./output/${folderpath}`; | ||
opts.fileName = proofFileName || `${proofJsonFile}`; | ||
|
||
try { | ||
logger.info('Compute witness...'); | ||
await zokrates.computeWitness( | ||
`${outputPath}/${folderpath}/${circuitName}_out`, | ||
`${outputPath}/${folderpath}/`, | ||
`${witnessFile}`, | ||
inputs, | ||
); | ||
|
||
logger.info('Generate proof...'); | ||
await zokrates.generateProof( | ||
`${outputPath}/${folderpath}/${circuitName}_pk.key`, | ||
`${outputPath}/${folderpath}/${circuitName}_out`, | ||
`${outputPath}/${folderpath}/${witnessFile}`, | ||
provingScheme, | ||
backend, | ||
opts, | ||
); | ||
|
||
const { proof, inputs: publicInputs } = await getProofFromFile(`${folderpath}/${proofJsonFile}`); | ||
|
||
logger.info(`Complete`); | ||
logger.debug(`Responding with proof and inputs:`); | ||
logger.debug(JSON.stringify(req.body, null, 2)); | ||
logger.debug(publicInputs); | ||
res.send({ | ||
proof, | ||
inputs: publicInputs, | ||
type: folderpath, | ||
transactionInputs, | ||
}); | ||
res.send(await generateProof(req.body)); | ||
} catch (err) { | ||
next(err); | ||
} | ||
|
||
// Delete previous witness/proof files if they exist. | ||
// Prevents bad inputs from going through anyway. | ||
try { | ||
await unlink(`${outputPath}/${folderpath}/${witnessFile}`); | ||
await unlink(`${outputPath}/${folderpath}/${proofJsonFile}`); | ||
} catch { | ||
// Do nothing. It's okay if files don't exist. | ||
} | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.