// Import the Nostr SDK package import { NDKNip07Signer } from "@nostr-dev-kit/ndk"; import NDK from "@nostr-dev-kit/ndk"; document.querySelector("button").addEventListener("click", login); async function fetchUserProfile(npub, ndk) { try { console.log("Fetching user profile for npub:", npub); 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; } } const relays = ["wss://purplepag.es"]; 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); const nip05 = userProfile.profile.nip05; console.log("User's nip05:", nip05); // Check if user's public key exists in the nostr.json const nostrJsonResponse = await fetch("/.well-known/nostr.json"); if (!nostrJsonResponse.ok) { throw new Error( "Failed to fetch nostr.json: " + nostrJsonResponse.statusText ); } const nostrJson = await nostrJsonResponse.json(); console.log("nostrJson object:", nostrJson); // Extract the username from the NIP-05 identifier const nip05Username = nip05.split("@")[0]; if (nostrJson.names[nip05Username] === user.pubkey) { // User exists in nostr.json, proceed with further authentication or actions console.log("User authenticated successfully."); localStorage.setItem("nip05Username", nip05Username); // Redirect or show UI for file upload showFileUploadUI(); } else { console.log("User not found in the authentication database."); alert("User not found in the authentication database."); } } else { console.log("User information not available from signer."); } } catch (error) { console.error("Error during authentication:", error); alert("Error during authentication: " + error.message); } } function showFileUploadUI() { // 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); } const serverUrl = "http://localhost:34537"; // Adjust the port as needed async function handleUpload() { const fileInput = document.querySelector('input[type="file"]'); const selectedFile = fileInput.files[0]; const nip05Username = localStorage.getItem("nip05Username"); const formData = new FormData(); formData.append("file", selectedFile); try { const response = await fetch( `${serverUrl}/upload?nip05Username=${nip05Username}`, { method: "POST", body: formData, } ); if (response.ok) { const data = await response.json(); console.log("File uploaded successfully:", data); // Handle success } else { const errorMessage = await response.text(); // Get error message from response console.error("Error uploading file. Server response:", errorMessage); // Handle error } } catch (error) { console.error("Error uploading file:", error); // Handle network errors } }