From e33200058da8be2b5584a23b6b7a3b2cd8febe48 Mon Sep 17 00:00:00 2001 From: Kukks Date: Wed, 29 Jul 2020 08:53:30 +0200 Subject: [PATCH] Generate Payment link from PaymentType --- .../Monero/BTCPayNetworkProvider.Monero.cs | 3 ++- .../Monero/MoneroLikeSpecificBtcPayNetwork.cs | 1 + BTCPayServer/Payments/PaymentTypes.Bitcoin.cs | 15 +++++++++++++++ BTCPayServer/Payments/PaymentTypes.Lightning.cs | 16 ++++++++++++++-- BTCPayServer/Payments/PaymentTypes.cs | 3 +++ .../Payments/MoneroLikePaymentMethodHandler.cs | 11 +++-------- .../Monero/Payments/MoneroPaymentType.cs | 7 +++++++ BTCPayServer/Services/Invoices/InvoiceEntity.cs | 14 +++++--------- 8 files changed, 50 insertions(+), 20 deletions(-) diff --git a/BTCPayServer.Common/Altcoins/Monero/BTCPayNetworkProvider.Monero.cs b/BTCPayServer.Common/Altcoins/Monero/BTCPayNetworkProvider.Monero.cs index 012c4bdd3..6ad4ceadb 100644 --- a/BTCPayServer.Common/Altcoins/Monero/BTCPayNetworkProvider.Monero.cs +++ b/BTCPayServer.Common/Altcoins/Monero/BTCPayNetworkProvider.Monero.cs @@ -20,7 +20,8 @@ namespace BTCPayServer "XMR_X = XMR_BTC * BTC_X", "XMR_BTC = kraken(XMR_BTC)" }, - CryptoImagePath = "/imlegacy/monero.svg" + CryptoImagePath = "/imlegacy/monero.svg", + UriScheme = "monero" }); } } diff --git a/BTCPayServer.Common/Altcoins/Monero/MoneroLikeSpecificBtcPayNetwork.cs b/BTCPayServer.Common/Altcoins/Monero/MoneroLikeSpecificBtcPayNetwork.cs index 6cf4b769e..76f532989 100644 --- a/BTCPayServer.Common/Altcoins/Monero/MoneroLikeSpecificBtcPayNetwork.cs +++ b/BTCPayServer.Common/Altcoins/Monero/MoneroLikeSpecificBtcPayNetwork.cs @@ -3,5 +3,6 @@ namespace BTCPayServer public class MoneroLikeSpecificBtcPayNetwork : BTCPayNetworkBase { public int MaxTrackedConfirmation = 10; + public string UriScheme { get; set; } } } diff --git a/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs b/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs index 6f4467ff6..384b02a0e 100644 --- a/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs +++ b/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs @@ -2,7 +2,9 @@ using System; using System.Globalization; using System.Linq; using BTCPayServer.Payments.Bitcoin; +using BTCPayServer.Services; using BTCPayServer.Services.Invoices; +using NBitcoin; using Newtonsoft.Json.Linq; namespace BTCPayServer.Payments @@ -64,6 +66,19 @@ namespace BTCPayServer.Payments txId = txId.Split('-').First(); return string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink, txId); } + + public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails, + Money cryptoInfoDue, string serverUri) + { + var bip21 = ((BTCPayNetwork)network).GenerateBIP21(paymentMethodDetails.GetPaymentDestination(), cryptoInfoDue); + + if ((paymentMethodDetails as BitcoinLikeOnChainPaymentMethod)?.PayjoinEnabled is true) + { + bip21 += $"&{PayjoinClient.BIP21EndpointKey}={serverUri.WithTrailingSlash()}{network.CryptoCode}/{PayjoinClient.BIP21EndpointKey}"; + } + return bip21; + } + public override string InvoiceViewPaymentPartialName { get; } = "ViewBitcoinLikePaymentData"; } } diff --git a/BTCPayServer/Payments/PaymentTypes.Lightning.cs b/BTCPayServer/Payments/PaymentTypes.Lightning.cs index d3613dc50..0e6b4044a 100644 --- a/BTCPayServer/Payments/PaymentTypes.Lightning.cs +++ b/BTCPayServer/Payments/PaymentTypes.Lightning.cs @@ -1,5 +1,7 @@ +using System; using BTCPayServer.Payments.Lightning; using BTCPayServer.Services.Invoices; +using NBitcoin; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -8,13 +10,14 @@ namespace BTCPayServer.Payments public class LightningPaymentType : PaymentType { public static LightningPaymentType Instance { get; } = new LightningPaymentType(); + private LightningPaymentType() { - } public override string ToPrettyString() => "Off-Chain"; public override string GetId() => "LightningLike"; + public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str) { return ((BTCPayNetwork)network).ToObject(str); @@ -35,7 +38,8 @@ namespace BTCPayServer.Payments return JsonConvert.SerializeObject(details); } - public override ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value) + public override ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, + JToken value) { return JsonConvert.DeserializeObject(value.ToString()); } @@ -44,6 +48,14 @@ namespace BTCPayServer.Payments { return null; } + + public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails, + Money cryptoInfoDue, string serverUri) + { + return + $"lightning:{paymentMethodDetails.GetPaymentDestination().ToUpperInvariant().Replace("LIGHTNING:", "", StringComparison.InvariantCultureIgnoreCase)}"; + } + public override string InvoiceViewPaymentPartialName { get; } = "ViewLightningLikePaymentData"; } } diff --git a/BTCPayServer/Payments/PaymentTypes.cs b/BTCPayServer/Payments/PaymentTypes.cs index 352b67dd7..e83cad53c 100644 --- a/BTCPayServer/Payments/PaymentTypes.cs +++ b/BTCPayServer/Payments/PaymentTypes.cs @@ -1,6 +1,7 @@ using System; using BTCPayServer.Services.Altcoins.Monero.Payments; using BTCPayServer.Services.Invoices; +using NBitcoin; using Newtonsoft.Json.Linq; namespace BTCPayServer.Payments @@ -63,6 +64,8 @@ namespace BTCPayServer.Payments public abstract string SerializePaymentMethodDetails(BTCPayNetworkBase network, IPaymentMethodDetails details); public abstract ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value); public abstract string GetTransactionLink(BTCPayNetworkBase network, string txId); + public abstract string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails, + Money cryptoInfoDue, string serverUri); public abstract string InvoiceViewPaymentPartialName { get; } } } diff --git a/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroLikePaymentMethodHandler.cs b/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroLikePaymentMethodHandler.cs index 5815bfb18..d0c9da666 100644 --- a/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroLikePaymentMethodHandler.cs +++ b/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroLikePaymentMethodHandler.cs @@ -75,20 +75,15 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments public override void PreparePaymentModel(PaymentModel model, InvoiceResponse invoiceResponse, StoreBlob storeBlob) { var paymentMethodId = new PaymentMethodId(model.CryptoCode, PaymentType); - - var client = _moneroRpcProvider.WalletRpcClients[model.CryptoCode]; - var cryptoInfo = invoiceResponse.CryptoInfo.First(o => o.GetpaymentMethodId() == paymentMethodId); var network = _networkProvider.GetNetwork(model.CryptoCode); model.IsLightning = false; model.PaymentMethodName = GetPaymentMethodName(network); model.CryptoImage = GetCryptoImage(network); - model.InvoiceBitcoinUrl = client.SendCommandAsync("make_uri", new MakeUriRequest() + model.InvoiceBitcoinUrl = MoneroPaymentType.Instance.GetPaymentLink(network, new MoneroLikeOnChainPaymentMethodDetails() { - Address = cryptoInfo.Address, - Amount = MoneroMoney.Convert(decimal.Parse(cryptoInfo.Due, CultureInfo.InvariantCulture)) - }).GetAwaiter() - .GetResult().Uri; + DepositAddress = cryptoInfo.Address + }, cryptoInfo.Due, null); model.InvoiceBitcoinUrlQR = model.InvoiceBitcoinUrl; } public override string GetCryptoImage(PaymentMethodId paymentMethodId) diff --git a/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroPaymentType.cs b/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroPaymentType.cs index 06df200f8..e16eaff2a 100644 --- a/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroPaymentType.cs +++ b/BTCPayServer/Services/Altcoins/Monero/Payments/MoneroPaymentType.cs @@ -1,6 +1,7 @@ using System.Globalization; using BTCPayServer.Payments; using BTCPayServer.Services.Invoices; +using NBitcoin; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -44,6 +45,12 @@ namespace BTCPayServer.Services.Altcoins.Monero.Payments return string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink, txId); } + public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails, Money cryptoInfoDue, string serverUri) + { + return + $"{(network as MoneroLikeSpecificBtcPayNetwork).UriScheme}:{paymentMethodDetails.GetPaymentDestination()}?tx_amount={cryptoInfoDue.ToDecimal(MoneyUnit.BTC)}"; + } + public override string InvoiceViewPaymentPartialName { get; } = "Monero/ViewMoneroLikePaymentData"; } } diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index 85fead7a7..0ee720b1f 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -456,7 +456,8 @@ namespace BTCPayServer.Services.Invoices { cryptoInfo.PaymentUrls = new InvoicePaymentUrls() { - BOLT11 = $"lightning:{cryptoInfo.Address}" + BOLT11 = paymentId.PaymentType.GetPaymentLink(info.Network, details, cryptoInfo.Due, + ServerUrl) }; } else if (paymentId.PaymentType == PaymentTypes.BTCLike) @@ -466,15 +467,10 @@ namespace BTCPayServer.Services.Invoices minerInfo.SatoshiPerBytes = ((BitcoinLikeOnChainPaymentMethod)details).FeeRate .GetFee(1).Satoshi; dto.MinerFees.TryAdd(cryptoInfo.CryptoCode, minerInfo); - var bip21 = ((BTCPayNetwork)info.Network).GenerateBIP21(cryptoInfo.Address, cryptoInfo.Due); - - if ((details as BitcoinLikeOnChainPaymentMethod)?.PayjoinEnabled is true) + cryptoInfo.PaymentUrls = new InvoicePaymentUrls() { - bip21 += $"&{PayjoinClient.BIP21EndpointKey}={ServerUrl.WithTrailingSlash()}{cryptoCode}/{PayjoinClient.BIP21EndpointKey}"; - } - cryptoInfo.PaymentUrls = new NBitpayClient.InvoicePaymentUrls() - { - BIP21 = bip21, + BIP21 = paymentId.PaymentType.GetPaymentLink(info.Network, details, cryptoInfo.Due, + ServerUrl) }; #pragma warning disable 618