update and have working invoice gen

This commit is contained in:
Kukks 2024-05-08 12:16:25 +02:00 committed by Dennis Reimann
parent dc18f97d88
commit 5af47aa3f3
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
7 changed files with 98 additions and 21 deletions

View file

@ -14,6 +14,9 @@ public partial class LightningPayment
public DateTimeOffset Timestamp { get; set; }
public long Value { get; set; }
public LightningPaymentStatus Status { get; set; }
//you can have multiple requests generated for the same payment hash, but once you reveal the preimage, you should reject any attempt to pay the same payment hash
public List<string> PaymentRequests { get; set; }
}
public class AppUserInfo

View file

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Lightning;
namespace BTCPayApp.CommonServer;
@ -14,6 +15,10 @@ public interface IBTCPayAppHubClient
Task NewBlock(string block);
Task<LightningPayment> CreateInvoice(CreateLightningInvoiceRequest createLightningInvoiceRequest);
Task<LightningPayment?> GetLightningInvoice(string paymentHash);
Task<LightningPayment?> GetLightningPayment(string paymentHash);
Task<List<LightningPayment>> GetLightningPayments(ListPaymentsParams request);
Task<List<LightningPayment>> GetLightningInvoices(ListInvoicesParams request);
}
//methods available on the hub in the server
public interface IBTCPayAppHubServer

View file

@ -262,7 +262,7 @@ var resultPsbt = PSBT.Parse(psbt, explorerClient.Network.NBitcoinNetwork);
public async Task SendPaymentUpdate(string identifier, LightningPayment lightningPayment)
{
throw new NotImplementedException();
await _appState.PaymentUpdate(identifier, lightningPayment);
}

View file

@ -25,26 +25,25 @@ public class BTCPayAppState : IHostedService
private readonly ExplorerClientProvider _explorerClientProvider;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly EventAggregator _eventAggregator;
private readonly HubLifetimeManager<BTCPayAppHub> _lifetimeManager;
private CompositeDisposable? _compositeDisposable;
public ExplorerClient ExplorerClient { get; private set; }
private DerivationSchemeParser _derivationSchemeParser;
// private readonly ConcurrentDictionary<string, TrackedSource> _connectionScheme = new();
public event EventHandler<(string,LightningPayment)>? OnPaymentUpdate;
public BTCPayAppState(
IHubContext<BTCPayAppHub, IBTCPayAppHubClient> hubContext,
ILogger<BTCPayAppState> logger,
ExplorerClientProvider explorerClientProvider,
BTCPayNetworkProvider networkProvider,
EventAggregator eventAggregator,
HubLifetimeManager<BTCPayAppHub> lifetimeManager)
EventAggregator eventAggregator)
{
_hubContext = hubContext;
_logger = logger;
_explorerClientProvider = explorerClientProvider;
_networkProvider = networkProvider;
_eventAggregator = eventAggregator;
_lifetimeManager = lifetimeManager;
}
public Task StartAsync(CancellationToken cancellationToken)
@ -152,4 +151,11 @@ public class BTCPayAppState : IHostedService
public async Task Connected(string contextConnectionId)
{
}
public async Task PaymentUpdate(string identifier, LightningPayment lightningPayment)
{
OnPaymentUpdate?.Invoke(this, (identifier, lightningPayment));
}
}

View file

