diff --git a/Cargo.toml b/Cargo.toml index 0313273..f9c1400 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ num-bigint = "0.4.4" num-traits = "0.2.17" rand = "0.8.5" rand_chacha = "0.3.1" -reqwest = { version = "0.11", features = ["stream"] } +reqwest = { version = "0.11", features = ["stream", "json"] } secp256k1 = "0.28.0" serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0" } @@ -41,6 +41,7 @@ tokio = { version = "1.34", features = [ "macros", ] } tokio-stream = "0.1.14" +versions = "6.1.0" xml = "0.8.10" fancy-regex = "0.13.0" chrono = "0.4.33" diff --git a/src/bin/zkbtc-admin.rs b/src/bin/zkbtc-admin.rs index a2c7e46..08057cd 100644 --- a/src/bin/zkbtc-admin.rs +++ b/src/bin/zkbtc-admin.rs @@ -6,6 +6,7 @@ use zkbitcoin::{ committee::orchestrator::{CommitteeConfig, Member}, constants::{ZKBITCOIN_FEE_PUBKEY, ZKBITCOIN_PUBKEY}, frost, taproot_addr_from, + utils::version, }; #[derive(Parser)] @@ -77,6 +78,9 @@ async fn main() -> Result<()> { taproot_addr_from(ZKBITCOIN_FEE_PUBKEY).unwrap().to_string() ); + // ignore if there is any error + let _ = version::check_version().await; + // parse CLI let cli = Cli::parse(); match &cli.command { diff --git a/src/bin/zkbtc.rs b/src/bin/zkbtc.rs index 95601ec..3d979cd 100644 --- a/src/bin/zkbtc.rs +++ b/src/bin/zkbtc.rs @@ -16,6 +16,7 @@ use zkbitcoin::{ }, snarkjs::{self, CompilationResult}, taproot_addr_from, + utils::version, }; #[derive(Parser)] @@ -140,6 +141,9 @@ async fn main() -> Result<()> { taproot_addr_from(ZKBITCOIN_FEE_PUBKEY).unwrap().to_string() ); + // ignore if there is any error + let _ = version::check_version().await; + // parse CLI let cli = Cli::parse(); match &cli.command { diff --git a/src/lib.rs b/src/lib.rs index afc8536..a1d3f4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ pub mod json_rpc_stuff; pub mod plonk; pub mod snarkjs; pub mod srs; +pub mod utils; /// 1. Alice signs a transaction to deploy a smart contract. pub mod alice_sign_tx; diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..a6db76a --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod version; diff --git a/src/utils/version.rs b/src/utils/version.rs new file mode 100644 index 0000000..a076fd8 --- /dev/null +++ b/src/utils/version.rs @@ -0,0 +1,42 @@ +use anyhow::Result; +use log::warn; +use reqwest::Client; +use serde::Deserialize; +use versions::Versioning; + +const RELEASES_URL: &str = "https://api.github.com/repos/sigma0-xyz/zkbitcoin/releases/latest"; + +#[derive(Deserialize, Debug)] +struct Release { + url: String, + tag_name: String, +} + +async fn fetch_latest_version() -> Result { + let client = Client::new(); + + let release = client + .get(RELEASES_URL) + .header("User-Agent", "zkbitcoin cli") + .send() + .await? + .json::() + .await?; + + Ok(release) +} + +pub async fn check_version() -> Result<()> { + let current_version = Versioning::new(env!("CARGO_PKG_VERSION")); + let latest_release = fetch_latest_version().await?; + let latest_version = Versioning::new(&latest_release.tag_name.replace('v', "")); + + if current_version < latest_version { + warn!( + "You are using an old version. Please download the latest version: {}", + latest_release.url + ) + } + + Ok(()) +}