Files
nostrlib/nip05/nip05.go
2023-02-05 16:21:50 -03:00

60 lines
1003 B
Go

package nip05
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
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) string {
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 ""
}
if strings.Index(domain, ".") == -1 {
return ""
}
res, err := http.Get(fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name))
if err != nil {
return ""
}
var result WellKnownResponse
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return ""
}
pubkey, _ := result.Names[name]
return pubkey
}
func NormalizeIdentifier(fullname string) string {
if strings.HasPrefix(fullname, "_@") {
return fullname[2:]
}
return fullname
}