Merge pull request #32 from boltcard/lndhub

Lndhub
This commit is contained in:
Peter Rounce 2023-02-19 17:30:45 +00:00 committed by GitHub
commit f84014d2ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 16 deletions

View file

@ -81,6 +81,6 @@ func main() {
fmt.Println()
fmt.Println(url)
fmt.Println()
q, err := qrcode.New(url, qrcode.Medium)
q, _ := qrcode.New(url, qrcode.Medium)
fmt.Println(q.ToSmallString(false))
}

13
docs/LNDHUB.md Normal file
View file

@ -0,0 +1,13 @@
## lndhub
### in the `settings` table
- set 'LNDHUB_URL' to 'lndhub.yourdomain.com'
- set 'FUNCTION_LNDHUB' to 'ENABLE'
### create card
- set the card_name to the 'login:password' for the lndhub account
e.g. '11111111111111111111:22222222222222222222'
### limits
- there are currently no payment rules in this code, only the lndhub account limit
i.e. tx_limit_sats & day_limit_sats are not enforced

View file

@ -2,6 +2,7 @@ package lnurlw
import (
"bytes"
"encoding/json"
"github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/lnd"
"github.com/boltcard/boltcard/resp_err"
@ -9,9 +10,26 @@ import (
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strconv"
"strings"
)
func lndhub_payment(w http.ResponseWriter, p *db.Payment) {
type LndhubAuthRequest struct {
Login string `json:"login"`
Password string `json:"password"`
}
type LndhubAuthResponse struct {
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
}
type LndhubPayInvoiceRequest struct {
Invoice string `json:"invoice"`
FreeAmount string `json:"freeamount"`
}
func lndhub_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, param_pr string) {
//get setting for LNDHUB_URL
lndhub_url := "https://" + db.Get_setting("LNDHUB_URL")
@ -26,43 +44,118 @@ func lndhub_payment(w http.ResponseWriter, p *db.Payment) {
//lndhub.auth API call
//the login JSON is held in the Card_name field
body := []byte(c.Card_name)
// as "login:password"
card_name_parts := strings.Split(c.Card_name, ":")
r, err := http.NewRequest("POST", lndhub_url+"/auth", bytes.NewBuffer(body))
if len(card_name_parts) != 2 {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
if len(card_name_parts[0]) != 20 || len(card_name_parts[1]) != 20 {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
var lhAuthRequest LndhubAuthRequest
lhAuthRequest.Login = card_name_parts[0]
lhAuthRequest.Password = card_name_parts[1]
authReq, err := json.Marshal(lhAuthRequest)
log.Info(string(authReq))
req_auth, err := http.NewRequest("POST", lndhub_url+"/auth", bytes.NewBuffer(authReq))
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
r.Header.Add("Access-Control-Allow-Origin", "*")
r.Header.Add("Content-Type", "application/json")
req_auth.Header.Add("Access-Control-Allow-Origin", "*")
req_auth.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(r)
resp_auth, err := client.Do(req_auth)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
defer res.Body.Close()
defer resp_auth.Body.Close()
b, err2 := io.ReadAll(res.Body)
if err2 != nil {
resp_auth_bytes, err := io.ReadAll(resp_auth.Body)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
log.Info(string(b))
var auth_keys LndhubAuthResponse
// fmt.Println(string(b))
err = json.Unmarshal([]byte(resp_auth_bytes), &auth_keys)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
//lndhub.payinvoice API call
var payInvoiceRequest LndhubPayInvoiceRequest
payInvoiceRequest.Invoice = param_pr
payInvoiceRequest.FreeAmount = strconv.Itoa(int(bolt11.MSatoshi / 1000))
req_payinvoice, err := json.Marshal(payInvoiceRequest)
log.Info(string(req_payinvoice))
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
req, err := http.NewRequest("POST", lndhub_url+"/payinvoice", bytes.NewBuffer(req_payinvoice))
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
req.Header.Add("Access-Control-Allow-Origin", "*")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+auth_keys.AccessToken)
res2, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
defer res2.Body.Close()
b2, err := io.ReadAll(res2.Body)
if err != nil {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
resp_err.Write(w)
return
}
log.Info(string(b2))
// var auth_keys LndhubAuthResponse
// err = json.Unmarshal([]byte(b), &auth_keys)
// if err != nil {
// log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Warn(err)
// resp_err.Write(w)
// return
// }
}
func lnd_payment(w http.ResponseWriter, bolt11 decodepay.Bolt11, p *db.Payment, param_pr string) {
func lnd_payment(w http.ResponseWriter, p *db.Payment, bolt11 decodepay.Bolt11, param_pr string) {
// check amount limits
invoice_sats := int(bolt11.MSatoshi / 1000)
@ -223,9 +316,9 @@ func Callback(w http.ResponseWriter, req *http.Request) {
lndhub := db.Get_setting("FUNCTION_LNDHUB")
if lndhub == "ENABLE" {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Info("initiating lndhub payment")
lndhub_payment(w, p)
lndhub_payment(w, p, bolt11, param_pr)
} else {
log.WithFields(log.Fields{"card_payment_id": p.Card_payment_id}).Info("initiating lnd payment")
lnd_payment(w, bolt11, p, param_pr)
lnd_payment(w, p, bolt11, param_pr)
}
}

View file

@ -65,6 +65,6 @@ func main() {
`}`
fmt.Println()
q, err := qrcode.New(qr, qrcode.Medium)
q, _ := qrcode.New(qr, qrcode.Medium)
fmt.Println(q.ToSmallString(false))
}