Created
May 22, 2025 09:00
-
-
Save sondreb/d51d173598f9947de8043962185b6908 to your computer and use it in GitHub Desktop.
Authentication against Nostr Relay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { kinds, SimplePool } from 'nostr-tools'; | |
import { makeAuthEvent } from 'nostr-tools/nip42'; | |
import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure' | |
let sk = generateSecretKey(); | |
let pk = getPublicKey(sk); | |
const relayUrls = ['wss://inbox.relays.land/', 'wss://lockbox.fiatjaf.com/']; | |
let userPool = new SimplePool(); | |
let pubkey = '3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'; | |
userPool.subscribeEose(relayUrls, { | |
kinds: [kinds.Metadata], | |
authors: [pubkey], | |
}, { | |
onevent: (evt) => { | |
console.log('Received event on the account subscription:', evt); | |
}, | |
onclose: (reasons) => { | |
console.log('Closed user pool', { reasons }); | |
}, | |
doauth: async (event) => { | |
console.log('Received doauth event', { event }); | |
const challenge = event.tags.find(t => t[0] === 'challenge')?.[1] || ''; | |
const relay = event.tags.find(t => t[0] === 'relay')?.[1]; | |
const unsignedEvent = makeAuthEvent(relay, challenge); | |
const signedEvent = finalizeEvent(unsignedEvent, sk); | |
console.log('Signed event:', { signedEvent }); | |
return signedEvent; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment