well-goknown/alby/well-known.go
2024-11-29 19:45:10 -05:00

73 lines
2 KiB
Go

package alby
import (
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"git.devvul.com/asara/gologger"
"git.devvul.com/asara/well-goknown/config"
"github.com/jmoiron/sqlx"
)
var (
DB *sqlx.DB
)
type lnurlp struct {
Status string `json:"status"`
Tag string `json:"tag"`
CommentAllowed int32 `json:"commentAllowed"`
Callback string `json:"callback"`
MinSendable int64 `json:"minSendable"`
MaxSendable int64 `json:"maxSendable"`
Metadata string `json:"metadata"`
AllowsNostr bool `json:"allowsNostr"`
NostrPubkey string `json:"nostrPubkey"`
}
func GetLnurlp(w http.ResponseWriter, r *http.Request) {
l := gologger.Get(config.GetConfig().LogLevel).With().Caller().Logger()
// normalize domain
domain, _, err := net.SplitHostPort(r.Host)
if err != nil {
domain = r.Host
}
name := r.PathValue("name")
var lnwallet string
err = DB.QueryRow("SELECT wallet FROM lnwallets WHERE name=$1 AND domain=$2", strings.ToLower(name), domain).Scan(&lnwallet)
if err != nil {
l.Debug().Msgf("user (%s@%s) doesn't exist: %s", name, domain, err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
lnurlpReturn := &lnurlp{
Status: "OK",
Tag: "payRequest",
CommentAllowed: 255,
Callback: fmt.Sprintf("https://%s/.well-known/lnurlp/%s/callback", name),
MinSendable: 1000,
MaxSendable: 10000000,
Metadata: fmt.Sprintf("[[\"text/plain\", \"ln address payment to %s on the devvul server\"],[\"text/identifier\", \"%s@%s\"]]", name, name, domain),
AllowsNostr: true,
NostrPubkey: lnwallet,
}
ret, err := json.Marshal(lnurlpReturn)
if err != nil {
l.Debug().Msgf("unable to marshal json for %s@%s: %s", name, domain, err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
l.Debug().Msgf("returning lnwallet for %s@%s", name, domain)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(ret)
return
}