lightning address receive works

This commit is contained in:
Peter Rounce 2022-09-15 15:47:50 +00:00
parent 524367ec3c
commit 27be212010
2 changed files with 67 additions and 9 deletions

View file

@ -6,10 +6,11 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" log "github.com/sirupsen/logrus"
"os" "os"
"strconv" "strconv"
"time" "time"
"crypto/sha256"
lnrpc "github.com/lightningnetwork/lnd/lnrpc" lnrpc "github.com/lightningnetwork/lnd/lnrpc"
routerrpc "github.com/lightningnetwork/lnd/lnrpc/routerrpc" routerrpc "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
@ -68,6 +69,38 @@ func getGrpcConn(hostname string, port int, tlsFile, macaroonFile string) *grpc.
return connection return connection
} }
func add_invoice(amount_sat int64, metadata string) (payment_request string, return_err error) {
ln_port, err := strconv.Atoi(os.Getenv("LN_PORT"))
if err != nil {
return "", err
}
dh := sha256.Sum256([]byte(metadata))
connection := getGrpcConn(
os.Getenv("LN_HOST"),
ln_port,
os.Getenv("LN_TLS_FILE"),
os.Getenv("LN_MACAROON_FILE"))
l_client := lnrpc.NewLightningClient(connection)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
result, err := l_client.AddInvoice(ctx, &lnrpc.Invoice {
Value: amount_sat,
DescriptionHash: dh[:],
})
if err != nil {
return "", err
}
return result.PaymentRequest, nil
}
func pay_invoice(invoice string) (payment_status string, failure_reason string, return_err error) { func pay_invoice(invoice string) (payment_status string, failure_reason string, return_err error) {
payment_status = "" payment_status = ""
@ -76,9 +109,6 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
// SendPaymentV2 // SendPaymentV2
ctx2, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// get node parameters from environment variables // get node parameters from environment variables
ln_port, err := strconv.Atoi(os.Getenv("LN_PORT")) ln_port, err := strconv.Atoi(os.Getenv("LN_PORT"))
@ -102,7 +132,10 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
return return
} }
stream, err := r_client.SendPaymentV2(ctx2, &routerrpc.SendPaymentRequest{ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
stream, err := r_client.SendPaymentV2(ctx, &routerrpc.SendPaymentRequest{
PaymentRequest: invoice, PaymentRequest: invoice,
NoInflightUpdates: true, NoInflightUpdates: true,
TimeoutSeconds: 30, TimeoutSeconds: 30,

33
main.go
View file

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"time" "time"
"os" "os"
"strconv"
) )
var router = mux.NewRouter() var router = mux.NewRouter()
@ -36,8 +37,8 @@ func lnurlp_response(w http.ResponseWriter, r *http.Request) {
// look up domain in env vars (HOST_DOMAIN) // look up domain in env vars (HOST_DOMAIN)
env_host_domain := os.Getenv("HOST_DOMAIN") domain := os.Getenv("HOST_DOMAIN")
if r.Host != env_host_domain { if r.Host != domain {
log.Warn("wrong host domain") log.Warn("wrong host domain")
write_error(w) write_error(w)
return return
@ -58,12 +59,14 @@ func lnurlp_response(w http.ResponseWriter, r *http.Request) {
return return
} }
metadata := "[[\\\"text/identifier\\\",\\\"" + name + "@" + domain + "\\\"],[\\\"text/plain\\\",\\\"" + name + "@" + domain + "\\\"]]"
jsonData := []byte(`{"status":"OK",` + jsonData := []byte(`{"status":"OK",` +
`"callback":"https://` + env_host_domain + `/lnurlp/` + name + `",` + `"callback":"https://` + domain + `/lnurlp/` + name + `",` +
`"tag":"payRequest",` + `"tag":"payRequest",` +
`"maxSendable":1000000000,` + `"maxSendable":1000000000,` +
`"minSendable":1000,` + `"minSendable":1000,` +
`"metadata":"[[\"text/plain\",\"` + name + `@` + env_host_domain + `\"]]",` + `"metadata":"` + metadata + `",` +
`"commentAllowed":0` + `"commentAllowed":0` +
`}`) `}`)
@ -84,7 +87,29 @@ func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
"req.Host": r.Host, "req.Host": r.Host,
},).Info("lnurlp_callback") },).Info("lnurlp_callback")
domain := os.Getenv("HOST_DOMAIN")
if r.Host != domain {
log.Warn("wrong host domain")
write_error(w)
return
}
//TODO add err
amount_msat, _ := strconv.ParseInt(amount, 10, 64)
amount_sat := amount_msat / 1000;
//TODO add err
metadata := "[[\"text/identifier\",\"" + name + "@" + domain + "\"],[\"text/plain\",\"" + name + "@" + domain + "\"]]"
pr, _ := add_invoice(amount_sat, metadata)
jsonData := []byte(`{` +
`"status":"OK","successAction":{"tag":"message","message":"Payment success!"}` +
`,"routes":[],"pr":"` + pr + `","disposable":false` +
`}`)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
} }
func main() { func main() {