From 1f38213b3fe45fae9df0d7e70339aa4828b22d70 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 25 Jun 2023 18:01:25 -0300 Subject: [PATCH] update auth example on readme to v0.19 --- README.md | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 837d563..046056f 100644 --- a/README.md +++ b/README.md @@ -122,34 +122,16 @@ E.g., https://github.com/fiatjaf/relayer with a relay implementing the relayer.A func main() { url := "ws://localhost:7447" - // Once the connection is initiated the server will send "AUTH" with the challenge string. - relay, err := nostr.RelayConnect(context.Background(), url) + sk := nostr.GeneratePrivateKey() + + relay, err := nostr.RelayConnect(context.Background(), url, + nostr.WithAuthHandler(func(ctx context.Context, authEvent *Event) (ok bool) { + authEvent.Sign(sk) + }), + ) if err != nil { panic(err) } - - // Initialize test user. - sk := nostr.GeneratePrivateKey() - pub, _ := nostr.GetPublicKey(sk) - npub, _ := nip19.EncodePublicKey(pub) - - // Relay.Challenges channel will receive the "AUTH" command. - challenge := <-relay.Challenges - - // Create the auth event to send back. - // The user will be authenticated as pub. - event := nip42.CreateUnsignedAuthEvent(challenge, pub, url) - event.Sign(sk) - - // Set-up context with 3 second time out. - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) - defer cancel() - - // Send the event by calling relay.Auth. - // Returned status is either success, fail, or sent (if no reply given in the 3 second timeout). - auth_status, err := relay.Auth(ctx, event) - - fmt.Printf("authenticated as %s: %s\n", npub, auth_status) } ```