Fix: Sometimes importing a wallet file from Electrum would fail

This commit is contained in:
nicolas.dorier 2024-01-24 22:53:24 +09:00
parent 8089a938f3
commit 09c761aa31
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
3 changed files with 37 additions and 1 deletions

View File

@ -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]

View File

@ -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<ElectrumFormat>(data);
var jobj = DeserializeObject<ElectrumFormat>(data);
if (jobj?.keystore is null)
return false;
@ -52,4 +55,14 @@ public class ElectrumWalletFileParser : IWalletFileParser
derivationSchemeSettings = result;
return true;
}
private T? DeserializeObject<T>(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<T>(o.ToString());
}
}

View File

@ -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