Skip to content

Commit

Permalink
feat: implement a check to the latest version of the CLI (#33)
Browse files Browse the repository at this point in the history
Fixes #11

The version is checked against the latest release which we can get from
this
[endpoint](https://api.github.com/repos//sigma0-xyz/zkbitcoin/releases/latest).
We compare this version to the crate's version.

For this to work we would need:

1. Publish releases of new versions
2. The name of the tag should be prefixed with `v` e.g. `v0.1.0`
3. Bump crate version in Cargo.lock upon new release.
  • Loading branch information
ppoliani authored Feb 18, 2024
1 parent 03c3c66 commit 34dc952
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
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(())
}

0 comments on commit 34dc952

Please sign in to comment.