go-nostr/nip05/nip05.go

106 lines
2.6 KiB
Go
Raw Permalink Normal View History

2022-05-04 11:15:26 -03:00
package nip05
import (
"context"
2022-05-04 11:15:26 -03:00
"fmt"
"net/http"
2024-03-01 15:54:28 -03:00
"regexp"
2022-05-04 11:15:26 -03:00
"strings"
2023-02-05 16:25:00 -03:00
jsoniter "github.com/json-iterator/go"
2023-02-05 16:25:00 -03:00
"github.com/nbd-wtf/go-nostr"
2022-05-04 11:15:26 -03:00
)
2024-03-01 15:54:28 -03:00
var NIP05_REGEX = regexp.MustCompile(`^(?:([\w.+-]+)@)?([\w_-]+(\.[\w_-]+)+)$`)
2022-11-26 19:31:49 -03:00
type WellKnownResponse struct {
2024-01-06 08:07:28 -03:00
Names map[string]string `json:"names"`
2024-02-28 09:41:11 -03:00
Relays map[string][]string `json:"relays,omitempty"`
NIP46 map[string][]string `json:"nip46,omitempty"`
2022-11-26 19:31:49 -03:00
}
2024-03-02 07:48:24 -03:00
func IsValidIdentifier(input string) bool {
return NIP05_REGEX.MatchString(input)
}
2024-03-01 15:54:28 -03:00
func ParseIdentifier(fullname string) (name string, domain string, err error) {
res := NIP05_REGEX.FindStringSubmatch(fullname)
if len(res) == 0 {
return "", "", fmt.Errorf("invalid identifier")
}
if res[1] == "" {
res[1] = "_"
}
return res[1], res[2], nil
}
func QueryIdentifier(ctx context.Context, fullname string) (*nostr.ProfilePointer, error) {
result, name, err := Fetch(ctx, fullname)
if err != nil {
return nil, err
}
pubkey, ok := result.Names[name]
if !ok {
return nil, fmt.Errorf("no entry for name '%s'", name)
}
if !nostr.IsValidPublicKey(pubkey) {
return nil, fmt.Errorf("got an invalid public key '%s'", pubkey)
}
relays, _ := result.Relays[pubkey]
return &nostr.ProfilePointer{
PublicKey: pubkey,
Relays: relays,
}, nil
}
2024-10-29 10:49:14 -03:00
var httpClient = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
func Fetch(ctx context.Context, fullname string) (resp WellKnownResponse, name string, err error) {
2024-03-01 15:54:28 -03:00
name, domain, err := ParseIdentifier(fullname)
if err != nil {
return resp, name, fmt.Errorf("failed to parse '%s': %w", fullname, err)
2022-05-04 11:15:26 -03:00
}
req, err := http.NewRequestWithContext(ctx, "GET",
fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", domain, name), nil)
if err != nil {
return resp, name, fmt.Errorf("failed to create a request: %w", err)
}
2024-10-29 10:49:14 -03:00
res, err := httpClient.Do(req)
2022-05-04 11:15:26 -03:00
if err != nil {
return resp, name, fmt.Errorf("request failed: %w", err)
2022-05-04 11:15:26 -03:00
}
defer res.Body.Close()
2022-05-04 11:15:26 -03:00
2022-11-26 19:31:49 -03:00
var result WellKnownResponse
if err := jsoniter.NewDecoder(res.Body).Decode(&result); err != nil {
return resp, name, fmt.Errorf("failed to decode json response: %w", err)
}
2024-03-11 13:44:58 -03:00
return result, name, nil
2022-05-04 11:15:26 -03:00
}
func NormalizeIdentifier(fullname string) string {
if strings.HasPrefix(fullname, "_@") {
return fullname[2:]
}
return fullname
}
2024-11-27 01:12:57 -03:00
func IdentifierToURL(address string) string {
spl := strings.Split(address, "@")
if len(spl) == 1 {
return fmt.Sprintf("https://%s/.well-known/nostr.json?name=_", spl[0])
}
return fmt.Sprintf("https://%s/.well-known/nostr.json?name=%s", spl[1], spl[0])
}