add common proj

This commit is contained in:
Kukks 2023-07-03 09:56:00 +02:00 committed by Dennis Reimann
parent ed4dea214d
commit d8c519d008
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
10 changed files with 131 additions and 54 deletions

View file

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,7 @@
namespace BTCPayApp.CommonServer
{
public interface IBTCPayAppServerClient
{
}
}

View file

@ -0,0 +1,13 @@
namespace BTCPayApp.CommonServer
{
public class PairSuccessResult
{
public string Key { get; set; }
public string StoreId { get; set; }
public string UserId { get; set; }
public string? ExistingWallet { get; set; }
public string? ExistingWalletSeed { get; set; }
public string Network { get; set; }
}
}

View file

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace BTCPayServer.Controllers;
public static class BTCPayAppExtensions
{
public static IServiceCollection AddBTCPayApp(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<BtcPayAppService>();
return serviceCollection;
}
public static void UseBTCPayApp(this IApplicationBuilder builder)
{
builder.UseEndpoints(routeBuilder =>
{
routeBuilder.MapHub<BTCPayAppHub>("hub/btcpayapp");
});
}
}

View file

@ -0,0 +1,8 @@
using BTCPayApp.CommonServer;
using Microsoft.AspNetCore.SignalR;
namespace BTCPayServer.Controllers;
public class BTCPayAppHub : Hub<IBTCPayAppServerClient>
{
}

View file

@ -0,0 +1,20 @@
using System;
using BTCPayServer.Abstractions.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace BTCPayServer.Controllers;
public class BTCPayAppPlugin : BaseBTCPayServerPlugin
{
public override void Execute(IApplicationBuilder applicationBuilder,
IServiceProvider applicationBuilderApplicationServices)
{
applicationBuilder.UseBTCPayApp();
}
public override void Execute(IServiceCollection applicationBuilder)
{
applicationBuilder.AddBTCPayApp();
}
}

View file

