btcpayserver/BTCPayServer/Services/WalletFileParsers.cs
Andrew Camilleri c5a0e28420
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>
2024-01-17 18:08:39 +09:00

47 lines
1.4 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using NBitcoin.DataEncoders;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using BTCPayServer.Services.WalletFileParsing;
namespace BTCPayServer.Services;
public class WalletFileParsers
{
public WalletFileParsers(IEnumerable<IWalletFileParser> parsers)
{
Parsers = parsers;
}
public IEnumerable<IWalletFileParser> Parsers { get; }
public bool TryParseWalletFile(string fileContents, BTCPayNetwork network, [MaybeNullWhen(false)] out DerivationSchemeSettings settings, [MaybeNullWhen(true)] out string error)
{
settings = null;
error = string.Empty;
ArgumentNullException.ThrowIfNull(fileContents);
ArgumentNullException.ThrowIfNull(network);
if (HexEncoder.IsWellFormed(fileContents))
{
fileContents = Encoding.UTF8.GetString(Encoders.Hex.DecodeData(fileContents));
}
foreach (IWalletFileParser onChainWalletParser in Parsers)
{
var result = onChainWalletParser.TryParse(network, fileContents);
if (result.DerivationSchemeSettings is not null)
{
settings = result.DerivationSchemeSettings;
error = null;
return true;
}
if (result.Error is not null)
{
error = result.Error;
}
}
return false;
}
}