wallet: expose functions to support multiple accounts
This commit is contained in:
@@ -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,29 @@ 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
|
||||
}
|
||||
|
||||
#[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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user