From 2b2e12b290ed26602d67c46c63311e74116b7b30 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 20 Mar 2018 11:59:43 +0900 Subject: [PATCH] Abstract ChargeClient to prepare for support of other lightning implementation --- BTCPayServer.Tests/ChargeTester.cs | 1 + BTCPayServer.Tests/LightningDTester.cs | 1 - BTCPayServer.Tests/ServerTester.cs | 5 +- BTCPayServer/BTCPayNetwork.cs | 1 - BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs | 5 +- .../BTCPayNetworkProvider.Litecoin.cs | 4 +- .../StoresController.LightningLike.cs | 2 +- BTCPayServer/Hosting/BTCPayServerServices.cs | 2 +- .../StoreViewModels/LightningNodeViewModel.cs | 1 - .../{RPC => }/CLightningRPCClient.cs | 3 +- .../CLightning/{RPC => }/NodeInfo.cs | 2 +- .../CLightning/{RPC => }/PeerInfo.cs | 2 +- .../{CLightning => Charge}/ChargeClient.cs | 47 ++++++++++++++++++- .../{CLightning => Charge}/ChargeSession.cs | 16 +++++-- .../CreateInvoiceRequest.cs | 2 +- .../CreateInvoiceResponse.cs | 2 +- .../{CLightning => Charge}/GetInfoResponse.cs | 2 +- .../Lightning/ILightningInvoiceClient.cs | 40 ++++++++++++++++ .../Lightning/LightningLikePaymentHandler.cs | 31 +++++------- ...ChargeListener.cs => LightningListener.cs} | 18 +++---- 20 files changed, 133 insertions(+), 54 deletions(-) rename BTCPayServer/Payments/Lightning/CLightning/{RPC => }/CLightningRPCClient.cs (97%) rename BTCPayServer/Payments/Lightning/CLightning/{RPC => }/NodeInfo.cs (91%) rename BTCPayServer/Payments/Lightning/CLightning/{RPC => }/PeerInfo.cs (97%) rename BTCPayServer/Payments/Lightning/{CLightning => Charge}/ChargeClient.cs (73%) rename BTCPayServer/Payments/Lightning/{CLightning => Charge}/ChargeSession.cs (90%) rename BTCPayServer/Payments/Lightning/{CLightning => Charge}/CreateInvoiceRequest.cs (82%) rename BTCPayServer/Payments/Lightning/{CLightning => Charge}/CreateInvoiceResponse.cs (81%) rename BTCPayServer/Payments/Lightning/{CLightning => Charge}/GetInfoResponse.cs (92%) create mode 100644 BTCPayServer/Payments/Lightning/ILightningInvoiceClient.cs rename BTCPayServer/Payments/Lightning/{ChargeListener.cs => LightningListener.cs} (94%) diff --git a/BTCPayServer.Tests/ChargeTester.cs b/BTCPayServer.Tests/ChargeTester.cs index 5efe0daf9..4eb5479bf 100644 --- a/BTCPayServer.Tests/ChargeTester.cs +++ b/BTCPayServer.Tests/ChargeTester.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using BTCPayServer.Payments.Lightning.Charge; using BTCPayServer.Payments.Lightning.CLightning; using NBitcoin; diff --git a/BTCPayServer.Tests/LightningDTester.cs b/BTCPayServer.Tests/LightningDTester.cs index d014310c1..49b0c75f3 100644 --- a/BTCPayServer.Tests/LightningDTester.cs +++ b/BTCPayServer.Tests/LightningDTester.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using BTCPayServer.Payments.Lightning.CLightning; -using BTCPayServer.Payments.Lightning.CLightning.RPC; using NBitcoin; namespace BTCPayServer.Tests diff --git a/BTCPayServer.Tests/ServerTester.cs b/BTCPayServer.Tests/ServerTester.cs index e001cddab..4fe6d989c 100644 --- a/BTCPayServer.Tests/ServerTester.cs +++ b/BTCPayServer.Tests/ServerTester.cs @@ -18,7 +18,8 @@ using System.Text; using System.Threading.Tasks; using System.Threading; using System.Globalization; -using BTCPayServer.Payments.Lightning.CLightning.RPC; +using BTCPayServer.Payments.Lightning.CLightning; +using BTCPayServer.Payments.Lightning.Charge; namespace BTCPayServer.Tests { @@ -117,7 +118,7 @@ namespace BTCPayServer.Tests } } - private async Task WaitLNSynched() + private async Task WaitLNSynched() { while (true) { diff --git a/BTCPayServer/BTCPayNetwork.cs b/BTCPayServer/BTCPayNetwork.cs index 5cf350259..7bc0001ef 100644 --- a/BTCPayServer/BTCPayNetwork.cs +++ b/BTCPayServer/BTCPayNetwork.cs @@ -68,7 +68,6 @@ namespace BTCPayServer public BTCPayDefaultSettings DefaultSettings { get; set; } public KeyPath CoinType { get; internal set; } public int MaxTrackedConfirmation { get; internal set; } = 6; - public string CLightningNetworkName { get; internal set; } public override string ToString() { diff --git a/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs b/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs index 098325d7a..ec3f4910c 100644 --- a/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs +++ b/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs @@ -28,10 +28,7 @@ namespace BTCPayServer CryptoImagePath = "imlegacy/bitcoin-symbol.svg", LightningImagePath = "imlegacy/btc-lightning.svg", DefaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NBXplorerNetworkProvider.ChainType), - CoinType = NBXplorerNetworkProvider.ChainType == ChainType.Main ? new KeyPath("0'") : new KeyPath("1'"), - CLightningNetworkName = ChainType == ChainType.Main ? "bitcoin" : - ChainType == ChainType.Test ? "testnet" : - ChainType == ChainType.Regtest ? "regtest" : null + CoinType = NBXplorerNetworkProvider.ChainType == ChainType.Main ? new KeyPath("0'") : new KeyPath("1'") }); } } diff --git a/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs b/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs index 49ac2de30..fd9a4e54b 100644 --- a/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs +++ b/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs @@ -27,9 +27,7 @@ namespace BTCPayServer CryptoImagePath = "imlegacy/litecoin-symbol.svg", LightningImagePath = "imlegacy/ltc-lightning.svg", DefaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NBXplorerNetworkProvider.ChainType), - CoinType = NBXplorerNetworkProvider.ChainType == ChainType.Main ? new KeyPath("2'") : new KeyPath("3'"), - CLightningNetworkName = ChainType == ChainType.Main ? "litecoin" : - ChainType == ChainType.Test ? "litecoin-testnet" : null + CoinType = NBXplorerNetworkProvider.ChainType == ChainType.Main ? new KeyPath("2'") : new KeyPath("3'") }); } } diff --git a/BTCPayServer/Controllers/StoresController.LightningLike.cs b/BTCPayServer/Controllers/StoresController.LightningLike.cs index 97c2a591c..e3d05862c 100644 --- a/BTCPayServer/Controllers/StoresController.LightningLike.cs +++ b/BTCPayServer/Controllers/StoresController.LightningLike.cs @@ -37,7 +37,7 @@ namespace BTCPayServer.Controllers var network = vm.CryptoCurrency == null ? null : _ExplorerProvider.GetNetwork(vm.CryptoCurrency); vm.SetCryptoCurrencies(_NetworkProvider, vm.CryptoCurrency); vm.InternalLightningNode = GetInternalLightningNodeIfAuthorized(); - if (network == null || network.CLightningNetworkName == null) + if (network == null) { ModelState.AddModelError(nameof(vm.CryptoCurrency), "Invalid network"); return View(vm); diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 3eecdefad..5e12ba98c 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -147,7 +147,7 @@ namespace BTCPayServer.Hosting services.AddSingleton(); services.AddSingleton, Payments.Lightning.LightningLikePaymentHandler>(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs b/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs index 8f2b4ca0f..1c0141bcc 100644 --- a/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs @@ -34,7 +34,6 @@ namespace BTCPayServer.Models.StoreViewModels public void SetCryptoCurrencies(BTCPayNetworkProvider networkProvider, string selectedScheme) { var choices = networkProvider.GetAll() - .Where(n => n.CLightningNetworkName != null) .Select(o => new Format() { Name = o.CryptoCode, Value = o.CryptoCode }).ToArray(); var chosen = choices.FirstOrDefault(f => f.Name == selectedScheme) ?? choices.FirstOrDefault(); CryptoCurrencies = new SelectList(choices, nameof(chosen.Value), nameof(chosen.Name), chosen); diff --git a/BTCPayServer/Payments/Lightning/CLightning/RPC/CLightningRPCClient.cs b/BTCPayServer/Payments/Lightning/CLightning/CLightningRPCClient.cs similarity index 97% rename from BTCPayServer/Payments/Lightning/CLightning/RPC/CLightningRPCClient.cs rename to BTCPayServer/Payments/Lightning/CLightning/CLightningRPCClient.cs index 361539820..eeeda6a1a 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/RPC/CLightningRPCClient.cs +++ b/BTCPayServer/Payments/Lightning/CLightning/CLightningRPCClient.cs @@ -6,12 +6,13 @@ using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; +using BTCPayServer.Payments.Lightning.Charge; using NBitcoin; using NBitcoin.RPC; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -namespace BTCPayServer.Payments.Lightning.CLightning.RPC +namespace BTCPayServer.Payments.Lightning.CLightning { public class CLightningRPCClient { diff --git a/BTCPayServer/Payments/Lightning/CLightning/RPC/NodeInfo.cs b/BTCPayServer/Payments/Lightning/CLightning/NodeInfo.cs similarity index 91% rename from BTCPayServer/Payments/Lightning/CLightning/RPC/NodeInfo.cs rename to BTCPayServer/Payments/Lightning/CLightning/NodeInfo.cs index 4f72eae99..87e5f4184 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/RPC/NodeInfo.cs +++ b/BTCPayServer/Payments/Lightning/CLightning/NodeInfo.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace BTCPayServer.Payments.Lightning.CLightning.RPC +namespace BTCPayServer.Payments.Lightning.CLightning { public class NodeInfo { diff --git a/BTCPayServer/Payments/Lightning/CLightning/RPC/PeerInfo.cs b/BTCPayServer/Payments/Lightning/CLightning/PeerInfo.cs similarity index 97% rename from BTCPayServer/Payments/Lightning/CLightning/RPC/PeerInfo.cs rename to BTCPayServer/Payments/Lightning/CLightning/PeerInfo.cs index 2c9789469..f053efc14 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/RPC/PeerInfo.cs +++ b/BTCPayServer/Payments/Lightning/CLightning/PeerInfo.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using NBitcoin; using Newtonsoft.Json; -namespace BTCPayServer.Payments.Lightning.CLightning.RPC +namespace BTCPayServer.Payments.Lightning.CLightning { public class ChannelInfo { diff --git a/BTCPayServer/Payments/Lightning/CLightning/ChargeClient.cs b/BTCPayServer/Payments/Lightning/Charge/ChargeClient.cs similarity index 73% rename from BTCPayServer/Payments/Lightning/CLightning/ChargeClient.cs rename to BTCPayServer/Payments/Lightning/Charge/ChargeClient.cs index fbae6b1c0..b29aa88f3 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/ChargeClient.cs +++ b/BTCPayServer/Payments/Lightning/Charge/ChargeClient.cs @@ -13,9 +13,9 @@ using NBitcoin; using NBXplorer; using Newtonsoft.Json; -namespace BTCPayServer.Payments.Lightning.CLightning +namespace BTCPayServer.Payments.Lightning.Charge { - public class ChargeClient + public class ChargeClient : ILightningInvoiceClient { private Uri _Uri; public Uri Uri @@ -126,5 +126,48 @@ namespace BTCPayServer.Payments.Lightning.CLightning uri += "/"; return new Uri(uri + partialUrl); } + + async Task ILightningInvoiceClient.GetInvoice(string invoiceId, CancellationToken cancellation) + { + var invoice = await GetInvoice(invoiceId, cancellation); + return ChargeClient.ToLightningInvoice(invoice); + } + + async Task ILightningInvoiceClient.Listen(CancellationToken cancellation) + { + return await Listen(cancellation); + } + + internal static LightningInvoice ToLightningInvoice(ChargeInvoice invoice) + { + return new LightningInvoice() + { + Id = invoice.Id, + Amount = invoice.MilliSatoshi, + BOLT11 = invoice.PaymentRequest, + PaidAt = invoice.PaidAt, + Status = invoice.Status + }; + } + + async Task ILightningInvoiceClient.CreateInvoice(LightMoney amount, TimeSpan expiry, CancellationToken cancellation) + { + var invoice = await CreateInvoiceAsync(new CreateInvoiceRequest() { Amont = amount, Expiry = expiry }); + return new LightningInvoice() { Id = invoice.Id, Amount = amount, BOLT11 = invoice.PayReq, Status = "unpaid" }; + } + + async Task ILightningInvoiceClient.GetInfo(CancellationToken cancellation) + { + var info = await GetInfoAsync(cancellation); + var address = info.Address.Select(a => a.Address).FirstOrDefault(); + var port = info.Port; + address = address ?? Uri.DnsSafeHost; + return new LightningNodeInformation() + { + P2PPort = port, + Address = address, + BlockHeight = info.BlockHeight + }; + } } } diff --git a/BTCPayServer/Payments/Lightning/CLightning/ChargeSession.cs b/BTCPayServer/Payments/Lightning/Charge/ChargeSession.cs similarity index 90% rename from BTCPayServer/Payments/Lightning/CLightning/ChargeSession.cs rename to BTCPayServer/Payments/Lightning/Charge/ChargeSession.cs index fdf96f7bd..b80328df1 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/ChargeSession.cs +++ b/BTCPayServer/Payments/Lightning/Charge/ChargeSession.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using NBXplorer; using Newtonsoft.Json; -namespace BTCPayServer.Payments.Lightning.CLightning +namespace BTCPayServer.Payments.Lightning.Charge { public class ChargeInvoice { @@ -28,7 +28,7 @@ namespace BTCPayServer.Payments.Lightning.CLightning [JsonProperty("payreq")] public string PaymentRequest { get; set; } } - public class ChargeSession : IDisposable + public class ChargeSession : ILightningListenInvoiceSession { private ClientWebSocket socket; @@ -42,7 +42,7 @@ namespace BTCPayServer.Payments.Lightning.CLightning } ArraySegment _Buffer; - public async Task NextEvent(CancellationToken cancellation = default(CancellationToken)) + public async Task WaitInvoice(CancellationToken cancellation = default(CancellationToken)) { var buffer = _Buffer; var array = _Buffer.Array; @@ -120,5 +120,15 @@ namespace BTCPayServer.Payments.Lightning.CLightning { await this.socket.CloseSocket(); } + + async Task ILightningListenInvoiceSession.WaitInvoice(CancellationToken token) + { + return ChargeClient.ToLightningInvoice(await WaitInvoice(token)); + } + + void IDisposable.Dispose() + { + Dispose(); + } } } diff --git a/BTCPayServer/Payments/Lightning/CLightning/CreateInvoiceRequest.cs b/BTCPayServer/Payments/Lightning/Charge/CreateInvoiceRequest.cs similarity index 82% rename from BTCPayServer/Payments/Lightning/CLightning/CreateInvoiceRequest.cs rename to BTCPayServer/Payments/Lightning/Charge/CreateInvoiceRequest.cs index 7d8b1eec8..9878da96a 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/CreateInvoiceRequest.cs +++ b/BTCPayServer/Payments/Lightning/Charge/CreateInvoiceRequest.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace BTCPayServer.Payments.Lightning.CLightning +namespace BTCPayServer.Payments.Lightning.Charge { public class CreateInvoiceRequest { diff --git a/BTCPayServer/Payments/Lightning/CLightning/CreateInvoiceResponse.cs b/BTCPayServer/Payments/Lightning/Charge/CreateInvoiceResponse.cs similarity index 81% rename from BTCPayServer/Payments/Lightning/CLightning/CreateInvoiceResponse.cs rename to BTCPayServer/Payments/Lightning/Charge/CreateInvoiceResponse.cs index 2b96fff8f..9c7271754 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/CreateInvoiceResponse.cs +++ b/BTCPayServer/Payments/Lightning/Charge/CreateInvoiceResponse.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace BTCPayServer.Payments.Lightning.CLightning +namespace BTCPayServer.Payments.Lightning.Charge { public class CreateInvoiceResponse { diff --git a/BTCPayServer/Payments/Lightning/CLightning/GetInfoResponse.cs b/BTCPayServer/Payments/Lightning/Charge/GetInfoResponse.cs similarity index 92% rename from BTCPayServer/Payments/Lightning/CLightning/GetInfoResponse.cs rename to BTCPayServer/Payments/Lightning/Charge/GetInfoResponse.cs index a02a236ec..2fd584d78 100644 --- a/BTCPayServer/Payments/Lightning/CLightning/GetInfoResponse.cs +++ b/BTCPayServer/Payments/Lightning/Charge/GetInfoResponse.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace BTCPayServer.Payments.Lightning.CLightning +namespace BTCPayServer.Payments.Lightning.Charge { //[{"type":"ipv4","address":"52.166.90.122","port":9735}] public class GetInfoResponse diff --git a/BTCPayServer/Payments/Lightning/ILightningInvoiceClient.cs b/BTCPayServer/Payments/Lightning/ILightningInvoiceClient.cs new file mode 100644 index 000000000..5111b901e --- /dev/null +++ b/BTCPayServer/Payments/Lightning/ILightningInvoiceClient.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Payments.Lightning.Charge; + +namespace BTCPayServer.Payments.Lightning +{ + public class LightningInvoice + { + public string Id { get; set; } + public string Status { get; set; } + public string BOLT11 { get; set; } + public DateTimeOffset? PaidAt + { + get; set; + } + public LightMoney Amount { get; set; } + } + + public class LightningNodeInformation + { + public string Address { get; internal set; } + public int P2PPort { get; internal set; } + public int BlockHeight { get; set; } + } + public interface ILightningInvoiceClient + { + Task GetInvoice(string invoiceId, CancellationToken cancellation = default(CancellationToken)); + Task CreateInvoice(LightMoney amount, TimeSpan expiry, CancellationToken cancellation = default(CancellationToken)); + Task Listen(CancellationToken cancellation = default(CancellationToken)); + Task GetInfo(CancellationToken cancellation = default(CancellationToken)); + } + + public interface ILightningListenInvoiceSession : IDisposable + { + Task WaitInvoice(CancellationToken token); + } +} diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index e8b852670..24a870161 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -6,6 +6,7 @@ using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; using BTCPayServer.HostedServices; +using BTCPayServer.Payments.Lightning.Charge; using BTCPayServer.Payments.Lightning.CLightning; using BTCPayServer.Services.Invoices; @@ -24,14 +25,12 @@ namespace BTCPayServer.Payments.Lightning var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8); var client = GetClient(supportedPaymentMethod, network); var expiry = invoice.ExpirationTime - DateTimeOffset.UtcNow; - var lightningInvoice = await client.CreateInvoiceAsync(new CreateInvoiceRequest() - { - Amont = new LightMoney(due, LightMoneyUnit.BTC), - Expiry = expiry < TimeSpan.Zero ? TimeSpan.FromSeconds(1) : expiry - }); + if (expiry < TimeSpan.Zero) + expiry = TimeSpan.FromSeconds(1); + var lightningInvoice = await client.CreateInvoice(new LightMoney(due, LightMoneyUnit.BTC), expiry); return new LightningLikePaymentMethodDetails() { - BOLT11 = lightningInvoice.PayReq, + BOLT11 = lightningInvoice.BOLT11, InvoiceId = lightningInvoice.Id }; } @@ -54,23 +53,15 @@ namespace BTCPayServer.Payments.Lightning var cts = new CancellationTokenSource(5000); var client = GetClient(supportedPaymentMethod, network); - GetInfoResponse info = null; + LightningNodeInformation info = null; try { - info = await client.GetInfoAsync(cts.Token); + info = await client.GetInfo(cts.Token); } catch (Exception ex) { - throw new Exception($"Error while connecting to the lightning charge {client.Uri} ({ex.Message})"); - } - var address = info.Address.Select(a=>a.Address).FirstOrDefault(); - var port = info.Port; - address = address ?? client.Uri.DnsSafeHost; - - if (info.Network != network.CLightningNetworkName) - { - throw new Exception($"Lightning node network {info.Network}, but expected is {network.CLightningNetworkName}"); + throw new Exception($"Error while connecting to the API ({ex.Message})"); } var blocksGap = Math.Abs(info.BlockHeight - summary.Status.ChainHeight); @@ -81,15 +72,15 @@ namespace BTCPayServer.Payments.Lightning try { - await TestConnection(address, port, cts.Token); + await TestConnection(info.Address, info.P2PPort, cts.Token); } catch (Exception ex) { - throw new Exception($"Error while connecting to the lightning node via {address}:{port} ({ex.Message})"); + throw new Exception($"Error while connecting to the lightning node via {info.Address}:{info.P2PPort} ({ex.Message})"); } } - private static ChargeClient GetClient(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) + private static ILightningInvoiceClient GetClient(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) { return new ChargeClient(supportedPaymentMethod.GetLightningChargeUrl(true), network.NBitcoinNetwork); } diff --git a/BTCPayServer/Payments/Lightning/ChargeListener.cs b/BTCPayServer/Payments/Lightning/LightningListener.cs similarity index 94% rename from BTCPayServer/Payments/Lightning/ChargeListener.cs rename to BTCPayServer/Payments/Lightning/LightningListener.cs index e418a4633..231c5e078 100644 --- a/BTCPayServer/Payments/Lightning/ChargeListener.cs +++ b/BTCPayServer/Payments/Lightning/LightningListener.cs @@ -6,14 +6,14 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Events; using BTCPayServer.Logging; -using BTCPayServer.Payments.Lightning.CLightning; using BTCPayServer.Services.Invoices; using Microsoft.Extensions.Hosting; using NBXplorer; +using BTCPayServer.Payments.Lightning.Charge; namespace BTCPayServer.Payments.Lightning { - public class ChargeListener : IHostedService + public class LightningListener : IHostedService { class ListenedInvoice { @@ -28,7 +28,7 @@ namespace BTCPayServer.Payments.Lightning EventAggregator _Aggregator; InvoiceRepository _InvoiceRepository; BTCPayNetworkProvider _NetworkProvider; - public ChargeListener(EventAggregator aggregator, + public LightningListener(EventAggregator aggregator, InvoiceRepository invoiceRepository, BTCPayNetworkProvider networkProvider) { @@ -128,13 +128,13 @@ namespace BTCPayServer.Payments.Lightning var session = await charge.Listen(_Cts.Token); while (true) { - var notification = await session.NextEvent(_Cts.Token); + var notification = await session.WaitInvoice(_Cts.Token); ListenedInvoice listenedInvoice = GetListenedInvoice(notification.Id); if (listenedInvoice == null) continue; if (notification.Id == listenedInvoice.PaymentMethodDetails.InvoiceId && - notification.PaymentRequest == listenedInvoice.PaymentMethodDetails.BOLT11) + notification.BOLT11 == listenedInvoice.PaymentMethodDetails.BOLT11) { if (notification.Status == "paid" && notification.PaidAt.HasValue) { @@ -161,18 +161,18 @@ namespace BTCPayServer.Payments.Lightning Logs.PayServer.LogInformation($"{supportedPaymentMethod.CryptoCode} (Lightning): Stop listening {supportedPaymentMethod.GetLightningChargeUrl(false)}"); } - private async Task AddPayment(BTCPayNetwork network, ChargeInvoice notification, ListenedInvoice listenedInvoice) + private async Task AddPayment(BTCPayNetwork network, LightningInvoice notification, ListenedInvoice listenedInvoice) { var payment = await _InvoiceRepository.AddPayment(listenedInvoice.InvoiceId, notification.PaidAt.Value, new LightningLikePaymentData() { - BOLT11 = notification.PaymentRequest, - Amount = notification.MilliSatoshi + BOLT11 = notification.BOLT11, + Amount = notification.Amount }, network.CryptoCode, accounted: true); if(payment != null) _Aggregator.Publish(new InvoiceEvent(listenedInvoice.InvoiceId, 1002, "invoice_receivedPayment")); } - private static ChargeClient GetChargeClient(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) + private static ILightningInvoiceClient GetChargeClient(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) { return new ChargeClient(supportedPaymentMethod.GetLightningChargeUrl(true), network.NBitcoinNetwork); }