nostr service and fix usage in invites

This commit is contained in:
counterweight 2025-03-06 02:19:23 +01:00
parent 15217dc77a
commit f8a185e879
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
4 changed files with 124 additions and 110 deletions

View file

@ -1,4 +1,5 @@
const nostrService = require('../services/nostrService');
const NostrServiceProvider = require('../services/nostrService');
const nostrService = new NostrServiceProvider().provide();
const InvitesServiceProvider = require('../services/invitesService');
const invitesService = new InvitesServiceProvider({
nostrService,

View file

@ -2,9 +2,10 @@ class ServicesProvider {
constructor() {}
provide() {
const nostrService = require('../services/nostrService');
const loginService = require('../services/loginService');
const NostrServiceProvider = require('../services/nostrService');
const nostrService = new NostrServiceProvider().provide();
const InvitesServiceProvider = require('../services/invitesService');
const invitesService = new InvitesServiceProvider({
nostrService,

View file

@ -1,6 +1,5 @@
const uuid = require('uuid');
const nostrService = require('./nostrService');
const models = require('../models');
const errors = require('../errors');
@ -49,7 +48,7 @@ class InvitesServiceProvider {
});
}
async function createSignUpChallenge(appInviteUuid) {
const createSignUpChallenge = async (appInviteUuid) => {
if (!(await appInviteExists(appInviteUuid))) {
throw new errors.NotFoundError("Invite doesn't exist.");
}
@ -58,7 +57,7 @@ class InvitesServiceProvider {
throw new errors.AlreadyUsedError('Invite has already been used.');
}
const nostrChallenge = await nostrService.createNostrChallenge();
const nostrChallenge = await this.nostrService.createNostrChallenge();
return await models.SignUpChallengeCreated.create({
uuid: uuid.v7(),
@ -66,15 +65,15 @@ class InvitesServiceProvider {
app_invite_uuid: appInviteUuid,
created_at: new Date().toISOString(),
});
}
};
async function verifySignUpChallenge(signedEvent) {
const verifySignUpChallenge = async (signedEvent) => {
const challengeTag = signedEvent.tags.find(
(tag) => tag[0] === 'challenge'
);
const challenge = challengeTag[1];
const nostrChallenge = await nostrService.getNostrChallenge(
const nostrChallenge = await this.nostrService.getNostrChallenge(
null,
challenge
);
@ -85,14 +84,14 @@ class InvitesServiceProvider {
},
});
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
if (await this.nostrService.hasNostrChallengeBeenCompleted(challenge)) {
throw new errors.AlreadyUsedError(
'This challenge has already been used.'
);
}
const completedNostrChallenge =
await nostrService.verifyNostrChallenge(signedEvent);
await this.nostrService.verifyNostrChallenge(signedEvent);
const completedSignUpChallenge =
await models.SignUpChallengeCompleted.create({
@ -104,7 +103,7 @@ class InvitesServiceProvider {
});
return completedSignUpChallenge;
}
};
async function isPublicKeySignedUp(publicKey) {
const signUpChallengeCompleted =

View file

@ -8,6 +8,10 @@ const models = require('../models');
const constants = require('../constants');
const errors = require('../errors');
class NostrServiceProvider {
constructor() {}
provide() {
async function createNostrChallenge() {
const currentTimestamp = new Date();
const expiryTimestamp = new Date(currentTimestamp.getTime());
@ -26,7 +30,10 @@ async function createNostrChallenge() {
return nostrChallenge;
}
async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
async function getNostrChallenge(
nostrChallengeUuid = null,
challenge = null
) {
if (nostrChallengeUuid) {
return await models.NostrChallengeCreated.findOne({
where: {
@ -47,7 +54,9 @@ async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
}
async function verifyNostrChallenge(signedEvent) {
const challengeTag = signedEvent.tags.find((tag) => tag[0] === 'challenge');
const challengeTag = signedEvent.tags.find(
(tag) => tag[0] === 'challenge'
);
const challenge = challengeTag[1];
if (!(await isNostrChallengeFresh(challenge))) {
@ -91,7 +100,8 @@ async function isNostrChallengeFresh(challengeString) {
}
async function hasNostrChallengeBeenCompleted(challengeString) {
const completedNostrChallenge = await models.NostrChallengeCompleted.findOne({
const completedNostrChallenge =
await models.NostrChallengeCompleted.findOne({
where: {
challenge: challengeString,
},
@ -103,10 +113,13 @@ async function hasNostrChallengeBeenCompleted(challengeString) {
return false;
}
module.exports = {
return {
createNostrChallenge,
getNostrChallenge,
verifyNostrChallenge,
isNostrChallengeFresh,
hasNostrChallengeBeenCompleted,
};
}
}
module.exports = NostrServiceProvider;