From 2b11cc1077da194400490b73da8a872627866e81 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 12 Apr 2018 11:48:33 +0900 Subject: [PATCH] Simplify root key path calculation --- BTCPayServer/BTCPayNetwork.cs | 8 +++++++- .../Controllers/StoresController.BTCLike.cs | 6 ++---- .../StoreViewModels/DerivationSchemeViewModel.cs | 3 ++- BTCPayServer/Services/HardwareWalletService.cs | 16 ++++------------ .../Views/Stores/AddDerivationScheme.cshtml | 2 +- .../wwwroot/js/StoreAddDerivationScheme.js | 1 - 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/BTCPayServer/BTCPayNetwork.cs b/BTCPayServer/BTCPayNetwork.cs index 7bc0001ef..2b2bf4006 100644 --- a/BTCPayServer/BTCPayNetwork.cs +++ b/BTCPayServer/BTCPayNetwork.cs @@ -72,6 +72,12 @@ namespace BTCPayServer public override string ToString() { return CryptoCode; - } + } + + internal KeyPath GetRootKeyPath() + { + return new KeyPath(NBitcoinNetwork.Consensus.SupportSegwit ? "49'" : "44'") + .Derive(CoinType); + } } } diff --git a/BTCPayServer/Controllers/StoresController.BTCLike.cs b/BTCPayServer/Controllers/StoresController.BTCLike.cs index 9409041f7..0bb73d207 100644 --- a/BTCPayServer/Controllers/StoresController.BTCLike.cs +++ b/BTCPayServer/Controllers/StoresController.BTCLike.cs @@ -35,7 +35,7 @@ namespace BTCPayServer.Controllers DerivationSchemeViewModel vm = new DerivationSchemeViewModel(); vm.ServerUrl = GetStoreUrl(storeId); vm.CryptoCode = cryptoCode; - vm.RootKeyPath = network.NBitcoinNetwork.Consensus.SupportSegwit ? "49'" : "44'"; + vm.RootKeyPath = network.GetRootKeyPath(); SetExistingValues(store, vm); return View(vm); } @@ -69,7 +69,7 @@ namespace BTCPayServer.Controllers { return NotFound(); } - vm.RootKeyPath = network.NBitcoinNetwork.Consensus.SupportSegwit ? "49'" : "44'"; + vm.RootKeyPath = network.GetRootKeyPath(); var wallet = _WalletProvider.GetWallet(network); if (wallet == null) { @@ -258,8 +258,6 @@ namespace BTCPayServer.Controllers if (command == "getxpub") { var getxpubResult = await hw.GetExtPubKey(network, account); - ; - getxpubResult.CoinType = (int)(getxpubResult.KeyPath.Indexes[1] - 0x80000000); result = getxpubResult; } if (command == "getinfo") diff --git a/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs b/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs index b67313d72..7e7d844ed 100644 --- a/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/DerivationSchemeViewModel.cs @@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Rendering; +using NBitcoin; namespace BTCPayServer.Models.StoreViewModels { @@ -29,6 +30,6 @@ namespace BTCPayServer.Models.StoreViewModels public string ServerUrl { get; set; } public string StatusMessage { get; internal set; } - public string RootKeyPath { get; set; } + public KeyPath RootKeyPath { get; set; } } } diff --git a/BTCPayServer/Services/HardwareWalletService.cs b/BTCPayServer/Services/HardwareWalletService.cs index 01c4236ab..3a138e918 100644 --- a/BTCPayServer/Services/HardwareWalletService.cs +++ b/BTCPayServer/Services/HardwareWalletService.cs @@ -84,19 +84,12 @@ namespace BTCPayServer.Services throw new ArgumentNullException(nameof(network)); var segwit = network.NBitcoinNetwork.Consensus.SupportSegwit; - var path = new KeyPath(segwit ? "49'" : "44'").Derive(network.CoinType).Derive(account, true); + var path = network.GetRootKeyPath().Derive(account, true); var pubkey = await GetExtPubKey(_Ledger, network, path, false); - var derivation = new DerivationStrategyFactory(network.NBitcoinNetwork).CreateDirectDerivationStrategy(pubkey, - segwit ? - new DerivationStrategyOptions() + var derivation = new DerivationStrategyFactory(network.NBitcoinNetwork).CreateDirectDerivationStrategy(pubkey, new DerivationStrategyOptions() { - P2SH = true, - Legacy = false - } : - new DerivationStrategyOptions() - { - P2SH = false, - Legacy = true + P2SH = segwit, + Legacy = !segwit }); return new GetXPubResult() { ExtPubKey = derivation.ToString(), KeyPath = path }; } @@ -248,6 +241,5 @@ namespace BTCPayServer.Services public string ExtPubKey { get; set; } [JsonConverter(typeof(NBitcoin.JsonConverters.KeyPathJsonConverter))] public KeyPath KeyPath { get; set; } - public int CoinType { get; internal set; } } } diff --git a/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml b/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml index 6e45c93d8..51c24364b 100644 --- a/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml +++ b/BTCPayServer/Views/Stores/AddDerivationScheme.cshtml @@ -35,7 +35,7 @@ diff --git a/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js b/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js index caea00502..0bb2a1951 100644 --- a/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js +++ b/BTCPayServer/wwwroot/js/StoreAddDerivationScheme.js @@ -52,7 +52,6 @@ Write('check', 'success', 'This store is configured to use your ledger'); $("#no-ledger-info").css("display", "none"); $("#ledger-info").css("display", "block"); - $(".ledger-info-cointype").text(result.coinType); } }); };