secajs/src/services/invitesService.js

121 lines
3 KiB
JavaScript
Raw Normal View History

2025-02-10 15:38:01 +01:00
const uuid = require('uuid');
2025-02-13 00:02:40 +01:00
const nostrService = require('./nostrService');
2025-03-05 16:49:03 +01:00
const models = require('../models');
2025-02-13 00:02:40 +01:00
const errors = require('../errors');
2025-02-10 00:01:38 +01:00
async function appInviteExists(inviteUuid) {
2025-03-05 16:49:03 +01:00
const invite = await models.AppInviteCreated.findOne({
2025-02-14 11:13:18 +01:00
where: { uuid: inviteUuid },
});
if (invite) {
return true;
}
return false;
2025-02-10 00:01:38 +01:00
}
2025-02-10 00:17:30 +01:00
async function getAppInvite(inviteUuid) {
2025-03-05 16:49:03 +01:00
const invite = await models.AppInviteCreated.findOne({
2025-02-14 11:13:18 +01:00
where: { uuid: inviteUuid },
});
return invite;
2025-02-10 00:01:38 +01:00
}
2025-02-13 00:02:40 +01:00
async function isAppInviteSpent(appInviteUuid) {
2025-03-05 16:49:03 +01:00
const signUpChallengeCompleted =
await models.SignUpChallengeCompleted.findOne({
where: {
app_invite_uuid: appInviteUuid,
},
});
2025-02-14 11:13:18 +01:00
if (signUpChallengeCompleted) {
return true;
}
return false;
2025-02-10 00:17:30 +01:00
}
2025-02-13 00:13:56 +01:00
async function createAppInvite(inviterPubKey) {
2025-03-05 16:49:03 +01:00
return await models.AppInviteCreated.create({
2025-02-14 11:13:18 +01:00
uuid: uuid.v7(),
inviter_pub_key: inviterPubKey,
created_at: new Date().toISOString(),
});
2025-02-10 15:38:01 +01:00
}
2025-02-13 00:02:40 +01:00
async function createSignUpChallenge(appInviteUuid) {
2025-02-14 11:13:18 +01:00
if (!(await appInviteExists(appInviteUuid))) {
throw new errors.NotFoundError("Invite doesn't exist.");
}
if (await isAppInviteSpent(appInviteUuid)) {
throw new errors.AlreadyUsedError('Invite has already been used.');
}
const nostrChallenge = await nostrService.createNostrChallenge();
2025-03-05 16:49:03 +01:00
return await models.SignUpChallengeCreated.create({
2025-02-14 11:13:18 +01:00
uuid: uuid.v7(),
nostr_challenge_uuid: nostrChallenge.uuid,
app_invite_uuid: appInviteUuid,
created_at: new Date().toISOString(),
});
2025-02-13 00:02:40 +01:00
}
async function verifySignUpChallenge(signedEvent) {
2025-02-14 11:13:18 +01:00
const challengeTag = signedEvent.tags.find((tag) => tag[0] === 'challenge');
const challenge = challengeTag[1];
2025-02-13 00:02:40 +01:00
2025-02-14 11:13:18 +01:00
const nostrChallenge = await nostrService.getNostrChallenge(null, challenge);
2025-02-13 00:02:40 +01:00
2025-03-05 16:49:03 +01:00
const signUpChallenge = await models.SignUpChallengeCreated.findOne({
2025-02-14 11:13:18 +01:00
where: {
nostr_challenge_uuid: nostrChallenge.uuid,
},
});
2025-02-13 00:02:40 +01:00
2025-02-14 11:13:18 +01:00
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError('This challenge has already been used.');
}
2025-02-13 02:20:07 +01:00
2025-02-14 11:13:18 +01:00
const completedNostrChallenge =
await nostrService.verifyNostrChallenge(signedEvent);
2025-02-13 00:02:40 +01:00
2025-03-05 16:49:03 +01:00
const completedSignUpChallenge = await models.SignUpChallengeCompleted.create(
{
uuid: uuid.v7(),
nostr_challenge_completed_uuid: completedNostrChallenge.uuid,
app_invite_uuid: signUpChallenge.app_invite_uuid,
public_key: completedNostrChallenge.public_key,
created_at: new Date().toISOString(),
}
);
2025-02-13 00:02:40 +01:00
2025-02-14 11:13:18 +01:00
return completedSignUpChallenge;
2025-02-13 00:02:40 +01:00
}
2025-02-13 02:20:07 +01:00
async function isPublicKeySignedUp(publicKey) {
2025-03-05 16:49:03 +01:00
const signUpChallengeCompleted =
await models.SignUpChallengeCompleted.findOne({
where: {
public_key: publicKey,
},
});
2025-02-13 02:20:07 +01:00
2025-02-14 11:13:18 +01:00
if (signUpChallengeCompleted) {
return true;
}
2025-02-13 02:20:07 +01:00
2025-02-14 11:13:18 +01:00
return false;
2025-02-13 02:20:07 +01:00
}
2025-02-12 16:30:11 +01:00
module.exports = {
2025-02-14 11:13:18 +01:00
appInviteExists,
getAppInvite,
isAppInviteSpent,
createAppInvite,
createSignUpChallenge,
verifySignUpChallenge,
isPublicKeySignedUp,
};