|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use anyhow::{bail, Context, Result}; |
| 4 | +use bitcoin::PublicKey; |
| 5 | +use miniscript::{descriptor::DescriptorType, Descriptor}; |
| 6 | + |
| 7 | +use super::{ |
| 8 | + common_limitations, complexity, summary, warning, ArtifactType, DescriptorAnalysis, RiskLevel, |
| 9 | + RiskReport, ScriptSignals, |
| 10 | +}; |
| 11 | +use crate::analyzer::script::{analyze_script_bytes, classify_script}; |
| 12 | + |
| 13 | +pub fn analyze_descriptor_input(input: &str) -> Result<RiskReport> { |
| 14 | + let descriptor = input.trim(); |
| 15 | + if descriptor.is_empty() { |
| 16 | + bail!("descriptor cannot be empty"); |
| 17 | + } |
| 18 | + |
| 19 | + let descriptor = Descriptor::<PublicKey>::from_str(descriptor) |
| 20 | + .context("descriptor input is not a valid public-key output descriptor")?; |
| 21 | + |
| 22 | + Ok(analyze_descriptor(&descriptor)) |
| 23 | +} |
| 24 | + |
| 25 | +fn analyze_descriptor(descriptor: &Descriptor<PublicKey>) -> RiskReport { |
| 26 | + let descriptor_type = descriptor_type_name(descriptor.desc_type()).to_owned(); |
| 27 | + let script_pubkey = descriptor.script_pubkey(); |
| 28 | + let script_type = classify_script(&script_pubkey); |
| 29 | + let sanity_check = descriptor.sanity_check().is_ok(); |
| 30 | + let max_satisfaction_weight_wu = descriptor |
| 31 | + .max_weight_to_satisfy() |
| 32 | + .ok() |
| 33 | + .map(|weight| weight.to_wu()); |
| 34 | + let signals = descriptor_signals(descriptor); |
| 35 | + |
| 36 | + let mut warnings = Vec::new(); |
| 37 | + let missing_data = Vec::new(); |
| 38 | + |
| 39 | + if !sanity_check { |
| 40 | + warnings.push(warning( |
| 41 | + "descriptor-sanity-check", |
| 42 | + RiskLevel::Medium, |
| 43 | + "Descriptor sanity check failed", |
| 44 | + "Miniscript parsed the descriptor, but its safety checks did not pass. Review spend paths, malleability, and standardness assumptions before operational use.", |
| 45 | + )); |
| 46 | + } |
| 47 | + |
| 48 | + if signals.multisig || signals.threshold { |
| 49 | + warnings.push(warning( |
| 50 | + "threshold-policy", |
| 51 | + RiskLevel::Low, |
| 52 | + "Threshold or multisig policy detected", |
| 53 | + "The descriptor includes threshold-like signing policy. Review signer count, quorum, backup paths, and key origin documentation.", |
| 54 | + )); |
| 55 | + } |
| 56 | + |
| 57 | + if signals.timelock || signals.relative_timelock { |
| 58 | + warnings.push(warning( |
| 59 | + "timelock-signal", |
| 60 | + RiskLevel::Medium, |
| 61 | + "Timelock signal detected", |
| 62 | + "The descriptor contains absolute or relative timelock policy. Confirm block height, median time, and sequence assumptions before relying on it.", |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + let complexity = descriptor_complexity(&descriptor_type, &signals); |
| 67 | + let mut summary_items = vec![ |
| 68 | + summary("descriptor_type", &descriptor_type), |
| 69 | + summary("script_type", &script_type), |
| 70 | + summary("sanity_check", sanity_check), |
| 71 | + ]; |
| 72 | + if let Some(weight) = max_satisfaction_weight_wu { |
| 73 | + summary_items.push(summary("max_satisfaction_weight_wu", weight)); |
| 74 | + } |
| 75 | + |
| 76 | + let risk = RiskLevel::from_warnings(&warnings, &missing_data); |
| 77 | + RiskReport { |
| 78 | + schema_version: super::REPORT_SCHEMA_VERSION.to_owned(), |
| 79 | + artifact_type: ArtifactType::Descriptor, |
| 80 | + risk, |
| 81 | + summary: summary_items, |
| 82 | + warnings, |
| 83 | + missing_data, |
| 84 | + limitations: common_limitations(), |
| 85 | + transaction: None, |
| 86 | + psbt: None, |
| 87 | + script: None, |
| 88 | + descriptor: Some(DescriptorAnalysis { |
| 89 | + descriptor_type, |
| 90 | + script_type, |
| 91 | + sanity_check, |
| 92 | + max_satisfaction_weight_wu, |
| 93 | + signals, |
| 94 | + complexity, |
| 95 | + }), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn descriptor_signals(descriptor: &Descriptor<PublicKey>) -> ScriptSignals { |
| 100 | + let normalized = descriptor.to_string(); |
| 101 | + let mut signals = descriptor |
| 102 | + .explicit_script() |
| 103 | + .map(|script| analyze_script_bytes(script.as_bytes()).signals) |
| 104 | + .unwrap_or_default(); |
| 105 | + |
| 106 | + signals.multisig |= has_any(&normalized, &["multi(", "sortedmulti(", "multi_a("]); |
| 107 | + signals.threshold |= has_any( |
| 108 | + &normalized, |
| 109 | + &["thresh(", "multi(", "sortedmulti(", "multi_a("], |
| 110 | + ); |
| 111 | + signals.timelock |= normalized.contains("after("); |
| 112 | + signals.relative_timelock |= normalized.contains("older("); |
| 113 | + signals |
| 114 | +} |
| 115 | + |
| 116 | +fn has_any(input: &str, needles: &[&str]) -> bool { |
| 117 | + needles.iter().any(|needle| input.contains(needle)) |
| 118 | +} |
| 119 | + |
| 120 | +fn descriptor_complexity(descriptor_type: &str, signals: &ScriptSignals) -> super::Complexity { |
| 121 | + let mut score = 0; |
| 122 | + let mut factors = vec![format!("descriptor type {descriptor_type}")]; |
| 123 | + |
| 124 | + if descriptor_type.contains("wsh") || descriptor_type.contains("sh_") { |
| 125 | + score += 1; |
| 126 | + factors.push("wrapped or witness script descriptor".to_owned()); |
| 127 | + } |
| 128 | + |
| 129 | + if signals.multisig || signals.threshold { |
| 130 | + score += 2; |
| 131 | + factors.push("threshold policy detected".to_owned()); |
| 132 | + } |
| 133 | + |
| 134 | + if signals.timelock || signals.relative_timelock { |
| 135 | + score += 2; |
| 136 | + factors.push("timelock policy detected".to_owned()); |
| 137 | + } |
| 138 | + |
| 139 | + complexity(score, factors) |
| 140 | +} |
| 141 | + |
| 142 | +fn descriptor_type_name(descriptor_type: DescriptorType) -> &'static str { |
| 143 | + match descriptor_type { |
| 144 | + DescriptorType::Bare => "bare", |
| 145 | + DescriptorType::Sh => "sh", |
| 146 | + DescriptorType::Pkh => "pkh", |
| 147 | + DescriptorType::Wpkh => "wpkh", |
| 148 | + DescriptorType::Wsh => "wsh", |
| 149 | + DescriptorType::ShWsh => "sh_wsh", |
| 150 | + DescriptorType::ShWpkh => "sh_wpkh", |
| 151 | + DescriptorType::ShSortedMulti => "sh_sortedmulti", |
| 152 | + DescriptorType::WshSortedMulti => "wsh_sortedmulti", |
| 153 | + DescriptorType::ShWshSortedMulti => "sh_wsh_sortedmulti", |
| 154 | + DescriptorType::Tr => "tr", |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +#[cfg(test)] |
| 159 | +mod tests { |
| 160 | + use super::*; |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn analyzes_simple_descriptor_fixture() { |
| 164 | + let report = |
| 165 | + analyze_descriptor_input(include_str!("../../tests/fixtures/descriptors/simple.txt")) |
| 166 | + .unwrap(); |
| 167 | + let analysis = report.descriptor.unwrap(); |
| 168 | + |
| 169 | + assert_eq!(report.schema_version, "0.3"); |
| 170 | + assert_eq!(analysis.descriptor_type, "wpkh"); |
| 171 | + assert_eq!(analysis.script_type, "p2wpkh"); |
| 172 | + assert!(analysis.sanity_check); |
| 173 | + assert!(analysis.max_satisfaction_weight_wu.is_some()); |
| 174 | + assert!(!analysis.signals.multisig); |
| 175 | + assert_eq!(report.risk, RiskLevel::Low); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn analyzes_sortedmulti_descriptor_fixture() { |
| 180 | + let report = analyze_descriptor_input(include_str!( |
| 181 | + "../../tests/fixtures/descriptors/sortedmulti.txt" |
| 182 | + )) |
| 183 | + .unwrap(); |
| 184 | + let analysis = report.descriptor.unwrap(); |
| 185 | + |
| 186 | + assert_eq!(analysis.descriptor_type, "wsh_sortedmulti"); |
| 187 | + assert_eq!(analysis.script_type, "p2wsh"); |
| 188 | + assert!(analysis.signals.multisig); |
| 189 | + assert!(analysis.signals.threshold); |
| 190 | + assert!(report |
| 191 | + .warnings |
| 192 | + .iter() |
| 193 | + .any(|warning| warning.code == "threshold-policy")); |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn analyzes_timelock_descriptor_fixture() { |
| 198 | + let report = analyze_descriptor_input(include_str!( |
| 199 | + "../../tests/fixtures/descriptors/timelock.txt" |
| 200 | + )) |
| 201 | + .unwrap(); |
| 202 | + let analysis = report.descriptor.unwrap(); |
| 203 | + |
| 204 | + assert_eq!(analysis.descriptor_type, "wsh"); |
| 205 | + assert!(analysis.signals.relative_timelock); |
| 206 | + assert_eq!(report.risk, RiskLevel::Medium); |
| 207 | + assert!(report |
| 208 | + .warnings |
| 209 | + .iter() |
| 210 | + .any(|warning| warning.code == "timelock-signal")); |
| 211 | + } |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn rejects_invalid_descriptor_fixture() { |
| 215 | + let err = |
| 216 | + analyze_descriptor_input(include_str!("../../tests/fixtures/descriptors/invalid.txt")) |
| 217 | + .unwrap_err(); |
| 218 | + |
| 219 | + assert!(err |
| 220 | + .to_string() |
| 221 | + .contains("descriptor input is not a valid public-key output descriptor")); |
| 222 | + } |
| 223 | +} |
0 commit comments