2024-04-15 00:04:50 +00:00
|
|
|
// Import the Nostr SDK package
|
|
|
|
import { NDKNip07Signer } from "@nostr-dev-kit/ndk";
|
|
|
|
import NDK from "@nostr-dev-kit/ndk";
|
|
|
|
|
2024-04-16 14:05:34 +00:00
|
|
|
document.querySelector("button").addEventListener("click", login);
|
2024-04-15 00:04:50 +00:00
|
|
|
|
|
|
|
async function fetchUserProfile(npub, ndk) {
|
|
|
|
try {
|
2024-04-16 14:05:34 +00:00
|
|
|
console.log("Fetching user profile for npub:", npub);
|
2024-04-15 00:04:50 +00:00
|
|
|
const user = ndk.getUser({ npub });
|
|
|
|
await user.fetchProfile();
|
|
|
|
console.log("User profile:", user.profile);
|
|
|
|
return user;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching user profile:", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 20:58:55 +00:00
|
|
|
const relays = ["wss://purplepag.es"];
|
2024-04-15 00:04:50 +00:00
|
|
|
const ndk = new NDK({ explicitRelayUrls: relays });
|
|
|
|
|
|
|
|
async function login() {
|
|
|
|
try {
|
|
|
|
const signer = new NDKNip07Signer();
|
|
|
|
|
|
|
|
// Connect to the NDK
|
|
|
|
await ndk.connect();
|
|
|
|
|
|
|
|
// Wait for the user to be obtained from the signer
|
|
|
|
const user = await signer.user();
|
|
|
|
|
|
|
|
if (!!user.pubkey && user.npub) {
|
|
|
|
console.log("Permission granted to read their public key:", user.pubkey);
|
|
|
|
|
|
|
|
// Fetch user profile
|
|
|
|
const userProfile = await fetchUserProfile(user.npub, ndk);
|
2024-04-15 00:42:01 +00:00
|
|
|
const nip05 = userProfile.profile.nip05;
|
2024-04-15 00:04:50 +00:00
|
|
|
|
2024-04-15 00:42:01 +00:00
|
|
|
console.log("User's nip05:", nip05);
|
2024-04-15 00:04:50 +00:00
|
|
|
|
|
|
|
// Check if user's public key exists in the nostr.json
|
2024-04-16 14:05:34 +00:00
|
|
|
const nostrJsonResponse = await fetch("/.well-known/nostr.json");
|
2024-04-15 00:04:50 +00:00
|
|
|
if (!nostrJsonResponse.ok) {
|
2024-04-16 14:05:34 +00:00
|
|
|
throw new Error(
|
|
|
|
"Failed to fetch nostr.json: " + nostrJsonResponse.statusText
|
|
|
|
);
|
2024-04-15 00:04:50 +00:00
|
|
|
}
|
|
|
|
const nostrJson = await nostrJsonResponse.json();
|
|
|
|
console.log("nostrJson object:", nostrJson);
|
|
|
|
|
2024-04-15 00:42:01 +00:00
|
|
|
// Extract the username from the NIP-05 identifier
|
2024-04-16 14:05:34 +00:00
|
|
|
const nip05Username = nip05.split("@")[0];
|
2024-04-15 00:42:01 +00:00
|
|
|
|
|
|
|
if (nostrJson.names[nip05Username] === user.pubkey) {
|
2024-04-15 00:04:50 +00:00
|
|
|
// User exists in nostr.json, proceed with further authentication or actions
|
2024-04-16 14:05:34 +00:00
|
|
|
console.log("User authenticated successfully.");
|
|
|
|
localStorage.setItem("nip05Username", nip05Username);
|
2024-04-15 00:04:50 +00:00
|
|
|
// Redirect or show UI for file upload
|
|
|
|
showFileUploadUI();
|
|
|
|
} else {
|
2024-04-16 14:05:34 +00:00
|
|
|
console.log("User not found in the authentication database.");
|
|
|
|
alert("User not found in the authentication database.");
|
2024-04-15 00:04:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-04-16 14:05:34 +00:00
|
|
|
console.log("User information not available from signer.");
|
2024-04-15 00:04:50 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-04-16 14:05:34 +00:00
|
|
|
console.error("Error during authentication:", error);
|
|
|
|
alert("Error during authentication: " + error.message);
|
2024-04-15 00:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showFileUploadUI() {
|
2024-04-16 14:05:34 +00:00
|
|
|
// Clear any existing content
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
|
|
|
|
// Create file input element
|
|
|
|
const fileInput = document.createElement("input");
|
|
|
|
fileInput.type = "file";
|
|
|
|
fileInput.addEventListener("change", handleFileSelect);
|
|
|
|
|
|
|
|
// Create submit button
|
|
|
|
const submitButton = document.createElement("button");
|
|
|
|
submitButton.textContent = "Upload";
|
|
|
|
submitButton.addEventListener("click", handleUpload);
|
|
|
|
|
|
|
|
// Append elements to the body
|
|
|
|
document.body.appendChild(fileInput);
|
|
|
|
document.body.appendChild(submitButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleFileSelect(event) {
|
|
|
|
const selectedFile = event.target.files[0];
|
|
|
|
console.log("Selected file:", selectedFile);
|
|
|
|
}
|
|
|
|
|
2024-04-16 15:33:11 +00:00
|
|
|
const serverUrl = "http://localhost:34537"; // Adjust the port as needed
|
2024-04-16 14:05:34 +00:00
|
|
|
|
|
|
|
async function handleUpload() {
|
|
|
|
const fileInput = document.querySelector('input[type="file"]');
|
|
|
|
const selectedFile = fileInput.files[0];
|
2024-04-16 15:15:06 +00:00
|
|
|
const nip05Username = localStorage.getItem("nip05Username");
|
2024-04-16 14:05:34 +00:00
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("file", selectedFile);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(
|
|
|
|
`${serverUrl}/upload?nip05Username=${nip05Username}`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.ok) {
|
2024-04-16 15:15:06 +00:00
|
|
|
const data = await response.json();
|
|
|
|
console.log("File uploaded successfully:", data);
|
2024-04-16 14:05:34 +00:00
|
|
|
// Handle success
|
|
|
|
} else {
|
2024-04-16 15:15:06 +00:00
|
|
|
const errorMessage = await response.text(); // Get error message from response
|
|
|
|
console.error("Error uploading file. Server response:", errorMessage);
|
|
|
|
// Handle error
|
2024-04-16 14:05:34 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error uploading file:", error);
|
2024-04-16 15:15:06 +00:00
|
|
|
// Handle network errors
|
2024-04-16 14:05:34 +00:00
|
|
|
}
|
|
|
|
}
|