v0.3.7 - working on cinfig api

This commit is contained in:
Your Name
2025-09-16 15:52:27 -04:00
parent 2d66b8bf1d
commit 3210b9e752
8 changed files with 1373 additions and 417 deletions

View File

@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -10,7 +11,7 @@
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Courier New', monospace;
background-color: white;
@@ -20,7 +21,7 @@
max-width: 1200px;
margin: 0 auto;
}
h1 {
border-bottom: 2px solid black;
padding-bottom: 10px;
@@ -28,7 +29,7 @@
font-weight: normal;
font-size: 24px;
}
h2 {
margin: 30px 0 15px 0;
font-weight: normal;
@@ -36,25 +37,28 @@
padding-left: 10px;
font-size: 16px;
}
.section {
border: 1px solid black;
padding: 20px;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
font-size: 14px;
}
input, textarea, select, button {
input,
textarea,
select,
button {
width: 100%;
padding: 8px;
border: 1px solid black;
@@ -63,7 +67,7 @@
background-color: white;
color: black;
}
button {
background-color: black;
color: white;
@@ -71,65 +75,65 @@
margin: 5px 0;
font-weight: bold;
}
button:hover {
background-color: #333;
}
button:disabled {
background-color: #ccc;
color: #666;
cursor: not-allowed;
}
.status {
padding: 10px;
margin: 10px 0;
border: 1px solid black;
font-weight: bold;
}
.status.connected {
background-color: black;
color: white;
}
.status.disconnected {
background-color: white;
color: black;
}
.status.authenticated {
background-color: black;
color: white;
}
.status.error {
background-color: white;
color: black;
border: 2px solid black;
}
.config-table {
border: 1px solid black;
width: 100%;
border-collapse: collapse;
margin: 10px 0;
}
.config-table th,
.config-table td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
.config-table th {
background-color: black;
color: white;
font-weight: bold;
}
.json-display {
background-color: white;
border: 1px solid black;
@@ -141,7 +145,7 @@
overflow-y: auto;
margin: 10px 0;
}
.log-panel {
height: 200px;
overflow-y: auto;
@@ -150,71 +154,72 @@
font-size: 12px;
background-color: white;
}
.log-entry {
margin-bottom: 5px;
border-bottom: 1px solid #ccc;
padding-bottom: 5px;
}
.log-timestamp {
font-weight: bold;
}
.inline-buttons {
display: flex;
gap: 10px;
}
.inline-buttons button {
flex: 1;
}
.user-info {
padding: 10px;
border: 1px solid black;
margin: 10px 0;
background-color: white;
}
.user-pubkey {
font-family: 'Courier New', monospace;
font-size: 12px;
word-break: break-all;
margin: 5px 0;
}
.hidden {
display: none;
}
#login-section {
text-align: center;
padding: 20px;
}
@media (max-width: 768px) {
body {
padding: 10px;
}
.inline-buttons {
flex-direction: column;
}
h1 {
font-size: 20px;
}
h2 {
font-size: 14px;
}
}
</style>
</head>
<body>
<h1>C-RELAY ADMIN API</h1>
<!-- Testing Section - Always Visible -->
<div class="section">
<h2>DEBUG - TEST FETCH WITHOUT LOGIN</h2>
@@ -225,7 +230,7 @@
<div class="status disconnected" id="relay-status">READY TO FETCH</div>
<button type="button" id="fetch-config-btn">FETCH CONFIGURATION (NO LOGIN)</button>
<div class="status disconnected" id="config-status">NO CONFIGURATION LOADED</div>
<div id="config-display" class="hidden">
<div id="config-view-mode">
<table class="config-table" id="config-table">
@@ -239,7 +244,7 @@
<tbody id="config-table-body">
</tbody>
</table>
<div class="inline-buttons">
<button type="button" id="edit-config-btn">EDIT CONFIGURATION</button>
<button type="button" id="copy-config-btn">COPY CONFIGURATION</button>
@@ -251,13 +256,13 @@
<div id="config-form" class="section">
<!-- Dynamic form will be generated here -->
</div>
<div class="inline-buttons">
<button type="button" id="save-config-btn">SAVE & PUBLISH</button>
<button type="button" id="cancel-edit-btn">CANCEL</button>
</div>
</div>
<div id="config-raw-display">
<h3>Raw Event JSON:</h3>
<div class="json-display" id="raw-config-json"></div>
@@ -273,21 +278,23 @@
<!-- nostr-lite login UI will be injected here -->
</div>
</div>
<!-- Main Interface (hidden until logged in) -->
<div id="main-interface" class="hidden">
<!-- User Info Section -->
<div class="section">
<h2>LOGGED IN USER</h2>
<div class="user-info">
<div><strong>Name:</strong> <span id="user-name">Loading...</span></div>
<div><strong>Public Key:</strong> <div class="user-pubkey" id="user-pubkey">Loading...</div></div>
<div><strong>Public Key:</strong>
<div class="user-pubkey" id="user-pubkey">Loading...</div>
</div>
<div><strong>About:</strong> <span id="user-about">Loading...</span></div>
</div>
<button type="button" id="logout-btn">LOGOUT</button>
</div>
<!-- Command Section -->
<div class="section">
<h2>ADMIN COMMANDS</h2>
@@ -300,20 +307,20 @@
<option value="get_status">Get Status</option>
</select>
</div>
<div class="input-group">
<label for="command-payload">Command Payload (JSON):</label>
<textarea id="command-payload" rows="4" placeholder='{"param": "value"}'></textarea>
</div>
<div class="input-group">
<label>Event Preview:</label>
<div class="json-display" id="event-preview">No event constructed</div>
</div>
<button type="button" id="send-command-btn">SEND COMMAND</button>
</div>
<!-- Log Section -->
<div class="section">
<h2>EVENT LOG</h2>
@@ -324,7 +331,7 @@
</div>
<button type="button" id="clear-log-btn">CLEAR LOG</button>
</div>
</div>
<!-- Load the official nostr-tools bundle first -->
@@ -342,10 +349,10 @@
<script>
// Global error handler to prevent page refreshes
window.addEventListener('error', function(e) {
window.addEventListener('error', function (e) {
console.error('Global error caught:', e.error);
console.error('Error message:', e.message);
console.error('Error filename:', e.filename);
@@ -354,7 +361,7 @@
return true; // Prevent page refresh
});
window.addEventListener('unhandledrejection', function(e) {
window.addEventListener('unhandledrejection', function (e) {
console.error('Unhandled promise rejection:', e.reason);
e.preventDefault(); // Prevent default browser error handling
return true; // Prevent page refresh
@@ -399,10 +406,10 @@
function log(message, type = 'INFO') {
const timestamp = new Date().toISOString().split('T')[1].split('.')[0];
const logMessage = `${timestamp} [${type}]: ${message}`;
// Always log to browser console so we don't lose logs on refresh
console.log(logMessage);
// Also log to UI if elements exist
if (logPanel) {
const logEntry = document.createElement('div');
@@ -418,35 +425,33 @@
try {
await window.NOSTR_LOGIN_LITE.init({
theme: 'default',
darkMode: false,
relays: ['wss://relay.damus.io', 'wss://nos.lol'],
methods: {
extension: true,
local: true,
readonly: true,
connect: true, // Enables "Nostr Connect" (NIP-46)
remote: true, // Also needed for "Nostr Connect" compatibility
otp: true // Enables "DM/OTP"
connect: true,
remote: true,
otp: true
},
floatingTab: {
enabled: true,
hPosition: 0.98, // 80% from left
vPosition: 0.00, // Top of page
hPosition: 1, // 0.0-1.0 or '95%' from left
vPosition: 0, // 0.0-1.0 or '50%' from top
appearance: {
style: 'minimal',
theme: 'auto',
icon: '',
text: 'Login',
iconOnly: false
style: 'pill', // 'pill', 'square', 'circle', 'minimal'
icon: '', // Clean display without icon placeholders
text: 'Login'
},
behavior: {
hideWhenAuthenticated: false,
showUserInfo: true,
autoSlide: false,
persistent: false
}
},
debug: true
autoSlide: true
},
getUserInfo: true, // Enable profile fetching
getUserRelay: [ // Specific relays for profile fetching
'wss://relay.laantungir.net'
]
}
});
nlLite = window.NOSTR_LOGIN_LITE;
@@ -462,26 +467,26 @@
// Handle authentication events
function handleAuthEvent(event) {
const {pubkey, method, error} = event.detail;
const { pubkey, method, error } = event.detail;
if (method && pubkey) {
userPubkey = pubkey;
isLoggedIn = true;
console.log(`Login successful! Method: ${method}`);
console.log(`Public key: ${pubkey}`);
showMainInterface();
loadUserProfile();
// Automatically fetch configuration after login
setTimeout(() => {
fetchConfiguration().catch(error => {
console.log('Auto-fetch configuration failed: ' + error.message);
});
}, 1000);
console.log('Login successful. Auto-fetching configuration...');
} else if (error) {
console.log(`Authentication error: ${error}`);
}
@@ -506,7 +511,7 @@
// Create a SimplePool instance
relayPool = new window.NostrTools.SimplePool();
const relays = ['wss://relay.laantungir.net'];
// Get profile event (kind 0) for the user
const events = await relayPool.querySync(relays, {
kinds: [0],
@@ -549,7 +554,7 @@
const relays = ['wss://relay.laantungir.net'];
relayPool = null;
}
// Clean up configuration WebSocket
if (configWebSocket) {
console.log('Closing configuration WebSocket...');
@@ -557,22 +562,22 @@
configWebSocket = null;
subscriptionId = null;
}
await nlLite.logout();
userPubkey = null;
isLoggedIn = false;
currentConfig = null;
// Reset UI
mainInterface.classList.add('hidden');
loginSection.classList.remove('hidden');
updateConfigStatus(false);
relayStatus.textContent = 'READY TO FETCH';
relayStatus.className = 'status disconnected';
console.log('Logged out successfully');
} catch (error) {
console.log('Logout failed: ' + error.message);
}
@@ -603,11 +608,11 @@
async function subscribeToConfiguration() {
try {
console.log('=== STARTING DIRECT WEBSOCKET CONFIGURATION SUBSCRIPTION ===');
if (!isLoggedIn) {
console.log('WARNING: Not logged in, but proceeding with subscription test');
}
const url = relayUrl.value.trim();
if (!url) {
console.error('Please enter a relay URL');
@@ -653,10 +658,10 @@
reject(new Error('Connection timeout'));
}, 10000); // 10 second timeout
configWebSocket.onopen = function(event) {
configWebSocket.onopen = function (event) {
console.log('WebSocket connection established');
clearTimeout(timeoutId);
relayStatus.textContent = 'CONNECTED - SUBSCRIBING...';
relayStatus.className = 'status connected';
@@ -683,7 +688,7 @@
resolve(true);
};
configWebSocket.onmessage = function(event) {
configWebSocket.onmessage = function (event) {
try {
const message = JSON.parse(event.data);
console.log('Received message:', message);
@@ -704,12 +709,12 @@
relayStatus.textContent = 'SUBSCRIBED - LIVE UPDATES';
relayStatus.className = 'status connected';
} else if (messageType === "EOSE" && subId === subscriptionId) {
clearTimeout(timeoutId);
console.log('EOSE received - End of stored events');
console.log('Current config after EOSE:', currentConfig);
if (!currentConfig) {
console.log('No configuration events were received');
configStatus.textContent = 'NO CONFIGURATION EVENTS FOUND';
@@ -720,22 +725,22 @@
relayStatus.textContent = 'SUBSCRIBED - LIVE UPDATES';
relayStatus.className = 'status connected';
}
} else if (messageType === "NOTICE") {
console.log('Received NOTICE:', eventData || message[1]);
} else if (messageType === "OK") {
console.log('Received OK response:', message);
}
}
} catch (parseError) {
console.error('Error parsing message:', parseError);
console.log('Raw message data:', event.data);
}
};
configWebSocket.onerror = function(error) {
configWebSocket.onerror = function (error) {
clearTimeout(timeoutId);
console.error('WebSocket error:', error);
console.error('WebSocket URL that failed:', urlsToTry[0]);
@@ -750,7 +755,7 @@
reject(new Error(`WebSocket connection error to ${urlsToTry[0]}`));
};
configWebSocket.onclose = function(event) {
configWebSocket.onclose = function (event) {
clearTimeout(timeoutId);
console.log('WebSocket connection closed:', event.code, event.reason);
relayStatus.textContent = 'CONNECTION CLOSED';
@@ -764,7 +769,7 @@
console.error('Configuration subscription failed:', error.message);
console.error('Configuration subscription failed:', error);
console.error('Error stack:', error.stack);
relayStatus.textContent = 'SUBSCRIPTION FAILED';
relayStatus.className = 'status error';
return false;
@@ -780,12 +785,12 @@
try {
console.log('=== DISPLAYING CONFIGURATION EVENT ===');
console.log('Event received for display:', event);
currentConfig = event;
// Clear existing table
configTableBody.innerHTML = '';
// Display basic event info
const basicInfo = [
['Event ID', event.id],
@@ -794,14 +799,14 @@
['Kind', event.kind],
['Content', event.content]
];
console.log(`Adding ${basicInfo.length} basic info rows`);
basicInfo.forEach(([key, value]) => {
const row = document.createElement('tr');
row.innerHTML = `<td>${key}</td><td>${value}</td><td>-</td>`;
configTableBody.appendChild(row);
});
// Display tags
console.log(`Processing ${event.tags.length} tags`);
event.tags.forEach(tag => {
@@ -811,13 +816,13 @@
configTableBody.appendChild(row);
}
});
// Display raw JSON
rawConfigJson.textContent = JSON.stringify(event, null, 2);
console.log('Configuration display completed successfully');
updateConfigStatus(true);
} catch (error) {
console.error('Error in displayConfiguration:', error.message);
console.error('Display configuration error:', error);
@@ -832,7 +837,7 @@
}
configForm.innerHTML = '';
// Define field types and validation for different config parameters
const fieldTypes = {
'auth_enabled': 'boolean',
@@ -897,16 +902,16 @@
Object.entries(configData).forEach(([key, value]) => {
const fieldType = fieldTypes[key] || 'text';
const description = descriptions[key] || key.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
const fieldGroup = document.createElement('div');
fieldGroup.className = 'input-group';
const label = document.createElement('label');
label.textContent = description;
label.setAttribute('for', `config-${key}`);
let input;
if (fieldType === 'boolean') {
input = document.createElement('select');
input.innerHTML = `
@@ -923,15 +928,15 @@
input.type = 'text';
input.value = value;
}
input.id = `config-${key}`;
input.name = key;
// Make relay_pubkey read-only
if (key === 'relay_pubkey' || key === 'd') {
input.disabled = true;
}
fieldGroup.appendChild(label);
fieldGroup.appendChild(input);
configForm.appendChild(fieldGroup);
@@ -945,7 +950,7 @@
console.log('No configuration loaded to edit');
return;
}
generateConfigForm(currentConfig);
configViewMode.classList.add('hidden');
configEditMode.classList.remove('hidden');
@@ -972,18 +977,18 @@
try {
console.log('Building new configuration event...');
// Collect form data
const formData = new FormData();
const formInputs = configForm.querySelectorAll('input, select');
const newTags = [];
// Preserve the 'd' tag (relay identifier) from original event
const dTag = currentConfig.tags.find(tag => tag[0] === 'd');
if (dTag) {
newTags.push(dTag);
}
// Add updated configuration tags
formInputs.forEach(input => {
if (!input.disabled && input.name) {
@@ -1000,11 +1005,11 @@
content: currentConfig.content || 'C Nostr Relay Configuration'
};
console.log('Signing event with nostr-lite...');
// Sign the event using nostr-lite
const signedEvent = await nlLite.signEvent(newEvent);
console.log('Signing event with window.nostr...');
// Sign the event using window.nostr (NIP-07 interface)
const signedEvent = await window.nostr.signEvent(newEvent);
if (!signedEvent || !signedEvent.sig) {
throw new Error('Event signing failed - no signature returned');
}
@@ -1032,7 +1037,7 @@
// Create a new WebSocket connection for publishing
const publishWs = new WebSocket(url);
return new Promise((resolve, reject) => {
let timeoutId = setTimeout(() => {
console.error('Publish timeout');
@@ -1040,31 +1045,31 @@
reject(new Error('Publish timeout'));
}, 10000);
publishWs.onopen = function() {
publishWs.onopen = function () {
console.log('Publish WebSocket connected, sending event...');
// Send EVENT message
const eventMessage = ["EVENT", signedEvent];
console.log('Sending EVENT message:', JSON.stringify(eventMessage));
publishWs.send(JSON.stringify(eventMessage));
};
publishWs.onmessage = function(event) {
publishWs.onmessage = function (event) {
try {
const message = JSON.parse(event.data);
console.log('Publish response:', message);
if (Array.isArray(message)) {
const [messageType, eventId, success, errorMsg] = message;
if (messageType === "OK") {
clearTimeout(timeoutId);
publishWs.close();
if (success) {
console.log('Configuration published successfully!');
console.log('The updated configuration should appear automatically via subscription');
// Exit edit mode
exitEditMode();
resolve(true);
@@ -1079,14 +1084,14 @@
}
};
publishWs.onerror = function(error) {
publishWs.onerror = function (error) {
clearTimeout(timeoutId);
console.error('Publish WebSocket error:', error);
publishWs.close();
reject(new Error('Publish WebSocket error'));
};
publishWs.onclose = function() {
publishWs.onclose = function () {
clearTimeout(timeoutId);
};
});
@@ -1101,12 +1106,12 @@
function updateEventPreview() {
const type = commandType.value;
const payload = commandPayload.value.trim();
if (!type || !userPubkey) {
eventPreview.textContent = 'No event constructed';
return;
}
const event = {
kind: 1,
pubkey: userPubkey,
@@ -1119,13 +1124,13 @@
id: 'EVENT_ID_PLACEHOLDER',
sig: 'SIGNATURE_PLACEHOLDER'
};
eventPreview.textContent = JSON.stringify(event, null, 2);
}
// Event handlers
logoutBtn.addEventListener('click', logout);
fetchConfigBtn.addEventListener('click', function(e) {
fetchConfigBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
fetchConfiguration().catch(error => {
@@ -1133,7 +1138,7 @@
});
});
copyConfigBtn.addEventListener('click', function(e) {
copyConfigBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
if (currentConfig) {
@@ -1143,13 +1148,13 @@
}
});
editConfigBtn.addEventListener('click', function(e) {
editConfigBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
enterEditMode();
});
saveConfigBtn.addEventListener('click', function(e) {
saveConfigBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
saveConfiguration().catch(error => {
@@ -1157,7 +1162,7 @@
});
});
cancelEditBtn.addEventListener('click', function(e) {
cancelEditBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
exitEditMode();
@@ -1166,7 +1171,7 @@
commandType.addEventListener('change', updateEventPreview);
commandPayload.addEventListener('input', updateEventPreview);
sendCommandBtn.addEventListener('click', function(e) {
sendCommandBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
const type = commandType.value;
@@ -1174,11 +1179,11 @@
console.log('Please select a command type');
return;
}
console.log(`Command sending not yet implemented: ${type}`);
});
clearLogBtn.addEventListener('click', function(e) {
clearLogBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
logPanel.innerHTML = '<div class="log-entry"><span class="log-timestamp">SYSTEM:</span> Log cleared.</div>';
@@ -1191,4 +1196,5 @@
});
</script>
</body>
</html>

View File

@@ -8,7 +8,7 @@
* Two-file architecture:
* 1. Load nostr.bundle.js (official nostr-tools bundle)
* 2. Load nostr-lite.js (this file - NOSTR_LOGIN_LITE library with CSS-only themes)
* Generated on: 2025-09-15T18:50:50.789Z
* Generated on: 2025-09-16T15:52:30.145Z
*/
// Verify dependencies are loaded
@@ -1128,23 +1128,62 @@ class Modal {
}
_handleExtension() {
// Detect all available real extensions
const availableExtensions = this._detectAllExtensions();
// SIMPLIFIED ARCHITECTURE: Check for single extension at window.nostr or preserved extension
let extension = null;
console.log(`Modal: Found ${availableExtensions.length} extensions:`, availableExtensions.map(e => e.displayName));
if (availableExtensions.length === 0) {
console.log('Modal: No real extensions found');
this._showExtensionRequired();
} else if (availableExtensions.length === 1) {
// Single extension - use it directly without showing choice UI
console.log('Modal: Single extension detected, using it directly:', availableExtensions[0].displayName);
this._tryExtensionLogin(availableExtensions[0].extension);
} else {
// Multiple extensions - show choice UI
console.log('Modal: Multiple extensions detected, showing choice UI for', availableExtensions.length, 'extensions');
this._showExtensionChoice(availableExtensions);
// Check if NostrLite instance has a preserved extension (real extension detected at init)
if (window.NOSTR_LOGIN_LITE?._instance?.preservedExtension) {
extension = window.NOSTR_LOGIN_LITE._instance.preservedExtension;
console.log('Modal: Using preserved extension:', extension.constructor?.name);
}
// Otherwise check current window.nostr
else if (window.nostr && this._isRealExtension(window.nostr)) {
extension = window.nostr;
console.log('Modal: Using current window.nostr extension:', extension.constructor?.name);
}
if (!extension) {
console.log('Modal: No extension detected yet, waiting for deferred detection...');
// DEFERRED EXTENSION CHECK: Extensions like nos2x might load after our library
let attempts = 0;
const maxAttempts = 10; // Try for 2 seconds
const checkForExtension = () => {
attempts++;
// Check again for preserved extension (might be set by deferred detection)
if (window.NOSTR_LOGIN_LITE?._instance?.preservedExtension) {
extension = window.NOSTR_LOGIN_LITE._instance.preservedExtension;
console.log('Modal: Found preserved extension after waiting:', extension.constructor?.name);
this._tryExtensionLogin(extension);
return;
}
// Check current window.nostr again
if (window.nostr && this._isRealExtension(window.nostr)) {
extension = window.nostr;
console.log('Modal: Found extension at window.nostr after waiting:', extension.constructor?.name);
this._tryExtensionLogin(extension);
return;
}
// Keep trying or give up
if (attempts < maxAttempts) {
setTimeout(checkForExtension, 200);
} else {
console.log('Modal: No browser extension found after waiting 2 seconds');
this._showExtensionRequired();
}
};
// Start checking after a brief delay
setTimeout(checkForExtension, 200);
return;
}
// Use the single detected extension directly - no choice UI
console.log('Modal: Single extension mode - using extension directly');
this._tryExtensionLogin(extension);
}
_detectAllExtensions() {
@@ -1190,17 +1229,38 @@ class Modal {
// Also check window.nostr but be extra careful to avoid our library
console.log('Modal: Checking window.nostr:', !!window.nostr, window.nostr?.constructor?.name);
if (window.nostr && this._isRealExtension(window.nostr) && !seenExtensions.has(window.nostr)) {
extensions.push({
name: 'window.nostr',
displayName: 'Extension (window.nostr)',
icon: '🔑',
extension: window.nostr
});
seenExtensions.add(window.nostr);
console.log(`Modal: ✓ Detected extension at window.nostr: ${window.nostr.constructor?.name}`);
} else if (window.nostr) {
console.log(`Modal: ✗ Filtered out window.nostr (${window.nostr.constructor?.name}) - likely our library`);
if (window.nostr) {
// Check if window.nostr is our WindowNostr facade with a preserved extension
if (window.nostr.constructor?.name === 'WindowNostr' && window.nostr.existingNostr) {
console.log('Modal: Found WindowNostr facade, checking existingNostr for preserved extension');
const preservedExtension = window.nostr.existingNostr;
console.log('Modal: Preserved extension:', !!preservedExtension, preservedExtension?.constructor?.name);
if (preservedExtension && this._isRealExtension(preservedExtension) && !seenExtensions.has(preservedExtension)) {
extensions.push({
name: 'window.nostr.existingNostr',
displayName: 'Extension (preserved by WindowNostr)',
icon: '🔑',
extension: preservedExtension
});
seenExtensions.add(preservedExtension);
console.log(`Modal: ✓ Detected preserved extension: ${preservedExtension.constructor?.name}`);
}
}
// Check if window.nostr is directly a real extension (not our facade)
else if (this._isRealExtension(window.nostr) && !seenExtensions.has(window.nostr)) {
extensions.push({
name: 'window.nostr',
displayName: 'Extension (window.nostr)',
icon: '🔑',
extension: window.nostr
});
seenExtensions.add(window.nostr);
console.log(`Modal: ✓ Detected extension at window.nostr: ${window.nostr.constructor?.name}`);
} else {
console.log(`Modal: ✗ Filtered out window.nostr (${window.nostr.constructor?.name}) - not a real extension`);
}
}
return extensions;
@@ -1790,6 +1850,63 @@ class Modal {
}
_setAuthMethod(method, options = {}) {
// SINGLE-EXTENSION ARCHITECTURE: Handle method switching
console.log('Modal: _setAuthMethod called with:', method, options);
// CRITICAL: Never install facade for extension methods - leave window.nostr as the extension
if (method === 'extension') {
console.log('Modal: Extension method - NOT installing facade, leaving window.nostr as extension');
// Emit auth method selection directly for extension
const event = new CustomEvent('nlMethodSelected', {
detail: { method, ...options }
});
window.dispatchEvent(event);
this.close();
return;
}
// For non-extension methods, we need to ensure WindowNostr facade is available
console.log('Modal: Non-extension method detected:', method);
// Check if we have a preserved extension but no WindowNostr facade installed
const hasPreservedExtension = !!window.NOSTR_LOGIN_LITE?._instance?.preservedExtension;
const hasWindowNostrFacade = window.nostr?.constructor?.name === 'WindowNostr';
console.log('Modal: Method switching check:');
console.log(' method:', method);
console.log(' hasPreservedExtension:', hasPreservedExtension);
console.log(' hasWindowNostrFacade:', hasWindowNostrFacade);
console.log(' current window.nostr constructor:', window.nostr?.constructor?.name);
// If we have a preserved extension but no facade, install facade for method switching
if (hasPreservedExtension && !hasWindowNostrFacade) {
console.log('Modal: Installing WindowNostr facade for method switching (non-extension authentication)');
// Get the NostrLite instance and install facade with preserved extension
const nostrLiteInstance = window.NOSTR_LOGIN_LITE?._instance;
if (nostrLiteInstance && typeof nostrLiteInstance._installFacade === 'function') {
const preservedExtension = nostrLiteInstance.preservedExtension;
console.log('Modal: Installing facade with preserved extension:', preservedExtension?.constructor?.name);
nostrLiteInstance._installFacade(preservedExtension);
console.log('Modal: WindowNostr facade installed for method switching');
} else {
console.error('Modal: Cannot access NostrLite instance or _installFacade method');
}
}
// If no extension at all, ensure facade is installed for local/NIP-46/readonly methods
else if (!hasPreservedExtension && !hasWindowNostrFacade) {
console.log('Modal: Installing WindowNostr facade for non-extension methods (no extension detected)');
const nostrLiteInstance = window.NOSTR_LOGIN_LITE?._instance;
if (nostrLiteInstance && typeof nostrLiteInstance._installFacade === 'function') {
nostrLiteInstance._installFacade();
console.log('Modal: WindowNostr facade installed for non-extension methods');
}
}
// Emit auth method selection
const event = new CustomEvent('nlMethodSelected', {
detail: { method, ...options }
@@ -1823,8 +1940,13 @@ class Modal {
title.style.cssText = 'margin: 0 0 16px 0; font-size: 18px; font-weight: 600;';
const message = document.createElement('p');
message.textContent = 'Please install a Nostr browser extension like Alby or getflattr and refresh the page.';
message.style.cssText = 'margin-bottom: 20px; color: #6b7280;';
message.innerHTML = `
Please install a Nostr browser extension and refresh the page.<br><br>
<strong>Important:</strong> If you have multiple extensions installed, please disable all but one to avoid conflicts.
<br><br>
Popular extensions: Alby, nos2x, Flamingo
`;
message.style.cssText = 'margin-bottom: 20px; color: #6b7280; font-size: 14px; line-height: 1.4;';
const backButton = document.createElement('button');
backButton.textContent = 'Back';
@@ -1867,27 +1989,11 @@ class Modal {
box-sizing: border-box;
`;
const urlLabel = document.createElement('label');
urlLabel.textContent = 'Remote URL (optional):';
urlLabel.style.cssText = 'display: block; margin-bottom: 8px; font-weight: 500;';
const urlInput = document.createElement('input');
urlInput.type = 'url';
urlInput.placeholder = 'ws://localhost:8080 (default)';
urlInput.style.cssText = `
width: 100%;
padding: 12px;
border: 1px solid #d1d5db;
border-radius: 6px;
margin-bottom: 16px;
box-sizing: border-box;
`;
// Users will enter the bunker URL manually from their bunker setup
// Users will enter the complete bunker connection string with relay info
const connectButton = document.createElement('button');
connectButton.textContent = 'Connect to Bunker';
connectButton.onclick = () => this._handleNip46Connect(pubkeyInput.value, urlInput.value);
connectButton.onclick = () => this._handleNip46Connect(pubkeyInput.value);
connectButton.style.cssText = this._getButtonStyle();
const backButton = document.createElement('button');
@@ -1897,8 +2003,6 @@ class Modal {
formGroup.appendChild(label);
formGroup.appendChild(pubkeyInput);
formGroup.appendChild(urlLabel);
formGroup.appendChild(urlInput);
this.modalBody.appendChild(title);
this.modalBody.appendChild(description);
@@ -1907,17 +2011,17 @@ class Modal {
this.modalBody.appendChild(backButton);
}
_handleNip46Connect(bunkerPubkey, bunkerUrl) {
_handleNip46Connect(bunkerPubkey) {
if (!bunkerPubkey || !bunkerPubkey.length) {
this._showError('Bunker pubkey is required');
return;
}
this._showNip46Connecting(bunkerPubkey, bunkerUrl);
this._performNip46Connect(bunkerPubkey, bunkerUrl);
this._showNip46Connecting(bunkerPubkey);
this._performNip46Connect(bunkerPubkey);
}
_showNip46Connecting(bunkerPubkey, bunkerUrl) {
_showNip46Connecting(bunkerPubkey) {
this.modalBody.innerHTML = '';
const title = document.createElement('h3');
@@ -1935,9 +2039,8 @@ class Modal {
bunkerInfo.style.cssText = 'background: #f1f5f9; padding: 12px; border-radius: 6px; margin-bottom: 20px; font-size: 14px;';
bunkerInfo.innerHTML = `
<strong>Connecting to bunker:</strong><br>
Pubkey: <code style="word-break: break-all;">${displayPubkey}</code><br>
Relay: <code style="word-break: break-all;">${bunkerUrl || 'ws://localhost:8080'}</code><br>
<small style="color: #6b7280;">If this relay is offline, the bunker server may be unavailable.</small>
Connection: <code style="word-break: break-all;">${displayPubkey}</code><br>
<small style="color: #6b7280;">Connection string contains all necessary relay information.</small>
`;
const connectingDiv = document.createElement('div');
@@ -1954,9 +2057,9 @@ class Modal {
this.modalBody.appendChild(connectingDiv);
}
async _performNip46Connect(bunkerPubkey, bunkerUrl) {
async _performNip46Connect(bunkerPubkey) {
try {
console.log('Starting NIP-46 connection to bunker:', bunkerPubkey, bunkerUrl);
console.log('Starting NIP-46 connection to bunker:', bunkerPubkey);
// Check if nostr-tools NIP-46 is available
if (!window.NostrTools?.nip46) {
@@ -2648,16 +2751,149 @@ class NostrLite {
_setupWindowNostrFacade() {
if (typeof window !== 'undefined') {
console.log('NOSTR_LOGIN_LITE: === TRUE SINGLE-EXTENSION ARCHITECTURE ===');
console.log('NOSTR_LOGIN_LITE: Initial window.nostr:', window.nostr);
console.log('NOSTR_LOGIN_LITE: Initial window.nostr constructor:', window.nostr?.constructor?.name);
// Store existing window.nostr if it exists (from extensions)
const existingNostr = window.nostr;
// Always install our facade
window.nostr = new WindowNostr(this, existingNostr);
console.log('NOSTR_LOGIN_LITE: window.nostr facade installed',
existingNostr ? '(with extension passthrough)' : '(no existing extension)');
// TRUE SINGLE-EXTENSION ARCHITECTURE: Don't install facade when extensions detected
if (this._isRealExtension(existingNostr)) {
console.log('NOSTR_LOGIN_LITE: ✓ REAL EXTENSION DETECTED IMMEDIATELY - PRESERVING WITHOUT FACADE');
console.log('NOSTR_LOGIN_LITE: Extension constructor:', existingNostr.constructor?.name);
console.log('NOSTR_LOGIN_LITE: Extension keys:', Object.keys(existingNostr));
console.log('NOSTR_LOGIN_LITE: Leaving window.nostr untouched for extension compatibility');
this.preservedExtension = existingNostr;
this.facadeInstalled = false;
// DON'T install facade - leave window.nostr as the extension
return;
}
// DEFERRED EXTENSION DETECTION: Extensions like nos2x may load after us
console.log('NOSTR_LOGIN_LITE: No real extension detected initially, starting deferred detection...');
this.facadeInstalled = false;
let checkCount = 0;
const maxChecks = 10; // Check for up to 2 seconds
const checkInterval = setInterval(() => {
checkCount++;
const currentNostr = window.nostr;
console.log('NOSTR_LOGIN_LITE: === DEFERRED CHECK ' + checkCount + '/' + maxChecks + ' ===');
console.log('NOSTR_LOGIN_LITE: Current window.nostr:', currentNostr);
console.log('NOSTR_LOGIN_LITE: Constructor:', currentNostr?.constructor?.name);
// Skip if it's our facade
if (currentNostr?.constructor?.name === 'WindowNostr') {
console.log('NOSTR_LOGIN_LITE: Skipping - this is our facade');
return;
}
if (this._isRealExtension(currentNostr)) {
console.log('NOSTR_LOGIN_LITE: ✓✓✓ LATE EXTENSION DETECTED - PRESERVING WITHOUT FACADE ✓✓✓');
console.log('NOSTR_LOGIN_LITE: Extension detected after ' + (checkCount * 200) + 'ms!');
console.log('NOSTR_LOGIN_LITE: Extension constructor:', currentNostr.constructor?.name);
console.log('NOSTR_LOGIN_LITE: Extension keys:', Object.keys(currentNostr));
console.log('NOSTR_LOGIN_LITE: Leaving window.nostr untouched for extension compatibility');
this.preservedExtension = currentNostr;
this.facadeInstalled = false;
clearInterval(checkInterval);
// DON'T install facade - leave window.nostr as the extension
return;
}
// Stop checking after max attempts - no extension found
if (checkCount >= maxChecks) {
console.log('NOSTR_LOGIN_LITE: ⚠️ MAX CHECKS REACHED - NO EXTENSION FOUND');
clearInterval(checkInterval);
console.log('NOSTR_LOGIN_LITE: Installing facade for local/NIP-46/readonly methods');
this._installFacade();
}
}, 200); // Check every 200ms
console.log('NOSTR_LOGIN_LITE: Waiting for deferred detection to complete...');
}
}
_installFacade(existingNostr = null) {
if (typeof window !== 'undefined' && !this.facadeInstalled) {
console.log('NOSTR_LOGIN_LITE: === _installFacade CALLED ===');
console.log('NOSTR_LOGIN_LITE: existingNostr parameter:', existingNostr);
console.log('NOSTR_LOGIN_LITE: existingNostr constructor:', existingNostr?.constructor?.name);
console.log('NOSTR_LOGIN_LITE: window.nostr before installation:', window.nostr);
console.log('NOSTR_LOGIN_LITE: window.nostr constructor before:', window.nostr?.constructor?.name);
const facade = new WindowNostr(this, existingNostr);
window.nostr = facade;
this.facadeInstalled = true;
console.log('NOSTR_LOGIN_LITE: === FACADE INSTALLED WITH EXTENSION ===');
console.log('NOSTR_LOGIN_LITE: window.nostr after installation:', window.nostr);
console.log('NOSTR_LOGIN_LITE: window.nostr constructor after:', window.nostr.constructor?.name);
console.log('NOSTR_LOGIN_LITE: facade.existingNostr:', window.nostr.existingNostr);
}
}
// Helper method to identify real browser extensions
_isRealExtension(obj) {
console.log('NOSTR_LOGIN_LITE: === _isRealExtension DEBUG ===');
console.log('NOSTR_LOGIN_LITE: obj:', obj);
console.log('NOSTR_LOGIN_LITE: typeof obj:', typeof obj);
if (!obj || typeof obj !== 'object') {
console.log('NOSTR_LOGIN_LITE: ✗ Not an object');
return false;
}
console.log('NOSTR_LOGIN_LITE: Object keys:', Object.keys(obj));
console.log('NOSTR_LOGIN_LITE: getPublicKey type:', typeof obj.getPublicKey);
console.log('NOSTR_LOGIN_LITE: signEvent type:', typeof obj.signEvent);
// Must have required Nostr methods
if (typeof obj.getPublicKey !== 'function' || typeof obj.signEvent !== 'function') {
console.log('NOSTR_LOGIN_LITE: ✗ Missing required methods');
return false;
}
// Exclude our own library classes
const constructorName = obj.constructor?.name;
console.log('NOSTR_LOGIN_LITE: Constructor name:', constructorName);
if (constructorName === 'WindowNostr' || constructorName === 'NostrLite') {
console.log('NOSTR_LOGIN_LITE: ✗ Is our library class');
return false;
}
// Exclude NostrTools library object
if (obj === window.NostrTools) {
console.log('NOSTR_LOGIN_LITE: ✗ Is NostrTools object');
return false;
}
// Real extensions typically have internal properties or specific characteristics
console.log('NOSTR_LOGIN_LITE: Extension property check:');
console.log(' _isEnabled:', !!obj._isEnabled);
console.log(' enabled:', !!obj.enabled);
console.log(' kind:', !!obj.kind);
console.log(' _eventEmitter:', !!obj._eventEmitter);
console.log(' _scope:', !!obj._scope);
console.log(' _requests:', !!obj._requests);
console.log(' _pubkey:', !!obj._pubkey);
console.log(' name:', !!obj.name);
console.log(' version:', !!obj.version);
console.log(' description:', !!obj.description);
const hasExtensionProps = !!(
obj._isEnabled || obj.enabled || obj.kind ||
obj._eventEmitter || obj._scope || obj._requests || obj._pubkey ||
obj.name || obj.version || obj.description
);
console.log('NOSTR_LOGIN_LITE: Extension detection result for', constructorName, ':', hasExtensionProps);
return hasExtensionProps;
}
launch(startScreen = 'login') {
console.log('NOSTR_LOGIN_LITE: Launching with screen:', startScreen);