Start extending nostr to add lnd support

This commit is contained in:
Amarpreet Minhas 2023-02-04 17:40:45 -05:00
parent 7ad1325c7a
commit f9106373c5
5 changed files with 104 additions and 31 deletions

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.19
require ( require (
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/go-redis/redis v6.15.9+incompatible github.com/go-redis/redis v6.15.9+incompatible
github.com/nbd-wtf/go-nostr v0.12.0
) )
require ( require (

2
go.sum
View file

@ -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.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 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.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=

View file

@ -1,8 +1,9 @@
package lnd 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 // https://bitcoin.stackexchange.com/questions/73869/rpc-call-to-lnd
fmt.Println("vim-go") spew.Dump(request)
return "x", 1, nil
} }

19
nostr/form.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
<form method="POST" action="/request/nostr">
<label>Username</label><br>
<input name="Name" type="text" value="" /><br>
<label>Key in npub format</label><br>
<input name="Key" type="text" value="" /><br>
<label>Relays as comma seperated list (optional)</label><br>
<input name="Relays" type="text" value="" /><br>
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>

View file

@ -2,11 +2,15 @@ package nostr
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"git.minhas.io/asara/well-goknown/lnd"
"git.minhas.io/asara/well-goknown/redis" "git.minhas.io/asara/well-goknown/redis"
"github.com/davecgh/go-spew/spew"
"github.com/nbd-wtf/go-nostr/nip19"
) )
type nostrWellKnown struct { type nostrWellKnown struct {
@ -21,6 +25,67 @@ type NostrRequest struct {
Relays []string `json:"relays,omitempty"` 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) { func AddNostrAddr(n NostrRequest) {
// transform request to well known struct // transform request to well known struct
relays := make(map[string][]string) 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 { func getHostname(hostname string) string {
// remove port for local testing // remove port for local testing
if strings.Contains(hostname, ":") { if strings.Contains(hostname, ":") {
@ -81,3 +118,16 @@ func getHostname(hostname string) string {
} }
return hostname 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
}