Extracting fetching of PaymentHash to extension method like Nicolas asked

This commit is contained in:
rockstardev
2025-04-28 14:55:28 -05:00
parent 0dfa6a7ff6
commit 1e2ce17a17
3 changed files with 13 additions and 12 deletions

View File

@@ -1,12 +1,12 @@
using BTCPayServer.Configuration;
using BTCPayServer.Lightning;
using BTCPayServer.Services;
using NBitcoin;
namespace BTCPayServer.Payments.Lightning
{
public static class LightningExtensions
{
public static bool IsConfigured(this LightningPaymentMethodConfig supportedPaymentMethod, BTCPayNetwork network, LightningNetworkOptions options)
{
return supportedPaymentMethod.GetExternalLightningUrl() is not null || (supportedPaymentMethod.IsInternalNode && options.InternalLightningByCryptoCode.ContainsKey(network.CryptoCode));
@@ -25,5 +25,12 @@ namespace BTCPayServer.Payments.Lightning
return connectionString;
}
}
public static uint256? GetPaymentHash(this LightningInvoice lightningInvoice, Network btcpayNetwork)
{
return lightningInvoice.PaymentHash != null ?
uint256.Parse(lightningInvoice.PaymentHash) :
BOLT11PaymentRequest.Parse(lightningInvoice.BOLT11, btcpayNetwork).PaymentHash;
}
}
}

View File

@@ -128,13 +128,9 @@ namespace BTCPayServer.Payments.Lightning
}
paymentPrompt.Destination = lightningInvoice.BOLT11;
var paymentHash = lightningInvoice.PaymentHash != null ?
uint256.Parse(lightningInvoice.PaymentHash) :
BOLT11PaymentRequest.Parse(lightningInvoice.BOLT11, _Network.NBitcoinNetwork).PaymentHash;
var details = new LigthningPaymentPromptDetails
{
PaymentHash = paymentHash,
PaymentHash = lightningInvoice.GetPaymentHash(_Network.NBitcoinNetwork),
Preimage = string.IsNullOrEmpty(lightningInvoice.Preimage) ? null : uint256.Parse(lightningInvoice.Preimage),
InvoiceId = lightningInvoice.Id,
NodeInfo = (await nodeInfo).FirstOrDefault()?.ToString()

View File

@@ -527,8 +527,7 @@ retry:
continue;
if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId &&
(notification.BOLT11 == listenedInvoice.PaymentMethod.Destination ||
BOLT11PaymentRequest.Parse(notification.BOLT11, _network.NBitcoinNetwork).PaymentHash ==
GetPaymentHash(listenedInvoice)))
notification.GetPaymentHash(_network.NBitcoinNetwork) == GetPaymentHash(listenedInvoice)))
{
if (notification.Status == LightningInvoiceStatus.Paid &&
notification.PaidAt.HasValue && notification.Amount != null)
@@ -559,7 +558,8 @@ retry:
private uint256? GetPaymentHash(ListenedInvoice listenedInvoice)
{
return listenedInvoice.PaymentMethodDetails.PaymentHash ?? BOLT11PaymentRequest.Parse(listenedInvoice.PaymentMethod.Destination, _network.NBitcoinNetwork).PaymentHash;
return listenedInvoice.PaymentMethodDetails.PaymentHash ??
BOLT11PaymentRequest.Parse(listenedInvoice.PaymentMethod.Destination, _network.NBitcoinNetwork).PaymentHash;
}
public DateTimeOffset? LastFullPoll { get; set; }
@@ -591,9 +591,7 @@ retry:
return false;
var handler = _handlers[paymentMethodId];
var paymentHash = notification.PaymentHash != null ?
uint256.Parse(notification.PaymentHash) :
BOLT11PaymentRequest.Parse(notification.BOLT11, _network.NBitcoinNetwork).PaymentHash;
var paymentHash = notification.GetPaymentHash(_network.NBitcoinNetwork);
var paymentData = new PaymentData()
{
Id = paymentHash?.ToString() ?? notification.BOLT11,