77 lines
2.0 KiB
Plaintext
77 lines
2.0 KiB
Plaintext
// === NOSTR INTEGRATION — SOVEREIGN COMMUNICATION ===
|
|
import { S } from './state.js';
|
|
|
|
export const NOSTR_RELAYS = [
|
|
'wss://relay.damus.io',
|
|
'wss://nos.lol',
|
|
'wss://relay.snort.social'
|
|
];
|
|
|
|
export const NOSTR_STATE = {
|
|
events: [],
|
|
connected: false,
|
|
lastEventTime: 0
|
|
};
|
|
|
|
export class NostrManager {
|
|
constructor() {
|
|
this.sockets = [];
|
|
}
|
|
|
|
connect() {
|
|
NOSTR_RELAYS.forEach(url => {
|
|
try {
|
|
const ws = new WebSocket(url);
|
|
ws.onopen = () => {
|
|
console.log(\`[nostr] Connected to \${url}\`);
|
|
NOSTR_STATE.connected = true;
|
|
this.subscribe(ws);
|
|
};
|
|
ws.onmessage = (e) => this.handleMessage(e.data);
|
|
ws.onerror = () => console.warn(\`[nostr] Connection error: \${url}\`);
|
|
this.sockets.push(ws);
|
|
} catch (err) {
|
|
console.error(\`[nostr] Failed to connect to \${url}\`, err);
|
|
}
|
|
});
|
|
}
|
|
|
|
subscribe(ws) {
|
|
const subId = 'nexus-sub-' + Math.random().toString(36).substring(7);
|
|
const filter = { kinds: [1, 7, 9735], limit: 20 }; // Notes, Reactions, Zaps
|
|
ws.send(JSON.stringify(['REQ', subId, filter]));
|
|
}
|
|
|
|
handleMessage(data) {
|
|
try {
|
|
const msg = JSON.parse(data);
|
|
if (msg[0] === 'EVENT') {
|
|
const event = msg[2];
|
|
this.processEvent(event);
|
|
}
|
|
} catch (err) { /* ignore parse errors */ }
|
|
}
|
|
|
|
processEvent(event) {
|
|
const simplified = {
|
|
id: event.id.substring(0, 8),
|
|
pubkey: event.pubkey.substring(0, 8),
|
|
content: event.content.length > 60 ? event.content.substring(0, 57) + '...' : event.content,
|
|
kind: event.kind,
|
|
created_at: event.created_at
|
|
};
|
|
|
|
NOSTR_STATE.events.unshift(simplified);
|
|
if (NOSTR_STATE.events.length > 50) NOSTR_STATE.events.pop();
|
|
NOSTR_STATE.lastEventTime = Date.now();
|
|
|
|
// Visual feedback via state pulse
|
|
if (event.kind === 9735) { // Zap!
|
|
S.energyBeamPulse = 1.0;
|
|
console.log('[nostr] ZAP RECEIVED!');
|
|
}
|
|
}
|
|
}
|
|
|
|
export const nostr = new NostrManager();
|