go-nostr/nip05/nip05.go

71 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-05-04 11:15:26 -03:00
package nip05
import (
"encoding/json"
"fmt"
"net/http"
"strings"
2023-02-05 16:25:00 -03:00
"github.com/nbd-wtf/go-nostr"
2022-05-04 11:15:26 -03:00
)
2022-11-26 19:31:49 -03:00
type (
2023-02-05 16:22:41 -03:00
name2KeyMap map[string]string
key2RelaysMap map[string][]string
2022-11-26 19:31:49 -03:00
)
type WellKnownResponse struct {
2023-02-05 16:22:41 -03:00
Names name2KeyMap `json:"names"` // NIP-05
Relays key2RelaysMap `json:"relays"` // NIP-35
2022-11-26 19:31:49 -03:00
}
2023-02-05 16:25:00 -03:00
func QueryIdentifier(fullname string) *nostr.ProfilePointer {
2022-05-04 11:15:26 -03:00
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:
2023-02-05 16:25:00 -03:00
return nil
}
if strings.Index(domain, ".") == -1 {
2023-02-05 16:25:00 -03:00
return nil
2022-05-04 11:15:26 -03:00
}
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))
2022-05-04 11:15:26 -03:00
if err != nil {
2023-02-05 16:25:00 -03:00
return nil
2022-05-04 11:15:26 -03:00
}
2022-11-26 19:31:49 -03:00
var result WellKnownResponse
2022-05-04 11:15:26 -03:00
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
2023-02-05 16:25:00 -03:00
return nil
2022-05-04 11:15:26 -03:00
}
pubkey, _ := result.Names[name]
2023-02-05 16:25:00 -03:00
relays, _ := result.Relays[pubkey]
return &nostr.ProfilePointer{
PublicKey: pubkey,
Relays: relays,
}
2022-05-04 11:15:26 -03:00
}
func NormalizeIdentifier(fullname string) string {
if strings.HasPrefix(fullname, "_@") {
return fullname[2:]
}
return fullname
}