Skip to content

Commit

Permalink
Merge branch 'add-allow-registration-config' of https://github.com/Ko…
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlVS committed Sep 25, 2024
2 parents 051f6fd + f44f78b commit a241075
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
3 changes: 3 additions & 0 deletions mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ impl MmCtx {
#[cfg(not(target_arch = "wasm32"))]
pub fn is_https(&self) -> bool { self.conf["https"].as_bool().unwrap_or(false) }

/// Whether or not new wallets can be created.
pub fn allow_registrations(&self) -> bool { self.conf["allow_registrations"].as_bool().unwrap_or(true) }

/// SANs for self-signed certificate generation.
#[cfg(not(target_arch = "wasm32"))]
pub fn alt_names(&self) -> Result<Vec<String>, String> {
Expand Down
38 changes: 29 additions & 9 deletions mm2src/mm2_main/src/lp_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum WalletInitError {
fmt = "Passphrase doesn't match the one from file, please create a new wallet if you want to use a new passphrase"
)]
PassphraseMismatch,
#[display(fmt = "Error initializing wallet. Wallet mnemonic/name is unknown and wallet creation is disabled")]
WalletCreationNotAllowed,
#[display(fmt = "Error generating or decrypting mnemonic: {}", _0)]
MnemonicError(String),
#[display(fmt = "Error initializing crypto context: {}", _0)]
Expand Down Expand Up @@ -172,13 +174,17 @@ async fn retrieve_or_create_passphrase(
// If an existing passphrase is found, return it
Ok(Some(passphrase_from_file))
},
None => {
// If no passphrase is found, generate a new one
None if ctx.allow_registrations() => {
// If no passphrase is found and registrations are allowed, generate a new one
let new_passphrase = generate_mnemonic(ctx)?.to_string();
// Encrypt and save the new passphrase
encrypt_and_save_passphrase(ctx, wallet_name, &new_passphrase, wallet_password).await?;
Ok(Some(new_passphrase))
},
None => {
// If no passphrase is found and registrations are not allowed, return an error
Err(WalletInitError::WalletCreationNotAllowed.into())
},
}
}

Expand All @@ -194,11 +200,15 @@ async fn confirm_or_encrypt_and_store_passphrase(
// If an existing passphrase is found and it matches the provided passphrase, return it
Ok(Some(passphrase_from_file))
},
None => {
// If no passphrase is found in the file, encrypt and save the provided passphrase
None if ctx.allow_registrations() => {
// If no passphrase is found in the file and registrations are allowed, encrypt and save the provided passphrase
encrypt_and_save_passphrase(ctx, wallet_name, passphrase, wallet_password).await?;
Ok(Some(passphrase.to_string()))
},
None => {
// If no passphrase is found and registrations are not allowed, return an error
Err(WalletInitError::WalletCreationNotAllowed.into())
},
_ => {
// If an existing passphrase is found and it does not match the provided passphrase, return an error
Err(WalletInitError::PassphraseMismatch.into())
Expand All @@ -222,10 +232,14 @@ async fn decrypt_validate_or_save_passphrase(
Ok(Some(decrypted_passphrase))
},
None => {
save_encrypted_passphrase(ctx, wallet_name, &encrypted_passphrase_data)
.await
.mm_err(|e| WalletInitError::WalletsStorageError(e.to_string()))?;
Ok(Some(decrypted_passphrase))
if ctx.allow_registrations() {
save_encrypted_passphrase(ctx, wallet_name, &encrypted_passphrase_data)
.await
.mm_err(|e| WalletInitError::WalletsStorageError(e.to_string()))?;
return Ok(Some(decrypted_passphrase));
}
// If no passphrase is found and registrations are not allowed, return an error
Err(WalletInitError::WalletCreationNotAllowed.into())
},
_ => {
// If an existing passphrase is found and it does not match the decrypted passphrase, return an error
Expand Down Expand Up @@ -259,8 +273,14 @@ async fn process_passphrase_logic(
match (wallet_name, passphrase) {
(None, None) => Ok(None),
// Legacy approach for passphrase, no `wallet_name` is needed in the config, in this case the passphrase is not encrypted and saved.
(None, Some(Passphrase::Decrypted(passphrase))) => Ok(Some(passphrase)),
// Importing an encrypted passphrase without a wallet name is not supported since it's not possible to save the passphrase.
(None, Some(Passphrase::Decrypted(passphrase))) => {
if ctx.allow_registrations() {
Ok(Some(passphrase))
} else {
Err(WalletInitError::WalletCreationNotAllowed.into())
}
},
(None, Some(Passphrase::Encrypted(_))) => Err(WalletInitError::FieldNotFoundInConfig {
field: "wallet_name".to_owned(),
}
Expand Down
11 changes: 11 additions & 0 deletions mm2src/mm2_main/src/lp_wallet/mnemonics_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ pub(super) async fn save_encrypted_passphrase(
encrypted_passphrase_data: &EncryptedData,
) -> WalletsStorageResult<()> {
let wallet_path = ctx.wallet_file_path(wallet_name);

// Check if the wallet file already exists
if !wallet_path.exists() {
// If it doesn't exist and registrations are not allowed, return an error
if !ctx.allow_registrations() {
return Err(MmError::new(WalletsStorageError::Internal(
"Wallet creation is not allowed. Registrations are disabled.".to_string(),
)));
}
}

ensure_file_is_writable(&wallet_path).map_to_mm(WalletsStorageError::FsWriteError)?;
mm2_io::fs::write_json(encrypted_passphrase_data, &wallet_path, true)
.await
Expand Down
11 changes: 11 additions & 0 deletions mm2src/mm2_main/src/lp_wallet/mnemonics_wasm_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ pub(super) async fn save_encrypted_passphrase(
let transaction = db.transaction().await?;
let table = transaction.table::<MnemonicsTable>().await?;

// Check if the wallet already exists
let existing_wallet = table
.get_item_by_unique_index("wallet_name", wallet_name.to_string())
.await?;

if existing_wallet.is_none() && !ctx.allow_registrations() {
return Err(MmError::new(WalletsDBError::Internal(
"Wallet creation is not allowed. Registrations are disabled.".to_string(),
)));
}

let mnemonics_table_item = MnemonicsTable {
wallet_name: wallet_name.to_string(),
encrypted_mnemonic: serde_json::to_string(encrypted_passphrase_data).map_err(|e| {
Expand Down

0 comments on commit a241075

Please sign in to comment.