Skip to content

Commit 2c71fd6

Browse files
committed
fix(ui): add wallet alias trimming and 64-char length limit
1 parent 594c556 commit 2c71fd6

3 files changed

Lines changed: 86 additions & 14 deletions

File tree

src/ui/wallets/add_new_wallet_screen.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,36 @@ use dash_sdk::dpp::dashcore::Address;
2121
use eframe::egui::{Context, TextureHandle, TextureOptions};
2222
use eframe::emath::Align;
2323
use egui::load::SizedTexture;
24-
use egui::{ComboBox, Frame, Grid, Layout, Margin, RichText, Stroke, Ui, Vec2};
24+
use egui::{ComboBox, Frame, Grid, Layout, Margin, RichText, Stroke, TextEdit, Ui, Vec2};
2525
use std::sync::Arc;
2626
use zxcvbn::zxcvbn;
2727

28+
const WALLET_ALIAS_MAX_CHARS: usize = 64;
29+
const WALLET_ALIAS_COUNTER_SHOW_THRESHOLD: usize = 50;
30+
31+
fn wallet_alias_validation_error(alias_input: &str) -> Option<String> {
32+
if alias_input.is_empty() {
33+
return None;
34+
}
35+
36+
if alias_input.chars().count() > WALLET_ALIAS_MAX_CHARS {
37+
return Some(format!(
38+
"Wallet name must be {} characters or fewer.",
39+
WALLET_ALIAS_MAX_CHARS
40+
));
41+
}
42+
43+
let trimmed = alias_input.trim();
44+
if trimmed.is_empty() {
45+
return Some(
46+
"Wallet name cannot be only whitespace. Clear the field to use the default name."
47+
.to_string(),
48+
);
49+
}
50+
51+
None
52+
}
53+
2854
/// Word count options for BIP39 mnemonic seed phrases
2955
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3056
pub enum WordCount {
@@ -147,7 +173,12 @@ impl AddNewWalletScreen {
147173
};
148174

149175
// Generate default wallet name if none provided
150-
let wallet_alias = if self.alias_input.trim().is_empty() {
176+
if let Some(error) = wallet_alias_validation_error(&self.alias_input) {
177+
return Err(error);
178+
}
179+
180+
let trimmed_alias = self.alias_input.trim();
181+
let wallet_alias = if trimmed_alias.is_empty() {
151182
let existing_wallet_count = self
152183
.app_context
153184
.wallets
@@ -156,7 +187,7 @@ impl AddNewWalletScreen {
156187
.unwrap_or(0);
157188
format!("Wallet {}", existing_wallet_count + 1)
158189
} else {
159-
self.alias_input.clone()
190+
trimmed_alias.to_string()
160191
};
161192

162193
let mut wallet = Wallet::new_from_seed(
@@ -669,8 +700,34 @@ impl ScreenLike for AddNewWalletScreen {
669700

670701
ui.horizontal(|ui| {
671702
ui.label("Wallet Name:");
672-
ui.text_edit_singleline(&mut self.alias_input);
703+
ui.add(
704+
TextEdit::singleline(&mut self.alias_input)
705+
.char_limit(WALLET_ALIAS_MAX_CHARS),
706+
);
673707
});
708+
let alias_validation_error = wallet_alias_validation_error(&self.alias_input);
709+
ui.horizontal(|ui| {
710+
ui.label(
711+
RichText::new("Leave blank to use a default wallet name.")
712+
.weak()
713+
.size(12.0),
714+
);
715+
let raw_char_count = self.alias_input.chars().count();
716+
let displayed_char_count = self.alias_input.trim().chars().count();
717+
if raw_char_count > WALLET_ALIAS_COUNTER_SHOW_THRESHOLD {
718+
ui.label(
719+
RichText::new(format!(
720+
"{}/{}",
721+
displayed_char_count, WALLET_ALIAS_MAX_CHARS
722+
))
723+
.weak()
724+
.size(12.0),
725+
);
726+
}
727+
});
728+
if let Some(alias_validation_error) = alias_validation_error {
729+
ui.colored_label(DashColors::ERROR, alias_validation_error);
730+
}
674731

675732
ui.add_space(10.0);
676733
ui.separator();

src/ui/wallets/import_mnemonic_screen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl ImportMnemonicScreen {
152152
.unwrap_or(0);
153153
Some(format!("Key {}", existing_wallet_count + 1))
154154
} else {
155-
Some(self.alias_input.clone())
155+
Some(self.alias_input.trim().to_string())
156156
};
157157

158158
// Try WIF first, then hex
@@ -220,7 +220,7 @@ impl ImportMnemonicScreen {
220220
.unwrap_or(0);
221221
format!("Wallet {}", existing_wallet_count + 1)
222222
} else {
223-
self.alias_input.clone()
223+
self.alias_input.trim().to_string()
224224
};
225225

226226
let mut wallet = Wallet::new_from_seed(
@@ -627,7 +627,7 @@ impl ScreenLike for ImportMnemonicScreen {
627627

628628
ui.horizontal(|ui| {
629629
ui.label("Name:");
630-
ui.text_edit_singleline(&mut self.alias_input);
630+
ui.add(egui::TextEdit::singleline(&mut self.alias_input).char_limit(64));
631631
});
632632

633633
step += 1;

src/ui/wallets/wallets_screen/mod.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ enum RefreshMode {
8181
PlatformOnly,
8282
}
8383

84+
const WALLET_ALIAS_MAX_CHARS: usize = 64;
85+
8486
impl RefreshMode {
8587
fn label(&self) -> &'static str {
8688
match self {
@@ -2347,6 +2349,7 @@ impl ScreenLike for WalletsBalancesScreen {
23472349

23482350
let text_edit = egui::TextEdit::singleline(&mut self.rename_input)
23492351
.hint_text("Enter wallet name")
2352+
.char_limit(WALLET_ALIAS_MAX_CHARS)
23502353
.desired_width(250.0);
23512354
ui.add(text_edit);
23522355

@@ -2363,23 +2366,35 @@ impl ScreenLike for WalletsBalancesScreen {
23632366
ui.add_space(8.0);
23642367

23652368
if ComponentStyles::add_primary_button(ui, "Save").clicked() {
2366-
// Limit the alias length to 64 characters
2367-
if self.rename_input.len() > 64 {
2368-
self.rename_input.truncate(64);
2369+
let trimmed_alias = self.rename_input.trim();
2370+
if trimmed_alias.is_empty() {
2371+
self.display_message(
2372+
"Wallet name cannot be empty or whitespace.",
2373+
MessageType::Error,
2374+
);
2375+
return;
2376+
}
2377+
2378+
if trimmed_alias.chars().count() > WALLET_ALIAS_MAX_CHARS {
2379+
self.display_message(
2380+
"Wallet name must be 64 characters or fewer.",
2381+
MessageType::Error,
2382+
);
2383+
return;
23692384
}
23702385

23712386
// Handle HD wallet rename
23722387
if let Some(selected_wallet) = &self.selected_wallet {
23732388
let mut wallet = selected_wallet.write().unwrap();
2374-
wallet.alias = Some(self.rename_input.clone());
2389+
wallet.alias = Some(trimmed_alias.to_string());
23752390

23762391
// Update the alias in the database
23772392
let seed_hash = wallet.seed_hash();
23782393
self.app_context
23792394
.db
23802395
.set_wallet_alias(
23812396
&seed_hash,
2382-
Some(self.rename_input.clone()),
2397+
Some(trimmed_alias.to_string()),
23832398
)
23842399
.ok();
23852400
}
@@ -2388,15 +2403,15 @@ impl ScreenLike for WalletsBalancesScreen {
23882403
&self.selected_single_key_wallet
23892404
{
23902405
let mut wallet = selected_sk_wallet.write().unwrap();
2391-
wallet.alias = Some(self.rename_input.clone());
2406+
wallet.alias = Some(trimmed_alias.to_string());
23922407

23932408
// Update the alias in the database
23942409
let key_hash = wallet.key_hash;
23952410
self.app_context
23962411
.db
23972412
.update_single_key_wallet_alias(
23982413
&key_hash,
2399-
Some(&self.rename_input),
2414+
Some(trimmed_alias),
24002415
)
24012416
.ok();
24022417
}

0 commit comments

Comments
 (0)