-
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.
Merge pull request #3 from proofcarryingdata/feat/server-verification
Server-side PCD verification
- Loading branch information
Showing
4 changed files
with
107 additions
and
80 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,5 +1,6 @@ | ||
{ | ||
"name": "server", | ||
"type": "module", | ||
"version": "0.1.0", | ||
"main": "src/index.ts", | ||
"packageManager": "[email protected]", | ||
|
@@ -8,6 +9,7 @@ | |
"start": "ts-node --esm src/index.ts" | ||
}, | ||
"dependencies": { | ||
"@pcd/eddsa-pcd": "^0.1.1", | ||
"cors": "^2.8.5", | ||
"dotenv": "^16.3.1", | ||
"express": "^4.18.2" | ||
|
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,60 +1,77 @@ | ||
import express, { Express, Request, Response } from 'express'; | ||
import dotenv from "dotenv" | ||
import { deserialize, init as initEdDSAPCD, verify } from "@pcd/eddsa-pcd" | ||
import cors from "cors" | ||
import dotenv from "dotenv" | ||
import express, { Express, Request, Response } from "express" | ||
|
||
dotenv.config({ path: `${process.cwd()}/../../.env` }) | ||
|
||
const app: Express = express(); | ||
const port = process.env.SERVER_PORT || 3000; | ||
// The PCD package must be initialized before using its methods. | ||
await initEdDSAPCD() | ||
|
||
const app: Express = express() | ||
const port = process.env.SERVER_PORT || 3000 | ||
|
||
// Middlewares. | ||
app.use(express.json()) | ||
app.use(express.urlencoded({ extended: true })) | ||
app.use(cors()) | ||
|
||
app.get('/', (req: Request, res: Response) => { | ||
res.send('Express + TypeScript Server'); | ||
}); | ||
app.get("/", (_req: Request, res: Response) => { | ||
res.send("Express + TypeScript Server") | ||
}) | ||
|
||
// Store a color on a NodeJS environment variable. | ||
app.post('/color/set', (req: Request, res: Response) => { | ||
try { | ||
if (!req.body.color) { | ||
console.error(`[ERROR] No color specified!`) | ||
app.post("/color", async (req: Request, res: Response) => { | ||
try { | ||
if (!req.body.pcd) { | ||
console.error(`[ERROR] No PCD specified`) | ||
|
||
res.status(400).send() | ||
return | ||
} | ||
|
||
const pcd = await deserialize(req.body.pcd) | ||
|
||
res.status(400).send() | ||
} else { | ||
// Set the color on NodeJS `COLOR` environment variable. | ||
process.env['COLOR'] = req.body.color; | ||
if (!(await verify(pcd))) { | ||
console.error(`[ERROR] PCD is not valid`) | ||
|
||
console.debug(`[OKAY] color has been set to ${process.env.COLOR}`) | ||
res.status(401).send() | ||
return | ||
} | ||
|
||
res.status(200).send() | ||
// Set the color on NodeJS `COLOR` environment variable. | ||
process.env.COLOR = `#${pcd.claim.message[0].toString(16)}` | ||
|
||
console.debug(`[OKAY] color has been set to ${process.env.COLOR}`) | ||
|
||
res.json({ color: process.env.COLOR }).status(200).send() | ||
} catch (error: any) { | ||
console.error(`[ERROR] ${error}`) | ||
|
||
res.send(500) | ||
} | ||
} catch (error: any) { | ||
console.error(`[ERROR] ${error}`) | ||
res.send(500) | ||
} | ||
}); | ||
}) | ||
|
||
// Get the color stored on the NodeJS environment variable. | ||
app.get('/color/get', (req: Request, res: Response) => { | ||
try { | ||
if (!process.env.COLOR) { | ||
console.error(`[ERROR] No color has been saved yet!`) | ||
app.get("/color", (_req: Request, res: Response) => { | ||
try { | ||
if (!process.env.COLOR) { | ||
console.error(`[ERROR] No color has been stored yet`) | ||
|
||
res.status(404).send() | ||
return | ||
} | ||
|
||
console.debug(`[OKAY] color ${process.env.COLOR} has been successfully sent`) | ||
|
||
res.status(404).send() | ||
} else { | ||
console.debug(`[OKAY] color ${process.env.COLOR} has been successfully sent`) | ||
res.json({ color: process.env.COLOR }).status(200) | ||
} catch (error: any) { | ||
console.error(`[ERROR] ${error}`) | ||
|
||
res.json({ color: process.env.COLOR }).status(200); | ||
res.send(500) | ||
} | ||
} catch (error: any) { | ||
console.error(`[ERROR] ${error}`) | ||
res.send(500) | ||
} | ||
}); | ||
}) | ||
|
||
app.listen(port, () => { | ||
console.log(`⚡️[server]: Server is running at http://localhost:${port}`); | ||
}); | ||
console.log(`⚡️[server]: Server is running at http://localhost:${port}`) | ||
}) |
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