@@ -71,6 +71,19 @@ function findDescriptorForAnyDerivationPath(
7171 return undefined ;
7272}
7373
74+ type WithBip32Derivation = { bip32Derivation ?: { path : string } [ ] } ;
75+ type WithTapBip32Derivation = { tapBip32Derivation ?: { path : string } [ ] } ;
76+
77+ function getDerivationPaths ( v : WithBip32Derivation | WithTapBip32Derivation ) : string [ ] | undefined {
78+ if ( 'bip32Derivation' in v && v . bip32Derivation ) {
79+ return v . bip32Derivation . map ( ( v ) => v . path ) ;
80+ }
81+ if ( 'tapBip32Derivation' in v && v . tapBip32Derivation ) {
82+ return v . tapBip32Derivation . map ( ( v ) => v . path ) . filter ( ( v ) => v !== '' && v !== 'm' ) ;
83+ }
84+ return undefined ;
85+ }
86+
7487/**
7588 * @param input
7689 * @param descriptorMap
@@ -84,21 +97,11 @@ export function findDescriptorForInput(
8497 if ( ! script ) {
8598 throw new Error ( 'Missing script' ) ;
8699 }
87- if ( input . bip32Derivation !== undefined ) {
88- return findDescriptorForAnyDerivationPath (
89- script ,
90- input . bip32Derivation . map ( ( v ) => v . path ) ,
91- descriptorMap
92- ) ;
93- }
94- if ( input . tapBip32Derivation !== undefined ) {
95- return findDescriptorForAnyDerivationPath (
96- script ,
97- input . tapBip32Derivation . filter ( ( v ) => v . path !== '' && v . path !== 'm' ) . map ( ( v ) => v . path ) ,
98- descriptorMap
99- ) ;
100+ const derivationPaths = getDerivationPaths ( input ) ;
101+ if ( ! derivationPaths ) {
102+ throw new Error ( 'Missing derivation paths' ) ;
100103 }
101- throw new Error ( 'Missing derivation path' ) ;
104+ return findDescriptorForAnyDerivationPath ( script , derivationPaths , descriptorMap ) ;
102105}
103106
104107/**
@@ -112,12 +115,9 @@ export function findDescriptorForOutput(
112115 output : PsbtOutput ,
113116 descriptorMap : DescriptorMap
114117) : DescriptorWithIndex | undefined {
115- if ( ! output . bip32Derivation ) {
118+ const derivationPaths = getDerivationPaths ( output ) ;
119+ if ( ! derivationPaths ) {
116120 return undefined ;
117121 }
118- return findDescriptorForAnyDerivationPath (
119- script ,
120- output . bip32Derivation . map ( ( d ) => d . path ) ,
121- descriptorMap
122- ) ;
122+ return findDescriptorForAnyDerivationPath ( script , derivationPaths , descriptorMap ) ;
123123}
0 commit comments