Skip to content

Commit 9d79682

Browse files
committed
Refactoring: Do not use keyPathTemplates directly
1 parent eaeeea2 commit 9d79682

File tree

5 files changed

+25
-37
lines changed

5 files changed

+25
-37
lines changed

NBXplorer.Client/Models/KeyPathTemplates.cs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ public class KeyPathTemplates
1313
private readonly KeyPathTemplate customKeyPathTemplate;
1414
private static readonly KeyPathTemplates _Default = new KeyPathTemplates();
1515
private readonly DerivationFeature[] derivationFeatures;
16-
17-
public static KeyPathTemplates Default
18-
{
19-
get
20-
{
21-
return _Default;
22-
}
23-
}
16+
public static KeyPathTemplates Default => _Default;
2417

2518
private KeyPathTemplates() : this(null)
2619
{
@@ -29,35 +22,27 @@ private KeyPathTemplates() : this(null)
2922
public KeyPathTemplates(KeyPathTemplate customKeyPathTemplate)
3023
{
3124
this.customKeyPathTemplate = customKeyPathTemplate;
32-
List<DerivationFeature> derivationFeatures = new List<DerivationFeature>();
33-
derivationFeatures.Add(DerivationFeature.Deposit);
34-
derivationFeatures.Add(DerivationFeature.Change);
35-
derivationFeatures.Add(DerivationFeature.Direct);
25+
List<DerivationFeature> derivationFeatures = new List<DerivationFeature>
26+
{
27+
DerivationFeature.Deposit,
28+
DerivationFeature.Change,
29+
DerivationFeature.Direct
30+
};
3631
if (customKeyPathTemplate != null)
3732
derivationFeatures.Add(DerivationFeature.Custom);
3833
this.derivationFeatures = derivationFeatures.ToArray();
3934
}
4035

4136
public KeyPathTemplate GetKeyPathTemplate(DerivationFeature derivationFeature)
42-
{
43-
switch (derivationFeature)
37+
=> derivationFeature switch
4438
{
45-
case DerivationFeature.Deposit:
46-
return depositKeyPathTemplate;
47-
case DerivationFeature.Change:
48-
return changeKeyPathTemplate;
49-
case DerivationFeature.Direct:
50-
return directKeyPathTemplate;
51-
case DerivationFeature.Custom when customKeyPathTemplate != null:
52-
return customKeyPathTemplate;
53-
default:
54-
throw new NotSupportedException(derivationFeature.ToString());
55-
}
56-
}
39+
DerivationFeature.Deposit => depositKeyPathTemplate,
40+
DerivationFeature.Change => changeKeyPathTemplate,
41+
DerivationFeature.Direct => directKeyPathTemplate,
42+
DerivationFeature.Custom when customKeyPathTemplate != null => customKeyPathTemplate,
43+
_ => throw new NotSupportedException(derivationFeature.ToString())
44+
};
5745

58-
public IEnumerable<DerivationFeature> GetSupportedDerivationFeatures()
59-
{
60-
return derivationFeatures;
61-
}
46+
public IEnumerable<DerivationFeature> GetSupportedDerivationFeatures() => derivationFeatures;
6247
}
6348
}

NBXplorer.Client/Models/TrackedSource.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using NBitcoin.DataEncoders;
33
using NBXplorer.DerivationStrategy;
44
using System;
5+
using System.Collections.Generic;
56
using System.Security.Cryptography;
67

78
namespace NBXplorer.Models
@@ -245,5 +246,8 @@ public override string ToPrettyString()
245246
}
246247
return strategy;
247248
}
249+
250+
public IEnumerable<DerivationFeature> GetDerivationFeatures(KeyPathTemplates keyPathTemplates)
251+
=> keyPathTemplates.GetSupportedDerivationFeatures();
248252
}
249253
}

NBXplorer/Backend/Repository.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,7 @@ public async Task UpdateAddressPool(DerivationSchemeTrackedSource trackedSource,
11371137
{
11381138
await using var conn = await GetConnection();
11391139

1140-
var parameters = KeyPathTemplates
1141-
.GetSupportedDerivationFeatures()
1140+
var parameters = trackedSource.GetDerivationFeatures(KeyPathTemplates)
11421141
.Select(p =>
11431142
{
11441143
if (highestKeyIndexFound.TryGetValue(p, out var highest) && highest is int h)

NBXplorer/Controllers/DerivationSchemesController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ public async Task<IActionResult> TrackWallet(
5656
{
5757
if (request.Wait)
5858
{
59-
foreach (var feature in KeyPathTemplates.GetSupportedDerivationFeatures())
59+
foreach (var feature in dts.GetDerivationFeatures(KeyPathTemplates))
6060
{
6161
await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, GenerateAddressQuery(request, feature));
6262
}
6363
}
6464
else
6565
{
66-
foreach (var feature in KeyPathTemplates.GetSupportedDerivationFeatures())
66+
foreach (var feature in dts.GetDerivationFeatures(KeyPathTemplates))
6767
{
6868
await RepositoryProvider.GetRepository(network).GenerateAddresses(dts.DerivationStrategy, feature, new GenerateAddressQuery(minAddresses: 3, null));
6969
}
70-
foreach (var feature in KeyPathTemplates.GetSupportedDerivationFeatures())
70+
foreach (var feature in dts.GetDerivationFeatures(KeyPathTemplates))
7171
{
7272
_ = AddressPoolService.GenerateAddresses(network, dts.DerivationStrategy, feature, GenerateAddressQuery(request, feature));
7373
}

NBXplorer/ScanUTXOSetService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private async Task Listen()
148148
From = workItem.Options.From,
149149
StartedAt = DateTimeOffset.UtcNow
150150
};
151-
foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
151+
foreach (var feature in workItem.DerivationStrategy.GetDerivationFeatures(keyPathTemplates))
152152
{
153153
workItem.State.Progress.HighestKeyIndexFound.Add(feature, null);
154154
}
@@ -301,7 +301,7 @@ private ScannedItems GetScannedItems(ScanUTXOWorkItem workItem, ScanUTXOProgress
301301
{
302302
var items = new ScannedItems();
303303
var derivationStrategy = workItem.DerivationStrategy;
304-
foreach (var feature in keyPathTemplates.GetSupportedDerivationFeatures())
304+
foreach (var feature in derivationStrategy.GetDerivationFeatures(keyPathTemplates))
305305
{
306306
var keyPathTemplate = keyPathTemplates.GetKeyPathTemplate(feature);
307307
var lineDerivation = workItem.DerivationStrategy.DerivationStrategy.GetLineFor(keyPathTemplate);

0 commit comments

Comments
 (0)