mirror of
https://github.com/jwinterm/grim.git
synced 2026-11-11 08:13:56 +01:00
build + wallet + ui: fix dependencies connection setup, mnemonic validation, wallet creation, methods to work with wallet, update translations
This commit is contained in:
@@ -168,7 +168,7 @@ impl View {
|
||||
let size = egui::Vec2::splat(2.0 * r + 5.0);
|
||||
let (rect, br) = ui.allocate_at_least(size, Sense::click_and_drag());
|
||||
|
||||
let mut icon_size = 22.0;
|
||||
let mut icon_size = 24.0;
|
||||
let mut icon_color = Colors::TEXT_BUTTON;
|
||||
|
||||
// Increase radius and change icon size and color on-hover.
|
||||
|
||||
@@ -23,6 +23,7 @@ use crate::gui::views::{Modal, ModalPosition, View};
|
||||
use crate::gui::views::wallets::creation::MnemonicSetup;
|
||||
use crate::gui::views::wallets::creation::types::{PhraseMode, Step};
|
||||
use crate::gui::views::wallets::setup::ConnectionSetup;
|
||||
use crate::wallet::WalletList;
|
||||
|
||||
/// Wallet creation content.
|
||||
pub struct WalletCreation {
|
||||
@@ -70,11 +71,12 @@ impl WalletCreation {
|
||||
pub const NAME_PASS_MODAL: &'static str = "name_pass_modal";
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
||||
// Show wallet creation step description and confirmation bottom panel.
|
||||
// Show wallet creation step description and confirmation panel.
|
||||
if self.step.is_some() {
|
||||
egui::TopBottomPanel::bottom("wallet_creation_step_panel")
|
||||
.frame(egui::Frame {
|
||||
stroke: View::DEFAULT_STROKE,
|
||||
fill: Colors::FILL_DARK,
|
||||
inner_margin: Margin {
|
||||
left: View::far_left_inset_margin(ui) + 4.0,
|
||||
right: View::get_right_inset() + 4.0,
|
||||
@@ -85,65 +87,15 @@ impl WalletCreation {
|
||||
})
|
||||
.show_inside(ui, |ui| {
|
||||
ui.vertical_centered(|ui| {
|
||||
if let Some(step) = &self.step {
|
||||
// Setup step description text and availability.
|
||||
let (step_text, step_available) = match step {
|
||||
Step::EnterMnemonic => {
|
||||
let mode = &self.mnemonic_setup.mnemonic.mode;
|
||||
let text = if mode == &PhraseMode::Generate {
|
||||
t!("wallets.create_phrase_desc")
|
||||
} else {
|
||||
t!("wallets.restore_phrase_desc")
|
||||
};
|
||||
let available = !self
|
||||
.mnemonic_setup
|
||||
.mnemonic
|
||||
.words
|
||||
.contains(&String::from(""));
|
||||
(text, available)
|
||||
}
|
||||
Step::ConfirmMnemonic => {
|
||||
let text = t!("wallets.restore_phrase_desc");
|
||||
let available = !self
|
||||
.mnemonic_setup
|
||||
.mnemonic
|
||||
.confirm_words
|
||||
.contains(&String::from(""));
|
||||
(text, available)
|
||||
},
|
||||
Step::SetupConnection => (t!("wallets.setup_conn_desc"), true)
|
||||
};
|
||||
// Show step description.
|
||||
ui.label(RichText::new(step_text).size(16.0).color(Colors::GRAY));
|
||||
|
||||
// Show next step button if there are no empty words.
|
||||
|
||||
if step_available {
|
||||
// Setup button text.
|
||||
let (next_text, color) = if step == &Step::SetupConnection {
|
||||
(format!("{} {}", CHECK, t!("complete")), Colors::GOLD)
|
||||
} else {
|
||||
let text = format!("{} {}", SHARE_FAT, t!("continue"));
|
||||
(text, Colors::WHITE)
|
||||
};
|
||||
|
||||
ui.add_space(4.0);
|
||||
// Show button.
|
||||
View::button(ui, next_text.to_uppercase(), color, || {
|
||||
self.forward();
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
}
|
||||
}
|
||||
self.step_control_ui(ui);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Show wallet creation step content.
|
||||
// Show wallet creation step content panel.
|
||||
egui::CentralPanel::default()
|
||||
.frame(egui::Frame {
|
||||
stroke: View::DEFAULT_STROKE,
|
||||
fill: if self.step.is_none() { Colors::FILL_DARK } else { Colors::WHITE },
|
||||
inner_margin: Margin {
|
||||
left: View::far_left_inset_margin(ui) + 4.0,
|
||||
right: View::get_right_inset() + 4.0,
|
||||
@@ -153,12 +105,74 @@ impl WalletCreation {
|
||||
..Default::default()
|
||||
})
|
||||
.show_inside(ui, |ui| {
|
||||
self.step_ui(ui, cb);
|
||||
self.step_content_ui(ui, cb);
|
||||
});
|
||||
}
|
||||
|
||||
/// Draw [`Step`] description and confirmation control.
|
||||
fn step_control_ui(&mut self, ui: &mut egui::Ui) {
|
||||
if let Some(step) = &self.step {
|
||||
// Setup step description text and availability.
|
||||
let (step_text, mut step_available) = match step {
|
||||
Step::EnterMnemonic => {
|
||||
let mode = &self.mnemonic_setup.mnemonic.mode;
|
||||
let text = if mode == &PhraseMode::Generate {
|
||||
t!("wallets.create_phrase_desc")
|
||||
} else {
|
||||
t!("wallets.restore_phrase_desc")
|
||||
};
|
||||
let available = !self
|
||||
.mnemonic_setup
|
||||
.mnemonic
|
||||
.words
|
||||
.contains(&String::from(""));
|
||||
(text, available)
|
||||
}
|
||||
Step::ConfirmMnemonic => {
|
||||
let text = t!("wallets.restore_phrase_desc");
|
||||
let available = !self
|
||||
.mnemonic_setup
|
||||
.mnemonic
|
||||
.confirm_words
|
||||
.contains(&String::from(""));
|
||||
(text, available)
|
||||
},
|
||||
Step::SetupConnection => (t!("wallets.setup_conn_desc"), true)
|
||||
};
|
||||
// Show step description.
|
||||
ui.label(RichText::new(step_text).size(16.0).color(Colors::GRAY));
|
||||
|
||||
// Show error if entered phrase is not valid.
|
||||
if !self.mnemonic_setup.valid_phrase {
|
||||
step_available = false;
|
||||
ui.label(RichText::new(t!("wallets.not_valid_phrase"))
|
||||
.size(16.0)
|
||||
.color(Colors::RED));
|
||||
ui.add_space(2.0);
|
||||
}
|
||||
|
||||
// Show next step button if there are no empty words.
|
||||
if step_available {
|
||||
// Setup button text.
|
||||
let (next_text, color) = if step == &Step::SetupConnection {
|
||||
(format!("{} {}", CHECK, t!("complete")), Colors::GOLD)
|
||||
} else {
|
||||
let text = format!("{} {}", SHARE_FAT, t!("continue"));
|
||||
(text, Colors::WHITE)
|
||||
};
|
||||
|
||||
ui.add_space(4.0);
|
||||
// Show button.
|
||||
View::button(ui, next_text.to_uppercase(), color, || {
|
||||
self.forward();
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw wallet creation [`Step`] content.
|
||||
fn step_ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
||||
fn step_content_ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
||||
match &self.step {
|
||||
None => {
|
||||
// Show wallet creation message if step is empty.
|
||||
@@ -233,12 +247,23 @@ impl WalletCreation {
|
||||
if self.mnemonic_setup.mnemonic.mode == PhraseMode::Generate {
|
||||
Some(Step::ConfirmMnemonic)
|
||||
} else {
|
||||
Some(Step::SetupConnection)
|
||||
// Check if entered phrase was valid.
|
||||
if self.mnemonic_setup.valid_phrase {
|
||||
Some(Step::SetupConnection)
|
||||
} else {
|
||||
Some(Step::EnterMnemonic)
|
||||
}
|
||||
}
|
||||
}
|
||||
Step::ConfirmMnemonic => Some(Step::SetupConnection),
|
||||
Step::SetupConnection => {
|
||||
//TODO: Confirm mnemonic
|
||||
// Create wallet at last step.
|
||||
WalletList::create_wallet(
|
||||
self.name_edit.clone(),
|
||||
self.pass_edit.clone(),
|
||||
self.mnemonic_setup.mnemonic.get_phrase(),
|
||||
self.network_setup.get_ext_conn_url()
|
||||
).unwrap();
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -249,7 +274,7 @@ impl WalletCreation {
|
||||
/// Start wallet creation from showing [`Modal`] to enter name and password.
|
||||
pub fn show_name_pass_modal(&mut self) {
|
||||
// Reset modal values.
|
||||
self.hide_pass = false;
|
||||
self.hide_pass = true;
|
||||
self.modal_just_opened = true;
|
||||
self.name_edit = String::from("");
|
||||
self.pass_edit = String::from("");
|
||||
|
||||
@@ -25,6 +25,9 @@ pub struct MnemonicSetup {
|
||||
/// Current mnemonic phrase.
|
||||
pub(crate) mnemonic: Mnemonic,
|
||||
|
||||
/// Flag to check if entered phrase was valid.
|
||||
pub(crate) valid_phrase: bool,
|
||||
|
||||
/// Current word number to edit at [`Modal`].
|
||||
word_num_edit: usize,
|
||||
/// Entered word value for [`Modal`].
|
||||
@@ -37,6 +40,7 @@ impl Default for MnemonicSetup {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mnemonic: Mnemonic::default(),
|
||||
valid_phrase: true,
|
||||
word_num_edit: 0,
|
||||
word_edit: String::from(""),
|
||||
valid_word_edit: true
|
||||
@@ -290,6 +294,10 @@ impl MnemonicSetup {
|
||||
let close_modal = words.len() == self.word_num_edit
|
||||
|| !words.get(self.word_num_edit).unwrap().is_empty();
|
||||
if close_modal {
|
||||
// Check if entered phrase was valid when all words were entered.
|
||||
if !self.mnemonic.words.contains(&String::from("")) {
|
||||
self.valid_phrase = self.mnemonic.is_valid_phrase();
|
||||
}
|
||||
cb.hide_keyboard();
|
||||
modal.close();
|
||||
} else {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use grin_keychain::mnemonic::{from_entropy, search};
|
||||
use grin_keychain::mnemonic::{from_entropy, search, to_entropy};
|
||||
use rand::{Rng, thread_rng};
|
||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
||||
|
||||
@@ -124,6 +124,16 @@ impl Mnemonic {
|
||||
valid && equal
|
||||
}
|
||||
|
||||
/// Check if current phrase is valid.
|
||||
pub fn is_valid_phrase(&self) -> bool {
|
||||
to_entropy(self.get_phrase().as_str()).is_ok()
|
||||
}
|
||||
|
||||
/// Get phrase from words.
|
||||
pub fn get_phrase(&self) -> String {
|
||||
self.words.iter().map(|x| x.to_string() + " ").collect::<String>()
|
||||
}
|
||||
|
||||
/// Generate list of words based on provided [`PhraseMode`] and [`PhraseSize`].
|
||||
fn generate_words(mode: &PhraseMode, size: &PhraseSize) -> Vec<String> {
|
||||
match mode {
|
||||
|
||||
@@ -52,6 +52,14 @@ impl ConnectionSetup {
|
||||
// Self { method: ConnectionMethod::Integrated }
|
||||
// }
|
||||
|
||||
/// Get external node connection URL.
|
||||
pub fn get_ext_conn_url(&self) -> Option<String> {
|
||||
match &self.method {
|
||||
ConnectionMethod::Integrated => None,
|
||||
ConnectionMethod::External(url) => Some(url.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
||||
ScrollArea::vertical()
|
||||
.id_source("wallet_connection_setup")
|
||||
@@ -99,11 +107,6 @@ impl ConnectionSetup {
|
||||
});
|
||||
}
|
||||
|
||||
/// Draw external connections setup.
|
||||
fn external_conn_ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
||||
|
||||
}
|
||||
|
||||
/// Draw modal content.
|
||||
pub fn modal_ui(&mut self, ui: &mut egui::Ui, modal: &Modal, cb: &dyn PlatformCallbacks) {
|
||||
ui.add_space(6.0);
|
||||
|
||||
@@ -23,13 +23,10 @@ use crate::gui::views::{Modal, ModalContainer, Root, TitlePanel, TitleType, View
|
||||
use crate::gui::views::wallets::creation::{MnemonicSetup, WalletCreation};
|
||||
use crate::gui::views::wallets::setup::ConnectionSetup;
|
||||
use crate::gui::views::wallets::wallet::WalletContent;
|
||||
use crate::wallet::{Wallet, WalletList};
|
||||
use crate::wallet::WalletList;
|
||||
|
||||
/// Wallets content.
|
||||
pub struct WalletsContent {
|
||||
/// List of wallets.
|
||||
list: Vec<Wallet>,
|
||||
|
||||
/// Selected list item content.
|
||||
item_content: Option<WalletContent>,
|
||||
/// Wallet creation content.
|
||||
@@ -42,7 +39,6 @@ pub struct WalletsContent {
|
||||
impl Default for WalletsContent {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
list: WalletList::list(),
|
||||
item_content: None,
|
||||
creation_content: WalletCreation::default(),
|
||||
modal_ids: vec![
|
||||
@@ -83,14 +79,22 @@ impl WalletsContent {
|
||||
// Show title panel.
|
||||
self.title_ui(ui, frame);
|
||||
|
||||
let is_wallet_panel_open = Self::is_dual_panel_mode(ui, frame) || self.list.is_empty();
|
||||
let wallets = WalletList::list();
|
||||
|
||||
let is_dual_panel = Self::is_dual_panel_mode(ui, frame);
|
||||
let is_wallet_creation = self.creation_content.can_go_back();
|
||||
let is_wallet_panel_open = is_dual_panel || is_wallet_creation || wallets.is_empty();
|
||||
let wallet_panel_width = self.wallet_panel_width(ui, frame);
|
||||
// Show wallet content.
|
||||
egui::SidePanel::right("wallet_panel")
|
||||
.resizable(false)
|
||||
.min_width(wallet_panel_width)
|
||||
.frame(egui::Frame {
|
||||
fill: if self.list.is_empty() { Colors::FILL_DARK } else { Colors::WHITE },
|
||||
fill: if !wallets.is_empty() || is_wallet_creation {
|
||||
Colors::WHITE
|
||||
} else {
|
||||
Colors::FILL_DARK
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
.show_animated_inside(ui, is_wallet_panel_open, |ui| {
|
||||
@@ -98,7 +102,7 @@ impl WalletsContent {
|
||||
});
|
||||
|
||||
// Show list of wallets.
|
||||
if !self.list.is_empty() {
|
||||
if !is_wallet_creation && !wallets.is_empty() {
|
||||
egui::CentralPanel::default()
|
||||
.frame(egui::Frame {
|
||||
stroke: View::DEFAULT_STROKE,
|
||||
@@ -113,6 +117,12 @@ impl WalletsContent {
|
||||
})
|
||||
.show_inside(ui, |ui| {
|
||||
//TODO: wallets list
|
||||
for w in WalletList::list() {
|
||||
ui.label(w.config.get_name());
|
||||
View::button(ui, "get info".to_string(), Colors::GOLD, || {
|
||||
println!("12345 amount {}", w.get_txs_info(10).unwrap().2.total);
|
||||
});
|
||||
}
|
||||
});
|
||||
// Show wallet creation button if wallet panel is not open.
|
||||
if !is_wallet_panel_open {
|
||||
@@ -154,7 +164,7 @@ impl WalletsContent {
|
||||
ui: &mut egui::Ui,
|
||||
frame: &mut eframe::Frame,
|
||||
cb: &dyn PlatformCallbacks) {
|
||||
if self.list.is_empty() || self.item_content.is_none() {
|
||||
if WalletList::list().is_empty() || self.item_content.is_none() {
|
||||
self.creation_content.ui(ui, cb)
|
||||
} else {
|
||||
self.item_content.as_mut().unwrap().ui(ui, frame, cb);
|
||||
@@ -163,18 +173,19 @@ impl WalletsContent {
|
||||
|
||||
/// Get [`WalletContent`] panel width.
|
||||
fn wallet_panel_width(&self, ui: &mut egui::Ui, frame: &mut eframe::Frame) -> f32 {
|
||||
let is_wallet_creation = self.creation_content.can_go_back();
|
||||
let available_width = if WalletList::list().is_empty() || is_wallet_creation {
|
||||
ui.available_width()
|
||||
} else {
|
||||
ui.available_width() - Root::SIDE_PANEL_MIN_WIDTH
|
||||
};
|
||||
if Self::is_dual_panel_mode(ui, frame) {
|
||||
let min_width = (Root::SIDE_PANEL_MIN_WIDTH + View::get_right_inset()) as i64;
|
||||
let available_width = if self.list.is_empty() {
|
||||
ui.available_width()
|
||||
} else {
|
||||
ui.available_width() - Root::SIDE_PANEL_MIN_WIDTH
|
||||
} as i64;
|
||||
max(min_width, available_width) as f32
|
||||
max(min_width, available_width as i64) as f32
|
||||
} else {
|
||||
let dual_panel_root = Root::is_dual_panel_mode(frame);
|
||||
if dual_panel_root {
|
||||
ui.available_width()
|
||||
available_width
|
||||
} else {
|
||||
frame.info().window_info.size.x
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user