impl pay invoice

This commit is contained in:
Kukks 2024-05-24 12:11:56 +02:00 committed by Dennis Reimann
parent 87d4038fc6
commit 2ffae18d8b
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
3 changed files with 31 additions and 7 deletions

View file

@ -31,6 +31,7 @@ public interface IBTCPayAppHubClient
Task<LightningPayment?> GetLightningPayment(string paymentHash);
Task<List<LightningPayment>> GetLightningPayments(ListPaymentsParams request);
Task<List<LightningPayment>> GetLightningInvoices(ListInvoicesParams request);
Task<PayResponse> PayInvoice(string bolt11, long? amountMilliSatoshi);
}
public record TxResp(long Confirmations, long? Height, decimal BalanceChange, DateTimeOffset Timestamp, string TransactionId)

View file

@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using AngleSharp.Dom.Events;
using BTCPayApp.CommonServer;
using BTCPayServer.Events;
using BTCPayServer.Payments.Lightning;
@ -203,10 +204,16 @@ public class BTCPayAppState : IHostedService
foreach (var group in GroupToConnectionId.Where(a => a.Value == contextConnectionId).Select(a => a.Key)
.ToArray())
{
GroupToConnectionId.TryRemove(group, out _);
if (GroupToConnectionId.TryRemove(group, out _))
{
GroupRemoved?.Invoke(this, group);
}
}
}
public event EventHandler<string>? GroupRemoved;
public async Task Connected(string contextConnectionId)
{
if(_nodeInfo.Length > 0)

View file

@ -162,36 +162,52 @@ public class BTCPayAppLightningClient:ILightningClient
public async Task<ILightningInvoiceListener> Listen(CancellationToken cancellation = new CancellationToken())
{
return new Listener(_appState, _network);
return new Listener(_appState, _network, _key);
}
public class Listener:ILightningInvoiceListener
{
private readonly BTCPayAppState _btcPayAppState;
private readonly Network _network;
private readonly string _key;
private readonly Channel<LightningPayment> _channel = Channel.CreateUnbounded<LightningPayment>();
private readonly CancellationTokenSource _cts;
public Listener(BTCPayAppState btcPayAppState, Network network)
public Listener(BTCPayAppState btcPayAppState, Network network, string key)
{
_btcPayAppState = btcPayAppState;
btcPayAppState.GroupRemoved += BtcPayAppStateOnGroupRemoved;
_network = network;
_key = key;
_cts = new CancellationTokenSource();
_btcPayAppState.OnPaymentUpdate += BtcPayAppStateOnOnPaymentUpdate;
}
private void BtcPayAppStateOnGroupRemoved(object sender, string e)
{
if(e == _key)
_channel.Writer.Complete();
}
private void BtcPayAppStateOnOnPaymentUpdate(object sender, (string, LightningPayment) e)
{
if(e.Item1 != _key)
return;
_channel.Writer.TryWrite(e.Item2);
}
public void Dispose()
{
_cts?.Cancel();
_btcPayAppState.OnPaymentUpdate -= BtcPayAppStateOnOnPaymentUpdate;
_channel.Writer.Complete();
_btcPayAppState.GroupRemoved -= BtcPayAppStateOnGroupRemoved;
_channel.Writer.TryComplete();
}
public async Task<LightningInvoice> WaitInvoice(CancellationToken cancellation)
{
return ToLightningInvoice(await _channel.Reader.ReadAsync(cancellation), _network);
return ToLightningInvoice(await _channel.Reader.ReadAsync( CancellationTokenSource.CreateLinkedTokenSource(cancellation, _cts.Token).Token), _network);
}
}
@ -207,12 +223,12 @@ public class BTCPayAppLightningClient:ILightningClient
public async Task<PayResponse> Pay(PayInvoiceParams payParams, CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
return await Pay(null, payParams, cancellation);
}
public async Task<PayResponse> Pay(string bolt11, PayInvoiceParams payParams, CancellationToken cancellation = new CancellationToken())
{
throw new NotImplementedException();
return await HubClient.PayInvoice(bolt11, payParams.Amount?.MilliSatoshi);
}
public async Task<PayResponse> Pay(string bolt11, CancellationToken cancellation = new CancellationToken())