start adding handling for lnurlp
This commit is contained in:
parent
79c6369c39
commit
e16fca179b
6 changed files with 107 additions and 8 deletions
21
database.go
21
database.go
|
|
@ -106,6 +106,27 @@ func db_get_card_count_for_uid(uid string) (int, error) {
|
|||
return card_count, nil
|
||||
}
|
||||
|
||||
func db_get_card_count_for_name(name string) (int, error) {
|
||||
|
||||
card_count := 0
|
||||
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `select count(card_id) from cards where card_name=$1;`
|
||||
|
||||
row := db.QueryRow(sqlStatement, name)
|
||||
err = row.Scan(&card_count)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return card_count, nil
|
||||
}
|
||||
|
||||
func db_get_cards_blank_uid() ([]Card, error) {
|
||||
|
||||
// open the database
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/lib/pq v1.10.6
|
||||
github.com/lightningnetwork/lnd v0.15.1-beta.rc2
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
google.golang.org/grpc v1.49.0
|
||||
gopkg.in/macaroon.v2 v2.1.0
|
||||
)
|
||||
|
|
@ -100,7 +101,6 @@ require (
|
|||
github.com/prometheus/common v0.37.0 // indirect
|
||||
github.com/prometheus/procfs v0.8.0 // indirect
|
||||
github.com/rogpeppe/fastuuid v1.2.0 // indirect
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
|
||||
github.com/soheilhy/cmux v0.1.5 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/stretchr/objx v0.4.0 // indirect
|
||||
|
|
|
|||
1
go.sum
1
go.sum
|
|
@ -592,6 +592,7 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
|
|||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
|
|
|
|||
|
|
@ -9,6 +9,13 @@ import (
|
|||
|
||||
func lnurlw_callback(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
env_host_domain := os.Getenv("HOST_DOMAIN")
|
||||
if req.Host != env_host_domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
url := req.URL.RequestURI()
|
||||
log.WithFields(log.Fields{"url": url}).Debug("cb request")
|
||||
|
||||
|
|
|
|||
|
|
@ -182,7 +182,7 @@ func parse_request(req *http.Request) (int, error) {
|
|||
card_count, err := db_get_card_count_for_uid(uid_str)
|
||||
|
||||
if err != nil {
|
||||
return 0, errors.New("could not get card records count")
|
||||
return 0, errors.New("could not get card count for uid")
|
||||
}
|
||||
|
||||
if card_count == 0 {
|
||||
|
|
@ -246,6 +246,13 @@ func parse_request(req *http.Request) (int, error) {
|
|||
|
||||
func lnurlw_response(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
env_host_domain := os.Getenv("HOST_DOMAIN")
|
||||
if req.Host != env_host_domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
card_id, err := parse_request(req)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
|||
73
main.go
73
main.go
|
|
@ -24,6 +24,68 @@ func write_error_message(w http.ResponseWriter, message string) {
|
|||
w.Write(jsonData)
|
||||
}
|
||||
|
||||
func lnurlp_response(w http.ResponseWriter, r *http.Request) {
|
||||
name := mux.Vars(r)["name"]
|
||||
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"url_path": r.URL.Path,
|
||||
"name": name,
|
||||
"r.Host": r.Host,
|
||||
},).Info("lnurlp_response")
|
||||
|
||||
// look up domain in env vars (HOST_DOMAIN)
|
||||
env_host_domain := os.Getenv("HOST_DOMAIN")
|
||||
if r.Host != env_host_domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
// look up name in database (table cards, field card_name)
|
||||
|
||||
card_count, err := db_get_card_count_for_name(name)
|
||||
if err != nil {
|
||||
log.Warn("could not get card count for name")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
if card_count != 1 {
|
||||
log.Info("not one card with that name")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
jsonData := []byte(`{"status":"OK",` +
|
||||
`"callback":"https://` + env_host_domain + `/lnurlp/` + name + `",` +
|
||||
`"tag":"payRequest",` +
|
||||
`"maxSendable":1000000000,` +
|
||||
`"minSendable":1000,` +
|
||||
`"metadata":"[[\"text/plain\",\"` + name + `@` + env_host_domain + `\"]]",` +
|
||||
`"commentAllowed":0` +
|
||||
`}`)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
||||
func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
|
||||
name := mux.Vars(r)["name"]
|
||||
amount := r.URL.Query().Get("amount");
|
||||
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"url_path": r.URL.Path,
|
||||
"name": name,
|
||||
"amount": amount,
|
||||
"req.Host": r.Host,
|
||||
},).Info("lnurlp_callback")
|
||||
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
log_level := os.Getenv("LOG_LEVEL")
|
||||
|
||||
|
|
@ -39,12 +101,13 @@ func main() {
|
|||
})
|
||||
|
||||
// createboltcard
|
||||
router.Path("/new").HandlerFunc(new_card_request)
|
||||
router.Path("/new").Methods("GET").HandlerFunc(new_card_request)
|
||||
// lnurlw for pos
|
||||
router.Path("/ln").HandlerFunc(lnurlw_response)
|
||||
router.Path("/cb").HandlerFunc(lnurlw_callback)
|
||||
// lnurlp for lightning address
|
||||
// mux.HandleFunc("/.well-known/lnurlp/", lnurlp_response)
|
||||
router.Path("/ln").Methods("GET").HandlerFunc(lnurlw_response)
|
||||
router.Path("/cb").Methods("GET").HandlerFunc(lnurlw_callback)
|
||||
// lnurlp for lightning address lnurlp
|
||||
router.Path("/.well-known/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_response)
|
||||
router.Path("/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_callback)
|
||||
|
||||
port := os.Getenv("HOST_PORT")
|
||||
if len(port) == 0 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue