feat(api): Implement Challenges API

This commit is contained in:
Jesse Wierzbinski 2024-06-13 22:03:51 -10:00
parent 924ff9b2d4
commit 8f9472b221
No known key found for this signature in database
26 changed files with 2656 additions and 104 deletions

View file

@ -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");
};