mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
feat(api): ✨ Implement Challenges API
This commit is contained in:
parent
924ff9b2d4
commit
8f9472b221
26 changed files with 2656 additions and 104 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { generateChallenge } from "@/challenges";
|
||||
import { consoleLogger } from "@/loggers";
|
||||
import { randomString } from "@/math";
|
||||
import { solveChallenge } from "altcha-lib";
|
||||
import { asc, inArray, like } from "drizzle-orm";
|
||||
import type { Status } from "~/database/entities/status";
|
||||
import { db } from "~/drizzle/db";
|
||||
|
|
@ -118,3 +120,34 @@ export const getTestStatuses = async (
|
|||
)
|
||||
).map((n) => n.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a solved challenge (with tiny difficulty)
|
||||
*
|
||||
* Only to be used in tests
|
||||
* @returns Base64 encoded payload
|
||||
*/
|
||||
export const getSolvedChallenge = async () => {
|
||||
const { challenge } = await generateChallenge(100);
|
||||
|
||||
const solution = await solveChallenge(
|
||||
challenge.challenge,
|
||||
challenge.salt,
|
||||
challenge.algorithm,
|
||||
challenge.maxnumber,
|
||||
).promise;
|
||||
|
||||
if (!solution) {
|
||||
throw new Error("Failed to solve challenge");
|
||||
}
|
||||
|
||||
return Buffer.from(
|
||||
JSON.stringify({
|
||||
number: solution.number,
|
||||
algorithm: challenge.algorithm,
|
||||
challenge: challenge.challenge,
|
||||
salt: challenge.salt,
|
||||
signature: challenge.signature,
|
||||
}),
|
||||
).toString("base64");
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue