import { defineEventHandler } from 'h3' import { validate_challenge } from '@impost/lib/validator'; import * as z from 'zod'; import { outstandingChallenges } from '~~/server/utils/pow'; const challengeSchema = z.object({ challenge: z.string().startsWith("s."), solution: z.string().startsWith("s.") }) // post handler that takes in the challenge, and the nonce export default defineEventHandler(async (event) => { console.log(await readBody(event)); const body = await readValidatedBody(event, challengeSchema.safeParse); if (!body.success) { throw createError({ statusCode: 400, statusMessage: 'Validation failed' }) } let { challenge, solution } = body.data; const outstanding_challenge = outstandingChallenges.get(challenge); if (outstanding_challenge === undefined) { throw createError({ statusCode: 400, statusMessage: 'Challenge not found' }) } // check if the challenge is valid const challenge_valid = await validate_challenge(outstanding_challenge.challenge, solution); console.log("CHALLENGE VALID", challenge_valid); if (challenge_valid) { // clear the challenge clearTimeout(outstandingChallenges.get(challenge)!.timeout); outstandingChallenges.delete(challenge); return { message: 'Challenge solved' }; } throw createError({ statusCode: 400, statusMessage: 'Challenge is not valid' }) })