a lot of renames
This commit is contained in:
parent
966fde1111
commit
619503e799
9 changed files with 80 additions and 73 deletions
7
scratch.txt
Normal file
7
scratch.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
npub -> publicKey
|
||||
|
||||
exports.createInvitedNpub = createPublicKeyInvite;
|
||||
exports.isNpubInvited = isPublicKeyInvited;
|
||||
|
||||
invitedNpubService -> PublicKeyInvitedService
|
||||
|
|
@ -3,5 +3,7 @@ const { exec } = require('child_process');
|
|||
const appInviteService = require('../services/appInviteService');
|
||||
|
||||
module.exports = async function createAppInvite(inviterNpub) {
|
||||
await appInviteService.createAppInvite(inviterNpub);
|
||||
const appInvite = await appInviteService.createAppInvite(inviterNpub);
|
||||
console.log('Invite created');
|
||||
console.log(`Check at http://localhost/invite/${appInvite.uuid}`);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
const { DataTypes } = require('sequelize');
|
||||
const sequelize = require('../database');
|
||||
|
||||
const PublicKeyInvited = sequelize.define('PublicKeyInvited', {
|
||||
const PublicKeySignedUp = sequelize.define('PublicKeySignedUp', {
|
||||
uuid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: false,
|
||||
|
|
@ -13,7 +13,7 @@ const PublicKeyInvited = sequelize.define('PublicKeyInvited', {
|
|||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
public_key_invited: {
|
||||
public_key: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
|
|
@ -22,7 +22,7 @@ const PublicKeyInvited = sequelize.define('PublicKeyInvited', {
|
|||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
tableName: 'public_key_invited'
|
||||
tableName: 'public_key_signed_up'
|
||||
});
|
||||
|
||||
module.exports = PublicKeyInvited;
|
||||
module.exports = PublicKeySignedUp;
|
||||
|
|
@ -1,27 +1,25 @@
|
|||
function acceptInvite() {
|
||||
const npub = window.nostr.getPublicKey();
|
||||
async function acceptInvite() {
|
||||
const publicKey = await window.nostr.getPublicKey();
|
||||
|
||||
npub.then(async function (npub) {
|
||||
try {
|
||||
const response = await fetch('/api/invited-npub', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"npub": npub
|
||||
})
|
||||
});
|
||||
try {
|
||||
const response = await fetch('/api/sign-public-key-up', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
publicKey
|
||||
})
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
console.log('invited-npub record created successfully:', data);
|
||||
} else {
|
||||
const error = await response.json();
|
||||
console.error('Failed to create invited-npub record:', error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('An error occurred:', error);
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
console.log('invited-npub record created successfully:', data);
|
||||
} else {
|
||||
const error = await response.json();
|
||||
console.error('Failed to create invited-npub record:', error);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('An error occurred:', error);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ const { getPublicKey, verifyEvent } = require("nostr-tools");
|
|||
const crypto = require("crypto");
|
||||
|
||||
const appInviteService = require('../services/appInviteService');
|
||||
const PublicKeyInvitedService = require('../services/PublicKeyInvitedService');
|
||||
const PublicKeySignedUpService = require('../services/PublicKeySignedUpService');
|
||||
const sessionService = require('../services/sessionService');
|
||||
const nostrService = require('../services/nostrService');
|
||||
|
||||
|
|
@ -37,9 +37,9 @@ router.post('/session-npubbed', async (req, res) => {
|
|||
});
|
||||
|
||||
|
||||
router.post('/invited-npub', async (req, res) => {
|
||||
router.post('/sign-public-key-up', async (req, res) => {
|
||||
const inviteUuid = req.cookies.inviteUuid;
|
||||
const npub = req.body.npub;
|
||||
const publicKey = req.body.publicKey;
|
||||
|
||||
if (await !appInviteService.appInviteExists(inviteUuid)) {
|
||||
return res.status(404).render('error', { message: 'Invite not found' });
|
||||
|
|
@ -49,12 +49,12 @@ router.post('/invited-npub', async (req, res) => {
|
|||
return res.status(410).json({ error: 'Invite has already been used.' });
|
||||
}
|
||||
|
||||
await PublicKeyInvitedService.createPublicKeyInvite(
|
||||
await PublicKeySignedUpService.signUpPublicKey(
|
||||
inviteUuid,
|
||||
npub
|
||||
publicKey
|
||||
);
|
||||
|
||||
return res.json({ message: "Invited npub successfully." })
|
||||
return res.json({ message: "Invited public key successfully." })
|
||||
|
||||
});
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ router.post("/nostr-verify", async (req, res) => {
|
|||
return res.status(400).json({ success: false, error: "Invalid signature" });
|
||||
}
|
||||
|
||||
if (!PublicKeyInvitedService.isPublicKeyInvited(signedEvent.pubkey)) {
|
||||
if (!PublicKeySignedUpService.isPublicKeyInvited(signedEvent.pubkey)) {
|
||||
return res.status(400).json(
|
||||
{
|
||||
success: false,
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
const uuid = require("uuid");
|
||||
|
||||
const PublicKeyInvited = require('../models/PublicKeyInvited');
|
||||
|
||||
const appInviteService = require('./appInviteService');
|
||||
|
||||
async function createPublicKeyInvite(inviteUuid, publicKey) {
|
||||
|
||||
if (await appInviteService.isAppInviteSpent(inviteUuid)) {
|
||||
throw new Error("Can't invite npub, invite is already spent.");
|
||||
}
|
||||
|
||||
await PublicKeyInvited.create({
|
||||
uuid: uuid.v7(),
|
||||
app_invite_uuid: inviteUuid,
|
||||
public_key_invited: publicKey,
|
||||
created_at: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
|
||||
async function isPublicKeyInvited(publicKey) {
|
||||
|
||||
if (await PublicKeyInvited.findOne({
|
||||
where: { public_key_invited: publicKey }
|
||||
})) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.createPublicKeyInvite = createPublicKeyInvite;
|
||||
exports.isPublicKeyInvited = isPublicKeyInvited;
|
||||
33
src/services/PublicKeySignedUpService.js
Normal file
33
src/services/PublicKeySignedUpService.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
const uuid = require("uuid");
|
||||
|
||||
const PublicKeySignedUp = require('../models/PublicKeySignedUp');
|
||||
|
||||
const appInviteService = require('./appInviteService');
|
||||
|
||||
async function signUpPublicKey(inviteUuid, publicKey) {
|
||||
|
||||
if (await appInviteService.isAppInviteSpent(inviteUuid)) {
|
||||
throw new Error("Can't invite npub, invite is already spent.");
|
||||
}
|
||||
|
||||
await PublicKeySignedUp.create({
|
||||
uuid: uuid.v7(),
|
||||
app_invite_uuid: inviteUuid,
|
||||
public_key: publicKey,
|
||||
created_at: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
|
||||
async function isPublicKeySignedUp(publicKey) {
|
||||
|
||||
if (await PublicKeySignedUp.findOne({
|
||||
where: { public_key: publicKey }
|
||||
})) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.signUpPublicKey = signUpPublicKey;
|
||||
exports.isPublicKeySignedUp = isPublicKeySignedUp;
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
const uuid = require('uuid');
|
||||
|
||||
const AppInviteCreated = require('../models/AppInviteCreated');
|
||||
const PublicKeyInvited = require('../models/PublicKeyInvited');
|
||||
const PublicKeySignedUp = require('../models/PublicKeySignedUp');
|
||||
|
||||
async function appInviteExists(inviteUuid) {
|
||||
const invite = await AppInviteCreated.findOne({ where: { uuid: inviteUuid } });
|
||||
|
|
@ -17,7 +17,7 @@ async function getAppInvite(inviteUuid) {
|
|||
}
|
||||
|
||||
async function isAppInviteSpent(inviteUuid) {
|
||||
const invitedNpub = await PublicKeyInvited.findOne({
|
||||
const invitedNpub = await PublicKeySignedUp.findOne({
|
||||
where: {
|
||||
app_invite_uuid: inviteUuid
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ async function isAppInviteSpent(inviteUuid) {
|
|||
}
|
||||
|
||||
async function createAppInvite(inviterNpub) {
|
||||
await AppInviteCreated.create({
|
||||
return await AppInviteCreated.create({
|
||||
uuid: uuid.v7(),
|
||||
inviter_npub: inviterNpub,
|
||||
created_at: new Date().toISOString()
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ const uuid = require("uuid");
|
|||
const SessionCreated = require('../models/SessionCreated');
|
||||
const SessionNpubbed = require('../models/SessionNpubbed');
|
||||
|
||||
const invitedNpubService = require('./PublicKeyInvitedService');
|
||||
const PublicKeySignedUpService = require('./PublicKeySignedUpService');
|
||||
|
||||
const constants = require('../constants');
|
||||
|
||||
|
|
@ -76,7 +76,7 @@ async function isSessionAuthorized(sessionUuid) {
|
|||
|
||||
const npub = await getNpubRelatedToSession(sessionUuid);
|
||||
|
||||
if (await invitedNpubService.isNpubInvited(npub)) {
|
||||
if (await PublicKeySignedUpService.isPublicKeySignedUp(npub)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue