ui + wallet: wallet list refactoring, wallet opening, round button fixes, update translations

This commit is contained in:
ardocrat 2023-07-29 00:17:54 +03:00
parent 2e12b17663
commit 3f0d8facac
12 changed files with 486 additions and 296 deletions

View file

@ -8,8 +8,7 @@ wallets:
add: Add wallet
name: 'Name:'
pass: 'Password:'
name_empty: Enter name of wallet
pass_empty: Enter password for wallet
pass_empty: Enter password from the wallet
create: Create
recover: Restore
saved_phrase: Saved phrase
@ -24,6 +23,8 @@ wallets:
ext_conn: 'External connections:'
add_node_url: Add node URL
invalid_url: Entered URL is invalid
open: Open the wallet
wrong_pass: Entered password is wrong
network:
self: Network
node: Integrated node

View file

@ -8,8 +8,7 @@ wallets:
add: Добавить кошелёк
name: 'Название:'
pass: 'Пароль:'
name_empty: Введите название кошелька
pass_empty: Введите пароль для кошелька
pass_empty: Введите пароль от кошелька
create: Создать
recover: Восстановить
saved_phrase: Сохранённая фраза
@ -24,6 +23,8 @@ wallets:
ext_conn: 'Внешние подключения:'
add_node_url: Добавить URL узла
invalid_url: Введенный URL-адрес недействителен
open: Открыть кошелёк
wrong_pass: Введён неправильный пароль
network:
self: Сеть
node: Встроенный узел

View file

@ -353,7 +353,7 @@ impl P2PSetup {
format!("{} {}", PLUS_CIRCLE, t!("network_settings.add_peer"))
};
View::button(ui, add_text, Colors::GOLD, || {
View::button(ui, add_text, Colors::BUTTON, || {
// Setup values for modal.
self.peer_edit = "".to_string();
// Select modal id.

View file

@ -159,23 +159,21 @@ impl View {
action: impl FnOnce()) {
ui.scope(|ui| {
// Setup colors.
ui.visuals_mut().widgets.inactive.bg_fill = Colors::GOLD;
ui.visuals_mut().widgets.inactive.bg_fill = Colors::BUTTON;
ui.visuals_mut().widgets.hovered.bg_fill = Colors::GOLD;
ui.visuals_mut().widgets.active.bg_fill = Colors::YELLOW;
// Setup radius.
let mut r = 42.0 * 0.5;
let mut r = 44.0 * 0.5;
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 = 24.0;
let mut icon_color = Colors::TEXT_BUTTON;
let mut icon_color = Colors::GRAY;
// Increase radius and change icon size and color on-hover.
if br.hovered() {
r = r * 1.05;
icon_size = icon_size * 1.07;
icon_color = Colors::BLACK;
r = r * 1.07;
icon_color = Colors::TEXT_BUTTON;
}
let visuals = ui.style().interact(&br);
@ -187,7 +185,7 @@ impl View {
});
ui.allocate_ui_at_rect(rect, |ui| {
ui.centered_and_justified(|ui| {
ui.label(RichText::new(icon).color(icon_color).size(icon_size));
ui.label(RichText::new(icon).color(icon_color).size(25.0));
});
});
if Self::touched(ui, br) {

View file

@ -17,13 +17,13 @@ use egui_extras::{RetainedImage, Size, StripBuilder};
use crate::built_info;
use crate::gui::Colors;
use crate::gui::icons::{CHECK, EYE, EYE_SLASH, PLUS_CIRCLE, SHARE_FAT};
use crate::gui::icons::{CHECK, EYE, EYE_SLASH, FOLDER_PLUS, SHARE_FAT};
use crate::gui::platform::PlatformCallbacks;
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;
use crate::wallet::Wallets;
/// Wallet creation content.
pub struct WalletCreation {
@ -196,7 +196,7 @@ impl WalletCreation {
.color(Colors::GRAY)
);
ui.add_space(8.0);
let add_text = format!("{} {}", PLUS_CIRCLE, t!("wallets.add"));
let add_text = format!("{} {}", FOLDER_PLUS, t!("wallets.add"));
View::button(ui, add_text, Colors::BUTTON, || {
self.show_name_pass_modal();
});
@ -258,7 +258,7 @@ impl WalletCreation {
Step::ConfirmMnemonic => Some(Step::SetupConnection),
Step::SetupConnection => {
// Create wallet at last step.
WalletList::create_wallet(
Wallets::create_wallet(
self.name_edit.clone(),
self.pass_edit.clone(),
self.mnemonic_setup.mnemonic.get_phrase(),
@ -357,19 +357,6 @@ impl WalletCreation {
});
})
});
// Show information when specified values are empty.
if self.name_edit.is_empty() {
ui.add_space(12.0);
ui.label(RichText::new(t!("wallets.name_empty"))
.size(17.0)
.color(Colors::INACTIVE_TEXT));
} else if self.pass_edit.is_empty() {
ui.add_space(12.0);
ui.label(RichText::new(t!("wallets.pass_empty"))
.size(17.0)
.color(Colors::INACTIVE_TEXT));
}
ui.add_space(12.0);
});

View file

@ -13,26 +13,29 @@
// limitations under the License.
use egui::Margin;
use crate::gui::Colors;
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::View;
use crate::wallet::Wallet;
/// Selected wallet list item content.
pub struct WalletContent {
/// Current wallet instance.
wallet: Wallet
}
impl WalletContent {
fn new(wallet: Wallet) -> Self {
Self { wallet }
impl Default for WalletContent {
fn default() -> Self {
Self {}
}
}
impl WalletContent {
pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
pub fn ui(&mut self,
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
wallet: &Wallet,
cb: &dyn PlatformCallbacks) {
// Show wallet content.
egui::CentralPanel::default()
.frame(egui::Frame {
@ -40,13 +43,14 @@ impl WalletContent {
fill: Colors::WHITE,
inner_margin: Margin {
left: View::far_left_inset_margin(ui) + 4.0,
right: View::far_right_inset_margin(ui, frame) + 4.0,
top: 3.0,
right: View::get_right_inset() + 4.0,
top: 4.0,
bottom: 4.0,
},
..Default::default()
})
.show_inside(ui, |ui| {
ui.label(&wallet.config.name);
//TODO: wallet content
});
}

View file

@ -14,21 +14,30 @@
use std::cmp::max;
use egui::{Align2, Margin, Vec2};
use egui::{Align2, Margin, RichText, TextStyle, Widget};
use egui_extras::{Size, StripBuilder};
use crate::gui::Colors;
use crate::gui::icons::{ARROW_LEFT, GEAR, GLOBE, PLUS};
use crate::gui::icons::{ARROW_LEFT, EYE, EYE_SLASH, FOLDER_PLUS, GEAR, GLOBE, PLUS};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, ModalContainer, Root, TitlePanel, TitleType, View};
use crate::gui::views::{Modal, ModalContainer, ModalPosition, 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::WalletList;
use crate::wallet::{Wallet, Wallets};
/// Wallets content.
pub struct WalletsContent {
/// Selected list item content.
item_content: Option<WalletContent>,
/// Password to open wallet for [`Modal`].
pass_edit: String,
/// Flag to show/hide password at [`egui::TextEdit`] field.
hide_pass: bool,
/// Flag to check if wrong password was entered.
wrong_pass: bool,
/// Selected [`Wallet`] content.
wallet_content: WalletContent,
/// Wallet creation content.
creation_content: WalletCreation,
@ -39,9 +48,13 @@ pub struct WalletsContent {
impl Default for WalletsContent {
fn default() -> Self {
Self {
item_content: None,
pass_edit: "".to_string(),
hide_pass: true,
wrong_pass: false,
wallet_content: WalletContent::default(),
creation_content: WalletCreation::default(),
modal_ids: vec![
Self::OPEN_WALLET_MODAL,
WalletCreation::NAME_PASS_MODAL,
MnemonicSetup::WORD_INPUT_MODAL,
ConnectionSetup::ADD_CONNECTION_URL_MODAL
@ -57,11 +70,17 @@ impl ModalContainer for WalletsContent {
}
impl WalletsContent {
/// Identifier for wallet opening [`Modal`].
pub const OPEN_WALLET_MODAL: &'static str = "open_wallet_modal";
pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
// Show modal content for current ui container.
if self.can_draw_modal() {
Modal::ui(ui, |ui, modal| {
match modal.id {
Self::OPEN_WALLET_MODAL => {
self.open_wallet_modal_ui(ui, modal, cb);
},
WalletCreation::NAME_PASS_MODAL => {
self.creation_content.modal_ui(ui, modal, cb);
},
@ -76,33 +95,51 @@ impl WalletsContent {
});
}
// Get wallets.
let wallets = Wallets::list();
let is_list_empty = wallets.is_empty();
let selected = wallets.iter().find(|x| Some(x.config.id) == Wallets::selected_id());
// Show title panel.
self.title_ui(ui, frame);
let wallets = WalletList::list();
// Setup wallet content flags.
let is_wallet_creating = self.creation_content.can_go_back();
let is_wallet_showing = if let Some(id) = Wallets::selected_id() {
Wallets::is_open(id)
} else {
false
};
// Setup panels parameters.
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.
let is_wallet_panel_open
= is_dual_panel || is_wallet_showing || is_wallet_creating || is_list_empty;
let wallet_panel_width
= self.wallet_panel_width(ui, is_list_empty, is_dual_panel, is_wallet_showing);
// Show wallet panel content.
egui::SidePanel::right("wallet_panel")
.resizable(false)
.min_width(wallet_panel_width)
.exact_width(wallet_panel_width)
.frame(egui::Frame {
fill: if !wallets.is_empty() || is_wallet_creation {
Colors::WHITE
} else {
fill: if is_list_empty && !is_wallet_creating {
Colors::FILL_DARK
} else {
Colors::WHITE
},
..Default::default()
})
.show_animated_inside(ui, is_wallet_panel_open, |ui| {
self.wallet_content_ui(ui, frame, cb);
if is_wallet_showing {
self.wallet_content.ui(ui, frame, selected.unwrap(), cb);
} else {
self.creation_content.ui(ui, cb);
}
});
// Show list of wallets.
if !is_wallet_creation && !wallets.is_empty() {
// Show wallets list.
if !is_list_empty && (!is_wallet_panel_open || is_dual_panel) {
egui::CentralPanel::default()
.frame(egui::Frame {
stroke: View::DEFAULT_STROKE,
@ -116,22 +153,22 @@ impl WalletsContent {
..Default::default()
})
.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);
});
}
self.list_ui(ui, &wallets, cb);
});
// Show wallet creation button if wallet panel is not open.
if !is_wallet_panel_open {
self.create_wallet_btn_ui(ui);
// Do not show creation button if wallets panel is not showing at root
// or if wallet is not showing at dual panel mode.
let root_dual_panel = Root::is_dual_panel_mode(frame);
let wallets_panel_not_open = !root_dual_panel && Root::is_network_panel_open();
if wallets_panel_not_open || (is_wallet_panel_open && !is_wallet_showing) {
return;
} else {
self.create_wallet_btn_ui(ui, if is_dual_panel { wallet_panel_width } else { 0.0 });
}
}
}
/// Draw title content.
/// Draw [`TitlePanel`] content.
fn title_ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
// Setup title text.
let title_text = if self.creation_content.can_go_back() {
@ -143,7 +180,13 @@ impl WalletsContent {
// Draw title panel.
TitlePanel::ui(title_content, |ui, frame| {
if self.creation_content.can_go_back() {
if Wallets::selected_id().is_some() {
if !Self::is_dual_panel_mode(ui, frame) {
View::title_button(ui, ARROW_LEFT, || {
Wallets::select(None);
});
}
} else if self.creation_content.can_go_back() {
View::title_button(ui, ARROW_LEFT, || {
self.creation_content.back();
});
@ -159,53 +202,39 @@ impl WalletsContent {
}, ui, frame);
}
/// Draw [`WalletContent`] ui.
fn wallet_content_ui(&mut self,
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
cb: &dyn PlatformCallbacks) {
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);
}
}
/// Draw list of wallets.
fn list_ui(&mut self, ui: &mut egui::Ui, wallets: &Vec<Wallet>, cb: &dyn PlatformCallbacks) {
for w in wallets {
ui.label(&w.config.name);
/// 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;
max(min_width, available_width as i64) as f32
} else {
let dual_panel_root = Root::is_dual_panel_mode(frame);
if dual_panel_root {
available_width
} else {
frame.info().window_info.size.x
/// Show open/close button
let id = w.config.id;
let is_selected = Some(id) == Wallets::selected_id();
let is_open = Wallets::is_open(id);
if !is_open {
View::button(ui, "open me".to_string(), Colors::GOLD, || {
Wallets::select(Some(id));
self.show_open_wallet_modal(cb);
});
} else if !is_selected {
View::button(ui, "select me".to_string(), Colors::GOLD, || {
Wallets::select(Some(id));
});
}
if Wallets::is_open(id) {
ui.label("opened!");
}
}
}
/// Check if ui can show [`WalletsContent`] list and [`WalletContent`] content at same time.
fn is_dual_panel_mode(ui: &mut egui::Ui, frame: &mut eframe::Frame) -> bool {
let dual_panel_root = Root::is_dual_panel_mode(frame);
let max_width = ui.available_width();
dual_panel_root && max_width >= (Root::SIDE_PANEL_MIN_WIDTH * 2.0) + View::get_right_inset()
}
/// Draw floating button to create the wallet.
fn create_wallet_btn_ui(&mut self, ui: &mut egui::Ui) {
/// Draw floating button to show wallet creation [`Modal`].
fn create_wallet_btn_ui(&mut self, ui: &mut egui::Ui, right_margin: f32) {
egui::Window::new("create_wallet_button")
.title_bar(false)
.resizable(false)
.collapsible(false)
.anchor(Align2::RIGHT_BOTTOM, Vec2::new(-8.0, -8.0))
.anchor(Align2::RIGHT_BOTTOM, egui::Vec2::new(-6.0 - right_margin, -6.0))
.frame(egui::Frame::default())
.show(ui.ctx(), |ui| {
View::round_button(ui, PLUS, || {
@ -214,6 +243,164 @@ impl WalletsContent {
});
}
/// Show [`Modal`] to open selected wallet.
pub fn show_open_wallet_modal(&mut self, cb: &dyn PlatformCallbacks) {
// Reset modal values.
self.hide_pass = true;
self.pass_edit = String::from("");
self.wrong_pass = false;
// Show modal.
Modal::new(Self::OPEN_WALLET_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.open"))
.show();
cb.show_keyboard();
}
/// Draw wallet opening [`Modal`] content.
fn open_wallet_modal_ui(&mut self,
ui: &mut egui::Ui,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.pass"))
.size(17.0)
.color(Colors::GRAY));
ui.add_space(10.0);
StripBuilder::new(ui)
.size(Size::exact(34.0))
.vertical(|mut strip| {
strip.strip(|builder| {
builder
.size(Size::remainder())
.size(Size::exact(48.0))
.horizontal(|mut strip| {
strip.cell(|ui| {
// Draw wallet password text edit.
let pass_resp = egui::TextEdit::singleline(&mut self.pass_edit)
.id(ui.id().with("wallet_pass_edit"))
.font(TextStyle::Heading)
.desired_width(ui.available_width())
.cursor_at_end(true)
.password(self.hide_pass)
.ui(ui);
pass_resp.request_focus();
if pass_resp.clicked() {
cb.show_keyboard();
}
});
strip.cell(|ui| {
ui.vertical_centered(|ui| {
// Draw button to show/hide password.
let eye_icon = if self.hide_pass { EYE } else { EYE_SLASH };
View::button(ui, eye_icon.to_string(), Colors::WHITE, || {
self.hide_pass = !self.hide_pass;
});
});
});
});
})
});
// Show information when password is empty.
if self.pass_edit.is_empty() {
ui.add_space(10.0);
ui.label(RichText::new(t!("wallets.pass_empty"))
.size(17.0)
.color(Colors::INACTIVE_TEXT));
} else if self.wrong_pass {
ui.add_space(10.0);
ui.label(RichText::new(t!("wallets.wrong_pass"))
.size(17.0)
.color(Colors::RED));
}
ui.add_space(12.0);
});
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
// Clear values.
self.pass_edit = "".to_string();
self.wrong_pass = false;
self.hide_pass = true;
// Close modal.
cb.hide_keyboard();
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
// Callback for continue button.
let mut on_continue = || {
if self.pass_edit.is_empty() {
return;
}
let selected_id = Wallets::selected_id().unwrap();
match Wallets::open(selected_id, self.pass_edit.clone()) {
Ok(_) => {
// Clear values.
self.pass_edit = "".to_string();
self.wrong_pass = false;
self.hide_pass = true;
// Close modal.
cb.hide_keyboard();
modal.close();
}
Err(_) => self.wrong_pass = true
}
};
// Continue on Enter key press.
View::on_enter_key(ui, || {
(on_continue)();
});
View::button(ui, t!("continue"), Colors::WHITE, on_continue);
});
});
ui.add_space(6.0);
});
}
/// Calculate [`WalletContent`] panel width.
fn wallet_panel_width(
&self,
ui:&mut egui::Ui,
is_list_empty: bool,
is_dual_panel: bool,
is_wallet_showing: bool
) -> f32 {
let is_wallet_creation = self.creation_content.can_go_back();
let available_width = if is_list_empty || is_wallet_creation {
ui.available_width()
} else {
ui.available_width() - Root::SIDE_PANEL_MIN_WIDTH
};
if is_dual_panel {
let min_width = (Root::SIDE_PANEL_MIN_WIDTH + View::get_right_inset()) as i64;
max(min_width, available_width as i64) as f32
} else {
if is_wallet_showing {
ui.available_width()
} else {
available_width
}
}
}
/// Check if it's possible to show [`WalletsContent`] and [`WalletContent`] panels at same time.
fn is_dual_panel_mode(ui: &mut egui::Ui, frame: &mut eframe::Frame) -> bool {
let dual_panel_root = Root::is_dual_panel_mode(frame);
let max_width = ui.available_width();
dual_panel_root && max_width >= (Root::SIDE_PANEL_MIN_WIDTH * 2.0) + View::get_right_inset()
}
/// Handle Back key event.
/// Return `false` when event was handled.
pub fn on_back(&mut self) -> bool {

View file

@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use serde::de::DeserializeOwned;
use crate::node::NodeConfig;
use crate::wallet::WalletList;
use crate::wallet::Wallets;
lazy_static! {
/// Static settings state to be accessible globally.
@ -94,7 +94,7 @@ impl AppConfig {
w_node_config.peers = node_config.peers;
// Reload wallets.
WalletList::reload(chain_type);
Wallets::reload(chain_type);
}
}

View file

@ -18,7 +18,7 @@ use grin_core::global::ChainTypes;
use serde_derive::{Deserialize, Serialize};
use crate::{AppConfig, Settings};
use crate::wallet::WalletList;
use crate::wallet::Wallets;
/// Wallet configuration.
#[derive(Serialize, Deserialize, Clone)]
@ -26,9 +26,9 @@ pub struct WalletConfig {
/// Chain type for current wallet.
chain_type: ChainTypes,
/// Identifier for a wallet.
id: i64,
/// Readable wallet name.
name: String,
pub(crate) id: i64,
/// Human-readable wallet name for ui.
pub(crate) name: String,
/// External node connection URL.
external_node_url: Option<String>,
}
@ -60,7 +60,7 @@ impl WalletConfig {
/// Get config file path for provided [`ChainTypes`] and wallet identifier.
fn get_config_file_path(chain_type: &ChainTypes, id: i64) -> PathBuf {
let mut config_path = WalletList::get_base_path(chain_type);
let mut config_path = Wallets::get_base_path(chain_type);
config_path.push(id.to_string());
// Create if the config path doesn't exist.
if !config_path.exists() {
@ -73,7 +73,7 @@ impl WalletConfig {
/// Get current wallet data path.
pub fn get_data_path(&self) -> String {
let chain_type = AppConfig::chain_type();
let mut config_path = WalletList::get_base_path(&chain_type);
let mut config_path = Wallets::get_base_path(&chain_type);
config_path.push(self.id.to_string());
config_path.to_str().unwrap().to_string()
}
@ -84,17 +84,6 @@ impl WalletConfig {
Settings::write_to_file(self, config_path);
}
/// Get readable wallet name.
pub fn get_name(&self) -> &String {
&self.name
}
/// Set readable wallet name.
pub fn set_name(&mut self, name: String) {
self.name = name;
self.save();
}
/// Get external node connection URL.
pub fn get_external_node_url(&self) -> &Option<String> {
&self.external_node_url

View file

@ -1,97 +0,0 @@
// Copyright 2023 The Grim Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use grin_core::global::ChainTypes;
use grin_wallet_libwallet::Error;
use lazy_static::lazy_static;
use crate::{AppConfig, Settings};
use crate::wallet::Wallet;
lazy_static! {
/// Global wallets state.
static ref WALLETS_STATE: Arc<RwLock<WalletList>> = Arc::new(RwLock::new(WalletList::init()));
}
/// Wallets manager.
pub struct WalletList {
pub(crate) list: Vec<Wallet>
}
/// Base wallets directory name.
pub const BASE_DIR_NAME: &'static str = "wallets";
impl WalletList {
/// Initialize manager by loading list of wallets into state.
fn init() -> Self {
Self { list: Self::load_wallets(&AppConfig::chain_type()) }
}
/// Create new wallet and add it to state.
pub fn create_wallet(
name: String,
password: String,
mnemonic: String,
external_node_url: Option<String>
)-> Result<(), Error> {
let wallet = Wallet::create(name, password, mnemonic, external_node_url)?;
let mut w_state = WALLETS_STATE.write().unwrap();
w_state.list.push(wallet);
Ok(())
}
/// Load wallets for provided [`ChainType`].
fn load_wallets(chain_type: &ChainTypes) -> Vec<Wallet> {
let mut wallets = Vec::new();
let wallets_dir = Self::get_base_path(chain_type);
// Load wallets from base directory.
for dir in wallets_dir.read_dir().unwrap() {
let wallet_dir = dir.unwrap().path();
if wallet_dir.is_dir() {
let wallet = Wallet::init(wallet_dir);
if let Some(w) = wallet {
wallets.push(w);
}
}
}
wallets
}
/// Get wallets base directory path for provided [`ChainTypes`].
pub fn get_base_path(chain_type: &ChainTypes) -> PathBuf {
let mut wallets_path = Settings::get_base_path(Some(chain_type.shortname()));
wallets_path.push(BASE_DIR_NAME);
// Create wallets base directory if it doesn't exist.
if !wallets_path.exists() {
let _ = fs::create_dir_all(wallets_path.clone());
}
wallets_path
}
/// Get list of wallets.
pub fn list() -> Vec<Wallet> {
let r_state = WALLETS_STATE.read().unwrap();
r_state.list.clone()
}
/// Reload list of wallets for provided [`ChainTypes`].
pub fn reload(chain_type: &ChainTypes) {
let mut w_state = WALLETS_STATE.write().unwrap();
w_state.list = Self::load_wallets(chain_type);
}
}

View file

@ -18,10 +18,7 @@ pub mod tx;
pub mod keys;
mod wallet;
pub use wallet::Wallet;
pub use wallet::{Wallet, Wallets};
mod config;
pub use config::*;
mod list;
pub use list::WalletList;
pub use config::*;

View file

@ -12,32 +12,164 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::BTreeSet;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::{Arc, RwLock};
use grin_core::global;
use grin_core::global::ChainTypes;
use grin_keychain::{ExtKeychain, Identifier, Keychain};
use grin_util::types::ZeroingString;
use grin_wallet_api::{Foreign, ForeignCheckMiddlewareFn, Owner};
use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl, HTTPNodeClient};
use grin_wallet_libwallet::{Error, NodeClient, NodeVersionInfo, OutputStatus, Slate, slate_versions, SlatepackArmor, Slatepacker, SlatepackerArgs, TxLogEntry, wallet_lock, WalletBackend, WalletInfo, WalletInst, WalletLCProvider};
use grin_wallet_libwallet::Error::GenericError;
use lazy_static::lazy_static;
use log::debug;
use parking_lot::Mutex;
use uuid::Uuid;
use crate::AppConfig;
use crate::{AppConfig, Settings};
use crate::node::NodeConfig;
use crate::wallet::selection::lock_tx_context;
use crate::wallet::tx::{add_inputs_to_slate, new_tx_slate};
use crate::wallet::updater::{cancel_tx, refresh_output_state, retrieve_txs};
use crate::wallet::WalletConfig;
lazy_static! {
/// Global wallets state.
static ref WALLETS_STATE: Arc<RwLock<Wallets>> = Arc::new(RwLock::new(Wallets::init()));
}
/// Manages [`Wallet`] list and state.
pub struct Wallets {
/// List of wallets.
list: Vec<Wallet>,
/// Selected [`Wallet`] identifier from config.
selected_id: Option<i64>,
/// Identifiers of opened wallets.
opened_ids: BTreeSet<i64>
}
impl Wallets {
/// Base wallets directory name.
pub const BASE_DIR_NAME: &'static str = "wallets";
/// Initialize manager by loading list of wallets into state.
fn init() -> Self {
Self {
list: Self::load_wallets(&AppConfig::chain_type()),
selected_id: None,
opened_ids: Default::default() }
}
/// Create new wallet and add it to state.
pub fn create_wallet(
name: String,
password: String,
mnemonic: String,
external_node_url: Option<String>
)-> Result<(), Error> {
let wallet = Wallet::create(name, password, mnemonic, external_node_url)?;
let mut w_state = WALLETS_STATE.write().unwrap();
w_state.list.push(wallet);
Ok(())
}
/// Load wallets for provided [`ChainType`].
fn load_wallets(chain_type: &ChainTypes) -> Vec<Wallet> {
let mut wallets = Vec::new();
let wallets_dir = Self::get_base_path(chain_type);
// Load wallets from base directory.
for dir in wallets_dir.read_dir().unwrap() {
let wallet_dir = dir.unwrap().path();
if wallet_dir.is_dir() {
let wallet = Wallet::init(wallet_dir);
if let Some(w) = wallet {
wallets.push(w);
}
}
}
wallets
}
/// Get list of wallets.
pub fn list() -> Vec<Wallet> {
let r_state = WALLETS_STATE.read().unwrap();
r_state.list.clone()
}
/// Select [`Wallet`] with provided identifier.
pub fn select(id: Option<i64>) {
let mut w_state = WALLETS_STATE.write().unwrap();
w_state.selected_id = id;
}
/// Get selected [`Wallet`] identifier.
pub fn selected_id() -> Option<i64> {
let r_state = WALLETS_STATE.read().unwrap();
r_state.selected_id
}
/// Open [`Wallet`] with provided identifier and password.
pub fn open(id: i64, password: String) -> Result<(), Error> {
let list = Self::list();
let mut w_state = WALLETS_STATE.write().unwrap();
for mut w in list {
if w.config.id == id {
w.open(password)?;
break;
}
}
w_state.opened_ids.insert(id);
Ok(())
}
/// Close [`Wallet`] with provided identifier.
pub fn close(id: i64) -> Result<(), Error> {
let list = Self::list();
let mut w_state = WALLETS_STATE.write().unwrap();
for mut w in list {
if w.config.id == id {
w.close()?;
break;
}
}
w_state.opened_ids.remove(&id);
Ok(())
}
/// Check if [`Wallet`] with provided identifier was open.
pub fn is_open(id: i64) -> bool {
let r_state = WALLETS_STATE.read().unwrap();
r_state.opened_ids.contains(&id)
}
/// Get wallets base directory path for provided [`ChainTypes`].
pub fn get_base_path(chain_type: &ChainTypes) -> PathBuf {
let mut wallets_path = Settings::get_base_path(Some(chain_type.shortname()));
wallets_path.push(Self::BASE_DIR_NAME);
// Create wallets base directory if it doesn't exist.
if !wallets_path.exists() {
let _ = fs::create_dir_all(wallets_path.clone());
}
wallets_path
}
/// Reload list of wallets for provided [`ChainTypes`].
pub fn reload(chain_type: &ChainTypes) {
let mut w_state = WALLETS_STATE.write().unwrap();
w_state.list = Self::load_wallets(chain_type);
}
}
/// Wallet instance and config wrapper.
#[derive(Clone)]
pub struct Wallet {
/// Wallet instance, exists when wallet is open.
instance: Option<WalletInstance>,
/// Wallet instance.
instance: WalletInstance,
/// Wallet data path.
path: String,
/// Wallet configuration.
@ -59,8 +191,8 @@ type WalletInstance = Arc<
>;
impl Wallet {
/// Create and open new wallet.
pub fn create(
/// Create new wallet, make it open and selected.
fn create(
name: String,
password: String,
mnemonic: String,
@ -68,43 +200,43 @@ impl Wallet {
) -> Result<Wallet, Error> {
let config = WalletConfig::create(name.clone(), external_node_url);
let wallet = Self::create_wallet_instance(config.clone())?;
let mut w_lock = wallet.lock();
let p = w_lock.lc_provider()?;
// Create wallet.
p.create_wallet(None,
Some(ZeroingString::from(mnemonic.clone())),
mnemonic.len(),
ZeroingString::from(password.clone()),
false,
)?;
// Open wallet.
p.open_wallet(None, ZeroingString::from(password), false, false)?;
let w = Wallet {
instance: Some(wallet.clone()),
instance: wallet,
path: config.get_data_path(),
config,
};
{
let mut w_lock = w.instance.lock();
let p = w_lock.lc_provider()?;
// Create wallet.
p.create_wallet(None,
Some(ZeroingString::from(mnemonic.clone())),
mnemonic.len(),
ZeroingString::from(password.clone()),
false,
)?;
// Open wallet.
p.open_wallet(None, ZeroingString::from(password), false, false)?;
}
Ok(w)
}
/// Initialize wallet from provided data path.
pub fn init(data_path: PathBuf) -> Option<Wallet> {
fn init(data_path: PathBuf) -> Option<Wallet> {
let wallet_config = WalletConfig::load(data_path.clone());
if let Some(config) = wallet_config {
let path = data_path.to_str().unwrap().to_string();
return Some(Self { instance: None, path, config });
if let Ok(instance) = Self::create_wallet_instance(config.clone()) {
return Some(Self { instance, path, config });
}
}
None
}
/// Check if wallet is open (instance exists).
pub fn is_open(&self) -> bool {
self.instance.is_some()
}
/// Create wallet instance from provided config.
fn create_wallet_instance(config: WalletConfig) -> Result<WalletInstance, Error> {
// Assume global chain type has already been initialized.
@ -152,42 +284,35 @@ impl Wallet {
}
/// Open wallet.
pub fn open_wallet(&mut self, password: ZeroingString) -> Result<(), Error> {
if let None = self.instance {
let wallet = Self::create_wallet_instance(self.config.clone())?;
let mut wallet_lock = wallet.lock();
let lc = wallet_lock.lc_provider()?;
lc.open_wallet(None, password, false, false)?;
self.instance = Some(wallet.clone());
}
fn open(&mut self, password: String) -> Result<(), Error> {
let mut wallet_lock = self.instance.lock();
let lc = wallet_lock.lc_provider()?;
lc.open_wallet(None, ZeroingString::from(password), false, false)?;
Ok(())
}
/// Close wallet.
pub fn close_wallet(&self) -> Result<(), Error> {
if let Some(wallet) = &self.instance {
let mut wallet_lock = wallet.lock();
let lc = wallet_lock.lc_provider()?;
lc.close_wallet(None)?;
}
fn close(&mut self) -> Result<(), Error> {
let mut wallet_lock = self.instance.lock();
let lc = wallet_lock.lc_provider()?;
lc.close_wallet(None)?;
Ok(())
}
/// Create transaction.
fn tx_create(
pub fn tx_create(
&self,
amount: u64,
minimum_confirmations: u64,
selection_strategy_is_use_all: bool,
) -> Result<(Vec<TxLogEntry>, String), Error> {
let wallet = self.instance.clone().ok_or(GenericError("Wallet was not open".to_string()))?;
let parent_key_id = {
wallet_lock!(wallet.clone(), w);
wallet_lock!(self.instance, w);
w.parent_key_id().clone()
};
let slate = {
wallet_lock!(wallet, w);
wallet_lock!(self.instance, w);
let mut slate = new_tx_slate(&mut **w, amount, false, 2, false, None)?;
let height = w.w2n_client().get_chain_tip()?.0;
@ -223,7 +348,7 @@ impl Wallet {
dec_key: None,
});
let slatepack = packer.create_slatepack(&slate)?;
let api = Owner::new(self.instance.clone().unwrap(), None);
let api = Owner::new(self.instance.clone(), None);
let txs = api.retrieve_txs(None, false, None, Some(slate.id), None)?;
let result = (
txs.1,
@ -263,18 +388,19 @@ impl Wallet {
}
/// Receive transaction.
fn tx_receive(
pub fn tx_receive(
&self,
account: &str,
slate_armored: &str
) -> Result<(Vec<TxLogEntry>, String), Error> {
let wallet = self.instance.clone().ok_or(GenericError("Wallet was not open".to_string()))?;
let foreign_api = Foreign::new(wallet.clone(), None, Some(Self::check_middleware), false);
let owner_api = Owner::new(wallet, None);
let foreign_api =
Foreign::new(self.instance.clone(), None, Some(Self::check_middleware), false);
let owner_api = Owner::new(self.instance.clone(), None);
let mut slate =
owner_api.slate_from_slatepack_message(None, slate_armored.to_owned(), vec![0])?;
let slatepack = owner_api.decode_slatepack_message(None, slate_armored.to_owned(), vec![0])?;
let slatepack =
owner_api.decode_slatepack_message(None, slate_armored.to_owned(), vec![0])?;
let _ret_address = slatepack.sender;
@ -294,9 +420,8 @@ impl Wallet {
}
/// Cancel transaction.
fn tx_cancel(&self, id: u32) -> Result<String, Error> {
let wallet = self.instance.clone().ok_or(GenericError("Wallet was not open".to_string()))?;
wallet_lock!(wallet, w);
pub fn tx_cancel(&self, id: u32) -> Result<String, Error> {
wallet_lock!(self.instance, w);
let parent_key_id = w.parent_key_id();
cancel_tx(&mut **w, None, &parent_key_id, Some(id), None)?;
Ok("".to_owned())
@ -304,19 +429,19 @@ impl Wallet {
/// Get transaction info.
pub fn get_tx(&self, tx_slate_id: &str) -> Result<(bool, Vec<TxLogEntry>), Error> {
let api = Owner::new(self.instance.clone().unwrap(), None);
let api = Owner::new(self.instance.clone(), None);
let uuid = Uuid::parse_str(tx_slate_id).unwrap();
let txs = api.retrieve_txs(None, true, None, Some(uuid), None)?;
Ok(txs)
}
/// Finalize transaction.
fn tx_finalize(&self, slate_armored: &str) -> Result<(bool, Vec<TxLogEntry>), Error> {
let wallet = self.instance.clone().ok_or(GenericError("Wallet was not open".to_string()))?;
let owner_api = Owner::new(wallet, None);
pub fn tx_finalize(&self, slate_armored: &str) -> Result<(bool, Vec<TxLogEntry>), Error> {
let owner_api = Owner::new(self.instance.clone(), None);
let mut slate =
owner_api.slate_from_slatepack_message(None, slate_armored.to_owned(), vec![0])?;
let slatepack = owner_api.decode_slatepack_message(None, slate_armored.to_owned(), vec![0])?;
let slatepack =
owner_api.decode_slatepack_message(None, slate_armored.to_owned(), vec![0])?;
let _ret_address = slatepack.sender;
@ -326,9 +451,8 @@ impl Wallet {
}
/// Post transaction to node for broadcasting.
fn tx_post(&self, tx_slate_id: &str) -> Result<(), Error> {
let wallet = self.instance.clone().ok_or(GenericError("Wallet was not open".to_string()))?;
let api = Owner::new(wallet, None);
pub fn tx_post(&self, tx_slate_id: &str) -> Result<(), Error> {
let api = Owner::new(self.instance.clone(), None);
let tx_uuid = Uuid::parse_str(tx_slate_id).unwrap();
let (_, txs) = api.retrieve_txs(None, true, None, Some(tx_uuid.clone()), None)?;
if txs[0].confirmed {
@ -355,14 +479,13 @@ impl Wallet {
&self,
minimum_confirmations: u64
) -> Result<(bool, Vec<TxLogEntry>, WalletInfo), Error> {
let wallet = self.instance.clone().ok_or(GenericError("Wallet was not open".to_string()))?;
let refreshed = Self::update_state(wallet.clone()).unwrap_or(false);
let refreshed = Self::update_state(self.instance.clone()).unwrap_or(false);
let wallet_info = {
wallet_lock!(wallet, w);
wallet_lock!(self.instance, w);
let parent_key_id = w.parent_key_id();
Self::get_info(&mut **w, &parent_key_id, minimum_confirmations)?
};
let api = Owner::new(wallet, None);
let api = Owner::new(self.instance.clone(), None);
let txs = api.retrieve_txs(None, false, None, None, None)?;
Ok((refreshed, txs.1, wallet_info))