@@ -214,15 +214,15 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
214
214
/// If spend data is already computed (i.e it is not `None`), this does not recompute it.
215
215
///
216
216
/// [`TaprootSpendInfo`] is only required for spending via the script paths.
217
- pub fn spend_info ( & self ) -> Arc < TaprootSpendInfo >
217
+ pub fn spend_info ( & self ) -> Result < Arc < TaprootSpendInfo > , Error >
218
218
where
219
219
Pk : ToPublicKey ,
220
220
{
221
221
// If the value is already cache, read it
222
222
// read only panics if the lock is poisoned (meaning other thread having a lock panicked)
223
223
let read_lock = self . spend_info . lock ( ) . expect ( "Lock poisoned" ) ;
224
224
if let Some ( ref spend_info) = * read_lock {
225
- return Arc :: clone ( spend_info) ;
225
+ return Ok ( Arc :: clone ( spend_info) ) ;
226
226
}
227
227
drop ( read_lock) ;
228
228
@@ -236,9 +236,13 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
236
236
let mut builder = TaprootBuilder :: new ( ) ;
237
237
for ( depth, ms) in self . iter_scripts ( ) {
238
238
let script = ms. encode ( ) ;
239
- builder = builder
240
- . add_leaf ( depth, script)
241
- . expect ( "Computing spend data on a valid Tree should always succeed" ) ;
239
+ if let Ok ( taproot_builder) = builder. add_leaf ( depth, script) {
240
+ builder = taproot_builder;
241
+ } else {
242
+ return Err ( Error :: Unexpected ( String :: from (
243
+ "Computing spend data on a valid Tree should always succeed" ,
244
+ ) ) ) ;
245
+ }
242
246
}
243
247
// Assert builder cannot error here because we have a well formed descriptor
244
248
match builder. finalize ( & secp, self . internal_key . to_x_only_pubkey ( ) ) {
@@ -267,7 +271,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
267
271
} ;
268
272
let spend_info = Arc :: new ( data) ;
269
273
* self . spend_info . lock ( ) . expect ( "Lock poisoned" ) = Some ( Arc :: clone ( & spend_info) ) ;
270
- spend_info
274
+ Ok ( spend_info)
271
275
}
272
276
273
277
/// Checks whether the descriptor is safe.
@@ -315,7 +319,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
315
319
impl < Pk : MiniscriptKey + ToPublicKey > Tr < Pk > {
316
320
/// Obtains the corresponding script pubkey for this descriptor.
317
321
pub fn script_pubkey ( & self ) -> Script {
318
- let output_key = self . spend_info ( ) . output_key ( ) ;
322
+ let output_key = self . spend_info ( ) . unwrap ( ) . output_key ( ) ;
319
323
let builder = bitcoin:: blockdata:: script:: Builder :: new ( ) ;
320
324
builder
321
325
. push_opcode ( opcodes:: all:: OP_PUSHNUM_1 )
@@ -324,9 +328,9 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
324
328
}
325
329
326
330
/// Obtains the corresponding address for this descriptor.
327
- pub fn address ( & self , network : Network ) -> Address {
328
- let spend_info = self . spend_info ( ) ;
329
- Address :: p2tr_tweaked ( spend_info. output_key ( ) , network)
331
+ pub fn address ( & self , network : Network ) -> Result < Address , Error > {
332
+ let spend_info = self . spend_info ( ) ? ;
333
+ Ok ( Address :: p2tr_tweaked ( spend_info. output_key ( ) , network) )
330
334
}
331
335
332
336
/// Returns satisfying non-malleable witness and scriptSig with minimum
@@ -658,7 +662,7 @@ where
658
662
Pk : ToPublicKey ,
659
663
S : Satisfier < Pk > ,
660
664
{
661
- let spend_info = desc. spend_info ( ) ;
665
+ let spend_info = desc. spend_info ( ) . unwrap ( ) ;
662
666
// First try the key spend path
663
667
if let Some ( sig) = satisfier. lookup_tap_key_spend_sig ( ) {
664
668
Ok ( ( vec ! [ sig. to_vec( ) ] , Script :: new ( ) ) )
0 commit comments