Skip to content

Commit

Permalink
Hide LN Balance when using internal node and not server admin (#5639)
Browse files Browse the repository at this point in the history
* Hide LN Balance when using internal node and not server admin

* Minor updates

---------

Co-authored-by: Dennis Reimann <mail@dennisreimann.de>
  • Loading branch information
Kukks and dennisreimann committed Jan 6, 2024
1 parent 78882dc commit e90414b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
5 changes: 4 additions & 1 deletion BTCPayServer/Components/StoreLightningBalance/Default.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@model BTCPayServer.Components.StoreLightningBalance.StoreLightningBalanceViewModel

@if(!Model.InitialRendering && Model.Balance == null)
{
return;
}
<div id="StoreLightningBalance-@Model.Store.Id" class="widget store-lightning-balance">
<div class="d-flex gap-3 align-items-center justify-content-between mb-2">
<h6>Lightning Balance</h6>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
using BTCPayServer.Configuration;
using BTCPayServer.Data;
using BTCPayServer.Lightning;
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Payments;
using BTCPayServer.Payments.Lightning;
using BTCPayServer.Security;
using BTCPayServer.Services;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

Expand All @@ -26,6 +29,7 @@ public class StoreLightningBalance : ViewComponent
private readonly LightningClientFactoryService _lightningClientFactory;
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
private readonly IOptions<ExternalServicesOptions> _externalServiceOptions;
private readonly IAuthorizationService _authorizationService;

public StoreLightningBalance(
StoreRepository storeRepo,
Expand All @@ -34,13 +38,15 @@ public class StoreLightningBalance : ViewComponent
BTCPayServerOptions btcpayServerOptions,
LightningClientFactoryService lightningClientFactory,
IOptions<LightningNetworkOptions> lightningNetworkOptions,
IOptions<ExternalServicesOptions> externalServiceOptions)
IOptions<ExternalServicesOptions> externalServiceOptions,
IAuthorizationService authorizationService)
{
_storeRepo = storeRepo;
_currencies = currencies;
_networkProvider = networkProvider;
_btcpayServerOptions = btcpayServerOptions;
_externalServiceOptions = externalServiceOptions;
_authorizationService = authorizationService;
_lightningClientFactory = lightningClientFactory;
_lightningNetworkOptions = lightningNetworkOptions;
}
Expand All @@ -54,13 +60,19 @@ public async Task<IViewComponentResult> InvokeAsync(StoreLightningBalanceViewMod

vm.DefaultCurrency = vm.Store.GetStoreBlob().DefaultCurrency;
vm.CurrencyData = _currencies.GetCurrencyData(vm.DefaultCurrency, true);

if (vm.InitialRendering)
return View(vm);


try
{
var lightningClient = GetLightningClient(vm.Store, vm.CryptoCode);
var lightningClient = await GetLightningClient(vm.Store, vm.CryptoCode);
if (lightningClient == null)
{
vm.InitialRendering = false;
return View(vm);
}

if (vm.InitialRendering)
return View(vm);

var balance = await lightningClient.GetBalance();
vm.Balance = balance;
vm.TotalOnchain = balance.OnchainBalance != null
Expand All @@ -72,7 +84,8 @@ public async Task<IViewComponentResult> InvokeAsync(StoreLightningBalanceViewMod
(balance.OffchainBalance.Closing ?? 0)
: null;
}
catch (NotSupportedException)

catch (Exception ex) when (ex is NotImplementedException or NotSupportedException)
{
// not all implementations support balance fetching
vm.ProblemDescription = "Your node does not support balance fetching.";
Expand All @@ -85,7 +98,7 @@ public async Task<IViewComponentResult> InvokeAsync(StoreLightningBalanceViewMod
return View(vm);
}

private ILightningClient GetLightningClient(StoreData store, string cryptoCode)
private async Task<ILightningClient> GetLightningClient(StoreData store, string cryptoCode )
{
var network = _networkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
var id = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike);
Expand All @@ -101,7 +114,9 @@ private ILightningClient GetLightningClient(StoreData store, string cryptoCode)
}
if (existing.IsInternalNode && _lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(cryptoCode, out var internalLightningNode))
{
return internalLightningNode;
var result = await _authorizationService.AuthorizeAsync(HttpContext.User, null,
new PolicyRequirement(Policies.CanUseInternalLightningNode));
return result.Succeeded ? internalLightningNode : null;
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion BTCPayServer/Views/UIStores/Dashboard.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@using BTCPayServer.Components.AppSales
@using BTCPayServer.Components.AppTopItems
@using BTCPayServer.Services.Apps
@using BTCPayServer.Client
@model StoreDashboardViewModel
@{
ViewData.SetActivePage(StoreNavPages.Dashboard, Model.StoreName, Model.StoreId);
Expand Down Expand Up @@ -82,7 +83,7 @@
@if (Model.LightningEnabled)
{
<vc:store-lightning-balance vm="@(new StoreLightningBalanceViewModel { Store = store, CryptoCode = Model.CryptoCode, InitialRendering = true })"/>
<vc:store-lightning-services vm="@(new StoreLightningServicesViewModel { Store = store, CryptoCode = Model.CryptoCode })"/>
<vc:store-lightning-services vm="@(new StoreLightningServicesViewModel { Store = store, CryptoCode = Model.CryptoCode })" permission="@Policies.CanModifyServerSettings"/>
}
@if (Model.WalletEnabled)
{
Expand Down

0 comments on commit e90414b

Please sign in to comment.