Skip to content

Commit

Permalink
Merge pull request #19 from fishcakeday/handle-multidomain-nip05
Browse files Browse the repository at this point in the history
fix(nip05): make nip05 work with multi-domain config
  • Loading branch information
believethehype authored May 8, 2023
2 parents faabe69 + c031ea1 commit 24a22c8
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,39 +119,53 @@ func sendMessage(receiverKey string, message string) {
}

func handleNip05(w http.ResponseWriter, r *http.Request) {
var err error
var response string

var allusers []Params
allusers, err = GetAllUsers(s.Domain)
firstpartstring := "{\n \"names\": {\n"
finalpartstring := " \t}\n}"
var middlestring = ""

for _, user := range allusers {
nostrnpubHex := DecodeBench32(user.Npub)
if user.Npub != "" { //do some more validation checks
middlestring = middlestring + "\t\"" + user.Name + "\"" + ": " + "\"" + nostrnpubHex + "\"" + ",\n"
}
username := r.URL.Query().Get("name")
if username == "" {
http.Error(w, `{"error": "missing 'name' parameter"}`, http.StatusBadRequest)
return
}

if s.Nip05 {
//Remove ',' from last entry
if len(middlestring) > 2 {
middlestringtrim := middlestring[:len(middlestring)-2]
middlestringtrim += "\n"
domains := getDomains(s.Domain)
domain := ""

response = firstpartstring + middlestringtrim + finalpartstring
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, response)
if len(domains) == 1 {
domain = domains[0]
} else {
return
hostname := r.URL.Host
if hostname == "" {
hostname = r.Host
}

for _, one := range domains {
if strings.Contains(hostname, one) {
domain = one
break
}
}
if domain == "" {
http.Error(w, `{"error": "incorrect domain"}`, http.StatusBadRequest)
return
}
}

params, err := GetName(username, domain)
if err != nil {
log.Error().Err(err).Str("name", username).Str("domain", domain).Msg("failed to get name")
http.Error(w, fmt.Sprintf(`{"error": "failed to get name %s@%s"}`, username, domain), http.StatusNotFound)
return
}

nostrnpubHex := DecodeBench32(params.Npub)
response := map[string]interface{}{
"names": map[string]interface{}{
username: nostrnpubHex,
},
}

if s.Nip05 {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(response)
}
}

func GetNostrProfileMetaData(npub string, index int) (nostr.ProfileMetadata, error) {
Expand Down

0 comments on commit 24a22c8

Please sign in to comment.