Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement a check to the latest version of the CLI #33

Merged
merged 8 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/bin/zkbtc-admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use zkbitcoin::{
committee::orchestrator::{CommitteeConfig, Member},
constants::{ZKBITCOIN_FEE_PUBKEY, ZKBITCOIN_PUBKEY},
frost, taproot_addr_from,
utils::version,
};

#[derive(Parser)]
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/bin/zkbtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use zkbitcoin::{
},
snarkjs::{self, CompilationResult},
taproot_addr_from,
utils::version,
};

#[derive(Parser)]
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod version;
42 changes: 42 additions & 0 deletions src/utils/version.rs
Original file line number Diff line number Diff line change
@@ -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<Release> {
let client = Client::new();

let release = client
.get(RELEASES_URL)
.header("User-Agent", "zkbitcoin cli")
.send()
.await?
.json::<Release>()
.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(())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a test would be nice :)

}
Loading