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(), nonce: z.string() }) // post handler that takes in the challenge, and the nonce export default defineEventHandler(async (event) => { const body = await readValidatedBody(event, challengeSchema.safeParse); if (!body.success) { throw createError({ statusCode: 400, statusMessage: 'Validation failed' }) } let target = body.data.challenge; let nonce = body.data.nonce; // check if the challenge is valid let challenge_valid = await validate_challenge(outstandingChallenges.get(target)!.challenge, { challenge: target, nonce: nonce }); if (challenge_valid) { // clear the challenge clearTimeout(outstandingChallenges.get(target)!.timeout); outstandingChallenges.delete(target); return { message: 'Challenge solved' }; } throw createError({ statusCode: 400, statusMessage: 'Challenge is not valid' }) })