Refactor ElectrumMapping with proper enum

This commit is contained in:
nicolas.dorier 2019-05-09 19:05:08 +09:00
parent 51faa39636
commit bbeb2d5009
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
6 changed files with 40 additions and 38 deletions

View File

@ -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<string>();
public bool SupportRBF { get; internal set; }
public Dictionary<uint, string[]> ElectrumMapping = new Dictionary<uint, string[]>();
public Dictionary<uint, DerivationType> ElectrumMapping = new Dictionary<uint, DerivationType>();
public override string ToString()
{
return CryptoCode;

View File

@ -29,17 +29,17 @@ namespace BTCPayServer
SupportRBF = true,
//https://github.com/spesmilo/electrum/blob/11733d6bc271646a00b69ff07657119598874da4/electrum/constants.py
ElectrumMapping = NetworkType == NetworkType.Mainnet
? new Dictionary<uint, string[]>()
? new Dictionary<uint, DerivationType>()
{
{0x0488b21eU, new[] {"legacy"}}, // xpub
{0x049d7cb2U, new[] {"p2sh"}}, // ypub
{0x4b24746U, Array.Empty<string>()}, //zpub
{0x0488b21eU, DerivationType.Legacy }, // xpub
{0x049d7cb2U, DerivationType.SegwitP2SH }, // ypub
{0x4b24746U, DerivationType.Segwit }, //zpub
}
: new Dictionary<uint, string[]>()
: new Dictionary<uint, DerivationType>()
{
{0x043587cfU, new[] {"legacy"}},
{0x044a5262U, new[] {"p2sh"}},
{0x045f1cf6U, Array.Empty<string>()}
{0x043587cfU, DerivationType.Legacy},
{0x044a5262U, DerivationType.SegwitP2SH},
{0x045f1cf6U, DerivationType.Segwit}
}
});
}

View File

@ -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<uint, string[]>()
{
{0x0488b21eU, new[] {"legacy"}},
{0x049d7cb2U, new[] {"p2sh"}},
{0x04b24746U, Array.Empty<string>()},
}
: new Dictionary<uint, string[]>()
{
{0x043587cfU, new[] {"legacy"}},
{0x044a5262U, new[] {"p2sh"}},
{0x045f1cf6U, Array.Empty<string>()}
}
CoinType = NetworkType == NetworkType.Mainnet ? new KeyPath("17'") : new KeyPath("1'")
});
}
}

View File

@ -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<uint, string[]>()
? new Dictionary<uint, DerivationType>()
{
{0x0488b21eU, new[] {"legacy"}},
{0x049d7cb2U, new[] {"p2sh"}},
{0x04b24746U, Array.Empty<string>()},
{0x0488b21eU, DerivationType.Legacy },
{0x049d7cb2U, DerivationType.SegwitP2SH },
{0x04b24746U, DerivationType.Segwit },
}
: new Dictionary<uint, string[]>()
: new Dictionary<uint, DerivationType>()
{
{0x043587cfU, new[] {"legacy"}},
{0x044a5262U, new[] {"p2sh"}},
{0x045f1cf6U, Array.Empty<string>()}
{0x043587cfU, DerivationType.Legacy },
{0x044a5262U, DerivationType.SegwitP2SH },
{0x045f1cf6U, DerivationType.Segwit }
}
});
}

View File

@ -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);
}
}

View File

@ -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<string> labels = new List<string>();
switch (type)
{
case DerivationType.Legacy:
labels.Add("legacy");
break;
case DerivationType.SegwitP2SH:
labels.Add("p2sh");
break;
}
if (labels != null)
{
foreach (var label in labels)