From 5400811c68ff64765edd163cb44cde43fda67fd0 Mon Sep 17 00:00:00 2001 From: Kukks Date: Fri, 4 Aug 2023 12:31:16 +0200 Subject: [PATCH] prism: support onchain source & fix catach all with bolt 11 payments --- .../BTCPayServer.Plugins.Prism.csproj | 2 +- .../Components/PrismBalances.razor | 2 +- .../Components/PrismDestinationManager.razor | 54 +++++++++++++++++++ .../BTCPayServer.Plugins.Prism/SatBreaker.cs | 35 +++++++----- .../PrismClaimCreate.cs | 2 +- 5 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 Plugins/BTCPayServer.Plugins.Prism/Components/PrismDestinationManager.razor diff --git a/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj b/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj index a662edb..e0ff487 100644 --- a/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj +++ b/Plugins/BTCPayServer.Plugins.Prism/BTCPayServer.Plugins.Prism.csproj @@ -11,7 +11,7 @@ LN Prism Automated value splits for lightning. - 1.1.8 + 1.1.9 diff --git a/Plugins/BTCPayServer.Plugins.Prism/Components/PrismBalances.razor b/Plugins/BTCPayServer.Plugins.Prism/Components/PrismBalances.razor index 674e1f5..8446dac 100644 --- a/Plugins/BTCPayServer.Plugins.Prism/Components/PrismBalances.razor +++ b/Plugins/BTCPayServer.Plugins.Prism/Components/PrismBalances.razor @@ -25,7 +25,7 @@ else { - + } diff --git a/Plugins/BTCPayServer.Plugins.Prism/Components/PrismDestinationManager.razor b/Plugins/BTCPayServer.Plugins.Prism/Components/PrismDestinationManager.razor new file mode 100644 index 0000000..4734e7f --- /dev/null +++ b/Plugins/BTCPayServer.Plugins.Prism/Components/PrismDestinationManager.razor @@ -0,0 +1,54 @@ +@* @using NBitcoin *@ +@* @if (Destinations.Any()) *@ +@* { *@ +@*
*@ +@* *@ +@*
*@ +@* *@ +@* @if (SelectedDestinationId is not null && SelectedDestinationId != "null") *@ +@* { *@ +@* *@ +@* } *@ +@* } *@ +@* *@ +@* @code { *@ +@* *@ +@* [Parameter] *@ +@* public Dictionary 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); *@ +@* } *@ +@* } *@ +@* } *@ +@* *@ +@* } *@ \ No newline at end of file diff --git a/Plugins/BTCPayServer.Plugins.Prism/SatBreaker.cs b/Plugins/BTCPayServer.Plugins.Prism/SatBreaker.cs index f461286..ab1e85d 100644 --- a/Plugins/BTCPayServer.Plugins.Prism/SatBreaker.cs +++ b/Plugins/BTCPayServer.Plugins.Prism/SatBreaker.cs @@ -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))); diff --git a/Plugins/BTCPayServer.Plugins.SideShift/PrismClaimCreate.cs b/Plugins/BTCPayServer.Plugins.SideShift/PrismClaimCreate.cs index ab2ac1a..7de463d 100644 --- a/Plugins/BTCPayServer.Plugins.SideShift/PrismClaimCreate.cs +++ b/Plugins/BTCPayServer.Plugins.SideShift/PrismClaimCreate.cs @@ -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