mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 22:44:29 +01:00
@@ -1,88 +1,61 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using BTCPayServer;
|
||||
using NBitcoin;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
namespace BTCPayServer.Services.WalletFileParsing;
|
||||
public class ElectrumWalletFileParser : IWalletFileParser
|
||||
{
|
||||
public (BTCPayServer.DerivationSchemeSettings? DerivationSchemeSettings, string? Error) TryParse(BTCPayNetwork network,
|
||||
string data)
|
||||
class ElectrumFormat
|
||||
{
|
||||
try
|
||||
internal class KeyStoreFormat
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
public string? xpub { get; set; }
|
||||
public string? label { get; set; }
|
||||
public uint? ckcc_xfp { get; set; }
|
||||
public string? derivation { get; set; }
|
||||
public string? ColdCardFirmwareVersion { get; set; }
|
||||
public string? CoboVaultFirmwareVersion { get; set; }
|
||||
}
|
||||
catch (FormatException)
|
||||
public KeyStoreFormat? keystore { get; set; }
|
||||
}
|
||||
public bool TryParse(BTCPayNetwork network, string data, [MaybeNullWhen(false)] out DerivationSchemeSettings derivationSchemeSettings)
|
||||
{
|
||||
derivationSchemeSettings = null;
|
||||
var jobj = JsonConvert.DeserializeObject<ElectrumFormat>(data);
|
||||
if (jobj?.keystore is null)
|
||||
return false;
|
||||
|
||||
var result = new BTCPayServer.DerivationSchemeSettings() { Network = network };
|
||||
var derivationSchemeParser = network.GetDerivationSchemeParser();
|
||||
result.Source = "ElectrumFile";
|
||||
|
||||
if (jobj.keystore.xpub is null || jobj.keystore.ckcc_xfp is null || jobj.keystore.derivation is null)
|
||||
return false;
|
||||
|
||||
var strategy = derivationSchemeParser.Parse(jobj.keystore.xpub, false, false, true);
|
||||
result.AccountDerivation = strategy;
|
||||
result.AccountOriginal = jobj.keystore.xpub;
|
||||
result.GetSigningAccountKeySettings();
|
||||
|
||||
if (jobj.keystore.label is not null)
|
||||
result.Label = jobj.keystore.label;
|
||||
|
||||
result.AccountKeySettings[0].RootFingerprint = new HDFingerprint(jobj.keystore.ckcc_xfp.Value);
|
||||
result.AccountKeySettings[0].AccountKeyPath = new KeyPath(jobj.keystore.derivation);
|
||||
|
||||
|
||||
if (jobj.keystore.ColdCardFirmwareVersion is not null)
|
||||
{
|
||||
return (null, "invalid xpub");
|
||||
result.Source = "ColdCard";
|
||||
}
|
||||
return (null, null);
|
||||
else if (jobj.keystore.CoboVaultFirmwareVersion is not null)
|
||||
{
|
||||
result.Source = "CoboVault";
|
||||
}
|
||||
derivationSchemeSettings = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user