@ -1,6 +1,8 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using BTCPayApp.CommonServer;
using BTCPayServer.Client.Models;
@ -8,7 +10,7 @@ using BTCPayServer.Controllers;
using BTCPayServer.Lightning;
using Microsoft.AspNetCore.SignalR;
using NBitcoin;
using LightningPayment = BTCPayServer.Lightning.LightningPayment;
using LightningPayment = BTCPayApp.CommonServer.LightningPayment;
namespace BTCPayServer.App;
@ -46,7 +48,7 @@ public class BTCPayAppLightningConnectionStringHandler:ILightningConnectionStrin
return null;
}
error = null;
return new BTCPayAppLightningClient(_hubContext, _appState, key);
return new BTCPayAppLightningClient(_hubContext, _appState, key, network );
}
@ -57,12 +59,14 @@ public class BTCPayAppLightningClient:ILightningClient
private readonly IHubContext<BTCPayAppHub, IBTCPayAppHubClient> _hubContext;
private readonly BTCPayAppState _appState;
private readonly string _key;
private readonly Network _network;
public BTCPayAppLightningClient(IHubContext<BTCPayAppHub, IBTCPayAppHubClient> hubContext, BTCPayAppState appState, string key)
public BTCPayAppLightningClient(IHubContext<BTCPayAppHub, IBTCPayAppHubClient> hubContext, BTCPayAppState appState, string key, Network network)
{
_hubContext = hubContext;
_appState = appState;
_key = key;
_network = network;
}
public override string ToString()
@ -80,7 +84,8 @@ public class BTCPayAppLightningClient:ILightningClient
public async Task<LightningInvoice> GetInvoice(uint256 paymentHash, CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
var lp = await HubClient.GetLightningInvoice(paymentHash.ToString());
return ToLightningInvoice(lp, _network);
}
public async Task<LightningInvoice[]> ListInvoices(CancellationToken cancellation = new CancellationToken())
@ -90,22 +95,38 @@ public class BTCPayAppLightningClient:ILightningClient
public async Task<LightningInvoice[]> ListInvoices(ListInvoicesParams request, CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
var invs = await HubClient.GetLightningInvoices(request);
return invs.Select(i => ToLightningInvoice(i, _network)).ToArray();
}
public async Task<LightningPayment> GetPayment(string paymentHash, CancellationToken cancellation = new CancellationToken())
public async Task<Lightning.LightningPayment> GetPayment(string paymentHash, CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
return ToLightningPayment(await HubClient.GetLightningPayment(paymentHash));
}
public async Task<LightningPayment[]> ListPayments(CancellationToken cancellation = new CancellationToken())
private static Lightning.LightningPayment ToLightningPayment(LightningPayment lightningPayment)
{
return new Lightning.LightningPayment()
{
Id = lightningPayment.PaymentHash,
Amount = LightMoney.MilliSatoshis(lightningPayment.Value),
PaymentHash = lightningPayment.PaymentHash,
Preimage = lightningPayment.Preimage,
BOLT11 = lightningPayment.PaymentRequests.FirstOrDefault(),
Status = lightningPayment.Status
};
}
public async Task<Lightning.LightningPayment[]> ListPayments(CancellationToken cancellation = new CancellationToken())
{
return await ListPayments(new ListPaymentsParams(), cancellation);
}
public async Task<LightningPayment[]> ListPayments(ListPaymentsParams request, CancellationToken cancellation = new CancellationToken())
public async Task<Lightning.LightningPayment[]> ListPayments(ListPaymentsParams request, CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
var invs = await HubClient.GetLightningPayments(request);
return invs.Select(ToLightningPayment).ToArray();
}
public async Task<LightningInvoice> CreateInvoice(LightMoney amount, string description, TimeSpan expiry,
@ -122,20 +143,61 @@ public class BTCPayAppLightningClient:ILightningClient
PrivateRouteHints = createInvoiceRequest.PrivateRouteHints,
});
return null;
return ToLightningInvoice(lp, _network);
}
private static LightningInvoice ToLightningInvoice(LightningPayment lightningPayment, Network _network)
{
var paymenRequest = BOLT11PaymentRequest.Parse(lightningPayment.PaymentRequests.First(), _network);
return new LightningInvoice()
{
Id = lightningPayment.PaymentHash,
Amount = LightMoney.MilliSatoshis(lightningPayment.Value),
PaymentHash = lightningPayment.PaymentHash,
Preimage = lightningPayment.Preimage,
BOLT11 = lightningPayment.PaymentRequests.FirstOrDefault(),
Status = lightningPayment.Status == LightningPaymentStatus.Complete? LightningInvoiceStatus.Paid: paymenRequest.ExpiryDate < DateTimeOffset.UtcNow? LightningInvoiceStatus.Expired: LightningInvoiceStatus.Unpaid
};
}
public async Task<ILightningInvoiceListener> Listen(CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
return new Listener(_appState, _network);
}
public class Listener:ILightningInvoiceListener
{
private readonly BTCPayAppState _btcPayAppState;
private readonly Network _network;
private readonly Channel<LightningPayment> _channel = Channel.CreateUnbounded<LightningPayment>();
public Listener(BTCPayAppState btcPayAppState, Network network)
{
_btcPayAppState = btcPayAppState;
_network = network;
_btcPayAppState.OnPaymentUpdate += BtcPayAppStateOnOnPaymentUpdate;
}
private void BtcPayAppStateOnOnPaymentUpdate(object sender, (string, LightningPayment) e)
{
_channel.Writer.TryWrite(e.Item2);
}
public void Dispose()
{
_btcPayAppState.OnPaymentUpdate -= BtcPayAppStateOnOnPaymentUpdate;
_channel.Writer.Complete();
}
public async Task<LightningInvoice> WaitInvoice(CancellationToken cancellation)
{
return ToLightningInvoice(await _channel.Reader.ReadAsync(cancellation), _network);
}
}
public async Task<LightningNodeInformation> GetInfo(CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
throw new NotSupportedException();
}
public async Task<LightningNodeBalance> GetBalance(CancellationToken cancellation = new CancellationToken())

View file

@ -47,6 +47,7 @@
<ItemGroup>
<PackageReference Include="BTCPayServer.NTag424" Version="1.0.23" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="8.0.4" />
<PackageReference Include="YamlDotNet" Version="8.0.0" />
<PackageReference Include="BIP78.Sender" Version="0.2.2" />
<PackageReference Include="BTCPayServer.Hwi" Version="2.0.2" />

View file

@ -114,7 +114,7 @@ namespace BTCPayServer.Hosting
services.AddBTCPayServer(Configuration, Logs);
services.AddProviderStorage();
services.AddSession();
services.AddSignalR();
services.AddSignalR().AddNewtonsoftJsonProtocol();
services.AddFido2(options =>
{
options.ServerName = "BTCPay Server";