-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
52 lines (39 loc) · 1.58 KB
/
index.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
const Koa = require("koa");
const Router = require("@koa/router");
const { isReady, PrivateKey, Field, Signature } = require("snarkyjs");
const PORT = process.env.PORT || 3000;
const app = new Koa();
const router = new Router();
async function getSignedCreditScore(userId) {
// We need to wait for SnarkyJS to finish loading before we can do anything
await isReady;
// The private key of our account. When running locally the hardcoded key will
// be used. In production the key will be loaded from a Vercel environment
// variable.
const privateKey = PrivateKey.fromBase58(
process.env.PRIVATE_KEY ??
"EKF65JKw9Q1XWLDZyZNGysBbYG21QbJf3a4xnEoZPZ28LKYGMw53"
);
// We get the users credit score. In this case it's 787 for user 1, and 536
// for anybody else :)
const knownCreditScore = (userId) => (userId === "1" ? 787 : 536);
// We compute the public key associated with our private key
const publicKey = privateKey.toPublicKey();
// Define a Field with the value of the users id
const id = Field(userId);
// Define a Field with the users credit score
const creditScore = Field(knownCreditScore(userId));
// Use our private key to sign an array of Fields containing the users id and
// credit score
const signature = Signature.create(privateKey, [id, creditScore]);
return {
data: { id: id, creditScore: creditScore },
signature: signature,
publicKey: publicKey,
};
}
router.get("/user/:id", async (ctx) => {
ctx.body = await getSignedCreditScore(ctx.params.id);
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(PORT);