mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 01:43:50 +01:00
c5a0e28420
* 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>
213 lines
7.2 KiB
C#
213 lines
7.2 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using BTCPayServer.Payments;
|
|
using NBitcoin;
|
|
using NBitcoin.DataEncoders;
|
|
using NBXplorer.Client;
|
|
using NBXplorer.DerivationStrategy;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace BTCPayServer
|
|
{
|
|
public class DerivationSchemeSettings : ISupportedPaymentMethod
|
|
{
|
|
public static DerivationSchemeSettings Parse(string derivationStrategy, BTCPayNetwork network)
|
|
{
|
|
string error = null;
|
|
ArgumentNullException.ThrowIfNull(network);
|
|
ArgumentNullException.ThrowIfNull(derivationStrategy);
|
|
var result = new DerivationSchemeSettings();
|
|
result.Network = network;
|
|
var parser = network.GetDerivationSchemeParser();
|
|
if (parser.TryParseXpub(derivationStrategy, ref result, out error))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
throw new FormatException($"Invalid Derivation Scheme: {error}");
|
|
}
|
|
|
|
public static bool TryParseFromJson(string config, BTCPayNetwork network, out DerivationSchemeSettings strategy)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(network);
|
|
ArgumentNullException.ThrowIfNull(config);
|
|
strategy = null;
|
|
try
|
|
{
|
|
strategy = network.NBXplorerNetwork.Serializer.ToObject<DerivationSchemeSettings>(config);
|
|
strategy.Network = network;
|
|
}
|
|
catch { }
|
|
return strategy != null;
|
|
}
|
|
|
|
public string GetNBXWalletId()
|
|
{
|
|
return AccountDerivation is null ? null : DBUtils.nbxv1_get_wallet_id(Network.CryptoCode, AccountDerivation.ToString());
|
|
}
|
|
|
|
public DerivationSchemeSettings()
|
|
{
|
|
|
|
}
|
|
|
|
public DerivationSchemeSettings(DerivationStrategyBase derivationStrategy, BTCPayNetwork network)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(network);
|
|
ArgumentNullException.ThrowIfNull(derivationStrategy);
|
|
AccountDerivation = derivationStrategy;
|
|
Network = network;
|
|
AccountKeySettings = derivationStrategy.GetExtPubKeys().Select(c => new AccountKeySettings()
|
|
{
|
|
AccountKey = c.GetWif(network.NBitcoinNetwork)
|
|
}).ToArray();
|
|
}
|
|
|
|
|
|
BitcoinExtPubKey _SigningKey;
|
|
public BitcoinExtPubKey SigningKey
|
|
{
|
|
get
|
|
{
|
|
return _SigningKey ?? AccountKeySettings?.Select(k => k.AccountKey).FirstOrDefault();
|
|
}
|
|
set
|
|
{
|
|
_SigningKey = value;
|
|
}
|
|
}
|
|
|
|
[JsonIgnore]
|
|
public BTCPayNetwork Network { get; set; }
|
|
public string Source { get; set; }
|
|
|
|
public bool IsHotWallet { get; set; }
|
|
|
|
[Obsolete("Use GetSigningAccountKeySettings().AccountKeyPath instead")]
|
|
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
|
public KeyPath AccountKeyPath { get; set; }
|
|
|
|
public DerivationStrategyBase AccountDerivation { get; set; }
|
|
public string AccountOriginal { get; set; }
|
|
|
|
[Obsolete("Use GetSigningAccountKeySettings().RootFingerprint instead")]
|
|
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
|
public HDFingerprint? RootFingerprint { get; set; }
|
|
|
|
[Obsolete("Use GetSigningAccountKeySettings().AccountKey instead")]
|
|
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
|
public BitcoinExtPubKey ExplicitAccountKey { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Obsolete("Use GetSigningAccountKeySettings().AccountKey instead")]
|
|
public BitcoinExtPubKey AccountKey
|
|
{
|
|
get
|
|
{
|
|
return ExplicitAccountKey ?? new BitcoinExtPubKey(AccountDerivation.GetExtPubKeys().First(), Network.NBitcoinNetwork);
|
|
}
|
|
}
|
|
|
|
public AccountKeySettings GetSigningAccountKeySettings()
|
|
{
|
|
return AccountKeySettings.Single(a => a.AccountKey == SigningKey);
|
|
}
|
|
|
|
AccountKeySettings[] _AccountKeySettings;
|
|
public AccountKeySettings[] AccountKeySettings
|
|
{
|
|
get
|
|
{
|
|
// Legacy
|
|
if (_AccountKeySettings == null)
|
|
{
|
|
if (this.Network == null)
|
|
return null;
|
|
_AccountKeySettings = AccountDerivation.GetExtPubKeys().Select(e => new AccountKeySettings()
|
|
{
|
|
AccountKey = e.GetWif(this.Network.NBitcoinNetwork),
|
|
}).ToArray();
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
_AccountKeySettings[0].AccountKeyPath = AccountKeyPath;
|
|
_AccountKeySettings[0].RootFingerprint = RootFingerprint;
|
|
ExplicitAccountKey = null;
|
|
AccountKeyPath = null;
|
|
RootFingerprint = null;
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
}
|
|
return _AccountKeySettings;
|
|
}
|
|
set
|
|
{
|
|
_AccountKeySettings = value;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<NBXplorer.Models.PSBTRebaseKeyRules> GetPSBTRebaseKeyRules()
|
|
{
|
|
foreach (var accountKey in AccountKeySettings)
|
|
{
|
|
if (accountKey.GetRootedKeyPath() is RootedKeyPath rootedKeyPath)
|
|
{
|
|
yield return new NBXplorer.Models.PSBTRebaseKeyRules()
|
|
{
|
|
AccountKey = accountKey.AccountKey,
|
|
AccountKeyPath = rootedKeyPath
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
public string Label { get; set; }
|
|
|
|
[JsonIgnore]
|
|
public PaymentMethodId PaymentId => new PaymentMethodId(Network.CryptoCode, PaymentTypes.BTCLike);
|
|
|
|
public override string ToString()
|
|
{
|
|
return AccountDerivation.ToString();
|
|
}
|
|
public string ToPrettyString()
|
|
{
|
|
return !string.IsNullOrEmpty(Label) ? Label :
|
|
!String.IsNullOrEmpty(AccountOriginal) ? AccountOriginal :
|
|
ToString();
|
|
}
|
|
|
|
public string ToJson()
|
|
{
|
|
return Network.NBXplorerNetwork.Serializer.ToString(this);
|
|
}
|
|
|
|
public void RebaseKeyPaths(PSBT psbt)
|
|
{
|
|
foreach (var rebase in GetPSBTRebaseKeyRules())
|
|
{
|
|
psbt.RebaseKeyPaths(rebase.AccountKey, rebase.AccountKeyPath);
|
|
}
|
|
}
|
|
}
|
|
public class AccountKeySettings
|
|
{
|
|
public HDFingerprint? RootFingerprint { get; set; }
|
|
public KeyPath AccountKeyPath { get; set; }
|
|
|
|
public RootedKeyPath GetRootedKeyPath()
|
|
{
|
|
if (RootFingerprint is HDFingerprint fp && AccountKeyPath != null)
|
|
return new RootedKeyPath(fp, AccountKeyPath);
|
|
return null;
|
|
}
|
|
public BitcoinExtPubKey AccountKey { get; set; }
|
|
public bool IsFullySetup()
|
|
{
|
|
return AccountKeyPath != null && RootFingerprint is HDFingerprint;
|
|
}
|
|
}
|
|
}
|