support relays in nip05.

This commit is contained in:
fiatjaf 2023-02-05 16:25:00 -03:00
parent 024f89ac58
commit a3e3b25dd9
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 28 additions and 18 deletions

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"github.com/nbd-wtf/go-nostr"
) )
type ( type (
@ -17,7 +19,7 @@ type WellKnownResponse struct {
Relays key2RelaysMap `json:"relays"` // NIP-35 Relays key2RelaysMap `json:"relays"` // NIP-35
} }
func QueryIdentifier(fullname string) string { func QueryIdentifier(fullname string) *nostr.ProfilePointer {
spl := strings.Split(fullname, "@") spl := strings.Split(fullname, "@")
var name, domain string var name, domain string
@ -29,25 +31,30 @@ func QueryIdentifier(fullname string) string {
name = spl[0] name = spl[0]
domain = spl[1] domain = spl[1]
default: default:
return "" return nil
} }
if strings.Index(domain, ".") == -1 { if strings.Index(domain, ".") == -1 {
return "" return nil
} }
res, err := http.Get(fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name)) res, err := http.Get(fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name))
if err != nil { if err != nil {
return "" return nil
} }
var result WellKnownResponse var result WellKnownResponse
if err := json.NewDecoder(res.Body).Decode(&result); err != nil { if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return "" return nil
} }
pubkey, _ := result.Names[name] pubkey, _ := result.Names[name]
return pubkey relays, _ := result.Relays[pubkey]
return &nostr.ProfilePointer{
PublicKey: pubkey,
Relays: relays,
}
} }
func NormalizeIdentifier(fullname string) string { func NormalizeIdentifier(fullname string) string {

View File

@ -4,18 +4,10 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"fmt" "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) { func Decode(bech32string string) (prefix string, value any, err error) {
prefix, bits5, err := decode(bech32string) prefix, bits5, err := decode(bech32string)
if err != nil { 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 return prefix, hex.EncodeToString(data[0:32]), nil
case "nprofile": case "nprofile":
var result ProfilePointer var result nostr.ProfilePointer
curr := 0 curr := 0
for { for {
t, v := readTLVEntry(data[curr:]) t, v := readTLVEntry(data[curr:])
@ -60,7 +52,7 @@ func Decode(bech32string string) (prefix string, value any, err error) {
curr = curr + 2 + len(v) curr = curr + 2 + len(v)
} }
case "nevent": case "nevent":
var result EventPointer var result nostr.EventPointer
curr := 0 curr := 0
for { for {
t, v := readTLVEntry(data[curr:]) t, v := readTLVEntry(data[curr:])

11
pointers.go Normal file
View File

@ -0,0 +1,11 @@
package nostr
type ProfilePointer struct {
PublicKey string
Relays []string
}
type EventPointer struct {
ID string
Relays []string
}