Skip to content

Commit c6c8ae9

Browse files
committed
feat: add max_weight_to_satisfy to Descriptor
1 parent e433fe9 commit c6c8ae9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

bdk-ffi/src/descriptor.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ impl Descriptor {
322322
.collect()
323323
})
324324
}
325+
326+
/// Computes an upper bound on the difference between a non-satisfied `TxIn`'s
327+
/// `segwit_weight` and a satisfied `TxIn`'s `segwit_weight`.
328+
pub fn max_weight_to_satisfy(&self) -> Result<u64, DescriptorError> {
329+
let weight = self
330+
.extended_descriptor
331+
.max_weight_to_satisfy()
332+
.map_err(|e| DescriptorError::Miniscript {
333+
error_message: e.to_string(),
334+
})?;
335+
Ok(weight.to_wu())
336+
}
325337
}
326338

327339
impl Display for Descriptor {
@@ -456,4 +468,19 @@ mod test {
456468
assert!(descriptor1.is_ok());
457469
assert_matches!(descriptor2.unwrap_err(), DescriptorError::Key { .. });
458470
}
471+
472+
#[test]
473+
fn test_max_weight_to_satisfy() {
474+
// Test P2WPKH descriptor using standard test descriptor
475+
let descriptor = Descriptor::new(
476+
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/1h/0/*)".to_string(),
477+
Network::Testnet
478+
).unwrap();
479+
480+
let weight = descriptor.max_weight_to_satisfy().unwrap();
481+
println!("P2WPKH max weight to satisfy: {} wu", weight);
482+
483+
// Verify the method works and returns a positive weight
484+
assert!(weight > 0, "Weight must be positive");
485+
}
459486
}

0 commit comments

Comments
 (0)