@@ -71,6 +71,19 @@ function findDescriptorForAnyDerivationPath(
71
71
return undefined ;
72
72
}
73
73
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
+
74
87
/**
75
88
* @param input
76
89
* @param descriptorMap
@@ -84,21 +97,11 @@ export function findDescriptorForInput(
84
97
if ( ! script ) {
85
98
throw new Error ( 'Missing script' ) ;
86
99
}
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' ) ;
100
103
}
101
- throw new Error ( 'Missing derivation path' ) ;
104
+ return findDescriptorForAnyDerivationPath ( script , derivationPaths , descriptorMap ) ;
102
105
}
103
106
104
107
/**
@@ -112,12 +115,9 @@ export function findDescriptorForOutput(
112
115
output : PsbtOutput ,
113
116
descriptorMap : DescriptorMap
114
117
) : DescriptorWithIndex | undefined {
115
- if ( ! output . bip32Derivation ) {
118
+ const derivationPaths = getDerivationPaths ( output ) ;
119
+ if ( ! derivationPaths ) {
116
120
return undefined ;
117
121
}
118
- return findDescriptorForAnyDerivationPath (
119
- script ,
120
- output . bip32Derivation . map ( ( d ) => d . path ) ,
121
- descriptorMap
122
- ) ;
122
+ return findDescriptorForAnyDerivationPath ( script , derivationPaths , descriptorMap ) ;
123
123
}
0 commit comments