From a3e3b25dd99532b139a2ec7ba0aedcda48af30a8 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 5 Feb 2023 16:25:00 -0300 Subject: [PATCH] support relays in nip05. --- nip05/nip05.go | 19 +++++++++++++------ nip19/nip19.go | 16 ++++------------ pointers.go | 11 +++++++++++ 3 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 pointers.go diff --git a/nip05/nip05.go b/nip05/nip05.go index fb29a32..52eadfd 100644 --- a/nip05/nip05.go +++ b/nip05/nip05.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "strings" + + "github.com/nbd-wtf/go-nostr" ) type ( @@ -17,7 +19,7 @@ type WellKnownResponse struct { Relays key2RelaysMap `json:"relays"` // NIP-35 } -func QueryIdentifier(fullname string) string { +func QueryIdentifier(fullname string) *nostr.ProfilePointer { spl := strings.Split(fullname, "@") var name, domain string @@ -29,25 +31,30 @@ func QueryIdentifier(fullname string) string { name = spl[0] domain = spl[1] default: - return "" + return nil } if strings.Index(domain, ".") == -1 { - return "" + return nil } res, err := http.Get(fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name)) if err != nil { - return "" + return nil } var result WellKnownResponse if err := json.NewDecoder(res.Body).Decode(&result); err != nil { - return "" + return nil } pubkey, _ := result.Names[name] - return pubkey + relays, _ := result.Relays[pubkey] + + return &nostr.ProfilePointer{ + PublicKey: pubkey, + Relays: relays, + } } func NormalizeIdentifier(fullname string) string { diff --git a/nip19/nip19.go b/nip19/nip19.go index f45127e..afce90b 100644 --- a/nip19/nip19.go +++ b/nip19/nip19.go @@ -4,18 +4,10 @@ import ( "bytes" "encoding/hex" "fmt" + + "github.com/nbd-wtf/go-nostr" ) -type ProfilePointer struct { - PublicKey string - Relays []string -} - -type EventPointer struct { - ID string - Relays []string -} - func Decode(bech32string string) (prefix string, value any, err error) { prefix, bits5, err := decode(bech32string) if err != nil { @@ -35,7 +27,7 @@ func Decode(bech32string string) (prefix string, value any, err error) { return prefix, hex.EncodeToString(data[0:32]), nil case "nprofile": - var result ProfilePointer + var result nostr.ProfilePointer curr := 0 for { t, v := readTLVEntry(data[curr:]) @@ -60,7 +52,7 @@ func Decode(bech32string string) (prefix string, value any, err error) { curr = curr + 2 + len(v) } case "nevent": - var result EventPointer + var result nostr.EventPointer curr := 0 for { t, v := readTLVEntry(data[curr:]) diff --git a/pointers.go b/pointers.go new file mode 100644 index 0000000..4e62fd0 --- /dev/null +++ b/pointers.go @@ -0,0 +1,11 @@ +package nostr + +type ProfilePointer struct { + PublicKey string + Relays []string +} + +type EventPointer struct { + ID string + Relays []string +}