Skip to content

Commit b30c8fd

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

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
475+
let descriptor = Descriptor::new(
476+
"wpkh(tprv8ZgxMBicQKsPeitVUz3s6cfyCECovNP7t82FaKPa4UKqV1kssWcXgLkMDjzDbgG9GWoza4pL7z727QitfzkiwX99E1Has3T3a1MKHvYWmQZ/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)