worksss
This commit is contained in:
parent
768efaf3a2
commit
fb9832fabb
8 changed files with 306 additions and 92 deletions
|
|
@ -1,7 +1,15 @@
|
|||
const uuid = require('uuid');
|
||||
|
||||
const nostrService = require('./nostrService');
|
||||
const AppInviteCreated = require('../models/AppInviteCreated');
|
||||
const PublicKeySignedUp = require('../models/PublicKeySignedUp');
|
||||
const SignUpChallengeCreated = require('../models/SignUpChallengeCreated');
|
||||
const SignUpChallengeCompleted = require('../models/SignUpChallengeCompleted');
|
||||
|
||||
|
||||
const errors = require('../errors');
|
||||
const NostrChallengeCompleted = require('../models/NostrChallengeCompleted');
|
||||
const { sortEvents } = require('nostr-tools');
|
||||
|
||||
async function appInviteExists(inviteUuid) {
|
||||
const invite = await AppInviteCreated.findOne({ where: { uuid: inviteUuid } });
|
||||
|
|
@ -16,14 +24,14 @@ async function getAppInvite(inviteUuid) {
|
|||
return invite;
|
||||
}
|
||||
|
||||
async function isAppInviteSpent(inviteUuid) {
|
||||
const invitedNpub = await PublicKeySignedUp.findOne({
|
||||
async function isAppInviteSpent(appInviteUuid) {
|
||||
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne({
|
||||
where: {
|
||||
app_invite_uuid: inviteUuid
|
||||
app_invite_uuid: appInviteUuid
|
||||
}
|
||||
})
|
||||
|
||||
if (invitedNpub) {
|
||||
if (signUpChallengeCompleted) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -38,6 +46,79 @@ async function createAppInvite(inviterNpub) {
|
|||
);
|
||||
}
|
||||
|
||||
async function createSignUpChallenge(appInviteUuid) {
|
||||
|
||||
if (!(await appInviteExists(appInviteUuid))) {
|
||||
throw new errors.NotFoundError("Invite doesn't exist.")
|
||||
}
|
||||
|
||||
if (await isAppInviteSpent(appInviteUuid)) {
|
||||
throw new errors.AppInvitedUsedError("Invite has already been used.")
|
||||
}
|
||||
|
||||
const nostrChallenge = await nostrService.createNostrChallenge()
|
||||
|
||||
return await SignUpChallengeCreated.create({
|
||||
'uuid': uuid.v7(),
|
||||
nostr_challenge_uuid: nostrChallenge.uuid,
|
||||
app_invite_uuid: appInviteUuid,
|
||||
created_at: new Date().toISOString()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async function hasSignUpChallengeBeenCompleted(nostrChallengeCompletedUuid) {
|
||||
const signUpChallengeCompleted = await SignUpChallengeCompleted.findOne(
|
||||
{
|
||||
where:
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async function verifySignUpChallenge(signedEvent) {
|
||||
|
||||
const challengeTag = signedEvent.tags.find(tag => tag[0] === "challenge");
|
||||
const challenge = challengeTag[1];
|
||||
|
||||
const nostrChallenge = await nostrService.getNostrChallenge(
|
||||
null, challenge
|
||||
);
|
||||
|
||||
console.log(`Found this nostr challenge: ${nostrChallenge}`);
|
||||
|
||||
const signUpChallenge = await SignUpChallengeCreated.findOne({
|
||||
where: {
|
||||
nostr_challenge_uuid: nostrChallenge.uuid
|
||||
}
|
||||
})
|
||||
|
||||
console.log(`Found this signup challenge: ${signUpChallenge}`);
|
||||
|
||||
if (await nostrService.hasNostrChallengeBeenCompleted(challenge)) {
|
||||
throw new errors.ChallengedUsedError("This challenge has already been used.");
|
||||
}
|
||||
console.log(`I'm gonna verify the nostr challenge`);
|
||||
const completedNostrChallenge = await nostrService.verifyNostrChallenge(signedEvent);
|
||||
console.log(`Verified the NostrChallenge: ${completedNostrChallenge}`);
|
||||
|
||||
const completedSignUpChallenge = await 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()
|
||||
}
|
||||
);
|
||||
console.log(`Verified the SignUpChallenge: ${completedSignUpChallenge}`);
|
||||
|
||||
|
||||
return completedSignUpChallenge;
|
||||
}
|
||||
|
||||
async function signUpPublicKey(inviteUuid, publicKey) {
|
||||
|
||||
if (await isAppInviteSpent(inviteUuid)) {
|
||||
|
|
@ -68,6 +149,8 @@ module.exports = {
|
|||
getAppInvite,
|
||||
isAppInviteSpent,
|
||||
createAppInvite,
|
||||
createSignUpChallenge,
|
||||
verifySignUpChallenge,
|
||||
signUpPublicKey,
|
||||
isPublicKeySignedUp
|
||||
};
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
const uuid = require("uuid");
|
||||
const crypto = require("crypto");
|
||||
const { Op } = require('sequelize');
|
||||
const { Op, TimeoutError } = require('sequelize');
|
||||
const { verifyEvent } = require("nostr-tools");
|
||||
|
||||
const NostrChallengeCreated = require('../models/NostrChallengeCreated');
|
||||
const NostrChallengeCompleted = require("../models/NostrChallengeCompleted");
|
||||
|
||||
const constants = require('../constants');
|
||||
|
||||
const errors = require('../errors');
|
||||
|
||||
async function createNostrChallenge() {
|
||||
|
||||
|
|
@ -24,6 +25,59 @@ async function createNostrChallenge() {
|
|||
return nostrChallenge;
|
||||
}
|
||||
|
||||
async function getNostrChallenge(nostrChallengeUuid = null, challenge = null) {
|
||||
|
||||
if (nostrChallengeUuid) {
|
||||
return await NostrChallengeCreated.findOne({
|
||||
where: {
|
||||
'uuid': nostrChallengeUuid
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (challenge) {
|
||||
return await NostrChallengeCreated.findOne({
|
||||
where: {
|
||||
challenge
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
throw Error('You need to pass a uuid or a challenge.')
|
||||
|
||||
}
|
||||
|
||||
async function verifyNostrChallenge(signedEvent) {
|
||||
const challengeTag = signedEvent.tags.find(tag => tag[0] === "challenge");
|
||||
const challenge = challengeTag[1];
|
||||
|
||||
console.log("Checking if fresh")
|
||||
if (!(await isNostrChallengeFresh(challenge))) {
|
||||
throw TimeoutError("Challenge expired, request new one.");
|
||||
}
|
||||
|
||||
console.log("Checking if completed")
|
||||
if (await hasNostrChallengeBeenCompleted(challenge)) {
|
||||
throw new errors.ChallengedUsedError("Challenge already used, request new one.");
|
||||
}
|
||||
|
||||
console.log("Checking if valid")
|
||||
const isSignatureValid = verifyEvent(signedEvent);
|
||||
if (!isSignatureValid) {
|
||||
throw new errors.InvalidSignatureError("Signature is not valid.");
|
||||
}
|
||||
|
||||
console.log("Persisting")
|
||||
return await 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 NostrChallengeCreated.findOne({
|
||||
where: {
|
||||
|
|
@ -55,23 +109,11 @@ async function hasNostrChallengeBeenCompleted(challengeString) {
|
|||
return false;
|
||||
}
|
||||
|
||||
async function completeNostrChallenge(
|
||||
challenge,
|
||||
signedEvent
|
||||
) {
|
||||
await NostrChallengeCompleted.create({
|
||||
'uuid': uuid.v7(),
|
||||
challenge: challenge,
|
||||
signed_event: signedEvent,
|
||||
public_key: signedEvent.pubkey,
|
||||
created_at: new Date().toISOString()
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
exports.createNostrChallenge = createNostrChallenge;
|
||||
exports.isNostrChallengeFresh = isNostrChallengeFresh;
|
||||
exports.hasNostrChallengeBeenCompleted = hasNostrChallengeBeenCompleted;
|
||||
exports.completeNostrChallenge = completeNostrChallenge;
|
||||
module.exports = {
|
||||
createNostrChallenge,
|
||||
getNostrChallenge,
|
||||
verifyNostrChallenge,
|
||||
isNostrChallengeFresh,
|
||||
hasNostrChallengeBeenCompleted
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue