From 09c761aa319be204b7ecfcd45c51b9f434ca1625 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 24 Jan 2024 22:53:24 +0900 Subject: [PATCH] Fix: Sometimes importing a wallet file from Electrum would fail --- BTCPayServer.Tests/FastTests.cs | 22 +++++++++++++++++++ .../ElectrumWalletFileParser.cs | 15 ++++++++++++- Changelog.md | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/BTCPayServer.Tests/FastTests.cs b/BTCPayServer.Tests/FastTests.cs index 340e7d5ea..299d583b9 100644 --- a/BTCPayServer.Tests/FastTests.cs +++ b/BTCPayServer.Tests/FastTests.cs @@ -1113,6 +1113,28 @@ bc1qfzu57kgu5jthl934f9xrdzzx8mmemx7gn07tf0grnvz504j6kzusu2v0ku Assert.Equal("fbb5b37d", electrum.GetSigningAccountKeySettings().RootFingerprint.ToString()); Assert.Equal("zpub6oQLDcJLztdMD29C8D8eyZKdKVfX9txB4BxZsMif9avJZBdVWPg1wmK3Uh3VxU7KXon1wm1xzvjyqmKWguYMqyjKP5f5Cho9f7uLfmRt2Br", electrum.AccountOriginal); Assert.Equal(((DirectDerivationStrategy)electrum.AccountDerivation).GetExtPubKeys().First().ParentFingerprint.ToString(), electrum.GetSigningAccountKeySettings().RootFingerprint.ToString()); + + // Electrum with strange garbage at the end caused by the lightning support + electrumText = +""" +{ +"keystore": { + "derivation": "m/0h", + "pw_hash_version": 1, + "root_fingerprint": "fbb5b37d", + "seed": "tiger room acoustic bracket thing film umbrella rather pepper tired vault remain", + "seed_type": "segwit", + "type": "bip32", + "xprv": "zprvAaQyp6mTAX53zY4j2BbecRNtmTq2kSEKgy2y4yK3bFPKgPJLxrMmPxzZdRkWq5XvmtH2R4ko5YmJYH2MgnVkWr32pHi4Dc5627WyML32KTW", + "xpub": "zpub6oQLDcJLztdMD29C8D8eyZKdKVfX9txB4BxZsMif9avJZBdVWPg1wmK3Uh3VxU7KXon1wm1xzvjyqmKWguYMqyjKP5f5Cho9f7uLfmRt2Br" +}, +"wallet_type": "standard", +"use_encryption": false, +"seed_type": "bip39" +}, +{"op": "remove", "path": "/channels"} +"""; + Assert.True(parsers.TryParseWalletFile(electrumText, mainnet, out electrum, out _)); } [Fact] diff --git a/BTCPayServer/Services/WalletFileParsing/ElectrumWalletFileParser.cs b/BTCPayServer/Services/WalletFileParsing/ElectrumWalletFileParser.cs index 886fa7db3..a2e6d2bb2 100644 --- a/BTCPayServer/Services/WalletFileParsing/ElectrumWalletFileParser.cs +++ b/BTCPayServer/Services/WalletFileParsing/ElectrumWalletFileParser.cs @@ -1,7 +1,10 @@ #nullable enable +using System; using System.Diagnostics.CodeAnalysis; +using System.IO; using NBitcoin; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace BTCPayServer.Services.WalletFileParsing; public class ElectrumWalletFileParser : IWalletFileParser { @@ -22,7 +25,7 @@ public class ElectrumWalletFileParser : IWalletFileParser public bool TryParse(BTCPayNetwork network, string data, [MaybeNullWhen(false)] out DerivationSchemeSettings derivationSchemeSettings) { derivationSchemeSettings = null; - var jobj = JsonConvert.DeserializeObject(data); + var jobj = DeserializeObject(data); if (jobj?.keystore is null) return false; @@ -52,4 +55,14 @@ public class ElectrumWalletFileParser : IWalletFileParser derivationSchemeSettings = result; return true; } + + private T? DeserializeObject(string data) + { + // We can't call JsonConvert.DeserializeObject directly + // because some export of Electrum file can have more than one + // JSON object separated by commas in the file + JsonTextReader reader = new JsonTextReader(new StringReader(data)); + var o = JObject.ReadFrom(reader); + return JsonConvert.DeserializeObject(o.ToString()); + } } diff --git a/Changelog.md b/Changelog.md index a9bb7b710..2e283e168 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,6 +13,7 @@ * Fix: Checkout v1 was not applying the custom style (#5628 #5615 #5616) @dennisreimann * Fix: Test email with multiple recipients was crashing (#5649 #5648) @dennisreimann * Fix: Test webhook for payment requests (#5680) @Kukks +* Fix: Sometimes importing a wallet file from Electrum would fail @NicolasDorier ### Improvements