From 47f5d97eaf577ff88979164c929db90a04c24324 Mon Sep 17 00:00:00 2001 From: d11n Date: Mon, 24 Apr 2023 14:50:31 +0200 Subject: [PATCH] Prevent an NRE in LNURL (#4908) * Prevent an NRE in LNURL In addition to f05a7f9f14e0a2f906c79951a15311085774db85. Fixes #4904. * Revert "Prevent an NRE in LNURL" This reverts commit 0b241d61ab45b79297211e04da0e05c2cb10dada. * Fix NRE --------- Co-authored-by: nicolas.dorier --- BTCPayServer/Controllers/UILNURLController.cs | 4 +++- BTCPayServer/Payments/IPaymentMethodHandler.cs | 5 ----- BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs | 5 ++--- BTCPayServer/Services/Invoices/PaymentService.cs | 2 ++ 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/BTCPayServer/Controllers/UILNURLController.cs b/BTCPayServer/Controllers/UILNURLController.cs index d72444f27..13051a9d3 100644 --- a/BTCPayServer/Controllers/UILNURLController.cs +++ b/BTCPayServer/Controllers/UILNURLController.cs @@ -468,6 +468,8 @@ namespace BTCPayServer lnUrlMetadata ??= new Dictionary(); var pm = i.GetPaymentMethod(pmi); + if (pm is null) + return null; var paymentMethodDetails = (LNURLPayPaymentMethodDetails)pm.GetPaymentMethodDetails(); bool updatePaymentMethodDetails = false; if (lnUrlMetadata?.TryGetValue("text/identifier", out var lnAddress) is true && lnAddress is not null) @@ -494,7 +496,7 @@ namespace BTCPayServer lnurlRequest.Metadata = JsonConvert.SerializeObject(lnUrlMetadata.Select(kv => new[] { kv.Key, kv.Value })); if (i.Type != InvoiceType.TopUp) { - lnurlRequest.MinSendable = new LightMoney(i.GetPaymentMethod(pmi).Calculate().Due.ToDecimal(MoneyUnit.Satoshi), LightMoneyUnit.Satoshi); + lnurlRequest.MinSendable = new LightMoney(pm.Calculate().Due.ToDecimal(MoneyUnit.Satoshi), LightMoneyUnit.Satoshi); if (!allowOverpay) lnurlRequest.MaxSendable = lnurlRequest.MinSendable; } diff --git a/BTCPayServer/Payments/IPaymentMethodHandler.cs b/BTCPayServer/Payments/IPaymentMethodHandler.cs index da5be36d8..2d2de3503 100644 --- a/BTCPayServer/Payments/IPaymentMethodHandler.cs +++ b/BTCPayServer/Payments/IPaymentMethodHandler.cs @@ -90,11 +90,6 @@ namespace BTCPayServer.Payments }; } - public PaymentMethod GetPaymentMethodInInvoice(InvoiceEntity invoice, PaymentMethodId paymentMethodId) - { - return invoice.GetPaymentMethod(paymentMethodId); - } - public virtual object PreparePayment(TSupportedPaymentMethod supportedPaymentMethod, StoreData store, BTCPayNetworkBase network) { diff --git a/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs b/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs index 30d4ce98d..51b45724d 100644 --- a/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs +++ b/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs @@ -299,9 +299,8 @@ namespace BTCPayServer.Payments.PayJoin if (walletReceiveMatch is null && invoice is not null) { var paymentMethod = invoice.GetPaymentMethod(paymentMethodId); - var paymentDetails = - paymentMethod.GetPaymentMethodDetails() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod; - if (paymentDetails is null || !paymentDetails.PayjoinEnabled) + var paymentDetails = paymentMethod?.GetPaymentMethodDetails() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod; + if (paymentMethod is null || paymentDetails is null || !paymentDetails.PayjoinEnabled) continue; due = paymentMethod.Calculate().TotalDue - output.Value; if (due > Money.Zero) diff --git a/BTCPayServer/Services/Invoices/PaymentService.cs b/BTCPayServer/Services/Invoices/PaymentService.cs index 75493a167..440367f2d 100644 --- a/BTCPayServer/Services/Invoices/PaymentService.cs +++ b/BTCPayServer/Services/Invoices/PaymentService.cs @@ -44,6 +44,8 @@ namespace BTCPayServer.Services.Invoices return null; InvoiceEntity invoiceEntity = invoice.GetBlob(_btcPayNetworkProvider); PaymentMethod paymentMethod = invoiceEntity.GetPaymentMethod(new PaymentMethodId(network.CryptoCode, paymentData.GetPaymentType())); + if (paymentMethod is null) + return null; IPaymentMethodDetails paymentMethodDetails = paymentMethod.GetPaymentMethodDetails(); PaymentEntity entity = new PaymentEntity {