From 35f669aa155ea77db4fff5f038c99dda2d667136 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 19 Feb 2018 11:06:08 +0900 Subject: [PATCH] Isolating code of on chain specific payment in its own folder --- .../Controllers/InvoiceController.UI.cs | 4 +- BTCPayServer/Controllers/InvoiceController.cs | 2 +- BTCPayServer/Hosting/BTCPayServerServices.cs | 2 +- .../BitcoinLikeOnChainPaymentMethod.cs | 43 +++++++++++++++++++ .../BitcoinLikePaymentData.cs} | 34 +-------------- .../Bitcoin}/NBXplorerListener.cs | 5 ++- BTCPayServer/Payments/IPaymentMethod.cs | 16 +++++++ .../Services/Invoices/InvoiceEntity.cs | 25 ++++------- .../Services/Invoices/InvoiceRepository.cs | 3 +- 9 files changed, 77 insertions(+), 57 deletions(-) create mode 100644 BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs rename BTCPayServer/Payments/{BitcoinPayment.cs => Bitcoin/BitcoinLikePaymentData.cs} (69%) rename BTCPayServer/{HostedServices => Payments/Bitcoin}/NBXplorerListener.cs (99%) create mode 100644 BTCPayServer/Payments/IPaymentMethod.cs diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index f8e953862..2fcba5c88 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -74,7 +74,7 @@ namespace BTCPayServer.Controllers cryptoPayment.Due = accounting.Due.ToString() + $" {paymentNetwork.CryptoCode}"; cryptoPayment.Paid = accounting.CryptoPaid.ToString() + $" {paymentNetwork.CryptoCode}"; - var onchainMethod = data.GetPaymentMethod() as BitcoinLikeOnChainPaymentMethod; + var onchainMethod = data.GetPaymentMethod() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod; if(onchainMethod != null) { cryptoPayment.Address = onchainMethod.DepositAddress.ToString(); @@ -89,7 +89,7 @@ namespace BTCPayServer.Controllers .Where(p => p.GetCryptoDataId().PaymentType == PaymentTypes.BTCLike) .Select(async payment => { - var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData(); + var paymentData = (Payments.Bitcoin.BitcoinLikePaymentData)payment.GetCryptoPaymentData(); var m = new InvoiceDetailsModel.Payment(); var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode()); m.CryptoCode = payment.GetCryptoCode(); diff --git a/BTCPayServer/Controllers/InvoiceController.cs b/BTCPayServer/Controllers/InvoiceController.cs index 24506240f..dcc9b78a8 100644 --- a/BTCPayServer/Controllers/InvoiceController.cs +++ b/BTCPayServer/Controllers/InvoiceController.cs @@ -142,7 +142,7 @@ namespace BTCPayServer.Controllers { CryptoData cryptoData = new CryptoData(); cryptoData.SetId(new CryptoDataId(q.network.CryptoCode, PaymentTypes.BTCLike)); - BitcoinLikeOnChainPaymentMethod onchainMethod = new BitcoinLikeOnChainPaymentMethod(); + Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod onchainMethod = new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod(); onchainMethod.FeeRate = (await q.getFeeRate); onchainMethod.TxFee = GetTxFee(storeBlob, onchainMethod.FeeRate); // assume price for 100 bytes cryptoData.Rate = await q.getRate; diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index b8f076bef..322f2c4bf 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -143,7 +143,7 @@ namespace BTCPayServer.Hosting }); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs b/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs new file mode 100644 index 000000000..615608d7b --- /dev/null +++ b/BTCPayServer/Payments/Bitcoin/BitcoinLikeOnChainPaymentMethod.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BTCPayServer.Services.Invoices; +using NBitcoin; +using Newtonsoft.Json; + +namespace BTCPayServer.Payments.Bitcoin +{ + public class BitcoinLikeOnChainPaymentMethod : IPaymentMethod + { + public PaymentTypes GetPaymentType() + { + return PaymentTypes.BTCLike; + } + + public string GetPaymentDestination() + { + return DepositAddress?.ToString(); + } + + public Money GetTxFee() + { + return TxFee; + } + + public void SetPaymentDestination(string newPaymentDestination) + { + if (newPaymentDestination == null) + DepositAddress = null; + else + DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network); + } + + [JsonIgnore] + public FeeRate FeeRate { get; set; } + [JsonIgnore] + public Money TxFee { get; set; } + [JsonIgnore] + public BitcoinAddress DepositAddress { get; set; } + } +} diff --git a/BTCPayServer/Payments/BitcoinPayment.cs b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs similarity index 69% rename from BTCPayServer/Payments/BitcoinPayment.cs rename to BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs index 8d2bdff42..6f73d67a5 100644 --- a/BTCPayServer/Payments/BitcoinPayment.cs +++ b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs @@ -6,40 +6,8 @@ using BTCPayServer.Services.Invoices; using NBitcoin; using Newtonsoft.Json; -namespace BTCPayServer.Payments +namespace BTCPayServer.Payments.Bitcoin { - public class BitcoinLikeOnChainPaymentMethod : IPaymentMethod - { - public PaymentTypes GetPaymentType() - { - return PaymentTypes.BTCLike; - } - - public string GetPaymentDestination() - { - return DepositAddress?.ToString(); - } - - public Money GetTxFee() - { - return TxFee; - } - - public void SetPaymentDestination(string newPaymentDestination) - { - if (newPaymentDestination == null) - DepositAddress = null; - else - DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network); - } - - [JsonIgnore] - public FeeRate FeeRate { get; set; } - [JsonIgnore] - public Money TxFee { get; set; } - [JsonIgnore] - public BitcoinAddress DepositAddress { get; set; } - } public class BitcoinLikePaymentData : CryptoPaymentData { diff --git a/BTCPayServer/HostedServices/NBXplorerListener.cs b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs similarity index 99% rename from BTCPayServer/HostedServices/NBXplorerListener.cs rename to BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs index f370229b3..34f972a87 100644 --- a/BTCPayServer/HostedServices/NBXplorerListener.cs +++ b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs @@ -17,8 +17,9 @@ using BTCPayServer.Services.Wallets; using NBitcoin; using NBXplorer.Models; using BTCPayServer.Payments; +using BTCPayServer.HostedServices; -namespace BTCPayServer.HostedServices +namespace BTCPayServer.Payments.Bitcoin { public class NBXplorerListener : IHostedService { @@ -353,7 +354,7 @@ namespace BTCPayServer.HostedServices var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData(); var invoice = (await UpdatePaymentStates(wallet, invoiceId)); var cryptoData = invoice.GetCryptoData(wallet.Network, PaymentTypes.BTCLike, _ExplorerClients.NetworkProviders); - var method = cryptoData.GetPaymentMethod() as Payments.BitcoinLikeOnChainPaymentMethod; + var method = cryptoData.GetPaymentMethod() as BitcoinLikeOnChainPaymentMethod; if (method.DepositAddress.ScriptPubKey == paymentData.Output.ScriptPubKey && cryptoData.Calculate().Due > Money.Zero) { var address = await wallet.ReserveAddressAsync(strategy); diff --git a/BTCPayServer/Payments/IPaymentMethod.cs b/BTCPayServer/Payments/IPaymentMethod.cs new file mode 100644 index 000000000..7036a2e95 --- /dev/null +++ b/BTCPayServer/Payments/IPaymentMethod.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using NBitcoin; + +namespace BTCPayServer.Payments +{ + public interface IPaymentMethod + { + string GetPaymentDestination(); + PaymentTypes GetPaymentType(); + Money GetTxFee(); + void SetPaymentDestination(string newPaymentDestination); + } +} diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index 6d7cfa84d..7a5782bf1 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -366,8 +366,7 @@ namespace BTCPayServer.Services.Invoices cryptoInfo.TxCount = accounting.TxCount; cryptoInfo.CryptoPaid = accounting.CryptoPaid.ToString(); - if (info.GetPaymentMethod() is BitcoinLikeOnChainPaymentMethod onchainMethod) - cryptoInfo.Address = onchainMethod.DepositAddress?.ToString(); + cryptoInfo.Address = info.GetPaymentMethod()?.GetPaymentDestination(); cryptoInfo.ExRates = new Dictionary { { ProductInformation.Currency, (double)cryptoInfo.Rate } @@ -524,14 +523,6 @@ namespace BTCPayServer.Services.Invoices public Money NetworkFee { get; set; } } - public interface IPaymentMethod - { - string GetPaymentDestination(); - PaymentTypes GetPaymentType(); - Money GetTxFee(); - void SetPaymentDestination(string newPaymentDestination); - } - public class CryptoDataId { public CryptoDataId(string cryptoCode, PaymentTypes paymentType) @@ -627,7 +618,7 @@ namespace BTCPayServer.Services.Invoices // Legacy, old code does not have PaymentMethods if (string.IsNullOrEmpty(PaymentType) || PaymentMethod == null) { - return new BitcoinLikeOnChainPaymentMethod() + return new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod() { FeeRate = FeeRate, DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork), @@ -639,7 +630,7 @@ namespace BTCPayServer.Services.Invoices if (GetId().PaymentType == PaymentTypes.BTCLike) { - var method = DeserializePaymentMethod(PaymentMethod); + var method = DeserializePaymentMethod(PaymentMethod); method.TxFee = TxFee; method.DepositAddress = BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork); method.FeeRate = FeeRate; @@ -665,7 +656,7 @@ namespace BTCPayServer.Services.Invoices else if (PaymentType != paymentMethod.GetPaymentType().ToString()) throw new InvalidOperationException("Invalid payment method affected"); - if (paymentMethod is BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod) + if (paymentMethod is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod) { TxFee = bitcoinPaymentMethod.TxFee; FeeRate = bitcoinPaymentMethod.FeeRate; @@ -673,7 +664,7 @@ namespace BTCPayServer.Services.Invoices } var jobj = JObject.Parse(JsonConvert.SerializeObject(paymentMethod)); PaymentMethod = jobj; - + #pragma warning restore CS0618 // Type or member is obsolete } @@ -792,7 +783,7 @@ namespace BTCPayServer.Services.Invoices if (string.IsNullOrEmpty(CryptoPaymentDataType)) { // In case this is a payment done before this update, consider it unconfirmed with RBF for safety - var paymentData = new BitcoinLikePaymentData(); + var paymentData = new Payments.Bitcoin.BitcoinLikePaymentData(); paymentData.Outpoint = Outpoint; paymentData.Output = Output; paymentData.RBF = true; @@ -802,7 +793,7 @@ namespace BTCPayServer.Services.Invoices } if (GetCryptoDataId().PaymentType == PaymentTypes.BTCLike) { - var paymentData = JsonConvert.DeserializeObject(CryptoPaymentData); + var paymentData = JsonConvert.DeserializeObject(CryptoPaymentData); // legacy paymentData.Output = Output; paymentData.Outpoint = Outpoint; @@ -816,7 +807,7 @@ namespace BTCPayServer.Services.Invoices public void SetCryptoPaymentData(CryptoPaymentData cryptoPaymentData) { #pragma warning disable CS0618 - if (cryptoPaymentData is BitcoinLikePaymentData paymentData) + if (cryptoPaymentData is Payments.Bitcoin.BitcoinLikePaymentData paymentData) { // Legacy Outpoint = paymentData.Outpoint; diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 8404afaa2..c5347540b 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -18,6 +18,7 @@ using BTCPayServer.Data; using System.Globalization; using BTCPayServer.Models.InvoicingModels; using BTCPayServer.Logging; +using BTCPayServer.Payments; namespace BTCPayServer.Services.Invoices { @@ -166,7 +167,7 @@ namespace BTCPayServer.Services.Invoices ScriptId hash = null; if (cryptoData.GetId().PaymentType == Payments.PaymentTypes.BTCLike) { - hash = ((Payments.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash; + hash = ((Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash; } return hash; }