@ -1,53 +1,19 @@
#nullable enable #nullable enable
using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayApp.CommonServer;
using BTCPayServer.Client; using BTCPayServer.Client;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Plugins.Shopify;
using BTCPayServer.Security.Greenfield; using BTCPayServer.Security.Greenfield;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using NBitcoin; using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using NBXplorer; using NBXplorer;
namespace BTCPayServer.Controllers; namespace BTCPayServer.Controllers;
public class BtcPayAppService
{
private readonly IMemoryCache _memoryCache;
public BtcPayAppService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
private string CacheKey(string k) => $"BtcPayAppService_{k}";
public async Task<string> GeneratePairingCode(string storeId, string userId)
{
var code = Guid.NewGuid().ToString();
_memoryCache.Set(CacheKey(code), new PairingRequest() {Key = code, StoreId = storeId, UserId = userId},
TimeSpan.FromMinutes(5));
return code;
}
public PairingRequest? ConsumePairingCode(string code)
{
return _memoryCache.TryGetValue(CacheKey(code), out var pairingRequest)
? (PairingRequest?)pairingRequest
: null;
}
public class PairingRequest
{
public string Key { get; set; }
public string StoreId { get; set; }
public string UserId { get; set; }
}
}
[Route("btcpayapp")] [Route("btcpayapp")]
public class BtcPayAppController : Controller public class BtcPayAppController : Controller
{ {
@ -58,7 +24,8 @@ public class BtcPayAppController : Controller
private readonly ExplorerClientProvider _explorerClientProvider; private readonly ExplorerClientProvider _explorerClientProvider;
public BtcPayAppController(BtcPayAppService appService, APIKeyRepository apiKeyRepository, public BtcPayAppController(BtcPayAppService appService, APIKeyRepository apiKeyRepository,
StoreRepository storeRepository, BTCPayNetworkProvider btcPayNetworkProvider, ExplorerClientProvider explorerClientProvider) StoreRepository storeRepository, BTCPayNetworkProvider btcPayNetworkProvider,
ExplorerClientProvider explorerClientProvider)
{ {
_appService = appService; _appService = appService;
_apiKeyRepository = apiKeyRepository; _apiKeyRepository = apiKeyRepository;
@ -106,28 +73,21 @@ public class BtcPayAppController : Controller
Key = key.Id, Key = key.Id,
StoreId = store.Id, StoreId = store.Id,
UserId = res.UserId, UserId = res.UserId,
ExistingWallet = onchain?.AccountDerivation?.GetExtPubKeys()?.FirstOrDefault()?.ToString(onchain.Network.NBitcoinNetwork), ExistingWallet =
onchain?.AccountDerivation?.GetExtPubKeys()?.FirstOrDefault()
?.ToString(onchain.Network.NBitcoinNetwork),
ExistingWalletSeed = onchainSeed, ExistingWalletSeed = onchainSeed,
Network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>("BTC").NBitcoinNetwork.Name Network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>("BTC").NBitcoinNetwork.Name
}); });
} }
private async Task<string?> GetSeed(ExplorerClient client, DerivationSchemeSettings derivation) private async Task<string?> GetSeed(ExplorerClient client, DerivationSchemeSettings derivation)
{ {
return derivation.IsHotWallet && return derivation.IsHotWallet &&
await client.GetMetadataAsync<string>(derivation.AccountDerivation, WellknownMetadataKeys.Mnemonic) is { } seed && await client.GetMetadataAsync<string>(derivation.AccountDerivation, WellknownMetadataKeys.Mnemonic) is
!string.IsNullOrEmpty(seed) ? seed : null; { } seed &&
} !string.IsNullOrEmpty(seed)
? seed
: null;
public class PairSuccessResult
{
public string Key { get; set; }
public string StoreId { get; set; }
public string UserId { get; set; }
public string? ExistingWallet { get; set; }
public string? ExistingWalletSeed { get; set; }
public string Network { get; set; }
} }
} }

View file

@ -0,0 +1,39 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
namespace BTCPayServer.Controllers;
public class BtcPayAppService
{
private readonly IMemoryCache _memoryCache;
public BtcPayAppService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
private string CacheKey(string k) => $"BtcPayAppService_{k}";
public async Task<string> GeneratePairingCode(string storeId, string userId)
{
var code = Guid.NewGuid().ToString();
_memoryCache.Set(CacheKey(code), new PairingRequest() {Key = code, StoreId = storeId, UserId = userId},
TimeSpan.FromMinutes(5));
return code;
}
public PairingRequest? ConsumePairingCode(string code)
{
return _memoryCache.TryGetValue(CacheKey(code), out var pairingRequest)
? (PairingRequest?)pairingRequest
: null;
}
public class PairingRequest
{
public string Key { get; set; }
public string StoreId { get; set; }
public string UserId { get; set; }
}
}

View file

@ -99,6 +99,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BTCPayApp.CommonServer\BTCPayApp.CommonServer.csproj" />
<ProjectReference Include="..\BTCPayServer.Abstractions\BTCPayServer.Abstractions.csproj" /> <ProjectReference Include="..\BTCPayServer.Abstractions\BTCPayServer.Abstractions.csproj" />
<ProjectReference Include="..\BTCPayServer.Client\BTCPayServer.Client.csproj" /> <ProjectReference Include="..\BTCPayServer.Client\BTCPayServer.Client.csproj" />
<ProjectReference Include="..\BTCPayServer.Data\BTCPayServer.Data.csproj" /> <ProjectReference Include="..\BTCPayServer.Data\BTCPayServer.Data.csproj" />

View file

@ -517,7 +517,6 @@ o.GetRequiredService<IEnumerable<IPaymentLinkExtension>>().ToDictionary(o => o.P
}); });
} }
services.AddSingleton<BtcPayAppService>();
return services; return services;
} }