update auth example on readme to v0.19

This commit is contained in:
fiatjaf
2023-06-25 18:01:25 -03:00
parent 71f4594033
commit 1f38213b3f

View File

@@ -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)
}
```