diff --git a/BTCPayServer/Controllers/PublicLightningNodeInfoController.cs b/BTCPayServer/Controllers/PublicLightningNodeInfoController.cs new file mode 100644 index 000000000..a0f1257e6 --- /dev/null +++ b/BTCPayServer/Controllers/PublicLightningNodeInfoController.cs @@ -0,0 +1,87 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Data; +using BTCPayServer.Lightning; +using BTCPayServer.Models.StoreViewModels; +using BTCPayServer.Payments; +using BTCPayServer.Payments.Lightning; +using BTCPayServer.Services.Stores; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; + +namespace BTCPayServer.Controllers +{ + + [Route("embed/{storeId}/{cryptoCode}/ln")] + [AllowAnonymous] + public class PublicLightningNodeInfoController : Controller + { + private readonly BTCPayNetworkProvider _BtcPayNetworkProvider; + private readonly LightningLikePaymentHandler _LightningLikePaymentHandler; + private readonly StoreRepository _StoreRepository; + + public PublicLightningNodeInfoController(BTCPayNetworkProvider btcPayNetworkProvider, + LightningLikePaymentHandler lightningLikePaymentHandler, StoreRepository storeRepository) + { + _BtcPayNetworkProvider = btcPayNetworkProvider; + _LightningLikePaymentHandler = lightningLikePaymentHandler; + _StoreRepository = storeRepository; + } + + [HttpGet] + public async Task ShowLightningNodeInfo(string storeId, string cryptoCode) + { + var store = await _StoreRepository.FindStore(storeId); + if (store == null) + return NotFound(); + + try + { + var paymentMethodDetails = GetExistingLightningSupportedPaymentMethod(cryptoCode, store); + var network = _BtcPayNetworkProvider.GetNetwork(cryptoCode); + var nodeInfo = + await _LightningLikePaymentHandler.GetNodeInfo(paymentMethodDetails, + network); + + return View(new ShowLightningNodeInfoViewModel() + { + Available = true, + NodeInfo = nodeInfo.ToString(), + CryptoCode = cryptoCode, + CryptoImage = GetImage(paymentMethodDetails.PaymentId, network) + }); + } + catch (Exception) + { + return View(new ShowLightningNodeInfoViewModel() {Available = false, CryptoCode = cryptoCode}); + } + } + + private LightningSupportedPaymentMethod GetExistingLightningSupportedPaymentMethod(string cryptoCode, StoreData store) + { + var id = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike); + var existing = store.GetSupportedPaymentMethods(_BtcPayNetworkProvider) + .OfType() + .FirstOrDefault(d => d.PaymentId == id); + return existing; + } + + + private string GetImage(PaymentMethodId paymentMethodId, BTCPayNetwork network) + { + var res = paymentMethodId.PaymentType == PaymentTypes.BTCLike + ? Url.Content(network.CryptoImagePath) + : Url.Content(network.LightningImagePath); + return "/" + res; + } + } + + public class ShowLightningNodeInfoViewModel + { + public string NodeInfo { get; set; } + public bool Available { get; set; } + public string CryptoCode { get; set; } + public string CryptoImage { get; set; } + } +} diff --git a/BTCPayServer/Controllers/StoresController.LightningLike.cs b/BTCPayServer/Controllers/StoresController.LightningLike.cs index 60c05d028..9becff7e1 100644 --- a/BTCPayServer/Controllers/StoresController.LightningLike.cs +++ b/BTCPayServer/Controllers/StoresController.LightningLike.cs @@ -27,7 +27,8 @@ namespace BTCPayServer.Controllers LightningNodeViewModel vm = new LightningNodeViewModel { CryptoCode = cryptoCode, - InternalLightningNode = GetInternalLighningNode(cryptoCode)?.ToString() + InternalLightningNode = GetInternalLighningNode(cryptoCode)?.ToString(), + StoreId = storeId }; SetExistingValues(store, vm); return View(vm); @@ -154,7 +155,7 @@ namespace BTCPayServer.Controllers var handler = (LightningLikePaymentHandler)_ServiceProvider.GetRequiredService>(); try { - var info = await handler.Test(paymentMethod, network); + var info = await handler.GetNodeInfo(paymentMethod, network); if (!vm.SkipPortTest) { using (CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(20))) diff --git a/BTCPayServer/Controllers/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index 23aa50234..e9e314e7d 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -10,7 +10,9 @@ using BTCPayServer.Data; using BTCPayServer.Models; using BTCPayServer.Models.AppViewModels; using BTCPayServer.Models.StoreViewModels; +using BTCPayServer.Payments; using BTCPayServer.Payments.Changelly; +using BTCPayServer.Payments.Lightning; using BTCPayServer.Rating; using BTCPayServer.Security; using BTCPayServer.Services; diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index d8be48b88..a72317a54 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -39,6 +39,7 @@ using BTCPayServer.HostedServices; using Meziantou.AspNetCore.BundleTagHelpers; using System.Security.Claims; using BTCPayServer.Payments.Changelly; +using BTCPayServer.Payments.Lightning; using BTCPayServer.Security; using Microsoft.AspNetCore.Mvc.ModelBinding; using NBXplorer.DerivationStrategy; @@ -132,6 +133,7 @@ namespace BTCPayServer.Hosting services.AddSingleton(); services.AddSingleton, Payments.Lightning.LightningLikePaymentHandler>(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs b/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs index 14c380ca4..899b69671 100644 --- a/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/LightningNodeViewModel.cs @@ -25,5 +25,7 @@ namespace BTCPayServer.Models.StoreViewModels public string InternalLightningNode { get; internal set; } public bool SkipPortTest { get; set; } public bool Enabled { get; set; } = true; + + public string StoreId { get; set; } } } diff --git a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs index 71cefc096..e11be11cc 100644 --- a/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs +++ b/BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs @@ -25,7 +25,7 @@ namespace BTCPayServer.Payments.Lightning public override async Task CreatePaymentMethodDetails(LightningSupportedPaymentMethod supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, BTCPayNetwork network, object preparePaymentObject) { var storeBlob = store.GetStoreBlob(); - var test = Test(supportedPaymentMethod, network); + var test = GetNodeInfo(supportedPaymentMethod, network); var invoice = paymentMethod.ParentEntity; var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8); var client = supportedPaymentMethod.CreateClient(network); @@ -63,7 +63,7 @@ namespace BTCPayServer.Payments.Lightning }; } - public async Task Test(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) + public async Task GetNodeInfo(LightningSupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network) { if (!_Dashboard.IsFullySynched(network.CryptoCode, out var summary)) throw new PaymentMethodUnavailableException($"Full node not available"); @@ -78,7 +78,7 @@ namespace BTCPayServer.Payments.Lightning } catch (OperationCanceledException) when (cts.IsCancellationRequested) { - throw new PaymentMethodUnavailableException($"The lightning node did not replied in a timely maner"); + throw new PaymentMethodUnavailableException($"The lightning node did not replied in a timely manner"); } catch (Exception ex) { diff --git a/BTCPayServer/Views/PublicLightningNodeInfo/ShowLightningNodeInfo.cshtml b/BTCPayServer/Views/PublicLightningNodeInfo/ShowLightningNodeInfo.cshtml new file mode 100644 index 000000000..0c43dda00 --- /dev/null +++ b/BTCPayServer/Views/PublicLightningNodeInfo/ShowLightningNodeInfo.cshtml @@ -0,0 +1,142 @@ +@addTagHelper *, Meziantou.AspNetCore.BundleTagHelpers +@inject BTCPayServer.HostedServices.CssThemeManager themeManager + +@model BTCPayServer.Controllers.ShowLightningNodeInfoViewModel +@{ + Layout = null; +} + + + + + @Model.CryptoCode LN Node Info + + + + + + + + + + + + + + + + + +
+
+
+
+
+

+ {{srvModel.cryptoCode}} Lightning Node + - {{srvModel.available? "Online" : "Unavailable"}} + + + +

+
+
+ + + +
+
+ +
+ +
+
+
+
+
+
+
+ + diff --git a/BTCPayServer/Views/Stores/AddLightningNode.cshtml b/BTCPayServer/Views/Stores/AddLightningNode.cshtml index 3437fd2c0..9fc16515d 100644 --- a/BTCPayServer/Views/Stores/AddLightningNode.cshtml +++ b/BTCPayServer/Views/Stores/AddLightningNode.cshtml @@ -85,6 +85,15 @@ + + Open Public Node Info Page + diff --git a/BTCPayServer/bundleconfig.json b/BTCPayServer/bundleconfig.json index bda15ece5..e58b6c95d 100644 --- a/BTCPayServer/bundleconfig.json +++ b/BTCPayServer/bundleconfig.json @@ -53,6 +53,16 @@ "wwwroot/checkout/**/*.js" ] }, + { + "outputFileName": "wwwroot/bundles/lightning-node-info-bundle.min.js", + "inputFiles": [ + "wwwroot/vendor/clipboard.js/clipboard.js", + "wwwroot/vendor/jquery/jquery.js", + "wwwroot/vendor/vuejs/vue.min.js", + "wwwroot/vendor/vuejs/vue-qrcode.js", + "wwwroot/vendor/vue-toasted/vue-toasted.min.js" + ] + }, { "outputFileName": "wwwroot/bundles/cart-bundle.min.js", "inputFiles": [