use std::fs;
use std::path::{Path, PathBuf};
use std::result::Result;
#[derive(Debug, Clone, Default)]
pub struct BitcoinConfig {
pub network: String,
pub rpc_url: String,
pub auth_method: String,
pub username: Option<String>,
pub password: Option<String>,
pub timeout_seconds: u64,
}
const BIP341_SILENT_LEAF: bool = true;
#[derive(Debug)]
pub struct BIPCompliance {
pub taproot_enabled: bool,
pub schnorr_enabled: bool,
pub psbt_version: u8,
}
pub struct ConfigManager {
path: PathBuf,
}
impl ConfigManager {
pub fn new(install_dir: &Path) -> Self {
Self {
path: install_dir.join("conf/bitcoin.conf"),
}
}
pub fn generate(&self, _config: &BitcoinConfig) -> Result<(), std::io::Error> {
let content = format!(
"network=mainnet\n\
taproot=1\n\
silent_leaf={BIP341_SILENT_LEAF}\n\
psbt_version=2"
);
fs::write(&self.path, content)
}
pub fn validate_bips(&self) -> Result<BIPCompliance, std::io::Error> {
let content = fs::read_to_string(&self.path)?;
let taproot_enabled = content.contains("taproot=1");
let schnorr_enabled = content.contains("schnorr=1") || taproot_enabled;
let psbt_version = if content.contains("psbt_version=") {
let line = content
.lines()
.find(|line| line.starts_with("psbt_version="))
.unwrap_or("psbt_version=0");
line.split('=')
.nth(1)
.and_then(|v| v.parse::<u8>().ok())
.unwrap_or(0)
} else {
0 };
Ok(BIPCompliance {
taproot_enabled,
schnorr_enabled,
psbt_version,
})
}
}