-
Notifications
You must be signed in to change notification settings - Fork 8
/
submit.ts
135 lines (119 loc) · 3.71 KB
/
submit.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
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
import type { NextApiRequest, NextApiResponse } from "next";
import { getSigPublicSignals } from "../../utils/wasmPrecompute/wasmPrecompute.nodejs";
import { PublicSignatureData } from "../../utils/wasmPrecompute/wasmPrecompute.common";
import { postScreenshot } from "../../utils/post-screenshot";
import { prisma } from "../../utils/prisma";
import vkey from "../../utils/verification_key.json";
import _ from "lodash";
import { HOST, postToIpfs } from "../../utils/ipfs";
import { JSONStringifyCustom, POST_CHAR_LIMIT } from "../../utils/utils";
import { NounSet } from "../../components/anonPill";
const snarkjs = require("snarkjs");
export async function verifyProof(publicSignals: any, proof: any) {
const proofVerified = await snarkjs.groth16.verify(
vkey,
publicSignals,
proof
);
return proofVerified;
}
async function verifyRoot(
root: string,
propId: string,
groupType: string
): Promise<boolean> {
const group = await prisma.group.findFirst({
where: {
prop: {
num: Number(propId),
},
typeId: Number(groupType),
},
});
return group !== null && group.root === root;
}
export default async function submit(
req: NextApiRequest,
res: NextApiResponse
) {
try {
let body = req.body;
if (typeof req.body === "string") {
body = JSON.parse(body);
}
console.log(`Received request: ${JSON.stringify(body)}`);
const root = body.root;
const proof = body.proof;
const commentMsg = body.commentMsg;
const publicSignatureData: PublicSignatureData = body.publicSignatureData;
if (commentMsg.length > POST_CHAR_LIMIT) {
res.status(400).send("commentMsg is too long!");
return;
}
if (
!(await verifyRoot(
root,
publicSignatureData.eip712Value.propId,
publicSignatureData.eip712Value.groupType
))
) {
res.status(400).send("merkle root does not match group specified!");
return;
}
const { TPreComputes, U } = await getSigPublicSignals(publicSignatureData);
const verifiedProof = await verifyProof(
[
root,
// NOTE: when propId = -1, 2^32-1 must be passed to snarkjs
publicSignatureData.eip712Value.propId === "-1"
? "4294967295"
: publicSignatureData.eip712Value.propId,
publicSignatureData.eip712Value.groupType,
..._.flattenDeep(TPreComputes).map((el: any) => el.toString()),
..._.flattenDeep(U).map((el: any) => el.toString()),
],
proof
);
console.log(`Verified proof: ${verifiedProof}`);
if (!verifiedProof) {
res.status(400).send("proof is not valid!");
} else {
const cid = await postToIpfs(
JSONStringifyCustom({
proof,
commentMsg,
propId: publicSignatureData.eip712Value.propId,
groupType: publicSignatureData.eip712Value.groupType,
TPreComputes,
U,
})
);
const proofIPFS = `https://${HOST}/ipfs/${cid}`;
const propId = Number(publicSignatureData.eip712Value.propId);
const newComment = await prisma.comment.create({
data: {
prop: {
connect: {
num: propId,
},
},
group: {
connect: {
id: Number(publicSignatureData.eip712Value.groupType),
},
},
commentMsg,
ipfsProof: proofIPFS,
},
});
const nounSet = Number(
publicSignatureData.eip712Value.groupType
) as NounSet;
await postScreenshot({ text: commentMsg, nounSet }, proofIPFS, propId);
res.status(200).json(newComment);
}
} catch (ex: unknown) {
console.error(ex);
res.status(400).send("something went wrong!");
}
}