mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-26 15:41:29 +01:00
* 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>
88 lines
2.9 KiB
C#
88 lines
2.9 KiB
C#
#nullable enable
|
|
using System;
|
|
using BTCPayServer;
|
|
using NBitcoin;
|
|
using Newtonsoft.Json.Linq;
|
|
namespace BTCPayServer.Services.WalletFileParsing;
|
|
public class ElectrumWalletFileParser : IWalletFileParser
|
|
{
|
|
public (BTCPayServer.DerivationSchemeSettings? DerivationSchemeSettings, string? Error) TryParse(BTCPayNetwork network,
|
|
string data)
|
|
{
|
|
try
|
|
{
|
|
var derivationSchemeParser = network.GetDerivationSchemeParser();
|
|
var jobj = JObject.Parse(data);
|
|
var result = new BTCPayServer.DerivationSchemeSettings() {Network = network};
|
|
|
|
if (jobj["keystore"] is JObject keyStore)
|
|
{
|
|
result.Source = "ElectrumFile";
|
|
jobj = keyStore;
|
|
|
|
if (!jobj.TryGetValue("xpub", StringComparison.InvariantCultureIgnoreCase, out var xpubToken))
|
|
{
|
|
return (null, "no xpub");
|
|
}
|
|
var strategy = derivationSchemeParser.Parse(xpubToken.Value<string>(), false, false, true);
|
|
result.AccountDerivation = strategy;
|
|
result.AccountOriginal = xpubToken.Value<string>();
|
|
result.GetSigningAccountKeySettings();
|
|
|
|
if (jobj["label"]?.Value<string>() is string label)
|
|
{
|
|
try
|
|
{
|
|
result.Label = label;
|
|
}
|
|
catch
|
|
{
|
|
return (null, "Label was not a string");
|
|
}
|
|
}
|
|
|
|
if (jobj["ckcc_xfp"]?.Value<uint>() is uint xfp)
|
|
{
|
|
try
|
|
{
|
|
result.AccountKeySettings[0].RootFingerprint =
|
|
new HDFingerprint(xfp);
|
|
}
|
|
catch
|
|
{
|
|
return (null, "fingerprint was not a uint");
|
|
}
|
|
}
|
|
|
|
if (jobj["derivation"]?.Value<string>() is string derivation)
|
|
{
|
|
try
|
|
{
|
|
result.AccountKeySettings[0].AccountKeyPath = new KeyPath(derivation);
|
|
}
|
|
catch
|
|
{
|
|
return (null, "derivation keypath was not valid");
|
|
}
|
|
}
|
|
|
|
|
|
if (jobj.ContainsKey("ColdCardFirmwareVersion"))
|
|
{
|
|
result.Source = "ColdCard";
|
|
}
|
|
else if (jobj.ContainsKey("CoboVaultFirmwareVersion"))
|
|
{
|
|
result.Source = "CoboVault";
|
|
}
|
|
return (result, null);
|
|
}
|
|
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return (null, "invalid xpub");
|
|
}
|
|
return (null, null);
|
|
}
|
|
}
|