From 039f88d14cc609ad1db268bb9aacbc0780c6f107 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Thu, 7 Oct 2021 09:53:27 +0200 Subject: [PATCH] Match Lightning payment based on payment hash if BOLT11 is not the same. (#2773) * Match Lightning payment based on payment hash if BOLT11 is not the same. * Fixup * Fixup Co-authored-by: Nicolas Dorier --- .../Payments/Lightning/LightningLikePaymentHandler.cs | 1 + .../Lightning/LightningLikePaymentMethodDetails.cs | 9 +++++++++ BTCPayServer/Payments/Lightning/LightningListener.cs | 6 ++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index 31ac65667..578efb0a8 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -114,6 +114,7 @@ namespace BTCPayServer.Payments.Lightning { Activated = true, BOLT11 = lightningInvoice.BOLT11, + PaymentHash = BOLT11PaymentRequest.Parse(lightningInvoice.BOLT11, network.NBitcoinNetwork).PaymentHash, InvoiceId = lightningInvoice.Id, NodeInfo = nodeInfo.First().ToString() }; diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs index cd94537c0..13cb1b4d4 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentMethodDetails.cs @@ -1,8 +1,12 @@ +using BTCPayServer.Lightning; +using NBitcoin; + namespace BTCPayServer.Payments.Lightning { public class LightningLikePaymentMethodDetails : IPaymentMethodDetails { public string BOLT11 { get; set; } + public uint256 PaymentHash { get; set; } public string InvoiceId { get; set; } public string NodeInfo { get; set; } @@ -11,6 +15,11 @@ namespace BTCPayServer.Payments.Lightning return BOLT11; } + public uint256 GetPaymentHash(Network network) + { + return PaymentHash ?? BOLT11PaymentRequest.Parse(BOLT11, network).PaymentHash; + } + public PaymentType GetPaymentType() { return PaymentTypes.LightningLike; diff --git a/BTCPayServer/Payments/Lightning/LightningListener.cs b/BTCPayServer/Payments/Lightning/LightningListener.cs index 720284934..3d9e4c995 100644 --- a/BTCPayServer/Payments/Lightning/LightningListener.cs +++ b/BTCPayServer/Payments/Lightning/LightningListener.cs @@ -344,7 +344,7 @@ namespace BTCPayServer.Payments.Lightning internal async Task PollPayment(ListenedInvoice listenedInvoice, CancellationToken cancellation) { var client = _lightningClientFactory.Create(ConnectionString, _network); - LightningInvoice lightningInvoice = await client.GetInvoice(listenedInvoice.PaymentMethodDetails.InvoiceId); + LightningInvoice lightningInvoice = await client.GetInvoice(listenedInvoice.PaymentMethodDetails.InvoiceId, cancellation); if (lightningInvoice?.Status is LightningInvoiceStatus.Paid && await AddPayment(lightningInvoice, listenedInvoice.InvoiceId)) { @@ -385,7 +385,9 @@ namespace BTCPayServer.Payments.Lightning if (!_ListenedInvoices.TryGetValue(notification.Id, out var listenedInvoice)) continue; if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId && - notification.BOLT11 == listenedInvoice.PaymentMethodDetails.BOLT11) + (notification.BOLT11 == listenedInvoice.PaymentMethodDetails.BOLT11 || + BOLT11PaymentRequest.Parse(notification.BOLT11, _network.NBitcoinNetwork).PaymentHash == + listenedInvoice.PaymentMethodDetails.GetPaymentHash(_network.NBitcoinNetwork))) { if (notification.Status == LightningInvoiceStatus.Paid && notification.PaidAt.HasValue && notification.Amount != null)