server/utils/challenges.ts
Jesse Wierzbinski 6056a6622c
Some checks failed
CodeQL Scan / Analyze (javascript-typescript) (push) Failing after 1s
Build Docker Images / lint (push) Failing after 7s
Build Docker Images / check (push) Failing after 7s
Build Docker Images / tests (push) Failing after 7s
Build Docker Images / detect-circular (push) Failing after 7s
Deploy Docs to GitHub Pages / build (push) Failing after 0s
Build Docker Images / build (server, Dockerfile, ${{ github.repository_owner }}/server) (push) Has been skipped
Build Docker Images / build (worker, Worker.Dockerfile, ${{ github.repository_owner }}/worker) (push) Has been skipped
Deploy Docs to GitHub Pages / Deploy (push) Has been skipped
Mirror to Codeberg / Mirror (push) Failing after 0s
Nix Build / check (push) Failing after 0s
Test Publish / build (client) (push) Failing after 0s
Test Publish / build (sdk) (push) Failing after 0s
refactor(database): ♻️ Use dates instead of strings in database
2025-12-11 04:03:57 +01:00

49 lines
1.2 KiB
TypeScript

import { config } from "@versia-server/config";
import { db } from "@versia-server/kit/db";
import { Challenges } from "@versia-server/kit/tables";
import { createChallenge } from "altcha-lib";
import type { Challenge } from "altcha-lib/types";
import { randomUUIDv7 } from "bun";
export const generateChallenge = async (
maxNumber?: number,
): Promise<{
id: string;
challenge: Challenge;
expiresAt: Date;
createdAt: Date;
}> => {
if (!config.validation.challenges) {
throw new Error("Challenges are not enabled");
}
const expirationDate = new Date(
Date.now() + config.validation.challenges.expiration * 1000,
);
const uuid = randomUUIDv7();
const challenge = await createChallenge({
hmacKey: config.validation.challenges.key,
expires: expirationDate,
maxNumber: maxNumber ?? config.validation.challenges.difficulty,
algorithm: "SHA-256",
params: {
challenge_id: uuid,
},
});
const result = (
await db
.insert(Challenges)
.values({
id: uuid,
challenge,
expiresAt: expirationDate,
})
.returning()
)[0];
return result;
};