diff --git a/requirements.txt b/requirements.txt index 1827099..fe53441 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ hwi>=2.1.1,<3 protobuf==3.20.1 +requests>=2.27.1 \ No newline at end of file diff --git a/src/interface.rs b/src/interface.rs index 180e5e1..10ea4b8 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -1,4 +1,5 @@ use std::ops::Deref; +use std::process::Command; use bitcoin::consensus::encode::serialize; use bitcoin::util::bip32::DerivationPath; @@ -362,4 +363,37 @@ impl HWIClient { status.into() }) } + + /// Get the installed version of hwilib. Returns None if hwi is not installed. + pub fn get_version() -> Option { + Python::with_gil(|py| { + Some( + PyModule::import(py, "hwilib") + .ok()? + .getattr("__version__") + .expect("Should have a __version__") + .to_string(), + ) + }) + } + + /// Install hwi for the current user via pip. If no version is specified, the default version from pip will be installed. + pub fn install_hwilib(version: Option<&str>) -> Result<(), Error> { + let hwi_with_version = match version { + Some(ver) => "hwi==".to_owned() + ver, + None => "hwi".to_owned(), + }; + let output = Command::new("pip") + .args(vec!["install", "--user", hwi_with_version.as_str()]) + .output()?; + if output.status.success() { + Ok(()) + } else { + Err(Error::HWIError( + std::str::from_utf8(&output.stderr) + .expect("Non UTF-8 error while installing") + .to_string(), + )) + } + } } diff --git a/src/lib.rs b/src/lib.rs index 45fe89d..b4245f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -306,6 +306,11 @@ mod tests { break; } } + #[test] + #[serial] + fn test_get_version() { + HWIClient::get_version().unwrap(); + } #[test] #[serial] @@ -322,4 +327,10 @@ mod tests { client.wipe_device().unwrap(); } } + #[test] + #[serial] + #[ignore] + fn test_install_hwi() { + HWIClient::install_hwilib(Some("2.1.1")).unwrap(); + } }