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

2
.env
View file

@ -1,3 +1,5 @@
# Support agent's public key in npub format
VITE_SUPPORT_NPUB=npub1tm42jkmdn54zncjcylp34e85jagmgndr0skw4v0rsg8rucmu7r5swayth3
VITE_NOSTR_RELAYS=["wss://relay.damus.io","wss://relay.nostr.info"]

129
package-lock.json generated
View file

@ -15,6 +15,7 @@
"clsx": "^2.1.1",
"fuse.js": "^7.0.0",
"lucide-vue-next": "^0.474.0",
"nostr-tools": "^2.10.4",
"pinia": "^2.3.1",
"radix-vue": "^1.9.13",
"tailwind-merge": "^2.6.0",
@ -2557,6 +2558,51 @@
"@jridgewell/sourcemap-codec": "^1.4.14"
}
},
"node_modules/@noble/ciphers": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-0.5.3.tgz",
"integrity": "sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==",
"license": "MIT",
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@noble/curves": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz",
"integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==",
"license": "MIT",
"dependencies": {
"@noble/hashes": "1.3.2"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@noble/curves/node_modules/@noble/hashes": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz",
"integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==",
"license": "MIT",
"engines": {
"node": ">= 16"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@noble/hashes": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.1.tgz",
"integrity": "sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==",
"license": "MIT",
"engines": {
"node": ">= 16"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@polka/url": {
"version": "1.0.0-next.28",
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.28.tgz",
@ -2901,6 +2947,57 @@
"win32"
]
},
"node_modules/@scure/base": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.1.tgz",
"integrity": "sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==",
"funding": [
{
"type": "individual",
"url": "https://paulmillr.com/funding/"
}
],
"license": "MIT"
},
"node_modules/@scure/bip32": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.1.tgz",
"integrity": "sha512-osvveYtyzdEVbt3OfwwXFr4P2iVBL5u1Q3q4ONBfDY/UpOuXmOlbgwc1xECEboY8wIays8Yt6onaWMUdUbfl0A==",
"license": "MIT",
"dependencies": {
"@noble/curves": "~1.1.0",
"@noble/hashes": "~1.3.1",
"@scure/base": "~1.1.0"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@scure/bip32/node_modules/@noble/curves": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.1.0.tgz",
"integrity": "sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==",
"license": "MIT",
"dependencies": {
"@noble/hashes": "1.3.1"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@scure/bip39": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@scure/bip39/-/bip39-1.2.1.tgz",
"integrity": "sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==",
"license": "MIT",
"dependencies": {
"@noble/hashes": "~1.3.0",
"@scure/base": "~1.1.0"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@surma/rollup-plugin-off-main-thread": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/@surma/rollup-plugin-off-main-thread/-/rollup-plugin-off-main-thread-2.2.3.tgz",
@ -6152,6 +6249,38 @@
"dev": true,
"license": "MIT"
},
"node_modules/nostr-tools": {
"version": "2.10.4",
"resolved": "https://registry.npmjs.org/nostr-tools/-/nostr-tools-2.10.4.tgz",
"integrity": "sha512-biU7sk+jxHgVASfobg2T5ttxOGGSt69wEVBC51sHHOEaKAAdzHBLV/I2l9Rf61UzClhliZwNouYhqIso4a3HYg==",
"license": "Unlicense",
"dependencies": {
"@noble/ciphers": "^0.5.1",
"@noble/curves": "1.2.0",
"@noble/hashes": "1.3.1",
"@scure/base": "1.1.1",
"@scure/bip32": "1.3.1",
"@scure/bip39": "1.2.1"
},
"optionalDependencies": {
"nostr-wasm": "0.1.0"
},
"peerDependencies": {
"typescript": ">=5.0.0"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
}
},
"node_modules/nostr-wasm": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/nostr-wasm/-/nostr-wasm-0.1.0.tgz",
"integrity": "sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==",
"license": "MIT",
"optional": true
},
"node_modules/object-inspect": {
"version": "1.13.4",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",

View file

@ -10,13 +10,14 @@
"analyze": "vite build --mode analyze"
},
"dependencies": {
"@vueuse/core": "^12.5.0",
"@vueuse/components": "^12.5.0",
"@vueuse/core": "^12.5.0",
"@vueuse/head": "^2.0.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"fuse.js": "^7.0.0",
"lucide-vue-next": "^0.474.0",
"nostr-tools": "^2.10.4",
"pinia": "^2.3.1",
"radix-vue": "^1.9.13",
"tailwind-merge": "^2.6.0",

View file

@ -1,7 +1,20 @@
<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue'
import Navbar from '@/components/layout/Navbar.vue'
import Footer from '@/components/layout/Footer.vue'
import ConnectionStatus from '@/components/nostr/ConnectionStatus.vue'
import { useNostr } from '@/composables/useNostr'
const relays = JSON.parse(import.meta.env.VITE_NOSTR_RELAYS as string)
const { isConnected, error, connect, disconnect } = useNostr({ relays })
onMounted(async () => {
await connect()
})
onUnmounted(() => {
disconnect()
})
</script>
<template>
@ -10,8 +23,9 @@ import Footer from '@/components/layout/Footer.vue'
style="padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom)">
<header
class="sticky top-0 z-50 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
<nav class="container flex h-14 items-center">
<nav class="container flex h-14 items-center justify-between">
<Navbar />
<ConnectionStatus :is-connected="isConnected" :error="error" />
</nav>
</header>

View file

@ -0,0 +1,20 @@
<!-- A component that shows NOSTR connection status -->
<script setup lang="ts">
import { Badge } from '@/components/ui/badge'
defineProps<{
isConnected: boolean
error?: Error | null
}>()
</script>
<template>
<div class="flex items-center gap-2">
<Badge :variant="isConnected ? 'default' : 'destructive'" class="h-5">
NOSTR: {{ isConnected ? 'Connected' : 'Disconnected' }}
</Badge>
<p v-if="error" class="text-sm text-destructive">
{{ error.message }}
</p>
</div>
</template>

View file

@ -0,0 +1,32 @@
import { ref } from 'vue'
import { NostrClient, type NostrClientConfig } from '@/lib/nostr/client'
export function useNostr(config: NostrClientConfig) {
const client = new NostrClient(config)
const isConnected = ref(false)
const error = ref<Error | null>(null)
async function connect() {
try {
error.value = null
await client.connect()
isConnected.value = client.isConnected
} catch (err) {
error.value = err instanceof Error ? err : new Error('Failed to connect')
isConnected.value = false
}
}
function disconnect() {
client.disconnect()
isConnected.value = false
error.value = null
}
return {
isConnected,
error,
connect,
disconnect
}
}

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
}
}