feat(nostr): Add Nostr relay connection and status management

- Integrate nostr-tools for Nostr relay connectivity
- Create NostrClient for managing relay connections
- Implement useNostr composable for reactive connection handling
- Add ConnectionStatus component to display relay connection state
- Configure environment variable for Nostr relay endpoints
- Update App.vue to manage Nostr connection lifecycle
This commit is contained in:
padreug 2025-03-09 14:27:51 +01:00
parent 6a5b64a382
commit 2a83972b47
7 changed files with 244 additions and 2 deletions

44
src/lib/nostr/client.ts Normal file
View file

@ -0,0 +1,44 @@
import { SimplePool, getPublicKey, nip19 } from 'nostr-tools'
export interface NostrClientConfig {
relays: string[]
}
export class NostrClient {
private pool: SimplePool
private relays: string[]
private _isConnected: boolean = false
constructor(config: NostrClientConfig) {
this.pool = new SimplePool()
this.relays = config.relays
}
get isConnected(): boolean {
return this._isConnected
}
async connect(): Promise<void> {
try {
// Try to connect to at least one relay
const connections = await Promise.allSettled(
this.relays.map(relay => this.pool.ensureRelay(relay))
)
// Check if at least one connection was successful
this._isConnected = connections.some(result => result.status === 'fulfilled')
if (!this._isConnected) {
throw new Error('Failed to connect to any relay')
}
} catch (error) {
this._isConnected = false
throw error
}
}
disconnect(): void {
this.pool.close(this.relays)
this._isConnected = false
}
}