prism: support onchain source & fix catach all with bolt 11 payments

This commit is contained in:
Kukks
2023-08-04 12:31:16 +02:00
parent 8899bb96a6
commit 5400811c68
5 changed files with 78 additions and 17 deletions

View File

@@ -11,7 +11,7 @@
<PropertyGroup>
<Product>LN Prism</Product>
<Description>Automated value splits for lightning.</Description>
<Version>1.1.8</Version>
<Version>1.1.9</Version>
</PropertyGroup>
<!-- Plugin development properties -->
<PropertyGroup>

View File

@@ -25,7 +25,7 @@
else
{
<button type="button" class="btn btn-sm btn-link" @onclick="StartUpdate(dest,balance)">Update</button>
<button type="button" class="btn btn-sm btn-link" @onclick="() => StartUpdate(dest,balance)">Update</button>
}
</td>
</tr>

View File

@@ -0,0 +1,54 @@
@* @using NBitcoin *@
@* @if (Destinations.Any()) *@
@* { *@
@* <div class="form-group"> *@
@* <select class="form-select" @bind="SelectedDestinationId"> *@
@* *@
@* <option value="null">Select destination to configure</option> *@
@* @foreach (var destination in Destinations) *@
@* { *@
@* <option value="@destination.Key">@destination.Key</option> *@
@* } *@
@* </select> *@
@* </div> *@
@* *@
@* @if (SelectedDestinationId is not null && SelectedDestinationId != "null") *@
@* { *@
@* <PrismDestinationEditor ValidateId="ValidateId" Id="@SelectedDestinationId" IdChanged="OnIdRenamed" ValidateDestination="s => ValidateDestination(s, false)" @bind-Settings="SelectedDestination"></PrismDestinationEditor> *@
@* } *@
@* } *@
@* *@
@* @code { *@
@* *@
@* [Parameter] *@
@* public Dictionary<string, PrismDestination> Destinations { get; set; } *@
@* *@
@* *@
@* public string? SelectedDestinationId { get; set; } *@
@* *@
@* public PrismDestination? SelectedDestination *@
@* { *@
@* get *@
@* { *@
@* if (SelectedDestinationId is null or "null") *@
@* return null; *@
@* Destinations.TryGetValue(SelectedDestinationId, out var res); *@
@* return res; *@
@* } *@
@* set *@
@* { *@
@* if (SelectedDestinationId is null) *@
@* return; *@
@* if (value is null) *@
@* { *@
@* Destinations.Remove(SelectedDestinationId); *@
@* SelectedDestinationId = null; *@
@* } *@
@* else *@
@* { *@
@* Destinations.AddOrReplace(SelectedDestinationId, value); *@
@* } *@
@* } *@
@* } *@
@* *@
@* } *@

View File

@@ -303,8 +303,8 @@ namespace BTCPayServer.Plugins.Prism
return;
}
if (evt is InvoiceEvent invoiceEvent &&
new[] {InvoiceEventCode.Completed, InvoiceEventCode.MarkedCompleted}.Contains(
if (evt is InvoiceEvent invoiceEvent &&
new[] {InvoiceEventCode.Confirmed, InvoiceEventCode.MarkedCompleted}.Contains(
invoiceEvent.EventCode))
{
@@ -313,13 +313,13 @@ namespace BTCPayServer.Plugins.Prism
{
return;
}
var catchAllPrism = prismSettings.Splits.FirstOrDefault(split => split.Source == "*");
var onChainCatchAllIdentifier = "*"+ PaymentTypes.BTCLike.ToStringNormalized();
var catchAllPrism = prismSettings.Splits.FirstOrDefault(split => split.Source == "*" || split.Source ==onChainCatchAllIdentifier);
Split prism = null;
LightningAddressData address = null;
var pm = invoiceEvent.Invoice.GetPaymentMethod(new PaymentMethodId("BTC", LNURLPayPaymentType.Instance));
var pmdRaw = pm?.GetPaymentMethodDetails();
var pmd = pmdRaw as LNURLPayPaymentMethodDetails;
var pm = invoiceEvent.Invoice.GetPaymentMethod(new PaymentMethodId("BTC", LNURLPayPaymentType.Instance));
var pmd = pm?.GetPaymentMethodDetails() as LNURLPayPaymentMethodDetails;
if (string.IsNullOrEmpty(pmd?.ConsumedLightningAddress) && catchAllPrism is null)
{
return;
@@ -333,15 +333,18 @@ namespace BTCPayServer.Plugins.Prism
prism = prismSettings.Splits.FirstOrDefault(s => s.Source.Equals(address.Username, StringComparison.InvariantCultureIgnoreCase));
}
else if (catchAllPrism?.Source == onChainCatchAllIdentifier)
{
pm = invoiceEvent.Invoice.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike));
prism = catchAllPrism;
}
else
{
//do not run prism on payments not LN based.
if (invoiceEvent.Invoice.GetPayments("BTC", true).All(entity =>
{
var pmi = entity.GetPaymentMethodId();
return pmi.CryptoCode == "BTC" && (pmi.PaymentType == LightningPaymentType.Instance || pmi.PaymentType == LNURLPayPaymentType.Instance);
}))
prism = catchAllPrism;
if (pmd is null)
{
pm = invoiceEvent.Invoice.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.LightningLike));
}
prism = catchAllPrism;
}
var splits = prism?.Destinations;
if (splits?.Any() is not true)
@@ -351,6 +354,10 @@ namespace BTCPayServer.Plugins.Prism
var msats = LightMoney.FromUnit(pm.Calculate().CryptoPaid, LightMoneyUnit.BTC).ToUnit(LightMoneyUnit.MilliSatoshi);
if(msats <= 0)
{
return;
}
//compute the sats for each destination based on splits percentage
var msatsPerDestination =
splits.ToDictionary(s => s.Destination, s => (long) (msats * (s.Percentage / 100)));

View File

@@ -66,7 +66,7 @@ public class PrismClaimCreate : IPluginHookFilter
}
catch (Exception e)
{
if (BOLT11PaymentRequest.TryParse(shift.depositAddress, out var bolt11, network.NBitcoinNetwork))
if (BOLT11PaymentRequest.TryParse(shift.depositAddress, out var bolt11, network.NBitcoinNetwork))
{
claimRequest.Destination = new BoltInvoiceClaimDestination(shift.depositAddress, bolt11);
claimRequest.Metadata = JObject.FromObject(new