From 7ac6cad18cff32cfaf14c84bf49a887e41588394 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 12 Nov 2024 15:26:30 +0900 Subject: [PATCH] Be more flexible on derivation scheme parsing --- BTCPayServer.Tests/FastTests.cs | 11 +++++++---- BTCPayServer/DerivationSchemeParser.cs | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/BTCPayServer.Tests/FastTests.cs b/BTCPayServer.Tests/FastTests.cs index d2494d46b..07c1a5676 100644 --- a/BTCPayServer.Tests/FastTests.cs +++ b/BTCPayServer.Tests/FastTests.cs @@ -868,9 +868,12 @@ namespace BTCPayServer.Tests Assert.IsType(((P2WSHDerivationStrategy)strategyBase).Inner); Assert.Equal(expected, strategyBase.ToString()); - var expectedWithNewLines = "2-of-tpubDDXgATYzdQkHHhZZCMcNJj8BGDENvzMVou5v9NdxiP4rxDLj33nS233dGFW4htpVZSJ6zds9eVqAV9RyRHHiKtwQKX8eR4n4KN3Dwmj7A3h-\r\ntpubDC8a54NFtQtMQAZ97VhoU9V6jVTvi9w4Y5SaAXJSBYETKg3AoX5CCKndznhPWxJUBToPCpT44s86QbKdGpKAnSjcMTGW4kE6UQ8vpBjcybW-tpubDChjnP9LXNrJp43biqjY7FH93wgRRNrNxB4Q8pH7PPRy8UPcH2S6V46WGVJ47zVGF7SyBJNCpnaogsFbsybVQckGtVhCkng3EtFn8qmxptS"; - strategyBase = parser.Parse(expectedWithNewLines); - Assert.Equal(expected, strategyBase.ToString()); + foreach (var space in new[] { "\r\n", " ", "\t" }) + { + var expectedWithNewLines = $"2-of-tpubDDXgATYzdQkHHhZZCMcNJj8BGDENvzMVou5v9NdxiP4rxDLj33nS233dGFW4htpVZSJ6zds9eVqAV9RyRHHiKtwQKX8eR4n4KN3Dwmj7A3h-{space}tpubDC8a54NFtQtMQAZ97VhoU9V6jVTvi9w4Y5SaAXJSBYETKg3AoX5CCKndznhPWxJUBToPCpT44s86QbKdGpKAnSjcMTGW4kE6UQ8vpBjcybW-tpubDChjnP9LXNrJp43biqjY7FH93wgRRNrNxB4Q8pH7PPRy8UPcH2S6V46WGVJ47zVGF7SyBJNCpnaogsFbsybVQckGtVhCkng3EtFn8qmxptS"; + strategyBase = parser.Parse(expectedWithNewLines); + Assert.Equal(expected, strategyBase.ToString()); + } var inner = (MultisigDerivationStrategy)((P2WSHDerivationStrategy)strategyBase).Inner; Assert.False(inner.IsLegacy); @@ -888,7 +891,7 @@ namespace BTCPayServer.Tests Assert.True(((DirectDerivationStrategy)strategyBase).Segwit); // Failure cases - Assert.Throws(() => { parser.Parse("xpub 661MyMwAqRbcGVBsTGeNZN6QGVHmMHLdSA4FteGsRrEriu4pnVZMZWnruFFFXkMnyoBjyHndD3Qwcfz4MPzBUxjSevweNFQx7SAYZATtcDw"); }); // invalid format because of space + Assert.Throws(() => { parser.Parse("xpubZ661MyMwAqRbcGVBsTGeNZN6QGVHmMHLdSA4FteGsRrEriu4pnVZMZWnruFFFXkMnyoBjyHndD3Qwcfz4MPzBUxjSevweNFQx7SAYZATtcDw"); }); Assert.Throws(() => { parser.ParseOutputDescriptor("invalid"); }); // invalid in general Assert.Throws(() => { parser.ParseOutputDescriptor("wpkh([8b60afd1/49h/0h/0h]xpub661MyMwAFXkMnyoBjyHndD3QwRbcGVBsTGeNZN6QGVHcfz4MPzBUxjSevweNFQx7SqmMHLdSA4FteGsRrEriu4pnVZMZWnruFFAYZATtcDw/0/*)#9x4vkw48"); }); // invalid checksum } diff --git a/BTCPayServer/DerivationSchemeParser.cs b/BTCPayServer/DerivationSchemeParser.cs index fa895d841..69d4270d5 100644 --- a/BTCPayServer/DerivationSchemeParser.cs +++ b/BTCPayServer/DerivationSchemeParser.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Text.RegularExpressions; using NBitcoin; using NBitcoin.Scripting; using NBXplorer.DerivationStrategy; @@ -100,7 +101,7 @@ namespace BTCPayServer { ArgumentNullException.ThrowIfNull(str); str = str.Trim(); - str = str.Replace("\r\n","").Replace("\n", ""); + str = Regex.Replace(str, @"\s+", ""); HashSet hintedLabels = new HashSet(); if (!Network.Consensus.SupportSegwit) {