nostr challenge creation works

This commit is contained in:
counterweight 2025-02-11 18:15:06 +01:00
parent 7e4adf052c
commit 9ed583ecdb
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
5 changed files with 103 additions and 14 deletions

View file

@ -0,0 +1,23 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../database');
const NostrChallengeCreated = sequelize.define('NostrChallengeCreated', {
uuid: {
type: DataTypes.UUID,
allowNull: false,
unique: true,
primaryKey: true
},
challenge: {
type: DataTypes.STRING,
allowNull: false
},
created_at: {
type: DataTypes.DATE,
allowNull: false
}
}, {
tableName: 'nostr_challenge_created'
});
module.exports = NostrChallengeCreated;

View file

@ -1,3 +1,50 @@
async function login() {
if (!window.nostr) {
console.log("No Nostr extension found.");
return { success: false, error: "No Nostr extension detected." };
}
try {
const challengeResponse = await fetch("/api/nostr-challenge");
if (!challengeResponse.ok) throw new Error("Failed to fetch challenge");
const { challenge } = await challengeResponse.json();
console.log(`Received challenge: ${challenge}`);
} catch (error) { }
/* const pubkey = await window.nostr.getPublicKey();
const event = {
kind: 22242,
created_at: Math.floor(Date.now() / 1000),
tags: [["challenge", challenge]],
content: "Sign this challenge to authenticate",
pubkey: pubkey
};
const signedEvent = await window.nostr.signEvent(event);
const verifyResponse = await fetch("/api/nostr-verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(signedEvent),
});
if (!verifyResponse.ok) throw new Error("Verification failed");
const verifyResult = await verifyResponse.json();
if (verifyResult.success) {
console.log("Authentication successful!");
return { success: true, pubkey };
} else {
throw new Error("Invalid signature");
}
} catch (error) {
console.error("Error during Nostr authentication:", error.message);
return { success: false, error: error.message };
} */
}
/*
window.onload = function () {
setTimeout(function () {
console.log("This code runs 2 seconds after the window has loaded.");
@ -35,4 +82,4 @@ window.onload = function () {
)
}, 2000);
}
} */

View file

@ -1,8 +1,11 @@
const express = require('express');
//const { generatePrivateKey, getPublicKey, verifySignature } = require("nostr-tools");
const crypto = require("crypto");
const appInviteService = require('../services/appInviteService');
const invitedNpubService = require('../services/invitedNpubService');
const sessionService = require('../services/sessionService');
const nostrService = require('../services/nostrService');
const router = express.Router();
@ -55,4 +58,10 @@ router.post('/invited-npub', async (req, res) => {
});
router.get('/nostr-challenge', async (req, res) => {
const nostrChallenge = await nostrService.createNostrChallenge();
res.json({ 'challenge': nostrChallenge.challenge });
});
module.exports = router;

View file

@ -0,0 +1,15 @@
const uuid = require("uuid");
const crypto = require("crypto");
const NostrChallengeCreated = require('../models/NostrChallengeCreated');
async function createNostrChallenge() {
const nostrChallenge = await NostrChallengeCreated.create({
'uuid': uuid.v7(),
challenge: crypto.randomBytes(32).toString("hex"),
created_at: new Date().toISOString()
});
return nostrChallenge;
}
exports.createNostrChallenge = createNostrChallenge;

View file

@ -9,20 +9,15 @@
</head>
<body>
<h1>Hello World! We're live</h1>
<p>Now this is some production grade stuff, baby!</p>
<p>Your session's UUID: <%= uuid %>
<h1>Bienvenido a la seca</h1>
<p>Usa Nostr para logearte</p>
<form onsubmit="login();return false">
<button type="submit">Login con extensión de Nostr</button>
</form>
<p>
¿No tienes cuenta de Nostr? <a href="https://start.njump.me/" target="_blank" rel="noopener noreferrer">Crea una
gratis</a>.
</p>
<p id="pubkey-p" style="display:none">And your pubkey is: <span id="pubkey-span"></span></p>
<hr>
<p>Here's a kitty, cause why not</p>
<img width="300px" src="kitty.jpeg" alt="">
</body>
<script>
</script>
</html>