Skip to content

Commit

Permalink
Initial UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Jun 25, 2024
1 parent e378aff commit 18eb3c9
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugin/Persistence/StrikeDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public StrikeDbContextFactory(IOptions<DatabaseOptions> options)
{
}

public override StrikeDbContext CreateContext(Action<NpgsqlDbContextOptionsBuilder>? npgsqlOptionsAction = null)
public override StrikeDbContext CreateContext()
{
var builder = new DbContextOptionsBuilder<StrikeDbContext>();
ConfigureBuilder(builder);
Expand Down
40 changes: 40 additions & 0 deletions plugin/Persistence/StrikeDbContextMigrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace BTCPayServer.Plugins.Strike.Persistence;

internal class StrikeDbContextMigrator : IHostedService
{
private readonly StrikeDbContextFactory _dbContextFactory;
private readonly ILogger<StrikeDbContextMigrator> _logger;

public StrikeDbContextMigrator(StrikeDbContextFactory dbContextFactory, ILogger<StrikeDbContextMigrator> logger)
{
_dbContextFactory = dbContextFactory;
_logger = logger;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
_logger.LogDebug("Migrating Strike database");
await using var ctx = _dbContextFactory.CreateContext();
await using var dbContext = _dbContextFactory.CreateContext();
await ctx.Database.MigrateAsync(cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "Error while migrating Strike database: {error}", e.Message);
}
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
158 changes: 158 additions & 0 deletions plugin/StrikeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System.Linq;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Client;
using BTCPayServer.Data;
using BTCPayServer.Payments;
using BTCPayServer.Payments.Lightning;
using BTCPayServer.Services.Stores;
using BTCPayServer.Services.Wallets;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BTCPayServer.Plugins.Strike;

[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
[Route("plugins/{storeId}/strike")]
public class StrikeController : Controller
{
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
private readonly BTCPayWalletProvider _btcWalletProvider;
private readonly StoreRepository _storeRepository;

public StrikeController(BTCPayNetworkProvider btcPayNetworkProvider,
BTCPayWalletProvider btcWalletProvider, StoreRepository storeRepository)
{
_btcPayNetworkProvider = btcPayNetworkProvider;
_btcWalletProvider = btcWalletProvider;
_storeRepository = storeRepository;
}


[HttpGet("")]
public IActionResult Index(string storeId)
{
return RedirectToAction(nameof(Dashboard), new {storeId});
}

[HttpGet("dashboard")]
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public IActionResult Dashboard(string storeId)
{
return View((object)storeId);
}

[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
[HttpGet("configure")]
public IActionResult Configure(string storeId)
{
return View();
}

[HttpPost("configure")]
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public IActionResult Configure(string storeId, string command, string settings)
{
var store = HttpContext.GetStoreData();
var existing = store.GetSupportedPaymentMethods(_btcPayNetworkProvider).OfType<LightningSupportedPaymentMethod>()
.FirstOrDefault(method =>
method.PaymentId.PaymentType == LightningPaymentType.Instance &&
method.PaymentId.CryptoCode == "BTC");

// if (command == "clear")
// {
// await _breezService.Set(storeId, null);
// TempData[WellKnownTempData.SuccessMessage] = "Settings cleared successfully";
// var client = _breezService.GetClient(storeId);
// var isStoreSetToThisMicro = existing?.GetExternalLightningUrl() == client?.ToString();
// if (client is not null && isStoreSetToThisMicro)
// {
// store.SetSupportedPaymentMethod(existing.PaymentId, null);
// await _storeRepository.UpdateStore(store);
// }
// return RedirectToAction(nameof(Configure), new {storeId});
// }
//
// if (command == "save")
// {
//
// try
// {
// if (string.IsNullOrEmpty(settings.Mnemonic))
// {
// ModelState.AddModelError(nameof(settings.Mnemonic), "Mnemonic is required");
// return View(settings);
// }
// else
// {
// try
// {
// new Mnemonic(settings.Mnemonic);
// }
// catch (Exception e)
// {
// ModelState.AddModelError(nameof(settings.Mnemonic), "Invalid mnemonic");
// return View(settings);
// }
// }
//
// if (settings.GreenlightCredentials is not null)
// {
// await using var stream = settings.GreenlightCredentials .OpenReadStream();
// using var archive = new ZipArchive(stream);
// var deviceClientArchiveEntry = archive.GetEntry("client.crt");
// var deviceKeyArchiveEntry = archive.GetEntry("client-key.pem");
// if(deviceClientArchiveEntry is null || deviceKeyArchiveEntry is null)
// {
// ModelState.AddModelError(nameof(settings.GreenlightCredentials), "Invalid zip file (does not have client.crt or client-key.pem)");
// return View(settings);
// }
// else
// {
// var deviceClient = await ReadAsByteArrayAsync(deviceClientArchiveEntry.Open());
// var deviceKey = await ReadAsByteArrayAsync(deviceKeyArchiveEntry.Open());
// var dir = _breezService.GetWorkDir(storeId);
// Directory.CreateDirectory(dir);
// await System.IO.File.WriteAllBytesAsync(Path.Combine(dir, "client.crt"), deviceClient);
// await System.IO.File.WriteAllBytesAsync(Path.Combine(dir, "client-key.pem"), deviceKey);
//
// await _breezService.Set(storeId, settings);
// }
//
// }
// else
// {
//
// await _breezService.Set(storeId, settings);
// }
// }
// catch (Exception e)
// {
// TempData[WellKnownTempData.ErrorMessage] = $"Couldnt use provided settings: {e.Message}";
// return View(settings);
// }
//
// if(existing is null)
// {
// existing = new LightningSupportedPaymentMethod()
// {
// CryptoCode = "BTC"
// };
// var client = _breezService.GetClient(storeId);
// existing.SetLightningUrl(client);
// store.SetSupportedPaymentMethod(existing);
// var lnurl = new LNURLPaySupportedPaymentMethod()
// {
// CryptoCode = "BTC",
// };
// store.SetSupportedPaymentMethod(lnurl);
// await _storeRepository.UpdateStore(store);
// }
//
// TempData[WellKnownTempData.SuccessMessage] = "Settings saved successfully";
// return RedirectToAction(nameof(Info), new {storeId});
// }
//return NotFound();

return RedirectToAction(nameof(Configure), new {storeId});
}
}
3 changes: 3 additions & 0 deletions plugin/StrikePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class StrikePlugin : BaseBTCPayServerPlugin
public override void Execute(IServiceCollection applicationBuilder)
{
applicationBuilder.AddSingleton<IUIExtension>(new UIExtension("Strike/LNPaymentMethodSetupTab", "ln-payment-method-setup-tab"));
applicationBuilder.AddSingleton<IUIExtension>(new UIExtension("Strike/StrikeNav", "store-integrations-nav"));

applicationBuilder.AddSingleton<ILightningConnectionStringHandler>(provider => provider.GetRequiredService<StrikeLightningConnectionStringHandler>());
applicationBuilder.AddSingleton<StrikeLightningConnectionStringHandler>();

Expand All @@ -28,6 +30,7 @@ public override void Execute(IServiceCollection applicationBuilder)
var factory = provider.GetRequiredService<StrikeDbContextFactory>();
factory.ConfigureBuilder(o);
});
applicationBuilder.AddHostedService<StrikeDbContextMigrator>();

applicationBuilder.AddSingleton<StrikeStorageFactory>();
applicationBuilder.AddTransient<StrikeStorage>();
Expand Down
18 changes: 18 additions & 0 deletions plugin/Views/Shared/Strike/StrikeNav.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@using BTCPayServer.Abstractions.Contracts
@using BTCPayServer.Client
@using Microsoft.AspNetCore.Mvc.TagHelpers
@inject IScopeProvider ScopeProvider
@{
var storeId = ScopeProvider.GetCurrentStoreId();
}
@if (!string.IsNullOrEmpty(storeId))
{
<li class="nav-item">
<a permission="@Policies.CanViewStoreSettings" asp-controller="Strike" asp-action="Index" asp-route-storeId="@storeId" class="nav-link @ViewData.IsActivePage("Strike")" id="Nav-Strike">
<svg style="width: 20px; margin-left: 1px; margin-right: 7px;" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" data-theme="dark" focusable="false">
<path fill-rule="evenodd" clip-rule="evenodd" d="M3.31317 8.49362C2.82011 8.30435 2.57384 7.75121 2.76311 7.25815C2.95238 6.76509 3.50552 6.51881 3.99858 6.70808L7.55779 8.07434C7.56174 8.07591 7.5657 8.07746 7.56967 8.07899L9.35521 8.76439C9.84827 8.95366 10.4014 8.70738 10.5907 8.21432C10.7799 7.72126 10.5337 7.16812 10.0406 6.97885L10.0404 6.97877C10.0405 6.97874 10.0405 6.9787 10.0406 6.97866L8.25506 6.29326C7.762 6.104 7.51572 5.55086 7.70499 5.0578C7.89426 4.56473 8.4474 4.31846 8.94046 4.50773L14.1921 6.52363C14.2046 6.52845 14.217 6.53352 14.2293 6.53881C14.2519 6.54711 14.2745 6.55559 14.2971 6.56425C16.7624 7.5106 17.9938 10.2763 17.0474 12.7416C16.1011 15.2069 13.3354 16.4383 10.8701 15.4919C10.8226 15.4737 10.7756 15.4548 10.729 15.4353C10.7065 15.4285 10.684 15.4208 10.6616 15.4122L5.51344 13.436C5.02038 13.2467 4.77411 12.6936 4.96338 12.2005C5.15265 11.7075 5.70579 11.4612 6.19885 11.6504L7.98415 12.3358C7.98411 12.3356 7.98407 12.3355 7.98403 12.3354L7.9844 12.3355C8.47746 12.5248 9.0306 12.2785 9.21987 11.7854C9.40914 11.2924 9.16287 10.7392 8.66981 10.55L7.86633 10.2416C7.86634 10.2415 7.86634 10.2415 7.86635 10.2414L3.31317 8.49362Z" fill="currentColor"></path>
</svg>
<span>Strike</span>
</a>
</li>
}
24 changes: 24 additions & 0 deletions plugin/Views/Strike/Configure.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@using BTCPayServer
@{
ViewData.SetActivePage("Strike", "Configure", "Configure");
var storeId = Context.GetCurrentStoreId();
}
<form method="post" asp-action="Configure" asp-controller="Strike" asp-route-storeId="@storeId" enctype="multipart/form-data">
<div class="row mb-4">
<div class="col-12">
<div class="d-flex align-items-center justify-content-between mb-3">
<h3 class="mb-0">
<span>@ViewData["Title"]</span>
</h3>
<div class="d-flex gap-3 mt-3 mt-sm-0">
<button name="command" type="submit" value="save" class="btn btn-primary">Save</button>
@* @if (active) *@
@* { *@
@* <button name="command" type="submit" value="clear" class="btn btn-danger">Clear</button> *@
@* } *@
</div>
</div>

</div>
</div>
</form>
14 changes: 14 additions & 0 deletions plugin/Views/Strike/Dashboard.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@using BTCPayServer.Models.StoreViewModels
@using BTCPayServer.Security
@model object
@{
var storeId = Model switch
{
string s => s,
StoreDashboardViewModel dashboardModel => dashboardModel.StoreId,
_ => Context.GetImplicitStoreId()
};
ViewData.SetActivePage("Strike", "Strike Dashboard", "Dashboard");
}

<h3>Dashboard</h3>
12 changes: 12 additions & 0 deletions plugin/Views/Strike/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@{
Layout = "../Shared/_NavLayout.cshtml";
ViewData["NavPartialName"] = "_Nav";
}
<style>
#mainContent > section {
padding: 3rem !important;
}
</style>

@RenderBody()

24 changes: 24 additions & 0 deletions plugin/Views/Strike/_Nav.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@using BTCPayServer.Client
@using BTCPayServer.Models.StoreViewModels
@using BTCPayServer.Security
@using Microsoft.AspNetCore.Mvc.TagHelpers

@{
var storeId = Model switch
{
string s => s,
StoreDashboardViewModel dashboardModel => dashboardModel.StoreId,
_ => Context.GetImplicitStoreId()
};
}

<div class="sticky-header-setup"></div>
<div class="sticky-header mb-xl">
<h2 class="mt-1 mb-2 mb-lg-4">Strike</h2>
<nav id="SectionNav">
<div class="nav">
<a permission="@Policies.CanViewStoreSettings" asp-action="Dashboard" asp-route-storeId="@storeId" class="nav-link @ViewData.IsActivePage("Strike", null, "Dashboard")">Dashboard</a>
<a permission="@Policies.CanModifyStoreSettings" asp-action="Configure" asp-route-storeId="@storeId" class="nav-link @ViewData.IsActivePage("Strike", null, "Configure")">Configuration</a>
</div>
</nav>
</div>
2 changes: 1 addition & 1 deletion submodules/btcpayserver
Submodule btcpayserver updated 836 files

0 comments on commit 18eb3c9

Please sign in to comment.