From f9106373c5d47416b88bf0bb6a324066929e2587 Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 4 Feb 2023 17:40:45 -0500 Subject: [PATCH] Start extending nostr to add lnd support --- go.mod | 1 + go.sum | 2 + lnd/lnd.go | 7 ++-- nostr/form.html | 19 +++++++++ nostr/nostr.go | 106 +++++++++++++++++++++++++++++++++++------------- 5 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 nostr/form.html diff --git a/go.mod b/go.mod index 877e3e8..778515b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/davecgh/go-spew v1.1.1 github.com/go-redis/redis v6.15.9+incompatible + github.com/nbd-wtf/go-nostr v0.12.0 ) require ( diff --git a/go.sum b/go.sum index dacac62..be3eb34 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/nbd-wtf/go-nostr v0.12.0 h1:6uo6D6jhcNzrzm6Fi8nA3jfZQqoXbeTWi9dIX5MsgZc= +github.com/nbd-wtf/go-nostr v0.12.0/go.mod h1:qFFTIxh15H5GGN0WsBI/P73DteqsevnhSEW/yk8nEf4= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= diff --git a/lnd/lnd.go b/lnd/lnd.go index 11893d7..04979be 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -1,8 +1,9 @@ package lnd -import "fmt" +import "github.com/davecgh/go-spew/spew" -func main() { +func Request(request []byte) (string, int, error) { // https://bitcoin.stackexchange.com/questions/73869/rpc-call-to-lnd - fmt.Println("vim-go") + spew.Dump(request) + return "x", 1, nil } diff --git a/nostr/form.html b/nostr/form.html new file mode 100644 index 0000000..adff759 --- /dev/null +++ b/nostr/form.html @@ -0,0 +1,19 @@ + + + + + + +
+
+
+
+
+
+
+
+ +
+
+ + diff --git a/nostr/nostr.go b/nostr/nostr.go index a93016b..d1af79a 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -2,11 +2,15 @@ package nostr import ( "encoding/json" + "errors" "fmt" "net/http" "strings" + "git.minhas.io/asara/well-goknown/lnd" "git.minhas.io/asara/well-goknown/redis" + "github.com/davecgh/go-spew/spew" + "github.com/nbd-wtf/go-nostr/nip19" ) type nostrWellKnown struct { @@ -21,6 +25,67 @@ type NostrRequest struct { Relays []string `json:"relays,omitempty"` } +func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + 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) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + names := map[string]string{r.FormValue("Name"): hexKey} + user := nostrWellKnown{} + if r.FormValue("Relays") != "" { + relays = map[string][]string{ + hexKey: strings.Split(r.FormValue("Relays"), ","), + } + user = nostrWellKnown{Names: names, Relays: relays} + } else { + user = nostrWellKnown{Names: names} + } + jsonUser, _ := json.Marshal(user) + paymentReq, exp, err := lnd.Request(jsonUser) + if err != nil { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + spew.Dump(paymentReq) + spew.Dump(exp) + + } +} + +func GetNostrAddr(w http.ResponseWriter, r *http.Request) { + // get query string for username + r.ParseForm() + + // connect to redis + redis, err := redis.New("localhost:6379", "", redis.NostrDb) + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } + + requestedName := r.FormValue("name") + // search for user@domain + search := fmt.Sprintf("%s@%s", requestedName, getHostname(r.Host)) + addr, err := redis.Client.Get(search).Result() + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, addr) +} + func AddNostrAddr(n NostrRequest) { // transform request to well known struct relays := make(map[string][]string) @@ -46,34 +111,6 @@ func AddNostrAddr(n NostrRequest) { } } -func RequestNostrAddr(w http.ResponseWriter, req *http.Request) { -} - -func GetNostrAddr(w http.ResponseWriter, req *http.Request) { - // get query string for username - req.ParseForm() - - // connect to redis - r, err := redis.New("localhost:6379", "", redis.NostrDb) - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } - - requestedName := req.FormValue("name") - // search for user@domain - search := fmt.Sprintf("%s@%s", requestedName, getHostname(req.Host)) - addr, err := r.Client.Get(search).Result() - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } - - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, addr) -} - func getHostname(hostname string) string { // remove port for local testing if strings.Contains(hostname, ":") { @@ -81,3 +118,16 @@ func getHostname(hostname string) string { } return hostname } + +func convertNpubtoHex(npub string) (string, error) { + if !strings.HasPrefix(npub, "npub1") { + return "", errors.New("Not an npub key") + } + _, hex, err := nip19.Decode(npub) + if err != nil { + spew.Dump(hex) + } + + return hex.(string), nil + +}