From 0c3e30ee2ccd6f5626cee96596fb8bf52bcd58f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Wed, 16 Mar 2022 09:30:42 +0100 Subject: [PATCH] wallets: provide placeholder functions for ui password --- nym-wallet/src-tauri/src/error.rs | 2 + nym-wallet/src-tauri/src/main.rs | 3 ++ .../src/operations/mixnet/account.rs | 47 +++++++++++++++++++ .../src/wallet_storage/account_data.rs | 3 +- .../src-tauri/src/wallet_storage/mod.rs | 5 +- 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/nym-wallet/src-tauri/src/error.rs b/nym-wallet/src-tauri/src/error.rs index 0f3aa9bd10..92cfc19562 100644 --- a/nym-wallet/src-tauri/src/error.rs +++ b/nym-wallet/src-tauri/src/error.rs @@ -81,6 +81,8 @@ pub enum BackendError { NoNymdValidatorConfigured, #[error("No validator API URL configured")] NoValidatorApiUrlConfigured, + #[error("The wallet file already exists")] + WalletFileAlreadyExists, } impl Serialize for BackendError { diff --git a/nym-wallet/src-tauri/src/main.rs b/nym-wallet/src-tauri/src/main.rs index 42fd4dcbff..6e592948c1 100644 --- a/nym-wallet/src-tauri/src/main.rs +++ b/nym-wallet/src-tauri/src/main.rs @@ -36,8 +36,11 @@ fn main() { .invoke_handler(tauri::generate_handler![ mixnet::account::connect_with_mnemonic, mixnet::account::create_new_account, + mixnet::account::create_password, + mixnet::account::does_password_file_exist, mixnet::account::get_balance, mixnet::account::logout, + mixnet::account::sign_in_with_password, mixnet::account::switch_network, mixnet::account::update_validator_urls, mixnet::admin::get_contract_settings, diff --git a/nym-wallet/src-tauri/src/operations/mixnet/account.rs b/nym-wallet/src-tauri/src/operations/mixnet/account.rs index a390fecb3d..01918bf844 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/account.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/account.rs @@ -4,8 +4,11 @@ use crate::error::BackendError; use crate::network::Network; use crate::nymd_client; use crate::state::State; +use crate::wallet_storage; use bip39::{Language, Mnemonic}; +use config::defaults::COSMOS_DERIVATION_PATH; +use cosmrs::bip32::DerivationPath; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::convert::TryInto; @@ -316,3 +319,47 @@ async fn is_validator_connection_ok(client: &Client) -> bool Err(_) | Ok(_) => true, } } + +#[tauri::command] +pub fn does_password_file_exist() -> Result { + println!("Checking wallet file"); + let file = wallet_storage::wallet_login_filepath()?; + if file.is_file() { + println!("Exists: {}", file.to_string_lossy()); + } else { + println!("Does not exist: {}", file.to_string_lossy()); + } + Ok(file.is_file()) +} + +#[tauri::command] +pub fn create_password(mnemonic: String, password: String) -> Result<(), BackendError> { + println!("Creating password"); + if does_password_file_exist()? { + return Err(BackendError::WalletFileAlreadyExists); + } + + let mnemonic = Mnemonic::from_str(&mnemonic)?; + let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap(); + let password = wallet_storage::UserPassword::new(password); + wallet_storage::store_wallet_login_information(mnemonic, hd_path, password)?; + Ok(()) +} + +#[tauri::command] +pub async fn sign_in_with_password( + password: String, + state: tauri::State<'_, Arc>>, +) -> Result { + println!("Signing in with password"); + let password = wallet_storage::UserPassword::new(password); + let stored_accounts = wallet_storage::load_existing_wallet_login_information(&password)?; + + // WIP: we are assuming just a single password is stored + let stored_account = stored_accounts.into_iter().next().unwrap(); + let mnemonic = match stored_account { + wallet_storage::account_data::StoredAccount::Mnemonic(ref mn) => mn.mnemonic().clone(), + }; + + _connect_with_mnemonic(mnemonic, state).await +} diff --git a/nym-wallet/src-tauri/src/wallet_storage/account_data.rs b/nym-wallet/src-tauri/src/wallet_storage/account_data.rs index 487b029eb0..f7ba738b93 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/account_data.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/account_data.rs @@ -34,7 +34,8 @@ pub(crate) struct MnemonicAccount { } // we only ever want to expose those getters in the test code -#[cfg(test)] +// WIP(JON): temporarily comment out +//#[cfg(test)] impl MnemonicAccount { pub(crate) fn mnemonic(&self) -> &bip39::Mnemonic { &self.mnemonic diff --git a/nym-wallet/src-tauri/src/wallet_storage/mod.rs b/nym-wallet/src-tauri/src/wallet_storage/mod.rs index de5c95c1e0..3d3b628046 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/mod.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/mod.rs @@ -1,15 +1,18 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +pub(crate) use crate::wallet_storage::password::UserPassword; + use crate::error::BackendError; use crate::wallet_storage::account_data::StoredAccount; use crate::wallet_storage::encryption::{encrypt_struct, EncryptedData}; -use crate::wallet_storage::password::UserPassword; use cosmrs::bip32::DerivationPath; use std::fs::{create_dir_all, OpenOptions}; use std::path::PathBuf; + pub(crate) mod account_data; pub(crate) mod encryption; + mod password; const STORAGE_DIR_NAME: &str = "NymWallet";