nostr verification working

This commit is contained in:
counterweight 2025-02-12 00:44:17 +01:00
parent f42ae5fc1d
commit 19667807bb
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
8 changed files with 258 additions and 7 deletions

View file

@ -1,15 +1,76 @@
const uuid = require("uuid");
const crypto = require("crypto");
const { Op } = require('sequelize');
const NostrChallengeCreated = require('../models/NostrChallengeCreated');
const NostrChallengeCompleted = require("../models/NostrChallengeCompleted");
const constants = require('../constants');
async function createNostrChallenge() {
const currentTimestamp = new Date();
const expiryTimestamp = new Date(currentTimestamp.getTime());
expiryTimestamp.setSeconds(expiryTimestamp.getSeconds() + constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS);
const nostrChallenge = await NostrChallengeCreated.create({
'uuid': uuid.v7(),
challenge: crypto.randomBytes(32).toString("hex"),
created_at: new Date().toISOString()
expires_at: expiryTimestamp.toISOString(),
created_at: currentTimestamp.toISOString()
});
return nostrChallenge;
}
exports.createNostrChallenge = createNostrChallenge;
async function isNostrChallengeFresh(challengeString) {
const nostrChallenge = await NostrChallengeCreated.findOne({
where: {
challenge: challengeString,
expires_at: {
[Op.gt]: new Date()
}
}
});
if (nostrChallenge) {
return true;
}
return false;
}
async function hasNostrChallengeBeenCompleted(challengeString) {
const completedNostrChallenge = await NostrChallengeCompleted.findOne(
{
where: {
challenge: challengeString
}
}
);
if (completedNostrChallenge) {
return true;
}
return false;
}
async function completeNostrChallenge(
challenge,
signedEvent
) {
await NostrChallengeCompleted.create({
'uuid': uuid.v7(),
challenge: challenge,
signed_event: signedEvent,
created_at: new Date().toISOString()
}
);
return;
}
exports.createNostrChallenge = createNostrChallenge;
exports.isNostrChallengeFresh = isNostrChallengeFresh;
exports.hasNostrChallengeBeenCompleted = hasNostrChallengeBeenCompleted;
exports.completeNostrChallenge = completeNostrChallenge;