From d5019f61ce7b2793f73509b823e0b68762baa133 Mon Sep 17 00:00:00 2001 From: Kukks Date: Mon, 12 Jul 2021 14:17:25 +0200 Subject: [PATCH] Expose ExplorerClientProvider to plugins via interface --- BTCPayServer.Common/Extensions.cs | 20 ++++++++++++++++++ .../IExplorerClientProvider.cs | 10 +++++++++ BTCPayServer/ExplorerClientProvider.cs | 3 ++- BTCPayServer/Extensions.cs | 21 ------------------- BTCPayServer/Hosting/BTCPayServerServices.cs | 6 +++--- 5 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 BTCPayServer.Common/IExplorerClientProvider.cs diff --git a/BTCPayServer.Common/Extensions.cs b/BTCPayServer.Common/Extensions.cs index c2d0fe45b..40274cc6d 100644 --- a/BTCPayServer.Common/Extensions.cs +++ b/BTCPayServer.Common/Extensions.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using NBitcoin; +using NBXplorer.DerivationStrategy; namespace BTCPayServer { @@ -11,5 +13,23 @@ namespace BTCPayServer hashSet.Add(item); } } + + public static ScriptPubKeyType ScriptPubKeyType(this DerivationStrategyBase derivationStrategyBase) + { + if (IsSegwitCore(derivationStrategyBase)) + { + return NBitcoin.ScriptPubKeyType.Segwit; + } + + return (derivationStrategyBase is P2SHDerivationStrategy p2shStrat && IsSegwitCore(p2shStrat.Inner)) + ? NBitcoin.ScriptPubKeyType.SegwitP2SH + : NBitcoin.ScriptPubKeyType.Legacy; + } + + private static bool IsSegwitCore(DerivationStrategyBase derivationStrategyBase) + { + return (derivationStrategyBase is P2WSHDerivationStrategy) || + (derivationStrategyBase is DirectDerivationStrategy direct) && direct.Segwit; + } } } diff --git a/BTCPayServer.Common/IExplorerClientProvider.cs b/BTCPayServer.Common/IExplorerClientProvider.cs new file mode 100644 index 000000000..4760e529b --- /dev/null +++ b/BTCPayServer.Common/IExplorerClientProvider.cs @@ -0,0 +1,10 @@ +using NBXplorer; + +namespace BTCPayServer.Common +{ + public interface IExplorerClientProvider + { + ExplorerClient GetExplorerClient(string cryptoCode); + ExplorerClient GetExplorerClient(BTCPayNetworkBase network); + } +} diff --git a/BTCPayServer/ExplorerClientProvider.cs b/BTCPayServer/ExplorerClientProvider.cs index e088d4055..027abdf32 100644 --- a/BTCPayServer/ExplorerClientProvider.cs +++ b/BTCPayServer/ExplorerClientProvider.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using BTCPayServer.Common; using BTCPayServer.Configuration; using BTCPayServer.HostedServices; using BTCPayServer.Logging; @@ -11,7 +12,7 @@ using NBXplorer; namespace BTCPayServer { - public class ExplorerClientProvider + public class ExplorerClientProvider : IExplorerClientProvider { readonly BTCPayNetworkProvider _NetworkProviders; diff --git a/BTCPayServer/Extensions.cs b/BTCPayServer/Extensions.cs index d0e14c65c..0e8c3c9b8 100644 --- a/BTCPayServer/Extensions.cs +++ b/BTCPayServer/Extensions.cs @@ -204,27 +204,6 @@ namespace BTCPayServer resp.Headers[name] = value; } - public static bool IsSegwit(this DerivationStrategyBase derivationStrategyBase) - { - return ScriptPubKeyType(derivationStrategyBase) != NBitcoin.ScriptPubKeyType.Legacy; - } - public static ScriptPubKeyType ScriptPubKeyType(this DerivationStrategyBase derivationStrategyBase) - { - if (IsSegwitCore(derivationStrategyBase)) - { - return NBitcoin.ScriptPubKeyType.Segwit; - } - - return (derivationStrategyBase is P2SHDerivationStrategy p2shStrat && IsSegwitCore(p2shStrat.Inner)) - ? NBitcoin.ScriptPubKeyType.SegwitP2SH - : NBitcoin.ScriptPubKeyType.Legacy; - } - private static bool IsSegwitCore(DerivationStrategyBase derivationStrategyBase) - { - return (derivationStrategyBase is P2WSHDerivationStrategy) || - (derivationStrategyBase is DirectDerivationStrategy direct) && direct.Segwit; - } - public static bool IsLocalNetwork(string server) { if (server == null) diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index b9db7bdd9..ee26756ab 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -1,12 +1,11 @@ using System; using System.IO; using System.Linq; -using System.Runtime.InteropServices.ComTypes; -using System.Security.Cryptography; using System.Threading; using BTCPayServer.Abstractions.Contracts; using BTCPayServer.Abstractions.Extensions; using BTCPayServer.Abstractions.Models; +using BTCPayServer.Common; using BTCPayServer.Configuration; using BTCPayServer.Controllers; using BTCPayServer.Data; @@ -19,7 +18,6 @@ using BTCPayServer.Payments.Bitcoin; using BTCPayServer.Payments.Lightning; using BTCPayServer.Payments.PayJoin; using BTCPayServer.Plugins; -using BTCPayServer.Plugins.Shopify; using BTCPayServer.Security; using BTCPayServer.Security.Bitpay; using BTCPayServer.Security.GreenField; @@ -363,6 +361,8 @@ namespace BTCPayServer.Hosting services.AddSingleton(); #endif services.TryAddSingleton(); + services.AddSingleton(x => + x.GetRequiredService()); services.TryAddSingleton(o => { if (o.GetRequiredService().NetworkType == ChainName.Mainnet)