diff --git a/demo/index.html b/demo/index.html index 612b4f0..797d492 100644 --- a/demo/index.html +++ b/demo/index.html @@ -18,7 +18,9 @@ button { margin-top: 10px; - font-family: system,-apple-system,"BlinkMacSystemFont",".SFNSText-Regular","San Francisco","Roboto","Segoe UI","Helvetica Neue","Lucida Grande","Ubuntu","arial",sans-serif; + font-family: system, -apple-system, "BlinkMacSystemFont", + ".SFNSText-Regular", "San Francisco", "Roboto", "Segoe UI", + "Helvetica Neue", "Lucida Grande", "Ubuntu", "arial", sans-serif; background-color: rgba(20, 20, 25); border: 1px solid rgba(60, 60, 70); border-radius: 12px; @@ -27,7 +29,7 @@ font-size: 14px; padding: 12px 16px; text-align: center; - transition: transform .2s, background-color .2s, border-color .2s; + transition: transform 0.2s, background-color 0.2s, border-color 0.2s; width: fit-content; } @@ -41,7 +43,7 @@ } button:active { - transform: scale(.98) + transform: scale(0.98); } .source { color: black; @@ -77,11 +79,12 @@ >Source code - + + - + --> + + + diff --git a/demo/package.json b/demo/package.json index 2abf940..d71ad76 100644 --- a/demo/package.json +++ b/demo/package.json @@ -12,7 +12,6 @@ }, "dependencies": { "@cap.js/server": "^1.0.5", - "@fastify/static": "^8.1.1", "fastify": "^5.2.1" } } diff --git a/demo/server.js b/demo/server.js index 4c99670..24c0d3b 100644 --- a/demo/server.js +++ b/demo/server.js @@ -1,6 +1,6 @@ +import fs from "fs"; import path from "path"; import Cap from "@cap.js/server"; -import fastifyStatic from "@fastify/static"; import Fastify from "fastify"; const fastify = Fastify(); @@ -8,21 +8,19 @@ const cap = new Cap({ tokens_store_path: ".data/tokensList.json", }); -fastify.register(fastifyStatic, { - root: path.join(__dirname), - prefix: "/", -}); - fastify.get("/", (req, res) => { - res.sendFile("index.html"); + res.header("Content-Type", "text/html"); + res.send(fs.createReadStream(path.join(__dirname, "index.html"))); }); fastify.get("/cap.js", (req, res) => { - res.sendFile("../widget/src/cap.js"); + res.header("Content-Type", "application/javascript"); + res.send(fs.createReadStream(path.join(__dirname, "../widget/src/cap.js"))); }); fastify.get("/cap-floating.js", (req, res) => { - res.sendFile("../widget/src/cap-floating.js"); + res.header("Content-Type", "application/javascript"); + res.send(fs.createReadStream(path.join(__dirname, "../widget/src/cap-floating.js"))); }); fastify.post("/api/challenge", (req, res) => { diff --git a/server/index.test.js b/server/index.test.js deleted file mode 100644 index ba28a7d..0000000 --- a/server/index.test.js +++ /dev/null @@ -1,77 +0,0 @@ -const express = require('express'); -const crypto = require('crypto'); -const Cap = require('./index.js'); - -const app = express(); -app.use(express.json()); - -const cap = new Cap({ - tokens_store_path: '.data/tokensList.json' -}); - -function solveChallenge(salt, target) { - let nonce = 0; - while (true) { - if (crypto.createHash('sha256').update(salt + nonce.toString()).digest('hex').startsWith(target)) { - return nonce.toString(); - } - nonce++; - } -} - -app.post('/api/challenge', (req, res) => { - res.json(cap.createChallenge({ - challengeCount: 2, - challengeSize: 16, - challengeDifficulty: 3, - expiresMs: 300000 - })); -}); - -app.post('/api/redeem', async (req, res) => { - const { token, solutions } = req.body; - if (!token || !solutions) { - return res.status(400).json({ success: false }); - } - res.json(await cap.redeemChallenge({ token, solutions })); -}); - -app.post('/api/validate', async (req, res) => { - const { token } = req.body; - if (!token) { - return res.status(400).json({ success: false }); - } - res.json(await cap.validateToken(token)); -}); - -async function test() { - const { challenge, token } = await fetch('http://localhost:3000/api/challenge', { - method: 'POST' - }).then(r => r.json()); - - const solutions = challenge.map(([salt, target]) => [salt, target, solveChallenge(salt, target)]); - const { success, token: verToken } = await fetch('http://localhost:3000/api/redeem', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ token, solutions }) - }).then(r => r.json()); - - if (!success || !verToken) { - throw new Error('Failed to redeem challenge'); - } - - const validation = await fetch('http://localhost:3000/api/validate', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ token: verToken }) - }).then(r => r.json()); - - if (!validation.success) { - throw new Error('Failed to validate token'); - } - - console.log('All tests passed!'); - process.exit(0); -} - -app.listen(3000, test); \ No newline at end of file