clean up demo code
This commit is contained in:
+11
-8
@@ -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</a
|
||||
>
|
||||
</body>
|
||||
|
||||
|
||||
<!--
|
||||
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget/cap-floating.min.js"></script>
|
||||
|
||||
<!-- to use local resources, uncomment this: -->
|
||||
<!-- <script src="/cap.js"></script>
|
||||
<script src="/cap-floating.js"></script> -->
|
||||
-->
|
||||
|
||||
<script src="/cap.js"></script>
|
||||
<script src="/cap-floating.js"></script>
|
||||
</html>
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@cap.js/server": "^1.0.5",
|
||||
"@fastify/static": "^8.1.1",
|
||||
"fastify": "^5.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
+7
-9
@@ -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) => {
|
||||
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user