From 895462ac7f39d7a8f8dcca92b8a29061552ac8b9 Mon Sep 17 00:00:00 2001 From: d11n Date: Tue, 11 Oct 2022 05:19:10 +0200 Subject: [PATCH] Import xpub: Surface error details (#4205) Checks if the input is an output descriptor and explicitely handles that case instead of catching any errors. This allows us to display more detailed information about why an import might fail. --- .../Controllers/UIStoresController.Onchain.cs | 9 ++++----- BTCPayServer/Controllers/UIStoresController.cs | 16 +++++++--------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/BTCPayServer/Controllers/UIStoresController.Onchain.cs b/BTCPayServer/Controllers/UIStoresController.Onchain.cs index 0accff977..fa257cfad 100644 --- a/BTCPayServer/Controllers/UIStoresController.Onchain.cs +++ b/BTCPayServer/Controllers/UIStoresController.Onchain.cs @@ -119,17 +119,16 @@ namespace BTCPayServer.Controllers accountSettings.AccountKeyPath = vm.KeyPath == null ? null : KeyPath.Parse(vm.KeyPath); accountSettings.RootFingerprint = string.IsNullOrEmpty(vm.RootFingerprint) - ? (HDFingerprint?)null - : new HDFingerprint( - NBitcoin.DataEncoders.Encoders.Hex.DecodeData(vm.RootFingerprint)); + ? null + : new HDFingerprint(Encoders.Hex.DecodeData(vm.RootFingerprint)); } } vm.DerivationScheme = strategy.AccountDerivation.ToString(); ModelState.Remove(nameof(vm.DerivationScheme)); } - catch + catch (Exception ex) { - ModelState.AddModelError(nameof(vm.DerivationScheme), "Invalid wallet format"); + ModelState.AddModelError(nameof(vm.DerivationScheme), $"Invalid wallet format: {ex.Message}"); return View(vm.ViewName, vm); } } diff --git a/BTCPayServer/Controllers/UIStoresController.cs b/BTCPayServer/Controllers/UIStoresController.cs index 09c32160d..e49e303e0 100644 --- a/BTCPayServer/Controllers/UIStoresController.cs +++ b/BTCPayServer/Controllers/UIStoresController.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Abstractions.Constants; @@ -672,10 +673,10 @@ namespace BTCPayServer.Controllers private DerivationSchemeSettings ParseDerivationStrategy(string derivationScheme, BTCPayNetwork network) { var parser = new DerivationSchemeParser(network); - try + var isOD = Regex.Match(derivationScheme, @"\(.*?\)"); + if (isOD.Success) { - var derivationSchemeSettings = new DerivationSchemeSettings(); - derivationSchemeSettings.Network = network; + var derivationSchemeSettings = new DerivationSchemeSettings { Network = network }; var result = parser.ParseOutputDescriptor(derivationScheme); derivationSchemeSettings.AccountOriginal = derivationScheme.Trim(); derivationSchemeSettings.AccountDerivation = result.Item1; @@ -687,12 +688,9 @@ namespace BTCPayServer.Controllers }).ToArray() ?? new AccountKeySettings[result.Item1.GetExtPubKeys().Count()]; return derivationSchemeSettings; } - catch (Exception) - { - // ignored - } - - return new DerivationSchemeSettings(parser.Parse(derivationScheme), network); + + var strategy = parser.Parse(derivationScheme); + return new DerivationSchemeSettings(strategy, network); } [HttpGet]