From bbeb2d50090cfa1d964004301209987b84ad1656 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 9 May 2019 19:05:08 +0900 Subject: [PATCH] Refactor ElectrumMapping with proper enum --- BTCPayServer/BTCPayNetwork.cs | 8 +++++++- BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs | 16 +++++++-------- .../BTCPayNetworkProvider.Groestlcoin.cs | 16 +-------------- .../BTCPayNetworkProvider.Litecoin.cs | 16 +++++++-------- BTCPayServer/BTCPayNetworkProvider.cs | 2 +- BTCPayServer/DerivationSchemeParser.cs | 20 ++++++++++++++----- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/BTCPayServer/BTCPayNetwork.cs b/BTCPayServer/BTCPayNetwork.cs index c6325e765..8cdfaa093 100644 --- a/BTCPayServer/BTCPayNetwork.cs +++ b/BTCPayServer/BTCPayNetwork.cs @@ -10,6 +10,12 @@ using NBXplorer; namespace BTCPayServer { + public enum DerivationType + { + Legacy, + SegwitP2SH, + Segwit + } public class BTCPayDefaultSettings { static BTCPayDefaultSettings() @@ -65,7 +71,7 @@ namespace BTCPayServer public int MaxTrackedConfirmation { get; internal set; } = 6; public string[] DefaultRateRules { get; internal set; } = Array.Empty(); public bool SupportRBF { get; internal set; } - public Dictionary ElectrumMapping = new Dictionary(); + public Dictionary ElectrumMapping = new Dictionary(); public override string ToString() { return CryptoCode; diff --git a/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs b/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs index b3a6ba5a0..22b333e21 100644 --- a/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs +++ b/BTCPayServer/BTCPayNetworkProvider.Bitcoin.cs @@ -29,17 +29,17 @@ namespace BTCPayServer SupportRBF = true, //https://github.com/spesmilo/electrum/blob/11733d6bc271646a00b69ff07657119598874da4/electrum/constants.py ElectrumMapping = NetworkType == NetworkType.Mainnet - ? new Dictionary() + ? new Dictionary() { - {0x0488b21eU, new[] {"legacy"}}, // xpub - {0x049d7cb2U, new[] {"p2sh"}}, // ypub - {0x4b24746U, Array.Empty()}, //zpub + {0x0488b21eU, DerivationType.Legacy }, // xpub + {0x049d7cb2U, DerivationType.SegwitP2SH }, // ypub + {0x4b24746U, DerivationType.Segwit }, //zpub } - : new Dictionary() + : new Dictionary() { - {0x043587cfU, new[] {"legacy"}}, - {0x044a5262U, new[] {"p2sh"}}, - {0x045f1cf6U, Array.Empty()} + {0x043587cfU, DerivationType.Legacy}, + {0x044a5262U, DerivationType.SegwitP2SH}, + {0x045f1cf6U, DerivationType.Segwit} } }); } diff --git a/BTCPayServer/BTCPayNetworkProvider.Groestlcoin.cs b/BTCPayServer/BTCPayNetworkProvider.Groestlcoin.cs index cd4f793f2..c20bd80bd 100644 --- a/BTCPayServer/BTCPayNetworkProvider.Groestlcoin.cs +++ b/BTCPayServer/BTCPayNetworkProvider.Groestlcoin.cs @@ -29,21 +29,7 @@ namespace BTCPayServer CryptoImagePath = "imlegacy/groestlcoin.png", LightningImagePath = "imlegacy/groestlcoin-lightning.svg", DefaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NetworkType), - CoinType = NetworkType == NetworkType.Mainnet ? new KeyPath("17'") : new KeyPath("1'"), - //https://github.com/Groestlcoin/electrum-grs/blob/6799baba60305164126a92b52e5e95284ed44543/electrum_grs/constants.py - ElectrumMapping = NetworkType == NetworkType.Mainnet - ? new Dictionary() - { - {0x0488b21eU, new[] {"legacy"}}, - {0x049d7cb2U, new[] {"p2sh"}}, - {0x04b24746U, Array.Empty()}, - } - : new Dictionary() - { - {0x043587cfU, new[] {"legacy"}}, - {0x044a5262U, new[] {"p2sh"}}, - {0x045f1cf6U, Array.Empty()} - } + CoinType = NetworkType == NetworkType.Mainnet ? new KeyPath("17'") : new KeyPath("1'") }); } } diff --git a/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs b/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs index c8e01b90d..26b321d68 100644 --- a/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs +++ b/BTCPayServer/BTCPayNetworkProvider.Litecoin.cs @@ -29,17 +29,17 @@ namespace BTCPayServer CoinType = NetworkType == NetworkType.Mainnet ? new KeyPath("2'") : new KeyPath("1'"), //https://github.com/pooler/electrum-ltc/blob/0d6989a9d2fb2edbea421c116e49d1015c7c5a91/electrum_ltc/constants.py ElectrumMapping = NetworkType == NetworkType.Mainnet - ? new Dictionary() + ? new Dictionary() { - {0x0488b21eU, new[] {"legacy"}}, - {0x049d7cb2U, new[] {"p2sh"}}, - {0x04b24746U, Array.Empty()}, + {0x0488b21eU, DerivationType.Legacy }, + {0x049d7cb2U, DerivationType.SegwitP2SH }, + {0x04b24746U, DerivationType.Segwit }, } - : new Dictionary() + : new Dictionary() { - {0x043587cfU, new[] {"legacy"}}, - {0x044a5262U, new[] {"p2sh"}}, - {0x045f1cf6U, Array.Empty()} + {0x043587cfU, DerivationType.Legacy }, + {0x044a5262U, DerivationType.SegwitP2SH }, + {0x045f1cf6U, DerivationType.Segwit } } }); } diff --git a/BTCPayServer/BTCPayNetworkProvider.cs b/BTCPayServer/BTCPayNetworkProvider.cs index 1d9a6e92a..ec40cb534 100644 --- a/BTCPayServer/BTCPayNetworkProvider.cs +++ b/BTCPayServer/BTCPayNetworkProvider.cs @@ -66,7 +66,7 @@ namespace BTCPayServer { network.Value.ElectrumMapping = network.Value.ElectrumMapping - .Where(kv => kv.Value.Contains("legacy")) + .Where(kv => kv.Value == DerivationType.Legacy) .ToDictionary(k => k.Key, k => k.Value); } } diff --git a/BTCPayServer/DerivationSchemeParser.cs b/BTCPayServer/DerivationSchemeParser.cs index afff7db41..b734e469c 100644 --- a/BTCPayServer/DerivationSchemeParser.cs +++ b/BTCPayServer/DerivationSchemeParser.cs @@ -43,15 +43,15 @@ namespace BTCPayServer for (int ii = 0; ii < 4; ii++) data[ii] = standardPrefix[ii]; var extPubKey = new BitcoinExtPubKey(Network.GetBase58CheckEncoder().EncodeData(data), Network.Main).ToNetwork(Network); - if (!BtcPayNetwork.ElectrumMapping.TryGetValue(prefix, out string[] labels)) + if (!BtcPayNetwork.ElectrumMapping.TryGetValue(prefix, out var type)) { throw new FormatException(); } - if (labels.Length == 0) + if (type == DerivationType.Segwit) return new DirectDerivationStrategy(extPubKey) { Segwit = true }; - if (labels[0] == "legacy") + if (type == DerivationType.Legacy) return new DirectDerivationStrategy(extPubKey) { Segwit = false }; - if (labels[0] == "p2sh") // segwit p2sh + if (type == DerivationType.SegwitP2SH) return new DerivationStrategyFactory(Network).Parse(extPubKey.ToString() + "-[p2sh]"); throw new FormatException(); } @@ -118,7 +118,17 @@ namespace BTCPayServer data[ii] = standardPrefix[ii]; var derivationScheme = new BitcoinExtPubKey(Network.GetBase58CheckEncoder().EncodeData(data), Network.Main).ToNetwork(Network).ToString(); - BtcPayNetwork.ElectrumMapping.TryGetValue(prefix, out string[] labels); + BtcPayNetwork.ElectrumMapping.TryGetValue(prefix, out var type); + List labels = new List(); + switch (type) + { + case DerivationType.Legacy: + labels.Add("legacy"); + break; + case DerivationType.SegwitP2SH: + labels.Add("p2sh"); + break; + } if (labels != null) { foreach (var label in labels)