@@ -210,15 +210,15 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
210
210
/// If spend data is already computed (i.e it is not `None`), this does not recompute it.
211
211
///
212
212
/// [`TaprootSpendInfo`] is only required for spending via the script paths.
213
- pub fn spend_info ( & self ) -> Arc < TaprootSpendInfo >
213
+ pub fn spend_info ( & self ) -> Result < Arc < TaprootSpendInfo > , Error >
214
214
where
215
215
Pk : ToPublicKey ,
216
216
{
217
217
// If the value is already cache, read it
218
218
// read only panics if the lock is poisoned (meaning other thread having a lock panicked)
219
219
let read_lock = self . spend_info . lock ( ) . expect ( "Lock poisoned" ) ;
220
220
if let Some ( ref spend_info) = * read_lock {
221
- return Arc :: clone ( spend_info) ;
221
+ return Ok ( Arc :: clone ( spend_info) ) ;
222
222
}
223
223
drop ( read_lock) ;
224
224
@@ -232,9 +232,13 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
232
232
let mut builder = TaprootBuilder :: new ( ) ;
233
233
for ( depth, ms) in self . iter_scripts ( ) {
234
234
let script = ms. encode ( ) ;
235
- builder = builder
236
- . add_leaf ( depth, script)
237
- . expect ( "Computing spend data on a valid Tree should always succeed" ) ;
235
+ if let Ok ( taproot_builder) = builder. add_leaf ( depth, script) {
236
+ builder = taproot_builder;
237
+ } else {
238
+ return Err ( Error :: Unexpected ( String :: from (
239
+ "Computing spend data on a valid Tree should always succeed" ,
240
+ ) ) ) ;
241
+ }
238
242
}
239
243
// Assert builder cannot error here because we have a well formed descriptor
240
244
match builder. finalize ( & secp, self . internal_key . to_x_only_pubkey ( ) ) {
@@ -263,7 +267,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
263
267
} ;
264
268
let spend_info = Arc :: new ( data) ;
265
269
* self . spend_info . lock ( ) . expect ( "Lock poisoned" ) = Some ( Arc :: clone ( & spend_info) ) ;
266
- spend_info
270
+ Ok ( spend_info)
267
271
}
268
272
269
273
/// Checks whether the descriptor is safe.
@@ -311,7 +315,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
311
315
impl < Pk : MiniscriptKey + ToPublicKey > Tr < Pk > {
312
316
/// Obtains the corresponding script pubkey for this descriptor.
313
317
pub fn script_pubkey ( & self ) -> Script {
314
- let output_key = self . spend_info ( ) . output_key ( ) ;
318
+ let output_key = self . spend_info ( ) . unwrap ( ) . output_key ( ) ;
315
319
let builder = bitcoin:: blockdata:: script:: Builder :: new ( ) ;
316
320
builder
317
321
. push_opcode ( opcodes:: all:: OP_PUSHNUM_1 )
@@ -320,9 +324,9 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
320
324
}
321
325
322
326
/// Obtains the corresponding address for this descriptor.
323
- pub fn address ( & self , network : Network ) -> Address {
324
- let spend_info = self . spend_info ( ) ;
325
- Address :: p2tr_tweaked ( spend_info. output_key ( ) , network)
327
+ pub fn address ( & self , network : Network ) -> Result < Address , Error > {
328
+ let spend_info = self . spend_info ( ) ? ;
329
+ Ok ( Address :: p2tr_tweaked ( spend_info. output_key ( ) , network) )
326
330
}
327
331
328
332
/// Returns satisfying non-malleable witness and scriptSig with minimum
@@ -630,7 +634,7 @@ where
630
634
Pk : ToPublicKey ,
631
635
S : Satisfier < Pk > ,
632
636
{
633
- let spend_info = desc. spend_info ( ) ;
637
+ let spend_info = desc. spend_info ( ) . unwrap ( ) ;
634
638
// First try the key spend path
635
639
if let Some ( sig) = satisfier. lookup_tap_key_spend_sig ( ) {
636
640
Ok ( ( vec ! [ sig. to_vec( ) ] , Script :: new ( ) ) )
0 commit comments