forked from privacy-scaling-explorations/maci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_v1.js
112 lines (99 loc) · 3.68 KB
/
handler_v1.js
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
const path = require('path')
const shelljs = require('shelljs')
const logger = require('./logger').logger
const db = require('./db')
const { whitelist } = require('utils')
const cliPath = path.join(
__dirname,
'..',
'cli',
)
const cliCmd = `cd ${cliPath} && node build/index.js`
async function handler(req, res, dbClient) {
let output, cmd, query, dbres
let silent = true
switch(req.body["method"]) {
case "signup":
if(!("pubkey" in req.body && req.body["pubkey"]) || !("maci" in req.body && req.body["maci"])){
res.send("missing parameters...")
break
}
const pubKey = req.body['pubkey']
const maci = req.body['maci']
if (!whitelist(pubKey) || !whitelist(maci)) break
query = { 'MACI': req.body["maci"]};
dbres = await dbClient.db(db.dbName).collection(db.collectionName).findOne(query)
if(!dbres) {
res.send(`MACI contract address ${req.body["maci"]} not exist`)
break
}
cmd = `${cliCmd} signup -p ${pubKey} -x ${maci}`
logger.debug(`process signup...${cmd}`)
output = shelljs.exec(cmd, { silent })
break
case "genkey":
cmd = `${cliCmd} genMaciKeypair`
logger.debug(`process genkey...${cmd}`)
output = shelljs.exec(cmd, { silent })
break
case "publish":
if(!("pubkey" in req.body && req.body["pubkey"]) || !("maci" in req.body && req.body["maci"])){
res.send("missing parameters...")
break
}
if(
!whitelist(req.body['pubKey']) ||
!whitelist(req.body['maci']) ||
!whitelist(req.body['privKey']) ||
!whitelist(req.body["state_index"]) ||
!whitelist(req.body["vote_option_index"]) ||
!whitelist(req.body["new_vote_weight"]) ||
!whitelist(req.body["nonce"]) ||
!whitelist(req.body["poll_id"])
) break
query = { 'MACI': req.body["maci"]}
dbres = await dbClient.db(db.dbName).collection(db.collectionName).findOne(query)
if(!dbres) {
res.send(`MACI contract address ${req.body["maci"]} not exist`)
break
}
cmd = `${cliCmd} publish -p ${req.body["pubkey"]} -x ${req.body["maci"]} -sk ${req.body["privkey"]} -i ${req.body["state_index"]} -v ${req.body["vote_option_index"]} -w ${req.body["new_vote_weight"]} -n ${req.body["nonce"]} -o ${req.body["poll_id"]}`
if (req.body["salt"]) {
cmd += ` -s ${req.body["salt"]}`
}
logger.debug(`publishMessage...${cmd}`)
output = shelljs.exec(cmd, { silent })
break
case "verify":
if(!("maci" in req.body && req.body["maci"])||!("poll_id" in req.body && req.body["poll_id"])){
res.send("missing parameters...")
break
}
if (!whitelist(req.body['maci']) || !whitelist(req.body['poll_id'])) break
query = { 'MACI': req.body["maci"]}
dbres = await dbClient.db(db.dbName).collection(db.collectionName).findOne(query)
if(!dbres) {
res.send(`MACI contract address ${req.body["maci"]} not exist`)
break
}
const pptKey = "PollProcessorAndTally-" + req.body["poll_id"]
if (!(pptKey in dbres)) {
res.send(`PollProcessAndTally contract for poll ${req.body["poll_id"]} not exists`)
break
}
pptAddr = dbres[pptKey]
cmd = `${cliCmd} verify -t ${req.body["tally_file"]} -x ${req.body["maci"]} -o ${req.body["poll_id"]} -q ${pptAddr}`
output = shelljs.exec(cmd, { silent })
break
default:
res.send("unknown method...")
}
if (!output) {
return
} else if(output.stderr) {
res.send(`${req.body.method} failed with error: ${output.stderr}`)
} else {
res.send(`${output}`)
}
}
module.exports = handler;