first commit

This commit is contained in:
Chris kerr 2024-04-14 20:04:50 -04:00
commit 753a90fe62
8 changed files with 1516 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

1
.well-known/nostr.json Normal file
View File

@ -0,0 +1 @@
{"names": {"oceanslim": "16f1a0100d4cfffbcc4230e8e0e4290cc5849c1adc64d6653fda07c031b1074b", "mobaphile": "8dce13c6e959298dcd058206469a0d29efe208abbd8a4e28a518a14d48cf8aa8", "baz.": "925e58d8fd209d9a8499f8709ac82daccd2d5c884a8c836478e688f29650e89a", "zachf": "b9360cd808b24ecbfd03575f3d637b1e62ca9fea415ac67b6c5b11ef15f28d06", "david": "de279ef2777cca0252b0865ec1bf5375f32122fcda65fdf223a5788718f105ff", "bitcoin": "fe2d5cf62e95aab419b07b6f8a7b75d3cb3066fae25c6b44ace0f9f30c59303d", "poe7645": "e034d654802d7cfaa2d41a952801054114e09ad6a352b28288e23075ca919814", "derekross": "3f770d65d3a764a9c5cb503ae123e62ec7598ad035d836e2a810f3877a745b24"}}

16
.well-known/readme.md Normal file
View File

@ -0,0 +1,16 @@
pre req:
get node js
google it
install:
clone the repo
cd into it
install dependencies:
`npm install`
run the dev server:
`vite`
go to http://localhost:5173
check out the console (right click inspect)

14
index.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nostr Authentication Example</title>
</head>
<body>
<h1>Nostr Authentication Example</h1>
<button>Log in with Browser Extension (NIP-07)</button>
<script type="module" src="index.js"></script>
</body>
</html>

72
index.js Normal file
View File

@ -0,0 +1,72 @@
// 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://nostr.happytavern.co"])
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 displayName = userProfile.profile.displayName;
console.log("User's display name:", displayName);
// 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);
if (nostrJson.names[displayName] === user.pubkey) {
// User exists in nostr.json, proceed with further authentication or actions
console.log('User authenticated successfully.');
// 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() {
// Show UI for file upload
console.log('Show file upload UI...');
// Implement UI logic for file upload
}

1321
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "nostr-media-upload",
"version": "1.0.0",
"description": "Upload Media to domain, auth with nip7 signer",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "OceanSlim",
"license": "MIT",
"dependencies": {
"@nostr-dev-kit/ndk": "^2.6.1",
"bech32": "^2.0.0",
"bitcoinjs-lib": "^6.1.5",
"vite": "^5.2.7"
}
}

74
upload.html Normal file
View File

@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h1>File Upload</h1>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file" multiple>
<button type="submit">Upload</button>
</form>
<script>
async function checkPermissions(npub) {
try {
const response = await fetch('https://happytaver.co/.well-known/nostr.json');
const data = await response.json();
return data.hasOwnProperty(npub);
} catch (error) {
console.error('Error checking permissions:', error);
return false;
}
}
document.getElementById('uploadForm').addEventListener('submit', async (event) => {
event.preventDefault();
const fileInput = document.querySelector('input[type="file"]');
const files = fileInput.files;
if (files.length === 0) {
alert('Please select a file to upload.');
return;
}
const npub = localStorage.getItem('npub'); // Assuming npub is stored in localStorage
if (!npub) {
alert('User not authenticated.');
return;
}
const hasPermission = await checkPermissions(npub);
if (!hasPermission) {
alert('User does not have permission to upload.');
return;
}
// Proceed with file upload
const formData = new FormData();
for (const file of files) {
formData.append('files', file);
}
try {
const uploadResponse = await fetch('https://your-upload-endpoint.com/upload', {
method: 'POST',
body: formData
});
const uploadData = await uploadResponse.json();
console.log('Upload successful:', uploadData);
alert('Files uploaded successfully.');
} catch (error) {
console.error('Error uploading files:', error);
alert('Error occurred during file upload.');
}
});
</script>
</body>
</html>