Skip to content

Commit

Permalink
fix wasm and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Jan 10, 2025
1 parent 5f7830e commit cfd85b8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 56 deletions.
71 changes: 44 additions & 27 deletions mm2src/mm2_main/src/lp_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use serde::de::DeserializeOwned;
use serde_json::{self as json, Value as Json};

use self::mnemonics_storage::update_seed_storage_password;
use enum_derives::EnumFromStringify;

cfg_wasm32! {
use crate::lp_wallet::mnemonics_wasm_db::{WalletsDb, WalletsDBError};
Expand Down Expand Up @@ -415,44 +414,45 @@ pub struct GetMnemonicResponse {
pub mnemonic: MnemonicForRpc,
}

#[derive(Debug, Display, Serialize, SerializeErrorType)]
#[derive(Debug, Display, Serialize, SerializeErrorType, EnumFromStringify)]
#[serde(tag = "error_type", content = "error_data")]
pub enum WalletStorageRpcError {
pub enum WalletsStorageRpcError {
#[display(fmt = "Invalid request error: {}", _0)]
InvalidRequest(String),
#[display(fmt = "Wallets storage error: {}", _0)]
WalletsStorageError(String),
#[display(fmt = "Internal error: {}", _0)]
Internal(String),
#[display(fmt = "Invalid password error: {}", _0)]
#[from_stringify("MnemonicError")]
InvalidPassword(String),
}

impl HttpStatusCode for WalletStorageRpcError {
impl HttpStatusCode for WalletsStorageRpcError {
fn status_code(&self) -> StatusCode {
match self {
WalletStorageRpcError::InvalidRequest(_) | WalletStorageRpcError::InvalidPassword(_) => {
WalletsStorageRpcError::InvalidRequest(_) | WalletsStorageRpcError::InvalidPassword(_) => {
StatusCode::BAD_REQUEST
},
WalletStorageRpcError::WalletsStorageError(_) | WalletStorageRpcError::Internal(_) => {
WalletsStorageRpcError::WalletsStorageError(_) | WalletsStorageRpcError::Internal(_) => {
StatusCode::INTERNAL_SERVER_ERROR
},
}
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<WalletsStorageError> for WalletStorageRpcError {
fn from(e: WalletsStorageError) -> Self { WalletStorageRpcError::WalletsStorageError(e.to_string()) }
impl From<WalletsStorageError> for WalletsStorageRpcError {
fn from(e: WalletsStorageError) -> Self { WalletsStorageRpcError::WalletsStorageError(e.to_string()) }
}

#[cfg(target_arch = "wasm32")]
impl From<WalletsDBError> for WalletStorageRpcError {
fn from(e: WalletsDBError) -> Self { WalletStorageRpcError::WalletsStorageError(e.to_string()) }
impl From<WalletsDBError> for WalletsStorageRpcError {
fn from(e: WalletsDBError) -> Self { WalletsStorageRpcError::WalletsStorageError(e.to_string()) }
}

impl From<ReadPassphraseError> for WalletStorageRpcError {
fn from(e: ReadPassphraseError) -> Self { WalletStorageRpcError::WalletsStorageError(e.to_string()) }
impl From<ReadPassphraseError> for WalletsStorageRpcError {
fn from(e: ReadPassphraseError) -> Self { WalletsStorageRpcError::WalletsStorageError(e.to_string()) }
}

/// Retrieves the wallet mnemonic in the requested format.
Expand All @@ -462,7 +462,7 @@ impl From<ReadPassphraseError> for WalletStorageRpcError {
/// A `Result` type containing:
///
/// * [`Ok`]([`GetMnemonicResponse`]) - The wallet mnemonic in the requested format.
/// * [`MmError`]<[`WalletStorageRpcError>`]> - Returns specific [`GetMnemonicError`] variants for different failure scenarios.
/// * [`MmError`]<[`WalletsStorageRpcError>`]> - Returns specific [`GetMnemonicError`] variants for different failure scenarios.
///
/// # Errors
///
Expand All @@ -489,20 +489,20 @@ impl From<ReadPassphraseError> for WalletStorageRpcError {
pub async fn get_mnemonic_rpc(
ctx: MmArc,
req: GetMnemonicRequest,
) -> MmResult<GetMnemonicResponse, WalletStorageRpcError> {
) -> MmResult<GetMnemonicResponse, WalletsStorageRpcError> {
match req.mnemonic_format {
MnemonicFormat::Encrypted => {
let encrypted_mnemonic = read_encrypted_passphrase_if_available(&ctx)
.await?
.ok_or_else(|| WalletStorageRpcError::InvalidRequest("Wallet mnemonic file not found".to_string()))?;
.ok_or_else(|| WalletsStorageRpcError::InvalidRequest("Wallet mnemonic file not found".to_string()))?;
Ok(GetMnemonicResponse {
mnemonic: encrypted_mnemonic.into(),
})
},
MnemonicFormat::PlainText(wallet_password) => {
let plaintext_mnemonic = read_and_decrypt_passphrase_if_available(&ctx, &wallet_password)
.await?
.ok_or_else(|| WalletStorageRpcError::InvalidRequest("Wallet mnemonic file not found".to_string()))?;
.ok_or_else(|| WalletsStorageRpcError::InvalidRequest("Wallet mnemonic file not found".to_string()))?;
Ok(GetMnemonicResponse {
mnemonic: plaintext_mnemonic.into(),
})
Expand All @@ -518,12 +518,12 @@ pub struct GetWalletNamesResponse {
}

/// Retrieves all created wallets and the currently activated wallet.
pub async fn get_wallet_names_rpc(ctx: MmArc, _req: Json) -> MmResult<GetWalletNamesResponse, WalletStorageRpcError> {
pub async fn get_wallet_names_rpc(ctx: MmArc, _req: Json) -> MmResult<GetWalletNamesResponse, WalletsStorageRpcError> {
// We want to return wallet names in the same order for both native and wasm32 targets.
let wallets = read_all_wallet_names(&ctx).await?.sorted().collect();
// Note: `ok_or` is used here on `Constructible<Option<String>>` to handle the case where the wallet name is not set.
// `wallet_name` can be `None` in the case of no-login mode.
let activated_wallet = ctx.wallet_name.get().ok_or(WalletStorageRpcError::Internal(
let activated_wallet = ctx.wallet_name.get().ok_or(WalletsStorageRpcError::Internal(
"`wallet_name` not initialized yet!".to_string(),
))?;

Expand All @@ -550,7 +550,7 @@ pub struct SeedPasswordUpdateRequest {
#[derive(Serialize)]
pub struct SeedPasswordUpdateResponse {
/// `true` if the password update was successful, `false` otherwise.
result: bool,
successful: bool,
}

/// RPC function to handle a request for updating the seed storage password.
Expand All @@ -566,15 +566,32 @@ pub struct SeedPasswordUpdateResponse {
/// new_password: "new_password".to_string(),
/// };
/// let response = update_seed_storage_password_rpc(ctx, request).await?;
/// assert!(response.result);
/// assert!(response.successful);
/// ```
pub async fn update_seed_storage_password_rpc(
ctx: MmArc,
req: SeedPasswordUpdateRequest,
) -> MmResult<SeedPasswordUpdateResponse, WalletStorageRpcError> {
Ok(
update_seed_storage_password(&ctx, &req.current_password, &req.new_password)
.await
.map(|_| SeedPasswordUpdateResponse { result: true })?,
)
) -> MmResult<SeedPasswordUpdateResponse, WalletsStorageRpcError> {
let wallet_name = ctx
.wallet_name
.get()
.ok_or(WalletsStorageRpcError::Internal(
"`wallet_name` not initialized yet!".to_string(),
))?
.clone()
.ok_or_else(|| WalletsStorageRpcError::Internal("`wallet_name` cannot be None!".to_string()))?;
// read mnemonic for a wallet_name using current user's password.
let current_password = req.current_password;
let new_password = req.new_password;
let mnemonic = read_and_decrypt_passphrase_if_available(&ctx, &current_password)
.await?
.ok_or(MmError::new(WalletsStorageRpcError::Internal(format!(
"{wallet_name}: wallet mnemonic file not found"
))))?;
// encrypt mnemonic with new passphrase.
let encrypted_data = encrypt_mnemonic(&mnemonic, &new_password)?;
// save new encrypted mnemonic data with new password
save_encrypted_passphrase(&ctx, &wallet_name, &encrypted_data).await?;

Ok(SeedPasswordUpdateResponse { successful: true })
}
31 changes: 2 additions & 29 deletions mm2src/mm2_main/src/lp_wallet/mnemonics_storage.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crypto::{encrypt_mnemonic, EncryptedData, MnemonicError};
use crypto::{EncryptedData, MnemonicError};
use enum_derives::EnumFromStringify;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use mm2_io::fs::{ensure_file_is_writable, list_files_by_extension};

use super::{read_and_decrypt_passphrase_if_available, ReadPassphraseError};
use super::{ReadPassphraseError};

type WalletsStorageResult<T> = Result<T, MmError<WalletsStorageError>>;

Expand Down Expand Up @@ -79,30 +79,3 @@ pub(super) async fn read_all_wallet_names(ctx: &MmArc) -> WalletsStorageResult<i
Ok(wallet_names)
}

/// Update the password to a file associated with the given wallet name.
pub async fn update_seed_storage_password(
ctx: &MmArc,
current_password: &str,
new_password: &str,
) -> WalletsStorageResult<()> {
let wallet_name = ctx
.wallet_name
.get()
.ok_or(WalletsStorageError::Internal(
"`wallet_name` not initialized yet!".to_string(),
))?
.clone()
.ok_or_else(|| WalletsStorageError::Internal("`wallet_name` cannot be None!".to_string()))?;
// read mnemonic for a wallet_name using current user's password.
let mnemonic = read_and_decrypt_passphrase_if_available(ctx, current_password)
.await?
.ok_or(MmError::new(WalletsStorageError::Internal(format!(
"{wallet_name}: wallet mnemonic file not found"
))))?;
// encrypt mnemonic with new passphrase.
let encrypted_data = encrypt_mnemonic(&mnemonic, new_password)?;
// save new encrypted mnemonic data with new password
save_encrypted_passphrase(ctx, &wallet_name, &encrypted_data).await?;

Ok(())
}

0 comments on commit cfd85b8

Please sign in to comment.