Skip to content

Commit 6b43c96

Browse files
committed
Allow external cache when deriving miniscripts
1 parent 210edb3 commit 6b43c96

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

NBitcoin/NBitcoin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryType>git</RepositoryType>
1313
</PropertyGroup>
1414
<PropertyGroup>
15-
<Version Condition=" '$(Version)' == '' ">8.0.13</Version>
15+
<Version Condition=" '$(Version)' == '' ">8.0.14</Version>
1616
<LangVersion>12.0</LangVersion>
1717
</PropertyGroup>
1818
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">

NBitcoin/WalletPolicies/DeriveParameters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if !NO_RECORDS
22
#nullable enable
33
using System;
4+
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Reflection;
@@ -9,6 +10,9 @@
910

1011
namespace NBitcoin.WalletPolicies
1112
{
13+
public class DerivationCache : ConcurrentDictionary<(IHDKey, int), Lazy<IHDKey>>
14+
{
15+
}
1216
public class DeriveParameters
1317
{
1418
/// <summary>
@@ -54,6 +58,7 @@ public DeriveParameters(AddressIntent intent, int startIndex, int count)
5458
}
5559
public AddressIntent Intent { get; }
5660
public int[] AddressIndexes { get; set; }
61+
public DerivationCache? DervivationCache { get; set; }
5762
}
5863
}
5964
#endif

NBitcoin/WalletPolicies/Miniscript.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ public DerivationResult Derive(AddressIntent intent, int index)
819819
/// <exception cref="InvalidOperationException"></exception>
820820
public DerivationResult[] Derive(DeriveParameters parameters)
821821
{
822-
var visitor = new DeriveVisitor(parameters.Intent, parameters.AddressIndexes, KeyType);
822+
var visitor = new DeriveVisitor(parameters.Intent, parameters.AddressIndexes, parameters.DervivationCache ?? new(), KeyType);
823823
return visitor.Derive(RootNode, Network);
824824
}
825825

NBitcoin/WalletPolicies/Visitors/DeriveVisitor.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace NBitcoin.WalletPolicies.Visitors;
1313

14-
internal class DeriveVisitor(AddressIntent Intent, int[] Indexes, KeyType KeyType) : MiniscriptRewriterVisitor
14+
internal class DeriveVisitor(AddressIntent Intent, int[] Indexes, DerivationCache DerivationCache, KeyType KeyType) : MiniscriptRewriterVisitor
1515
{
1616
Dictionary<MiniscriptNode.MultipathNode, BitcoinExtPubKey[]> _Replacements = new();
1717
int idx = -1;
@@ -21,10 +21,9 @@ public DerivationResult[] Derive(MiniscriptNode node, Network network)
2121
DerivationResult[] result = new DerivationResult[Indexes.Length];
2222
Parallel.For(0, Indexes.Length, i =>
2323
{
24-
var visitor = new DeriveVisitor(Intent, Indexes, KeyType)
24+
var visitor = new DeriveVisitor(Intent, Indexes, DerivationCache, KeyType)
2525
{
26-
idx = Indexes[i],
27-
_DerivedCache = _DerivedCache,
26+
idx = Indexes[i]
2827
};
2928
var miniscript = new Miniscript(visitor.Visit(node), network, KeyType);
3029
result[i] = new DerivationResult(miniscript, visitor._Derivations);
@@ -83,14 +82,13 @@ private Derivation GetPublicKey(MiniscriptNode.MultipathNode mki, IHDKey k)
8382
});
8483
}
8584
Dictionary<HDKeyNode, Derivation> _Derivations = new();
86-
ConcurrentDictionary<(IHDKey, int), Lazy<IHDKey>> _DerivedCache = new();
8785

8886
public bool _nestedMusig = false;
8987

9088
private IHDKey DeriveIntent(IHDKey k, int typeIndex)
9189
{
9290
// When we derive 0/1/*, "0/1" is common to multiple derivations, so we cache it
93-
return _DerivedCache.GetOrAdd((k, typeIndex), new Lazy<IHDKey>(() => k.Derive((uint)typeIndex))).Value;
91+
return DerivationCache.GetOrAdd((k, typeIndex), new Lazy<IHDKey>(() => k.Derive((uint)typeIndex))).Value;
9492
}
9593
}
9694
#endif

0 commit comments

Comments
 (0)