2024-01-17 10:08:39 +01:00
|
|
|
#nullable enable
|
|
|
|
using System;
|
2024-01-23 21:33:45 +09:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-01-17 10:08:39 +01:00
|
|
|
using BTCPayServer;
|
2024-01-23 21:33:45 +09:00
|
|
|
using Newtonsoft.Json;
|
2024-01-17 10:08:39 +01:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace BTCPayServer.Services.WalletFileParsing;
|
|
|
|
public class SpecterWalletFileParser : IWalletFileParser
|
|
|
|
{
|
|
|
|
private readonly OutputDescriptorWalletFileParser _outputDescriptorOnChainWalletParser;
|
|
|
|
|
2024-01-23 21:33:45 +09:00
|
|
|
class SpecterFormat
|
|
|
|
{
|
|
|
|
public string? descriptor { get; set; }
|
|
|
|
public int? blockheight { get; set; }
|
|
|
|
public string? label { get; set; }
|
|
|
|
}
|
2024-01-17 10:08:39 +01:00
|
|
|
public SpecterWalletFileParser(OutputDescriptorWalletFileParser outputDescriptorOnChainWalletParser)
|
|
|
|
{
|
|
|
|
_outputDescriptorOnChainWalletParser = outputDescriptorOnChainWalletParser;
|
|
|
|
}
|
2024-01-23 21:33:45 +09:00
|
|
|
public bool TryParse(BTCPayNetwork network, string data, [MaybeNullWhen(false)] out DerivationSchemeSettings derivationSchemeSettings)
|
2024-01-17 10:08:39 +01:00
|
|
|
{
|
2024-01-23 21:33:45 +09:00
|
|
|
derivationSchemeSettings = null;
|
|
|
|
var jobj = JsonConvert.DeserializeObject<SpecterFormat>(data);
|
|
|
|
if (jobj?.descriptor is null || jobj.blockheight is null)
|
|
|
|
return false;
|
|
|
|
if (!_outputDescriptorOnChainWalletParser.TryParse(network, jobj.descriptor, out derivationSchemeSettings))
|
|
|
|
return false;
|
2024-01-24 01:28:22 +01:00
|
|
|
|
2024-01-23 21:33:45 +09:00
|
|
|
derivationSchemeSettings.Source = "Specter";
|
|
|
|
if (jobj.label is not null)
|
2024-01-24 01:28:22 +01:00
|
|
|
derivationSchemeSettings.Label = jobj.label;
|
|
|
|
|
2024-01-23 21:33:45 +09:00
|
|
|
return true;
|
2024-01-17 10:08:39 +01:00
|
|
|
}
|
|
|
|
}
|