From e58cf38dc72daba974926dbe15b36f2fb7160c4e Mon Sep 17 00:00:00 2001 From: ppoliani Date: Sun, 4 Feb 2024 16:05:21 +0200 Subject: [PATCH] feat: implement a check to the latest version of the CLI --- .gitignore | 1 + Cargo.toml | 3 ++- src/bin/zkbtc.rs | 4 ++++ src/lib.rs | 1 + src/utils/mod.rs | 1 + src/utils/version.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/utils/mod.rs create mode 100644 src/utils/version.rs diff --git a/.gitignore b/.gitignore index 5696b65..5e026f9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb .vscode +local_keys diff --git a/Cargo.toml b/Cargo.toml index 092ab44..0012860 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" [patch.crates-io] # see docs/serialization.md diff --git a/src/bin/zkbtc.rs b/src/bin/zkbtc.rs index 0bb651d..d027d29 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 f4d2cff..cc556fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,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..a203223 --- /dev/null +++ b/src/utils/version.rs @@ -0,0 +1,43 @@ +use anyhow::Result; +use log::warn; +use reqwest::Client; +use serde::Deserialize; +use versions::Versioning; + +const RELEASES_URL: &'static 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", "Rust 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(()) +}