Merge pull request #4795 from Kukks/lnurl-disable-if-no-node

This commit is contained in:
Andrew Camilleri 2023-03-22 09:02:52 +01:00 committed by GitHub
commit 2f3e947027
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View file

@ -1,6 +1,8 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Configuration;
@ -20,16 +22,19 @@ namespace BTCPayServer.Payments.Lightning
private readonly BTCPayNetworkProvider _networkProvider;
private readonly DisplayFormatter _displayFormatter;
private readonly LightningLikePaymentHandler _lightningLikePaymentHandler;
private readonly LightningClientFactoryService _lightningClientFactoryService;
public LNURLPayPaymentHandler(
BTCPayNetworkProvider networkProvider,
DisplayFormatter displayFormatter,
IOptions<LightningNetworkOptions> options,
LightningLikePaymentHandler lightningLikePaymentHandler)
LightningLikePaymentHandler lightningLikePaymentHandler,
LightningClientFactoryService lightningClientFactoryService)
{
_networkProvider = networkProvider;
_displayFormatter = displayFormatter;
_lightningLikePaymentHandler = lightningLikePaymentHandler;
_lightningClientFactoryService = lightningClientFactoryService;
Options = options;
}
@ -62,6 +67,16 @@ namespace BTCPayServer.Payments.Lightning
{
throw new PaymentMethodUnavailableException("LNURL requires a lightning node to be configured for the store.");
}
using var cts = new CancellationTokenSource(LightningLikePaymentHandler.LightningTimeout);
try
{
var client = lnSupported.CreateLightningClient(network, Options.Value, _lightningClientFactoryService);
await client.GetInfo(cts.Token);
}
catch (OperationCanceledException) when (cts.IsCancellationRequested)
{
throw new PaymentMethodUnavailableException("The lightning node did not reply in a timely manner");
}
return new LNURLPayPaymentMethodDetails()
{

View file

@ -21,7 +21,7 @@ namespace BTCPayServer.Payments.Lightning
{
public class LightningLikePaymentHandler : PaymentMethodHandlerBase<LightningSupportedPaymentMethod, BTCPayNetwork>
{
public static int LIGHTNING_TIMEOUT = 5000;
public static readonly int LightningTimeout = 5000;
readonly NBXplorerDashboard _Dashboard;
private readonly LightningClientFactoryService _lightningClientFactory;
private readonly BTCPayNetworkProvider _networkProvider;
@ -93,7 +93,7 @@ namespace BTCPayServer.Payments.Lightning
description = description.Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase)
.Replace("{ItemDescription}", invoice.Metadata.ItemDesc ?? "", StringComparison.OrdinalIgnoreCase)
.Replace("{OrderId}", invoice.Metadata.OrderId ?? "", StringComparison.OrdinalIgnoreCase);
using (var cts = new CancellationTokenSource(LIGHTNING_TIMEOUT))
using (var cts = new CancellationTokenSource(LightningTimeout))
{
try
{
@ -129,7 +129,7 @@ namespace BTCPayServer.Payments.Lightning
try
{
using var cts = new CancellationTokenSource(LIGHTNING_TIMEOUT);
using var cts = new CancellationTokenSource(LightningTimeout);
var client = CreateLightningClient(supportedPaymentMethod, network);
LightningNodeInformation info;
try