From b3ac9e0a6c71d0d2e042135437f36a03b217d7ad Mon Sep 17 00:00:00 2001 From: xx979xx Date: Mon, 3 Jun 2024 13:33:30 +0300 Subject: [PATCH] LN invoice expiry param & use it for ctx timeout --- docker_init.sh | 1 + docs/SETTINGS.md | 1 + lnd/lnd.go | 15 +++++++++++++-- sql/settings.sql | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docker_init.sh b/docker_init.sh index 926ed5b..708070c 100755 --- a/docker_init.sh +++ b/docker_init.sh @@ -32,3 +32,4 @@ sed -i "s/[(]'FEE_LIMIT_PERCENT'[^)]*[)]/(\'FEE_LIMIT_PERCENT\', \'0.5\')/" sql/ sed -i "s/[(]'FUNCTION_LNURLW'[^)]*[)]/(\'FUNCTION_LNURLW\', \'ENABLE\')/" sql/settings.sql sed -i "s/[(]'FUNCTION_LNURLP'[^)]*[)]/(\'FUNCTION_LNURLP\', \'DISABLE\')/" sql/settings.sql sed -i "s/[(]'FUNCTION_EMAIL'[^)]*[)]/(\'FUNCTION_EMAIL\', \'DISABLE\')/" sql/settings.sql +sed -i "s/[(]'LN_INVOICE_EXPIRY_SEC'[^)]*[)]/(\'LN_INVOICE_EXPIRY_SEC\', \'3600\')/" sql/settings.sql diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index 371b4fc..1c52afa 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -34,3 +34,4 @@ Here are the descriptions of values available to use in the `settings` table: | FUNCTION_INTERNAL_API | DISABLE | system level switch for activating the internal API | | SENDGRID_API_KEY | | User API Key from SendGrid.com | | SENDGRID_EMAIL_SENDER | | Single Sender email address verified by SendGrid | +| LN_INVOICE_EXPIRY_SEC | 3600 | LN invoice's expiry time in seconds | diff --git a/lnd/lnd.go b/lnd/lnd.go index c5e0c1a..4982453 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -81,6 +81,10 @@ func Add_invoice(amount_sat int64, metadata string) (payment_request string, r_h if err != nil { return "", nil, err } + ln_invoice_expiry, err := strconv.ParseInt(db.Get_setting("LN_INVOICE_EXPIRY_SEC"), 10, 64) + if err != nil { + return "", nil, err + } dh := sha256.Sum256([]byte(metadata)) @@ -98,6 +102,7 @@ func Add_invoice(amount_sat int64, metadata string) (payment_request string, r_h result, err := l_client.AddInvoice(ctx, &lnrpc.Invoice{ Value: amount_sat, DescriptionHash: dh[:], + Expiry: ln_invoice_expiry, }) if err != nil { @@ -120,6 +125,11 @@ func Monitor_invoice_state(r_hash []byte) { log.Warn(err) return } + ln_invoice_expiry, err := strconv.Atoi(db.Get_setting("LN_INVOICE_EXPIRY_SEC")) + if err != nil { + log.Warn(err) + return + } connection := getGrpcConn( db.Get_setting("LN_HOST"), @@ -129,7 +139,7 @@ func Monitor_invoice_state(r_hash []byte) { i_client := invoicesrpc.NewInvoicesClient(connection) - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(ln_invoice_expiry) * time.Second) defer cancel() stream, err := i_client.SubscribeSingleInvoice(ctx, &invoicesrpc.SubscribeSingleInvoiceRequest{ @@ -228,10 +238,11 @@ func PayInvoice(card_payment_id int, invoice string) { bolt11, _ := decodepay.Decodepay(invoice) invoice_msats := bolt11.MSatoshi + invoice_expiry := bolt11.Expiry fee_limit_product := int64((fee_limit_percent / 100) * (float64(invoice_msats) / 1000)) - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(invoice_expiry) * time.Second) defer cancel() stream, err := r_client.SendPaymentV2(ctx, &routerrpc.SendPaymentRequest{ diff --git a/sql/settings.sql b/sql/settings.sql index 1f6a4e4..810a2bb 100644 --- a/sql/settings.sql +++ b/sql/settings.sql @@ -31,3 +31,4 @@ INSERT INTO settings (name, value) VALUES ('LNDHUB_URL', ''); INSERT INTO settings (name, value) VALUES ('FUNCTION_INTERNAL_API', ''); INSERT INTO settings (name, value) VALUES ('SENDGRID_API_KEY', ''); INSERT INTO settings (name, value) VALUES ('SENDGRID_EMAIL_SENDER', ''); +INSERT INTO settings (name, value) VALUES ('LN_INVOICE_EXPIRY_SEC', '3600');