import { nip19 } from '@nostr/tools'; import type { NPub } from '@nostr/tools/nip19'; export function getCCNList(): Promise<{ name: string; pubkey: string }[]> { return new Promise((resolve, reject) => { const ws = new WebSocket('ws://localhost:6942'); ws.onopen = () => { ws.send(JSON.stringify(['CCN', 'LIST'])); }; ws.onmessage = (event) => { const data = JSON.parse(event.data); if (data[0] !== 'OK' || data[1] !== 'CCN LIST' || data[2] !== true) return; resolve(JSON.parse(data[3])); }; ws.onerror = (event) => { reject(event); }; }); } export function getActiveCCNNpub() { const selectedCCN = localStorage.getItem('selectedCCN'); if (!selectedCCN) return null; const { pubkey } = JSON.parse(selectedCCN); return nip19.npubEncode(pubkey); } export async function getCCNName(npub: NPub) { const ccnList = await getCCNList(); const npubToPubkey = nip19.decode<'npub'>(npub); const ccn = ccnList.find((ccn) => ccn.pubkey === npubToPubkey.data); return ccn?.name; }