Added some NIP-42 functionality to the client (relay.go) (#38)

This commit is contained in:
barkyq
2023-01-16 06:27:11 -05:00
committed by GitHub
parent 9775016bf1
commit 87b6280299
5 changed files with 139 additions and 24 deletions

View File

@@ -87,6 +87,46 @@ for _, url := range []string{"wss://nostr.zebedee.cloud", "wss://nostr-pub.wello
}
```
### Authenticating with NIP-42
For this section, the user needs access to a relay implementing NIP-42.
E.g., https://github.com/fiatjaf/relayer with a relay implementing the relayer.Auther interface.
``` go
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)
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 := relay.Auth(ctx, event)
fmt.Printf("authenticated as %s: %s\n", npub, auth_status)
}
```
### Example script
```