From 9775016bf19c871d515bec293c163f9f27adef77 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 15 Jan 2023 22:00:38 -0300 Subject: [PATCH] implement nip-42 AUTH event validation. --- nip42/nip42.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 nip42/nip42.go diff --git a/nip42/nip42.go b/nip42/nip42.go new file mode 100644 index 0000000..a161e30 --- /dev/null +++ b/nip42/nip42.go @@ -0,0 +1,40 @@ +package nip42 + +import ( + "net/url" + "time" + + "github.com/nbd-wtf/go-nostr" +) + +func ValidateAuthEvent(event *nostr.Event, challenge string, relayURL string) (pubkey string, ok bool) { + if ok, _ := event.CheckSignature(); ok == false { + return "", false + } + if event.Kind != 22242 { + return "", false + } + + now := time.Now() + if event.CreatedAt.After(now.Add(10*time.Minute)) || event.CreatedAt.Before(now.Add(-10*time.Minute)) { + return "", false + } + + if event.Tags.GetFirst([]string{"challenge", challenge}) == nil { + return "", false + } + + expected, err1 := url.Parse(relayURL) + found, err2 := url.Parse(event.Tags.GetFirst([]string{"relay", ""}).Value()) + if err1 != nil || err2 != nil { + return "", false + } else { + if expected.Scheme != found.Scheme || + expected.Host != found.Host || + expected.Path != found.Path { + return "", false + } + } + + return event.PubKey, true +}