diff --git a/src/services/nostrService.js b/src/services/nostrService.js index 5239273..f881c5c 100644 --- a/src/services/nostrService.js +++ b/src/services/nostrService.js @@ -3,13 +3,11 @@ const crypto = require('crypto'); const { Op } = require('sequelize'); const { verifyEvent } = require('nostr-tools'); -const models = require('../models'); - -const errors = require('../errors'); - class NostrServiceProvider { - constructor({ constants }) { + constructor({ models, constants, errors }) { + this.models = models; this.constants = constants; + this.errors = errors; } provide() { @@ -21,7 +19,7 @@ class NostrServiceProvider { this.constants.DEFAULT_NOSTR_CHALLENGE_DURATION_SECONDS ); - const nostrChallenge = await models.NostrChallengeCreated.create({ + const nostrChallenge = await this.models.NostrChallengeCreated.create({ uuid: uuid.v7(), challenge: crypto.randomBytes(32).toString('hex'), expires_at: expiryTimestamp.toISOString(), @@ -31,12 +29,12 @@ class NostrServiceProvider { return nostrChallenge; }; - async function getNostrChallenge( + const getNostrChallenge = async ( nostrChallengeUuid = null, challenge = null - ) { + ) => { if (nostrChallengeUuid) { - return await models.NostrChallengeCreated.findOne({ + return await this.models.NostrChallengeCreated.findOne({ where: { uuid: nostrChallengeUuid, }, @@ -44,7 +42,7 @@ class NostrServiceProvider { } if (challenge) { - return await models.NostrChallengeCreated.findOne({ + return await this.models.NostrChallengeCreated.findOne({ where: { challenge, }, @@ -52,40 +50,40 @@ class NostrServiceProvider { } throw Error('You need to pass a uuid or a challenge.'); - } + }; - async function verifyNostrChallenge(signedEvent) { + const verifyNostrChallenge = async (signedEvent) => { const challengeTag = signedEvent.tags.find( (tag) => tag[0] === 'challenge' ); const challenge = challengeTag[1]; if (!(await isNostrChallengeFresh(challenge))) { - throw errors.ExpiredError('Challenge expired, request new one.'); + throw this.errors.ExpiredError('Challenge expired, request new one.'); } if (await hasNostrChallengeBeenCompleted(challenge)) { - throw new errors.AlreadyUsedError( + throw new this.errors.AlreadyUsedError( 'Challenge already used, request new one.' ); } const isSignatureValid = verifyEvent(signedEvent); if (!isSignatureValid) { - throw new errors.InvalidSignatureError('Signature is not valid.'); + throw new this.errors.InvalidSignatureError('Signature is not valid.'); } - return await models.NostrChallengeCompleted.create({ + return await this.models.NostrChallengeCompleted.create({ uuid: uuid.v7(), challenge: challenge, signed_event: signedEvent, public_key: signedEvent.pubkey, created_at: new Date().toISOString(), }); - } + }; - async function isNostrChallengeFresh(challengeString) { - const nostrChallenge = await models.NostrChallengeCreated.findOne({ + const isNostrChallengeFresh = async (challengeString) => { + const nostrChallenge = await this.models.NostrChallengeCreated.findOne({ where: { challenge: challengeString, expires_at: { @@ -98,11 +96,11 @@ class NostrServiceProvider { return true; } return false; - } + }; - async function hasNostrChallengeBeenCompleted(challengeString) { + const hasNostrChallengeBeenCompleted = async (challengeString) => { const completedNostrChallenge = - await models.NostrChallengeCompleted.findOne({ + await this.models.NostrChallengeCompleted.findOne({ where: { challenge: challengeString, }, @@ -112,7 +110,7 @@ class NostrServiceProvider { return true; } return false; - } + }; return { createNostrChallenge,