@@ -59,7 +59,7 @@ function parseMulti(node: unknown): {
5959 } ;
6060}
6161
62- export function parseDescriptorNode ( node : unknown ) : DescriptorBuilder {
62+ function parseWshMulti ( node : unknown ) : DescriptorBuilder | undefined {
6363 const wshMsMulti = unwrapNode ( node , [ 'Wsh' , 'Ms' , 'Multi' ] ) ;
6464 if ( wshMsMulti ) {
6565 const { threshold, keys, path } = parseMulti ( wshMsMulti ) ;
@@ -77,31 +77,47 @@ export function parseDescriptorNode(node: unknown): DescriptorBuilder {
7777 path,
7878 } ;
7979 }
80+ }
8081
81- const shWshMsAndV = unwrapNode ( node , [ 'Sh' , 'Wsh' , 'Ms' , 'AndV' ] ) ;
82- if ( shWshMsAndV ) {
83- if ( Array . isArray ( shWshMsAndV ) && shWshMsAndV . length === 2 ) {
84- const [ a , b ] = shWshMsAndV ;
85- const dropAfterAbsLocktime = unwrapNode ( a , [ 'Drop' , 'After' , 'absLockTime' ] ) ;
86- if ( typeof dropAfterAbsLocktime !== 'number' ) {
87- throw new Error ( 'Expected absLockTime number' ) ;
88- }
89- if ( ! isUnaryNode ( b , 'Multi' ) ) {
90- throw new Error ( 'Expected Multi node' ) ;
91- }
92- const multi = parseMulti ( b . Multi ) ;
93- if ( multi . threshold === 2 && multi . keys . length === 3 ) {
94- return {
95- name : 'ShWsh2Of3CltvDrop' ,
96- locktime : dropAfterAbsLocktime ,
97- keys : multi . keys ,
98- path : multi . path ,
99- } ;
100- }
82+ function parseCltvDrop (
83+ node : unknown ,
84+ name : 'Wsh2Of3CltvDrop' | 'ShWsh2Of3CltvDrop' ,
85+ wrapping : string [ ]
86+ ) : DescriptorBuilder | undefined {
87+ const unwrapped = unwrapNode ( node , wrapping ) ;
88+ if ( ! unwrapped ) {
89+ return ;
90+ }
91+ if ( Array . isArray ( unwrapped ) && unwrapped . length === 2 ) {
92+ const [ a , b ] = unwrapped ;
93+ const dropAfterAbsLocktime = unwrapNode ( a , [ 'Drop' , 'After' , 'absLockTime' ] ) ;
94+ if ( typeof dropAfterAbsLocktime !== 'number' ) {
95+ throw new Error ( 'Expected absLockTime number' ) ;
96+ }
97+ if ( ! isUnaryNode ( b , 'Multi' ) ) {
98+ throw new Error ( 'Expected Multi node' ) ;
99+ }
100+ const multi = parseMulti ( b . Multi ) ;
101+ if ( multi . threshold === 2 && multi . keys . length === 3 ) {
102+ return {
103+ name,
104+ locktime : dropAfterAbsLocktime ,
105+ keys : multi . keys ,
106+ path : multi . path ,
107+ } ;
101108 }
102109 }
110+ }
103111
104- throw new Error ( 'Not implemented' ) ;
112+ export function parseDescriptorNode ( node : unknown ) : DescriptorBuilder {
113+ const parsed =
114+ parseWshMulti ( node ) ??
115+ parseCltvDrop ( node , 'ShWsh2Of3CltvDrop' , [ 'Sh' , 'Wsh' , 'Ms' , 'AndV' ] ) ??
116+ parseCltvDrop ( node , 'Wsh2Of3CltvDrop' , [ 'Wsh' , 'Ms' , 'AndV' ] ) ;
117+ if ( ! parsed ) {
118+ throw new Error ( 'Failed to parse descriptor node' ) ;
119+ }
120+ return parsed ;
105121}
106122
107123export function parseDescriptor ( descriptor : Descriptor ) : DescriptorBuilder {
0 commit comments