Skip to content

Commit 6eb6c37

Browse files
committed
Can get the taproot leafs of derived miniscript
1 parent af2226b commit 6eb6c37

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

NBitcoin/WalletPolicies/DerivationResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ public Derivation(KeyPath keyPath, Value pubkey)
3636
/// The derived key. This is either a <see cref="Value.PubKeyValue"/> or a <see cref="Value.TaprootPubKeyValue"/>.
3737
/// </summary>
3838
public Value Pubkey { get; }
39+
public HashSet<uint256> TaprootLeafs = new();
3940
}
4041
#endif

NBitcoin/WalletPolicies/Visitors/DeriveVisitor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,21 @@ public DerivationResult[] Derive(MiniscriptNode node, Network network)
3232
}
3333

3434
internal static readonly byte[] BIP0328CC = Encoders.Hex.DecodeData("868087ca02a6f974c4598924c36b57762d32cb45717167e300622c7167e38965");
35+
Stack<TaprootBranchNode> _TaprootBranches = new();
3536
public override MiniscriptNode Visit(MiniscriptNode node)
3637
{
38+
if (node is TaprootBranchNode b)
39+
{
40+
_TaprootBranches.Push(b);
41+
try
42+
{
43+
return base.Visit(b);
44+
}
45+
finally
46+
{
47+
_TaprootBranches.Pop();
48+
}
49+
}
3750
if (node is MultipathNode { Target: MusigNode })
3851
{
3952
var wasNestedMusig = _nestedMusig;
@@ -55,7 +68,15 @@ public override MiniscriptNode Visit(MiniscriptNode node)
5568
{
5669
if (mki.Target is HDKeyNode { Key: var pk } xpub)
5770
{
58-
var value = GetPublicKey(mki, pk);
71+
if (!_Derivations.TryGetValue(xpub, out var value))
72+
{
73+
value = GetPublicKey(mki, pk);
74+
_Derivations.Add(xpub, value);
75+
}
76+
if (_TaprootBranches.Count > 0)
77+
{
78+
value.TaprootLeafs.Add(TaprootNodeInfo.NewLeaf(new TapScript(_TaprootBranches.Peek().GetScript(), TapLeafVersion.C0)).Hash);
79+
}
5980
_Derivations.TryAdd(xpub, value);
6081
node = value.Pubkey;
6182
}

0 commit comments

Comments
 (0)