mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-26 20:08:54 +02:00
update auth example on readme to v0.19
This commit is contained in:
32
README.md
32
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)
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user