Skip to content

Commit

Permalink
Refactoring: Do not use keyPathTemplates directly
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Dec 27, 2024
1 parent eaeeea2 commit 9d79682
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
45 changes: 15 additions & 30 deletions NBXplorer.Client/Models/KeyPathTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ public class KeyPathTemplates
private readonly KeyPathTemplate customKeyPathTemplate;
private static readonly KeyPathTemplates _Default = new KeyPathTemplates();
private readonly DerivationFeature[] derivationFeatures;

public static KeyPathTemplates Default
{
get
{
return _Default;
}
}
public static KeyPathTemplates Default => _Default;

private KeyPathTemplates() : this(null)
{
Expand All @@ -29,35 +22,27 @@ private KeyPathTemplates() : this(null)
public KeyPathTemplates(KeyPathTemplate customKeyPathTemplate)
{
this.customKeyPathTemplate = customKeyPathTemplate;
List<DerivationFeature> derivationFeatures = new List<DerivationFeature>();
derivationFeatures.Add(DerivationFeature.Deposit);
derivationFeatures.Add(DerivationFeature.Change);
derivationFeatures.Add(DerivationFeature.Direct);
List<DerivationFeature> derivationFeatures = new List<DerivationFeature>
{
DerivationFeature.Deposit,
DerivationFeature.Change,
DerivationFeature.Direct
};
if (customKeyPathTemplate != null)
derivationFeatures.Add(DerivationFeature.Custom);
this.derivationFeatures = derivationFeatures.ToArray();
}

public KeyPathTemplate GetKeyPathTemplate(DerivationFeature derivationFeature)
{
switch (derivationFeature)
=> derivationFeature switch
{
case DerivationFeature.Deposit:
return depositKeyPathTemplate;
case DerivationFeature.Change:
return changeKeyPathTemplate;
case DerivationFeature.Direct:
return directKeyPathTemplate;
case DerivationFeature.Custom when customKeyPathTemplate != null:
return customKeyPathTemplate;
default:
throw new NotSupportedException(derivationFeature.ToString());
}
}
DerivationFeature.Deposit => depositKeyPathTemplate,
DerivationFeature.Change => changeKeyPathTemplate,
DerivationFeature.Direct => directKeyPathTemplate,
DerivationFeature.Custom when customKeyPathTemplate != null => customKeyPathTemplate,
_ => throw new NotSupportedException(derivationFeature.ToString())
};

public IEnumerable<DerivationFeature> GetSupportedDerivationFeatures()
{
return derivationFeatures;
}
public IEnumerable<DerivationFeature> GetSupportedDerivationFeatures() => derivationFeatures;
}
}
4 changes: 4 additions & 0 deletions NBXplorer.Client/Models/TrackedSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NBitcoin.DataEncoders;
using NBXplorer.DerivationStrategy;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace NBXplorer.Models
Expand Down Expand Up @@ -245,5 +246,8 @@ public override string ToPrettyString()
}
return strategy;
}

public IEnumerable<DerivationFeature> GetDerivationFeatures(KeyPathTemplates keyPathTemplates)
=> keyPathTemplates.GetSupportedDerivationFeatures();
}
}
3 changes: 1 addition & 2 deletions NBXplorer/Backend/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,8 +1137,7 @@ public async Task UpdateAddressPool(DerivationSchemeTrackedSource trackedSource,
{
await using var conn = await GetConnection();

var parameters = KeyPathTemplates
.GetSupportedDerivationFeatures()
var parameters = trackedSource.GetDerivationFeatures(KeyPathTemplates)
.Select(p =>
{
if (highestKeyIndexFound.TryGetValue(p, out var highest) && highest is int h)
Expand Down
6 changes: 3 additions & 3 deletions NBXplorer/Controllers/DerivationSchemesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ public async Task<IActionResult> TrackWallet(
{
if (request.Wait)
{
foreach (var feature in KeyPathTemplates.GetSupportedDerivationFeatures())
foreach (var feature in dts.GetDerivationFeatures(KeyPathTemplates))
{
await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, GenerateAddressQuery(request, feature));
}
}
else
{
foreach (var feature in KeyPathTemplates.GetSupportedDerivationFeatures())
foreach (var feature in dts.GetDerivationFeatures(KeyPathTemplates))
{
await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, new GenerateAddressQuery(minAddresses: 3, null));
}
foreach (var feature in KeyPathTemplates.GetSupportedDerivationFeatures())
foreach (var feature in dts.GetDerivationFeatures(KeyPathTemplates))
{
_ = AddressPoolService.GenerateAddresses(network, dts.DerivationStrategy, feature, GenerateAddressQuery(request, feature));
}
Expand Down
4 changes: 2 additions & 2 deletions NBXplorer/ScanUTXOSetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private async Task Listen()
From = workItem.Options.From,
StartedAt = DateTimeOffset.UtcNow
};
foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
foreach (var feature in workItem.DerivationStrategy.GetDerivationFeatures(keyPathTemplates))
{
workItem.State.Progress.HighestKeyIndexFound.Add(feature, null);
}
Expand Down Expand Up @@ -301,7 +301,7 @@ private ScannedItems GetScannedItems(ScanUTXOWorkItem workItem, ScanUTXOProgress
{
var items = new ScannedItems();
var derivationStrategy = workItem.DerivationStrategy;
foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
foreach (var feature in derivationStrategy.GetDerivationFeatures(keyPathTemplates))
{
var keyPathTemplate = keyPathTemplates.GetKeyPathTemplate(feature);
var lineDerivation = workItem.DerivationStrategy.DerivationStrategy.GetLineFor(keyPathTemplate);
Expand Down

0 comments on commit 9d79682

Please sign in to comment.