@@ -32,8 +32,22 @@ public DerivationResult[] Derive(MiniscriptNode node, Network network)
3232 }
3333
3434 internal static readonly byte [ ] BIP0328CC = Encoders . Hex . DecodeData ( "868087ca02a6f974c4598924c36b57762d32cb45717167e300622c7167e38965" ) ;
35+ Stack < TaprootBranchNode > _TaprootBranches = new ( ) ;
36+ TaprootBranchNode ? TaprootBranch => _TaprootBranches . Count > 0 ? _TaprootBranches . Peek ( ) : null ;
3537 public override MiniscriptNode Visit ( MiniscriptNode node )
3638 {
39+ if ( node is TaprootBranchNode b )
40+ {
41+ _TaprootBranches . Push ( b ) ;
42+ try
43+ {
44+ return base . Visit ( b ) ;
45+ }
46+ finally
47+ {
48+ _TaprootBranches . Pop ( ) ;
49+ }
50+ }
3751 if ( node is MultipathNode { Target : MusigNode } )
3852 {
3953 var wasNestedMusig = _nestedMusig ;
@@ -53,10 +67,10 @@ public override MiniscriptNode Visit(MiniscriptNode node)
5367 }
5468 if ( node is MiniscriptNode . MultipathNode mki && mki . CanDerive ( Intent ) )
5569 {
56- if ( mki . Target is HDKeyNode { Key : var pk } xpub )
70+ if ( mki . Target is HDKeyNode xpub )
5771 {
58- var value = GetPublicKey ( mki , pk ) ;
59- _Derivations . TryAdd ( xpub , value ) ;
72+ var value = GetPublicKey ( mki , xpub . Key , xpub ) ;
73+ _Derivations . Add ( new ( value . KeyPath , value . Pubkey , TaprootBranch , xpub ) ) ;
6074 node = value . Pubkey ;
6175 }
6276 else if ( mki . Target is MusigNode musig )
@@ -69,19 +83,21 @@ public override MiniscriptNode Visit(MiniscriptNode node)
6983 return node ;
7084 }
7185
72- private Derivation GetPublicKey ( MiniscriptNode . MultipathNode mki , IHDKey k )
86+ private ( KeyPath KeyPath , Value Pubkey ) GetPublicKey ( MiniscriptNode . MultipathNode mki , IHDKey k , HDKeyNode ? source = null )
7387 {
7488 var type = mki . GetTypeIndex ( Intent ) ;
7589 k = DeriveIntent ( k , type ) ;
7690 k = k . Derive ( ( uint ) idx ) ;
7791 var keyType = _nestedMusig ? KeyType . Classic : KeyType ;
78- return new Derivation ( new KeyPath ( [ ( uint ) type , ( uint ) idx ] ) , keyType switch
79- {
80- KeyType . Taproot => MiniscriptNode . Create ( k . GetPublicKey ( ) . TaprootPubKey ) ,
81- _ => MiniscriptNode . Create ( k . GetPublicKey ( ) )
82- } ) ;
92+ return (
93+ new KeyPath ( [ ( uint ) type , ( uint ) idx ] ) ,
94+ keyType switch
95+ {
96+ KeyType . Taproot => MiniscriptNode . Create ( k . GetPublicKey ( ) . TaprootPubKey ) ,
97+ _ => MiniscriptNode . Create ( k . GetPublicKey ( ) )
98+ } ) ;
8399 }
84- Dictionary < HDKeyNode , Derivation > _Derivations = new ( ) ;
100+ List < Derivation > _Derivations = new ( ) ;
85101
86102 public bool _nestedMusig = false ;
87103
0 commit comments