From 9d796828f88d1f93e0eb04d4a426e6def354efdf Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 27 Dec 2024 16:31:54 +0900 Subject: [PATCH] Refactoring: Do not use keyPathTemplates directly --- NBXplorer.Client/Models/KeyPathTemplates.cs | 45 +++++++------------ NBXplorer.Client/Models/TrackedSource.cs | 4 ++ NBXplorer/Backend/Repository.cs | 3 +- .../DerivationSchemesController.cs | 6 +-- NBXplorer/ScanUTXOSetService.cs | 4 +- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/NBXplorer.Client/Models/KeyPathTemplates.cs b/NBXplorer.Client/Models/KeyPathTemplates.cs index 4585e8d01..0c406b8d7 100644 --- a/NBXplorer.Client/Models/KeyPathTemplates.cs +++ b/NBXplorer.Client/Models/KeyPathTemplates.cs @@ -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) { @@ -29,35 +22,27 @@ private KeyPathTemplates() : this(null) public KeyPathTemplates(KeyPathTemplate customKeyPathTemplate) { this.customKeyPathTemplate = customKeyPathTemplate; - List derivationFeatures = new List(); - derivationFeatures.Add(DerivationFeature.Deposit); - derivationFeatures.Add(DerivationFeature.Change); - derivationFeatures.Add(DerivationFeature.Direct); + List derivationFeatures = new List + { + 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 GetSupportedDerivationFeatures() - { - return derivationFeatures; - } + public IEnumerable GetSupportedDerivationFeatures() => derivationFeatures; } } diff --git a/NBXplorer.Client/Models/TrackedSource.cs b/NBXplorer.Client/Models/TrackedSource.cs index 9589b5a2b..b1ee5a3d9 100644 --- a/NBXplorer.Client/Models/TrackedSource.cs +++ b/NBXplorer.Client/Models/TrackedSource.cs @@ -2,6 +2,7 @@ using NBitcoin.DataEncoders; using NBXplorer.DerivationStrategy; using System; +using System.Collections.Generic; using System.Security.Cryptography; namespace NBXplorer.Models @@ -245,5 +246,8 @@ public override string ToPrettyString() } return strategy; } + + public IEnumerable GetDerivationFeatures(KeyPathTemplates keyPathTemplates) + => keyPathTemplates.GetSupportedDerivationFeatures(); } } diff --git a/NBXplorer/Backend/Repository.cs b/NBXplorer/Backend/Repository.cs index 79f931948..27df7282a 100644 --- a/NBXplorer/Backend/Repository.cs +++ b/NBXplorer/Backend/Repository.cs @@ -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) diff --git a/NBXplorer/Controllers/DerivationSchemesController.cs b/NBXplorer/Controllers/DerivationSchemesController.cs index f6e739321..91913f4dd 100644 --- a/NBXplorer/Controllers/DerivationSchemesController.cs +++ b/NBXplorer/Controllers/DerivationSchemesController.cs @@ -56,18 +56,18 @@ public async Task 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)); } diff --git a/NBXplorer/ScanUTXOSetService.cs b/NBXplorer/ScanUTXOSetService.cs index f95c97189..12a7c7e7b 100644 --- a/NBXplorer/ScanUTXOSetService.cs +++ b/NBXplorer/ScanUTXOSetService.cs @@ -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); } @@ -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);