From f014dffad9e148a59e0f654e75f0097628c569b9 Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 4 Feb 2023 18:52:50 -0500 Subject: [PATCH] more cleanup --- lnd/lnd.go | 1 - main.go | 2 +- nostr/nostr.go | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lnd/lnd.go b/lnd/lnd.go index 04979be..53ca8df 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -3,7 +3,6 @@ package lnd import "github.com/davecgh/go-spew/spew" func Request(request []byte) (string, int, error) { - // https://bitcoin.stackexchange.com/questions/73869/rpc-call-to-lnd spew.Dump(request) return "x", 1, nil } diff --git a/main.go b/main.go index 3dfcd9d..bc13f02 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,8 @@ func main() { Key: "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6", Hostname: "devvul.com", } + nostr.AddNostrAddr(nostrTest) */ - nostr.AddNostrAddr(nostrTest) // matrix endpoints http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer) http.HandleFunc("/.well-known/matrix/client", matrix.MatrixClient) diff --git a/nostr/nostr.go b/nostr/nostr.go index adad339..81d2e95 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -31,14 +31,28 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "nostr/form.html") case "POST": r.ParseForm() - relays := make(map[string][]string) - npub := r.FormValue("Key") - // convert npub to hex - hexKey, err := convertNpubToHex(npub) + rKey := getRkey(r.FormValue("Name"), getHostname(r.Host)) + // check if the user already exists + redis, err := redis.New("localhost:6379", "", redis.NostrDb) + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } + exists := redis.Client.Exists(rKey) + if exists.Val() == 1 { + w.WriteHeader(http.StatusConflict) + return + } + + // get the hexkey + hexKey, err := convertNpubToHex(r.FormValue("Key")) if err != nil { w.WriteHeader(http.StatusBadRequest) return } + + // create the struct + relays := make(map[string][]string) names := map[string]string{r.FormValue("Name"): hexKey} user := nostrWellKnown{} if r.FormValue("Relays") != "" { @@ -50,6 +64,8 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { user = nostrWellKnown{Names: names} } jsonUser, _ := json.Marshal(user) + + // generate the payment request paymentReq, exp, err := lnd.Request(jsonUser) if err != nil { w.WriteHeader(http.StatusServiceUnavailable) @@ -74,7 +90,7 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) { requestedName := r.FormValue("name") // search for user@domain - search := fmt.Sprintf("%s@%s", requestedName, getHostname(r.Host)) + search := getRkey(requestedName, getHostname(r.Host)) addr, err := redis.Client.Get(search).Result() if err != nil { w.WriteHeader(http.StatusNotFound) @@ -108,7 +124,7 @@ func AddNostrAddr(n NostrRequest) { if err != nil { fmt.Println("FAILED") } - nameKey := fmt.Sprintf("%s@%s", n.Name, n.Hostname) + nameKey := getRkey(n.Name, n.Hostname) err = r.Client.Set(nameKey, jsonUser, redis.NostrDb).Err() if err != nil { fmt.Println("FAILED") @@ -117,9 +133,6 @@ func AddNostrAddr(n NostrRequest) { func getHostname(hostname string) string { // remove port for local testing - if strings.Contains(hostname, ":") { - hostname = strings.Split(hostname, ":")[0] - } return hostname } @@ -135,3 +148,10 @@ func convertNpubToHex(npub string) (string, error) { return hex.(string), nil } + +func getRkey(user string, hostname string) string { + if strings.Contains(hostname, ":") { + hostname = strings.Split(hostname, ":")[0] + } + return fmt.Sprintf("%s@%s", user, hostname) +}