Compare commits

...

4 Commits

Author SHA1 Message Date
Jon Häggblad 1929530825 wallet: add comment about function being deprecated 2022-04-06 17:01:32 +02:00
Jon Häggblad ec355dd236 wallet: add try_decrypt_all 2022-04-06 17:01:05 +02:00
Jon Häggblad 1657dd5b57 wallet: add From impl for UserPassword 2022-04-06 17:00:47 +02:00
Jon Häggblad 05632e1c73 wallet: expose functions to support multiple accounts 2022-04-06 16:59:12 +02:00
5 changed files with 71 additions and 2 deletions
+4
View File
@@ -38,14 +38,18 @@ fn main() {
mixnet::account::validate_mnemonic,
mixnet::account::create_new_account,
mixnet::account::create_new_mnemonic,
mixnet::account::does_account_id_exist,
mixnet::account::create_password,
mixnet::account::create_password_for_id,
mixnet::account::does_password_file_exist,
mixnet::account::get_balance,
mixnet::account::get_validator_nymd_urls,
mixnet::account::get_validator_api_urls,
mixnet::account::logout,
mixnet::account::remove_password,
mixnet::account::remove_password_for_id,
mixnet::account::sign_in_with_password,
mixnet::account::sign_in_with_password_and_id,
mixnet::account::switch_network,
mixnet::account::update_validator_urls,
mixnet::account::validate_mnemonic,
@@ -393,6 +393,14 @@ pub fn does_password_file_exist() -> Result<bool, BackendError> {
}
}
#[tauri::command]
pub fn does_account_id_exist(id: String) -> Result<bool, BackendError> {
let wallet = wallet_storage::load_existing_wallet()?;
let id = wallet_storage::WalletAccountId::new(id);
Ok(wallet.account_id_exists(&id))
}
// Will be deprecated once we support multiple accounts.
#[tauri::command]
pub fn create_password(mnemonic: String, password: String) -> Result<(), BackendError> {
if does_password_file_exist()? {
@@ -408,6 +416,21 @@ pub fn create_password(mnemonic: String, password: String) -> Result<(), Backend
wallet_storage::store_wallet_login_information(mnemonic, hd_path, id, &password)
}
#[tauri::command]
pub fn create_password_for_id(
id: String,
mnemonic: String,
password: String,
) -> Result<(), BackendError> {
log::info!("Creating password for: {id}");
let mnemonic = Mnemonic::from_str(&mnemonic)?;
let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let id = wallet_storage::WalletAccountId::new(id);
let password = wallet_storage::UserPassword::new(password);
wallet_storage::store_wallet_login_information(mnemonic, hd_path, id, &password)
}
// Deprecated as soon as we support multiple accounts
#[tauri::command]
pub async fn sign_in_with_password(
password: String,
@@ -422,9 +445,30 @@ pub async fn sign_in_with_password(
_connect_with_mnemonic(stored_account.mnemonic().clone(), state).await
}
#[tauri::command]
pub async fn sign_in_with_password_and_id(
id: String,
password: String,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Account, BackendError> {
log::info!("Signing in with: {id}");
let id = wallet_storage::WalletAccountId::new(id);
let password = wallet_storage::UserPassword::new(password);
let stored_account = wallet_storage::load_existing_wallet_login_information(&id, &password)?;
_connect_with_mnemonic(stored_account.mnemonic().clone(), state).await
}
// Deprecated as soon as we support multiple accounts
#[tauri::command]
pub fn remove_password() -> Result<(), BackendError> {
log::info!("Removing password");
let id = wallet_storage::WalletAccountId::new(DEFAULT_WALLET_ACCOUNT_ID.to_string());
wallet_storage::remove_wallet_login_information(&id)
}
#[tauri::command]
pub fn remove_password_for_id(id: String) -> Result<(), BackendError> {
log::info!("Removing password");
let id = wallet_storage::WalletAccountId::new(id);
wallet_storage::remove_wallet_login_information(&id)
}
@@ -44,6 +44,14 @@ impl StoredWallet {
}
}
pub fn account_id_exists(&self, id: &WalletAccountId) -> bool {
self
.accounts
.iter()
.find(|account| &account.id == id)
.is_some()
}
#[allow(unused)]
pub fn encrypted_account_by_index(&self, index: usize) -> Option<&EncryptedAccount> {
self.accounts.get(index)
@@ -91,6 +99,14 @@ impl StoredWallet {
pub fn password_can_decrypt_all(&self, password: &UserPassword) -> bool {
self.decrypt_all(password).is_ok()
}
pub fn try_decrypt_all(&self, password: &UserPassword) -> Vec<StoredAccount> {
self
.accounts
.iter()
.filter_map(|account| account.account.decrypt_struct(password).ok())
.collect()
}
}
impl Default for StoredWallet {
@@ -30,8 +30,7 @@ pub(crate) fn wallet_login_filepath() -> Result<PathBuf, BackendError> {
get_storage_directory().map(|dir| dir.join(WALLET_INFO_FILENAME))
}
#[allow(unused)]
pub(crate) fn load_existing_wallet(password: &UserPassword) -> Result<StoredWallet, BackendError> {
pub(crate) fn load_existing_wallet() -> Result<StoredWallet, BackendError> {
let store_dir = get_storage_directory()?;
let filepath = store_dir.join(WALLET_INFO_FILENAME);
load_existing_wallet_at_file(filepath)
@@ -47,3 +47,9 @@ impl AsRef<str> for UserPassword {
self.0.as_ref()
}
}
impl From<String> for UserPassword {
fn from(username: String) -> Self {
Self::new(username)
}
}