fix expensive relay invoice handler

This commit is contained in:
Saul Freeberty
2022-12-25 23:09:48 +00:00
committed by fiatjaf
parent 627724f702
commit 3c953e4ae8
3 changed files with 10 additions and 5 deletions

View File

@@ -5,7 +5,7 @@ import (
"net/http"
)
func handleWebpage(w http.ResponseWriter, r *http.Request) {
func handleWebpage(w http.ResponseWriter, rq *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`
<meta charset=utf-8>
@@ -53,9 +53,9 @@ body {
`))
}
func handleInvoice(w http.ResponseWriter, r *http.Request) {
func handleInvoice(w http.ResponseWriter, rq *http.Request, r *Relay) {
w.Header().Set("Content-Type", "application/json")
invoice, err := generateInvoice(r.URL.Query().Get("pubkey"))
invoice, err := generateInvoice(r, rq.URL.Query().Get("pubkey"))
if err != nil {
json.NewEncoder(w).Encode(struct {
Error string `json:"error"`

View File

@@ -11,7 +11,7 @@ import (
func generateLabel(pubkey string) string { return fmt.Sprintf("relayer-expensive:ticket:%s", pubkey) }
func generateInvoice(pubkey string) (string, error) {
func generateInvoice(r *Relay, pubkey string) (string, error) {
cln := lnsocket.LNSocket{}
cln.GenKey()

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"time"
"net/http"
"github.com/fiatjaf/relayer"
"github.com/fiatjaf/relayer/storage/postgresql"
@@ -49,9 +50,13 @@ func (r *Relay) Init() error {
func (r *Relay) OnInitialized(s *relayer.Server) {
// special handlers
s.Router().Path("/").HandlerFunc(handleWebpage)
s.Router().Path("/invoice").HandlerFunc(handleInvoice)
s.Router().Path("/invoice").HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
handleInvoice(w, rq, r)
})
}
func (r *Relay) AcceptEvent(evt *nostr.Event) bool {
// only accept they have a good preimage for a paid invoice for their public key
if !checkInvoicePaidOk(evt.PubKey) {