@@ -174,26 +174,26 @@ impl<'a> miniscript::Translator<String, bitcoin::PublicKey, Error>
174
174
175
175
/// See `ParsedPolicy`.
176
176
#[ derive( Debug ) ]
177
- pub struct Wsh < ' a > {
178
- policy : & ' a Policy ,
177
+ pub struct Wsh {
179
178
miniscript_expr : miniscript:: Miniscript < String , miniscript:: Segwitv0 > ,
180
179
}
181
180
182
- /// Result of `parse() `.
181
+ /// See `ParsedPolicy `.
183
182
#[ derive( Debug ) ]
184
- pub enum ParsedPolicy < ' a > {
183
+ pub enum Descriptor {
185
184
// `wsh(...)` policies
186
- Wsh ( Wsh < ' a > ) ,
185
+ Wsh ( Wsh ) ,
187
186
// `tr(...)` Taproot etc. in the future.
188
187
}
189
188
190
- impl < ' a > ParsedPolicy < ' a > {
191
- fn get_policy ( & self ) -> & Policy {
192
- match self {
193
- Self :: Wsh ( Wsh { policy , .. } ) => policy ,
194
- }
195
- }
189
+ /// Result of `parse()`.
190
+ # [ derive ( Debug ) ]
191
+ pub struct ParsedPolicy < ' a > {
192
+ policy : & ' a Policy ,
193
+ pub descriptor : Descriptor ,
194
+ }
196
195
196
+ impl < ' a > ParsedPolicy < ' a > {
197
197
/// Check that it is impossible to create a derivation with duplicate pubkeys, assuming all the
198
198
/// keys in the key vector are distinct.
199
199
///
@@ -204,16 +204,13 @@ impl<'a> ParsedPolicy<'a> {
204
204
/// Also checks that each key is used, e.g. if there are 3 keys in the key vector, @0, @1 and @2
205
205
/// must be present.
206
206
fn validate_keys ( & self ) -> Result < ( ) , Error > {
207
- match self {
208
- Self :: Wsh ( Wsh {
209
- policy,
210
- miniscript_expr,
211
- } ) => {
207
+ match & self . descriptor {
208
+ Descriptor :: Wsh ( Wsh { miniscript_expr } ) => {
212
209
// in "@key_index/<left;right>", keeps track of (key_index,left) and
213
210
// (key_index,right) to check for duplicates.
214
211
let mut derivations_seen: Vec < ( usize , u32 ) > = Vec :: new ( ) ;
215
212
216
- let mut keys_seen: Vec < bool > = vec ! [ false ; policy. keys. len( ) ] ;
213
+ let mut keys_seen: Vec < bool > = vec ! [ false ; self . policy. keys. len( ) ] ;
217
214
218
215
for pk in miniscript_expr. iter_pk ( ) {
219
216
let ( key_index, multipath_index_left, multipath_index_right) =
@@ -248,7 +245,7 @@ impl<'a> ParsedPolicy<'a> {
248
245
pub fn validate ( & self , coin : BtcCoin ) -> Result < ( ) , Error > {
249
246
check_enabled ( coin) ?;
250
247
251
- let policy = self . get_policy ( ) ;
248
+ let policy = self . policy ;
252
249
253
250
if policy. keys . len ( ) > MAX_KEYS {
254
251
return Err ( Error :: InvalidInput ) ;
@@ -297,13 +294,10 @@ impl<'a> ParsedPolicy<'a> {
297
294
/// wsh(and_v(v:pk(@0/0/5),pk(@1/20/5))).
298
295
/// The same derived using `is_change=true` derives: wsh(and_v(v:pk(@0/1/5),pk(@1/21/5)))
299
296
pub fn witness_script ( & self , is_change : bool , address_index : u32 ) -> Result < Vec < u8 > , Error > {
300
- match self {
301
- Self :: Wsh ( Wsh {
302
- policy,
303
- miniscript_expr,
304
- } ) => {
297
+ match & self . descriptor {
298
+ Descriptor :: Wsh ( Wsh { miniscript_expr } ) => {
305
299
let mut translator = WalletPolicyPkTranslator {
306
- keys : policy. keys . as_ref ( ) ,
300
+ keys : self . policy . keys . as_ref ( ) ,
307
301
is_change,
308
302
address_index,
309
303
} ;
@@ -322,27 +316,27 @@ impl<'a> ParsedPolicy<'a> {
322
316
/// derived using keypath m/48'/1'/0'/3'/11/5 derives:
323
317
/// wsh(and_v(v:pk(@0/11/5),pk(@1/21/5))).
324
318
pub fn witness_script_at_keypath ( & self , keypath : & [ u32 ] ) -> Result < Vec < u8 > , Error > {
325
- match self {
326
- Self :: Wsh ( Wsh {
327
- policy ,
328
- miniscript_expr,
329
- } ) => {
330
- let ( is_change , address_index ) =
331
- get_change_and_address_index ( miniscript_expr . iter_pk ( ) , & policy . keys , keypath ) ?;
319
+ match & self . descriptor {
320
+ Descriptor :: Wsh ( Wsh { miniscript_expr } ) => {
321
+ let ( is_change , address_index ) = get_change_and_address_index (
322
+ miniscript_expr. iter_pk ( ) ,
323
+ & self . policy . keys ,
324
+ keypath ,
325
+ ) ?;
332
326
self . witness_script ( is_change, address_index)
333
327
}
334
328
}
335
329
}
336
330
337
331
/// Returns true if the address-level keypath points to a change address.
338
332
pub fn is_change_keypath ( & self , keypath : & [ u32 ] ) -> Result < bool , Error > {
339
- match self {
340
- Self :: Wsh ( Wsh {
341
- policy ,
342
- miniscript_expr,
343
- } ) => {
344
- let ( is_change , _ ) =
345
- get_change_and_address_index ( miniscript_expr . iter_pk ( ) , & policy . keys , keypath ) ?;
333
+ match & self . descriptor {
334
+ Descriptor :: Wsh ( Wsh { miniscript_expr } ) => {
335
+ let ( is_change , _ ) = get_change_and_address_index (
336
+ miniscript_expr. iter_pk ( ) ,
337
+ & self . policy . keys ,
338
+ keypath ,
339
+ ) ?;
346
340
Ok ( is_change)
347
341
}
348
342
}
@@ -364,10 +358,10 @@ pub fn parse(policy: &Policy) -> Result<ParsedPolicy, Error> {
364
358
miniscript:: Miniscript :: from_str ( & desc[ 4 ..desc. len ( ) - 1 ] )
365
359
. or ( Err ( Error :: InvalidInput ) ) ?;
366
360
367
- Ok ( ParsedPolicy :: Wsh ( Wsh {
361
+ Ok ( ParsedPolicy {
368
362
policy,
369
- miniscript_expr,
370
- } ) )
363
+ descriptor : Descriptor :: Wsh ( Wsh { miniscript_expr } ) ,
364
+ } )
371
365
}
372
366
_ => Err ( Error :: InvalidInput ) ,
373
367
}
@@ -595,10 +589,9 @@ mod tests {
595
589
fn test_parse_wsh_miniscript ( ) {
596
590
// Parse a valid example and check that the keys are collected as is as strings.
597
591
let policy = make_policy ( "wsh(pk(@0/**))" , & [ ] ) ;
598
- match parse ( & policy) . unwrap ( ) {
599
- ParsedPolicy :: Wsh ( Wsh {
600
- ref miniscript_expr,
601
- ..
592
+ match & parse ( & policy) . unwrap ( ) . descriptor {
593
+ Descriptor :: Wsh ( Wsh {
594
+ miniscript_expr, ..
602
595
} ) => {
603
596
assert_eq ! (
604
597
miniscript_expr. iter_pk( ) . collect:: <Vec <String >>( ) ,
@@ -609,10 +602,9 @@ mod tests {
609
602
610
603
// Parse another valid example and check that the keys are collected as is as strings.
611
604
let policy = make_policy ( "wsh(or_b(pk(@0/**),s:pk(@1/**)))" , & [ ] ) ;
612
- match parse ( & policy) . unwrap ( ) {
613
- ParsedPolicy :: Wsh ( Wsh {
614
- ref miniscript_expr,
615
- ..
605
+ match & parse ( & policy) . unwrap ( ) . descriptor {
606
+ Descriptor :: Wsh ( Wsh {
607
+ miniscript_expr, ..
616
608
} ) => {
617
609
assert_eq ! (
618
610
miniscript_expr. iter_pk( ) . collect:: <Vec <String >>( ) ,
0 commit comments