mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-18 13:53:03 +01:00
71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
package nip05
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
)
|
|
|
|
type (
|
|
name2KeyMap map[string]string
|
|
key2RelaysMap map[string][]string
|
|
)
|
|
|
|
type WellKnownResponse struct {
|
|
Names name2KeyMap `json:"names"` // NIP-05
|
|
Relays key2RelaysMap `json:"relays"` // NIP-35
|
|
}
|
|
|
|
func QueryIdentifier(fullname string) *nostr.ProfilePointer {
|
|
spl := strings.Split(fullname, "@")
|
|
|
|
var name, domain string
|
|
switch len(spl) {
|
|
case 1:
|
|
name = "_"
|
|
domain = spl[0]
|
|
case 2:
|
|
name = spl[0]
|
|
domain = spl[1]
|
|
default:
|
|
return nil
|
|
}
|
|
|
|
if strings.Index(domain, ".") == -1 {
|
|
return nil
|
|
}
|
|
client := &http.Client{
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
}
|
|
res, err := client.Get(fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name))
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var result WellKnownResponse
|
|
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
|
|
return nil
|
|
}
|
|
|
|
pubkey, _ := result.Names[name]
|
|
relays, _ := result.Relays[pubkey]
|
|
|
|
return &nostr.ProfilePointer{
|
|
PublicKey: pubkey,
|
|
Relays: relays,
|
|
}
|
|
}
|
|
|
|
func NormalizeIdentifier(fullname string) string {
|
|
if strings.HasPrefix(fullname, "_@") {
|
|
return fullname[2:]
|
|
}
|
|
|
|
return fullname
|
|
}
|