a big bundle of conversions and other changes.

This commit is contained in:
fiatjaf
2025-04-15 17:13:57 -03:00
parent f493293be2
commit 2b5b646a62
92 changed files with 852 additions and 2136 deletions

View File

@@ -10,7 +10,7 @@ import (
// CreateUnsignedAuthEvent creates an event which should be sent via an "AUTH" command.
// If the authentication succeeds, the user will be authenticated as pubkey.
func CreateUnsignedAuthEvent(challenge, pubkey, relayURL string) nostr.Event {
func CreateUnsignedAuthEvent(challenge string, pubkey nostr.PubKey, relayURL string) nostr.Event {
return nostr.Event{
PubKey: pubkey,
CreatedAt: nostr.Now(),
@@ -34,40 +34,40 @@ func parseURL(input string) (*url.URL, error) {
// ValidateAuthEvent checks whether event is a valid NIP-42 event for given challenge and relayURL.
// The result of the validation is encoded in the ok bool.
func ValidateAuthEvent(event *nostr.Event, challenge string, relayURL string) (pubkey string, ok bool) {
func ValidateAuthEvent(event nostr.Event, challenge string, relayURL string) (pubkey nostr.PubKey, ok bool) {
if event.Kind != nostr.KindClientAuthentication {
return "", false
return nostr.ZeroPK, false
}
if event.Tags.FindWithValue("challenge", challenge) == nil {
return "", false
return nostr.ZeroPK, false
}
expected, err := parseURL(relayURL)
if err != nil {
return "", false
return nostr.ZeroPK, false
}
found, err := parseURL(event.Tags.GetFirst([]string{"relay", ""}).Value())
found, err := parseURL(event.Tags.Find("relay")[1])
if err != nil {
return "", false
return nostr.ZeroPK, false
}
if expected.Scheme != found.Scheme ||
expected.Host != found.Host ||
expected.Path != found.Path {
return "", false
return nostr.ZeroPK, false
}
now := time.Now()
if event.CreatedAt.Time().After(now.Add(10*time.Minute)) || event.CreatedAt.Time().Before(now.Add(-10*time.Minute)) {
return "", false
return nostr.ZeroPK, false
}
// save for last, as it is most expensive operation
// no need to check returned error, since ok == true implies err == nil.
if ok, _ := event.CheckSignature(); !ok {
return "", false
if !event.VerifySignature() {
return nostr.ZeroPK, false
}
return event.PubKey, true