From de64da8e20c5c23c2c14b0a6c228cfe529ce2ada Mon Sep 17 00:00:00 2001 From: Pierre Dommerc Date: Mon, 17 Apr 2023 15:12:26 +0200 Subject: [PATCH] feat(wallet): app version check (#3308) --- nym-wallet/nym-wallet-types/src/app.rs | 12 ++++++++ nym-wallet/src-tauri/src/error.rs | 2 ++ nym-wallet/src-tauri/src/main.rs | 2 ++ .../src-tauri/src/operations/app/mod.rs | 1 + .../src-tauri/src/operations/app/version.rs | 29 +++++++++++++++++++ nym-wallet/src-tauri/src/operations/mod.rs | 1 + .../src/components/Settings/AppVersion.tsx | 19 +++++++----- nym-wallet/src/requests/app.ts | 4 +++ nym-wallet/src/requests/index.ts | 1 + nym-wallet/src/types/rust/AppVersion.ts | 7 +++++ tools/ts-rs-cli/src/main.rs | 2 ++ 11 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 nym-wallet/src-tauri/src/operations/app/mod.rs create mode 100644 nym-wallet/src-tauri/src/operations/app/version.rs create mode 100644 nym-wallet/src/requests/app.ts create mode 100644 nym-wallet/src/types/rust/AppVersion.ts diff --git a/nym-wallet/nym-wallet-types/src/app.rs b/nym-wallet/nym-wallet-types/src/app.rs index 792a5d51eb..b0779982fc 100644 --- a/nym-wallet/nym-wallet-types/src/app.rs +++ b/nym-wallet/nym-wallet-types/src/app.rs @@ -12,3 +12,15 @@ pub struct AppEnv { pub SHOW_TERMINAL: Option, pub ENABLE_QA_MODE: Option, } + +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export, export_to = "nym-wallet/src/types/rust/AppVersion.ts") +)] +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] +pub struct AppVersion { + pub current_version: String, + pub latest_version: String, + pub is_update_available: bool, +} diff --git a/nym-wallet/src-tauri/src/error.rs b/nym-wallet/src-tauri/src/error.rs index cf9e1b6563..c639a39aa5 100644 --- a/nym-wallet/src-tauri/src/error.rs +++ b/nym-wallet/src-tauri/src/error.rs @@ -124,6 +124,8 @@ pub enum BackendError { SignatureError(String), #[error("Unable to open a new window")] NewWindowError, + #[error("Failed to check for application update")] + CheckAppVersionError, #[error(transparent)] WalletError { diff --git a/nym-wallet/src-tauri/src/main.rs b/nym-wallet/src-tauri/src/main.rs index 8146bc9a18..e86b475409 100644 --- a/nym-wallet/src-tauri/src/main.rs +++ b/nym-wallet/src-tauri/src/main.rs @@ -8,6 +8,7 @@ use tauri::{Manager, Menu}; use nym_mixnet_contract_common::{Gateway, MixNode}; use crate::menu::AddDefaultSubmenus; +use crate::operations::app; use crate::operations::help; use crate::operations::mixnet; use crate::operations::nym_api; @@ -35,6 +36,7 @@ fn main() { tauri::Builder::default() .manage(WalletState::default()) .invoke_handler(tauri::generate_handler![ + app::version::check_version, mixnet::account::add_account_for_password, mixnet::account::archive_wallet_file, mixnet::account::connect_with_mnemonic, diff --git a/nym-wallet/src-tauri/src/operations/app/mod.rs b/nym-wallet/src-tauri/src/operations/app/mod.rs new file mode 100644 index 0000000000..a6db76ad4b --- /dev/null +++ b/nym-wallet/src-tauri/src/operations/app/mod.rs @@ -0,0 +1 @@ +pub mod version; diff --git a/nym-wallet/src-tauri/src/operations/app/version.rs b/nym-wallet/src-tauri/src/operations/app/version.rs new file mode 100644 index 0000000000..d9d475e931 --- /dev/null +++ b/nym-wallet/src-tauri/src/operations/app/version.rs @@ -0,0 +1,29 @@ +// Copyright 2022 - Nym Technologies SA +// SPDX-License-Identifier: Apache-2.0 + +use crate::error::BackendError; +use nym_wallet_types::app::AppVersion; + +#[tauri::command] +pub async fn check_version(handle: tauri::AppHandle) -> Result { + log::info!(">>> Getting app version info"); + let res = tauri::updater::builder(handle) + .check() + .await + .map(|u| AppVersion { + current_version: u.current_version().to_string(), + latest_version: u.latest_version().to_owned(), + is_update_available: u.is_update_available(), + }) + .map_err(|e| { + log::error!("An error ocurred while checking for app update {}", e); + BackendError::CheckAppVersionError + })?; + log::debug!( + "<<< update available: [{}], current version {}, latest version {}", + res.is_update_available, + res.current_version, + res.latest_version + ); + Ok(res) +} diff --git a/nym-wallet/src-tauri/src/operations/mod.rs b/nym-wallet/src-tauri/src/operations/mod.rs index 70c6928b0e..3c7e2cc6cd 100644 --- a/nym-wallet/src-tauri/src/operations/mod.rs +++ b/nym-wallet/src-tauri/src/operations/mod.rs @@ -1,6 +1,7 @@ // Copyright 2021-2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +pub mod app; pub mod help; pub(crate) mod helpers; pub mod mixnet; diff --git a/nym-wallet/src/components/Settings/AppVersion.tsx b/nym-wallet/src/components/Settings/AppVersion.tsx index 8a5df53e6c..8e62a0f6ab 100644 --- a/nym-wallet/src/components/Settings/AppVersion.tsx +++ b/nym-wallet/src/components/Settings/AppVersion.tsx @@ -1,18 +1,17 @@ import React, { useContext, useEffect, useState } from 'react'; -import { Stack, Typography } from '@mui/material'; +import { Button, Stack, Typography } from '@mui/material'; import { checkUpdate } from '@tauri-apps/api/updater'; import { AppContext } from '../../context'; +import { checkVersion } from '../../requests'; const AppVersion = () => { const [updateAvailable, setUpdateAvailable] = useState(false); const { appVersion } = useContext(AppContext); const updateCheck = async () => { - const update = await checkUpdate(); - if (update.shouldUpdate && update.manifest) { + const res = await checkVersion(); + if (res.is_update_available) { setUpdateAvailable(true); - } else { - setUpdateAvailable(false); } }; @@ -20,6 +19,12 @@ const AppVersion = () => { updateCheck(); }, [appVersion]); + const updateHandler = async () => { + // despite the name, this will spawn an external native window with + // an embedded "download and install" flow + checkUpdate(); + }; + return ( @@ -29,9 +34,9 @@ const AppVersion = () => { {`Nym Wallet ${appVersion}`} {updateAvailable && ( - + )} ); diff --git a/nym-wallet/src/requests/app.ts b/nym-wallet/src/requests/app.ts new file mode 100644 index 0000000000..4c26b8fcf6 --- /dev/null +++ b/nym-wallet/src/requests/app.ts @@ -0,0 +1,4 @@ +import { invokeWrapper } from './wrapper'; +import { AppVersion } from '../types/rust/AppVersion'; + +export const checkVersion = async () => invokeWrapper('check_version'); diff --git a/nym-wallet/src/requests/index.ts b/nym-wallet/src/requests/index.ts index 6e148116c2..e6fb5e482c 100644 --- a/nym-wallet/src/requests/index.ts +++ b/nym-wallet/src/requests/index.ts @@ -1,3 +1,4 @@ +export * from './app'; export * from './account'; export * from './actions'; export * from './contract'; diff --git a/nym-wallet/src/types/rust/AppVersion.ts b/nym-wallet/src/types/rust/AppVersion.ts new file mode 100644 index 0000000000..82b94c5596 --- /dev/null +++ b/nym-wallet/src/types/rust/AppVersion.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface AppVersion { + current_version: string; + latest_version: string; + is_update_available: boolean; +} diff --git a/tools/ts-rs-cli/src/main.rs b/tools/ts-rs-cli/src/main.rs index 8bfdf074eb..d5a5878008 100644 --- a/tools/ts-rs-cli/src/main.rs +++ b/tools/ts-rs-cli/src/main.rs @@ -29,6 +29,7 @@ use nym_types::vesting::{OriginalVestingResponse, PledgeData, VestingAccountInfo use nym_vesting_contract_common::Period; use nym_wallet_types::admin::TauriContractStateParams; use nym_wallet_types::app::AppEnv; +use nym_wallet_types::app::AppVersion; use nym_wallet_types::interval::Interval; use nym_wallet_types::network::Network; use nym_wallet_types::network_config::{Validator, ValidatorUrl, ValidatorUrls}; @@ -126,6 +127,7 @@ fn main() { // nym-wallet do_export!(AppEnv); + do_export!(AppVersion); do_export!(Interval); do_export!(Network); do_export!(TauriContractStateParams);