lightning address receive works
This commit is contained in:
parent
524367ec3c
commit
27be212010
2 changed files with 67 additions and 9 deletions
43
lightning.go
43
lightning.go
|
|
@ -6,10 +6,11 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
"crypto/sha256"
|
||||
|
||||
lnrpc "github.com/lightningnetwork/lnd/lnrpc"
|
||||
routerrpc "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
||||
|
|
@ -68,6 +69,38 @@ func getGrpcConn(hostname string, port int, tlsFile, macaroonFile string) *grpc.
|
|||
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) {
|
||||
|
||||
payment_status = ""
|
||||
|
|
@ -76,9 +109,6 @@ func pay_invoice(invoice string) (payment_status string, failure_reason string,
|
|||
|
||||
// SendPaymentV2
|
||||
|
||||
ctx2, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// get node parameters from environment variables
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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,
|
||||
NoInflightUpdates: true,
|
||||
TimeoutSeconds: 30,
|
||||
|
|
|
|||
33
main.go
33
main.go
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
env_host_domain := os.Getenv("HOST_DOMAIN")
|
||||
if r.Host != env_host_domain {
|
||||
domain := os.Getenv("HOST_DOMAIN")
|
||||
if r.Host != domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
|
|
@ -58,12 +59,14 @@ func lnurlp_response(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
metadata := "[[\\\"text/identifier\\\",\\\"" + name + "@" + domain + "\\\"],[\\\"text/plain\\\",\\\"" + name + "@" + domain + "\\\"]]"
|
||||
|
||||
jsonData := []byte(`{"status":"OK",` +
|
||||
`"callback":"https://` + env_host_domain + `/lnurlp/` + name + `",` +
|
||||
`"callback":"https://` + domain + `/lnurlp/` + name + `",` +
|
||||
`"tag":"payRequest",` +
|
||||
`"maxSendable":1000000000,` +
|
||||
`"minSendable":1000,` +
|
||||
`"metadata":"[[\"text/plain\",\"` + name + `@` + env_host_domain + `\"]]",` +
|
||||
`"metadata":"` + metadata + `",` +
|
||||
`"commentAllowed":0` +
|
||||
`}`)
|
||||
|
||||
|
|
@ -84,7 +87,29 @@ func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
|
|||
"req.Host": r.Host,
|
||||
},).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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue