Initial commit
Once again a weird place to commit, I have already done a lot of work, but I am just bad at using git, okay.
This commit is contained in:
44
example-app/server/api/pow/challenge.get.ts
Normal file
44
example-app/server/api/pow/challenge.get.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { defineEventHandler } from 'h3'
|
||||
import { config } from '~~/server/utils/config';
|
||||
import { generate_challenge } from '@impost/lib/validator';
|
||||
import { ChallengeStrategy } from '@impost/lib';
|
||||
import { CHALLENGE_TIMEOUT_MS, outstandingChallenges } from '~~/server/utils/pow';
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
let challenge_config;
|
||||
switch (config.strategy) {
|
||||
case ChallengeStrategy.LeadingZeroes:
|
||||
challenge_config = {
|
||||
parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
||||
strategy: config.strategy,
|
||||
difficulty: config.leading_zeroes?.difficulty!,
|
||||
};
|
||||
break;
|
||||
case ChallengeStrategy.TargetNumber:
|
||||
challenge_config = {
|
||||
parameters: { expires_at: CHALLENGE_TIMEOUT_MS },
|
||||
strategy: config.strategy,
|
||||
max_number: config.target_number.max_number,
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
let challenge = await generate_challenge(challenge_config);
|
||||
if (challenge === null) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to generate challenge'
|
||||
});
|
||||
}
|
||||
|
||||
outstandingChallenges.set(challenge.salt, {
|
||||
challenge, timeout: setTimeout(() => {
|
||||
console.log("Challenge timed out:", challenge.salt);
|
||||
outstandingChallenges.delete(challenge.salt);
|
||||
}, CHALLENGE_TIMEOUT_MS)
|
||||
});
|
||||
|
||||
return {
|
||||
challenge,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user