use anyhow::{Context, Result};
use std::fs;
use std::path::Path;
use std::process;
fn main() -> Result<()> {
println!("BIP Module Verification Tool");
println!("----------------------------");
println!("Based on Bitcoin Development Framework v2.5");
println!();
let bip_dir = Path::new("core/src/bip");
if !bip_dir.exists() {
println!(
"❌ Error: BIP implementation directory not found at {}",
bip_dir.display()
);
process::exit(1);
}
let mod_file = bip_dir.join("mod.rs");
if !mod_file.exists() {
println!(
"❌ Error: BIP module registry not found at {}",
mod_file.display()
);
process::exit(1);
}
let mod_content =
fs::read_to_string(&mod_file).context("Failed to read module registry file")?;
verify_bip_file(bip_dir, "bip341.rs", "BIP-341 (Taproot)")?;
verify_bip_file(bip_dir, "bip342.rs", "BIP-342 (Tapscript)")?;
verify_registry_entry(&mod_content, "BIP-341", "Taproot")?;
verify_registry_entry(&mod_content, "BIP-342", "Tapscript")?;
verify_registry_entry(&mod_content, "BIP-174", "PSBT")?;
verify_registry_entry(&mod_content, "BIP-370", "PSBT v2")?;
verify_module_structure(&mod_content)?;
verify_bitcoin_interface()?;
println!("\n✅ BIP module verification complete");
println!("All required BIP modules are present and properly structured");
println!("Registry contains all required BIPs with correct status");
Ok(())
}
fn verify_bip_file(dir: &Path, filename: &str, description: &str) -> Result<()> {
let file_path = dir.join(filename);
if !file_path.exists() {
println!(
"❌ Error: {} implementation not found at {}",
description,
file_path.display()
);
process::exit(1);
}
let content =
fs::read_to_string(&file_path).context(format!("Failed to read {description} file"))?;
if !content.contains("[AIR-3][AIS-3][BPC-3]") {
println!("⚠️ Warning: {description} file missing proper AI labeling");
}
println!(
"✅ {} implementation found at {}",
description,
file_path.display()
);
Ok(())
}
fn verify_registry_entry(content: &str, bip: &str, description: &str) -> Result<()> {
if !content.contains(&format!(
"registry.register(\"{bip}\", BIPStatus::Complete)"
)) {
println!("⚠️ Warning: BIP registry missing entry for {bip} ({description})");
} else {
println!("✅ BIP registry contains {bip} ({description}) with Complete status");
}
Ok(())
}
fn verify_module_structure(content: &str) -> Result<()> {
let required_modules = vec!["pub mod bip341;", "pub mod bip342;"];
for module in required_modules {
if !content.contains(module) {
println!("❌ Error: Module registry missing export for {module}");
process::exit(1);
}
}
if !content.contains("pub struct BIPRegistry") {
println!("❌ Error: Module registry missing BIPRegistry struct definition");
process::exit(1);
}
if !content.contains("pub enum BIPStatus") {
println!("❌ Error: Module registry missing BIPStatus enum definition");
process::exit(1);
}
println!("✅ Module structure verified with all required components");
Ok(())
}
fn verify_bitcoin_interface() -> Result<()> {
let interface_dir = Path::new("src/bitcoin/interface");
if !interface_dir.exists() {
println!(
"❌ Error: Bitcoin interface directory not found at {}",
interface_dir.display()
);
process::exit(1);
}
verify_file_exists(
&interface_dir.join("mod.rs"),
"Bitcoin interface module registry",
)?;
verify_file_exists(&interface_dir.join("block.rs"), "Block interface")?;
verify_file_exists(
&interface_dir.join("transaction.rs"),
"Transaction interface",
)?;
verify_file_exists(&interface_dir.join("network.rs"), "Network interface")?;
println!("✅ Bitcoin interface layer verified with all required components");
Ok(())
}
fn verify_file_exists(path: &Path, description: &str) -> Result<()> {
if !path.exists() {
println!("❌ Error: {} not found at {}", description, path.display());
process::exit(1);
}
println!("✅ {} found at {}", description, path.display());
Ok(())
}