mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 22:44:29 +01:00
Refactor Wallet import code (#5638)
* Refactor Wallet import code The code for wallet import was incredibly messy as it evolved over time from various requests. This PR: * splits up each supported format into its own file * Supports taproot descriptors (through a hack until NBitcoin supports it internally) fixes #5518 * Reduces different paths for handling electrum/non-electrum xpubs * Allows plugins to add their own import support formats for onchain wallets. * Update NBitcoin to parse tr descriptors * Fix warnings * Use dedicated type OnChainWalletParsers --------- Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using BTCPayServer;
|
||||
using Newtonsoft.Json.Linq;
|
||||
namespace BTCPayServer.Services.WalletFileParsing;
|
||||
public class SpecterWalletFileParser : IWalletFileParser
|
||||
{
|
||||
private readonly OutputDescriptorWalletFileParser _outputDescriptorOnChainWalletParser;
|
||||
|
||||
public SpecterWalletFileParser(OutputDescriptorWalletFileParser outputDescriptorOnChainWalletParser)
|
||||
{
|
||||
_outputDescriptorOnChainWalletParser = outputDescriptorOnChainWalletParser;
|
||||
}
|
||||
public (DerivationSchemeSettings? DerivationSchemeSettings, string? Error) TryParse(BTCPayNetwork network,
|
||||
string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jobj = JObject.Parse(data);
|
||||
if (!jobj.TryGetValue("descriptor", StringComparison.InvariantCultureIgnoreCase, out var descriptorObj)
|
||||
|| !jobj.ContainsKey("blockheight")
|
||||
|| descriptorObj?.Value<string>() is not string desc)
|
||||
return (null, null);
|
||||
|
||||
|
||||
var result = _outputDescriptorOnChainWalletParser.TryParse(network, desc);
|
||||
if (result.DerivationSchemeSettings is not null)
|
||||
result.DerivationSchemeSettings.Source = "Specter";
|
||||
|
||||
if (result.DerivationSchemeSettings is not null && jobj.TryGetValue("label",
|
||||
StringComparison.InvariantCultureIgnoreCase, out var label) && label?.Value<string>() is string labelValue)
|
||||
result.DerivationSchemeSettings.Label = labelValue;
|
||||
return result;
|
||||
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user