2023-01-25 05:47:06 +00:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
2023-02-03 04:56:15 +00:00
|
|
|
"encoding/json"
|
2023-02-04 22:40:45 +00:00
|
|
|
"errors"
|
2023-01-25 05:47:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-02-04 22:40:45 +00:00
|
|
|
"git.minhas.io/asara/well-goknown/lnd"
|
2023-02-03 04:56:15 +00:00
|
|
|
"git.minhas.io/asara/well-goknown/redis"
|
2023-02-04 22:40:45 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
2023-01-25 05:47:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nostrWellKnown struct {
|
|
|
|
Names map[string]string `json:"names"`
|
|
|
|
Relays map[string][]string `json:"relays,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NostrRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Hostname string
|
2023-02-03 04:56:15 +00:00
|
|
|
Relays []string `json:"relays,omitempty"`
|
2023-01-25 05:47:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 22:40:45 +00:00
|
|
|
func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
2023-02-05 00:11:50 +00:00
|
|
|
http.ServeFile(w, r, "html/nostr_form.html")
|
2023-02-04 22:40:45 +00:00
|
|
|
case "POST":
|
|
|
|
r.ParseForm()
|
2023-02-05 01:43:38 +00:00
|
|
|
rKey := getRkey("verified", r.FormValue("Name"), getHostname(r.Host))
|
2023-02-04 23:52:50 +00:00
|
|
|
// check if the user already exists
|
2023-02-05 01:43:38 +00:00
|
|
|
redisCli, err := redis.New("localhost:6379", "", redis.NostrDb)
|
2023-02-04 23:52:50 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2023-02-05 01:43:38 +00:00
|
|
|
exists := redisCli.Client.Exists(rKey)
|
2023-02-04 23:52:50 +00:00
|
|
|
if exists.Val() == 1 {
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the hexkey
|
|
|
|
hexKey, err := convertNpubToHex(r.FormValue("Key"))
|
2023-02-04 22:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-02-04 23:52:50 +00:00
|
|
|
|
|
|
|
// create the struct
|
|
|
|
relays := make(map[string][]string)
|
2023-02-04 22:40:45 +00:00
|
|
|
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)
|
2023-02-04 23:52:50 +00:00
|
|
|
|
|
|
|
// generate the payment request
|
2023-02-05 00:11:50 +00:00
|
|
|
paymentReq, exp, err := lnd.Request(rKey, jsonUser)
|
2023-02-04 22:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
2023-02-05 01:43:38 +00:00
|
|
|
requestKey := getRkey("requested", r.FormValue("Name"), getHostname(r.Host))
|
|
|
|
err = redisCli.Client.Set(requestKey, jsonUser, redis.NostrDb).Err()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED")
|
|
|
|
}
|
2023-02-04 22:40:45 +00:00
|
|
|
spew.Dump(paymentReq)
|
|
|
|
spew.Dump(exp)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// get query string for username
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
// connect to redis
|
2023-02-05 01:43:38 +00:00
|
|
|
redisCli, err := redis.New("localhost:6379", "", redis.NostrDb)
|
2023-02-04 22:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedName := r.FormValue("name")
|
|
|
|
// search for user@domain
|
2023-02-05 01:43:38 +00:00
|
|
|
search := getRkey("verified", requestedName, getHostname(r.Host))
|
|
|
|
addr, err := redisCli.Client.Get(search).Result()
|
2023-02-04 22:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, addr)
|
|
|
|
}
|
|
|
|
|
2023-01-25 05:47:06 +00:00
|
|
|
func AddNostrAddr(n NostrRequest) {
|
|
|
|
// transform request to well known struct
|
2023-02-03 04:56:15 +00:00
|
|
|
relays := make(map[string][]string)
|
|
|
|
user := nostrWellKnown{}
|
2023-02-04 22:46:13 +00:00
|
|
|
hexKey, err := convertNpubToHex(n.Key)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Not an npub")
|
|
|
|
}
|
|
|
|
names := map[string]string{n.Name: hexKey}
|
2023-02-04 05:32:25 +00:00
|
|
|
|
2023-02-03 04:56:15 +00:00
|
|
|
if n.Relays != nil {
|
2023-02-04 22:46:13 +00:00
|
|
|
relays = map[string][]string{hexKey: n.Relays}
|
2023-02-03 04:56:15 +00:00
|
|
|
user = nostrWellKnown{Names: names, Relays: relays}
|
|
|
|
} else {
|
|
|
|
user = nostrWellKnown{Names: names}
|
|
|
|
}
|
2023-01-25 05:47:06 +00:00
|
|
|
jsonUser, _ := json.Marshal(user)
|
|
|
|
|
2023-02-04 05:32:25 +00:00
|
|
|
r, err := redis.New("localhost:6379", "", redis.NostrDb)
|
2023-02-03 04:56:15 +00:00
|
|
|
if err != nil {
|
2023-02-05 00:11:50 +00:00
|
|
|
fmt.Println("Failed to connect to redis")
|
2023-02-03 04:56:15 +00:00
|
|
|
}
|
2023-02-05 01:43:38 +00:00
|
|
|
nameKey := getRkey("verified", n.Name, n.Hostname)
|
2023-02-04 05:32:25 +00:00
|
|
|
err = r.Client.Set(nameKey, jsonUser, redis.NostrDb).Err()
|
2023-01-25 05:47:06 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED")
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 04:56:15 +00:00
|
|
|
|
2023-02-04 22:40:45 +00:00
|
|
|
func getHostname(hostname string) string {
|
|
|
|
// remove port for local testing
|
|
|
|
return hostname
|
2023-02-03 04:56:15 +00:00
|
|
|
}
|
2023-01-25 05:47:06 +00:00
|
|
|
|
2023-02-04 22:46:13 +00:00
|
|
|
func convertNpubToHex(npub string) (string, error) {
|
2023-02-04 22:40:45 +00:00
|
|
|
if !strings.HasPrefix(npub, "npub1") {
|
|
|
|
return "", errors.New("Not an npub key")
|
2023-02-03 04:56:15 +00:00
|
|
|
}
|
2023-02-04 22:40:45 +00:00
|
|
|
_, hex, err := nip19.Decode(npub)
|
2023-01-25 05:47:06 +00:00
|
|
|
if err != nil {
|
2023-02-04 22:40:45 +00:00
|
|
|
spew.Dump(hex)
|
2023-01-25 05:47:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 22:40:45 +00:00
|
|
|
return hex.(string), nil
|
2023-01-25 05:47:06 +00:00
|
|
|
|
|
|
|
}
|
2023-02-04 23:52:50 +00:00
|
|
|
|
2023-02-05 01:43:38 +00:00
|
|
|
func getRkey(prefix string, user string, hostname string) string {
|
2023-02-04 23:52:50 +00:00
|
|
|
if strings.Contains(hostname, ":") {
|
|
|
|
hostname = strings.Split(hostname, ":")[0]
|
|
|
|
}
|
2023-02-05 01:43:38 +00:00
|
|
|
return fmt.Sprintf("%s:%s@%s", prefix, user, hostname)
|
2023-02-04 23:52:50 +00:00
|
|
|
}
|