tor: fix launch onion server, wallet tor service, send over tor

This commit is contained in:
ardocrat
2024-04-30 18:15:03 +03:00
parent 1d9c8533ad
commit 12650c94fd
17 changed files with 1408 additions and 607 deletions
+1 -79
View File
@@ -12,14 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::time::Duration;
use egui::{Align, Id, Layout, RichText, Rounding};
use url::Url;
use crate::tor::{TorServer, TorServerConfig};
use crate::AppConfig;
use crate::gui::Colors;
use crate::gui::icons::{CARET_RIGHT, CHECK_CIRCLE, COMPUTER_TOWER, DOTS_THREE_CIRCLE, GEAR_SIX, PENCIL, POWER, TRASH, WARNING_CIRCLE, X_CIRCLE};
use crate::gui::icons::{CARET_RIGHT, CHECK_CIRCLE, COMPUTER_TOWER, DOTS_THREE_CIRCLE, PENCIL, POWER, TRASH, X_CIRCLE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, NodeSetup, View};
use crate::gui::views::types::{ModalContainer, ModalPosition, TextEditOptions};
@@ -101,15 +99,6 @@ impl ConnectionsContent {
// Show integrated node info content.
Self::integrated_node_item_ui(ui);
// Show transport connections.
ui.add_space(6.0);
let transport_text = format!("{}:", t!("wallets.transport"));
ui.label(RichText::new(transport_text).size(16.0).color(Colors::GRAY));
ui.add_space(6.0);
// Show Tor SOCKS server.
Self::tor_transport_item_ui(ui);
// Show external connections.
let ext_conn_list = ConnectionsConfig::ext_conn_list();
if !ext_conn_list.is_empty() {
@@ -123,73 +112,6 @@ impl ConnectionsContent {
});
}
}
// Redraw after delay if Tor server is running.
if TorServer::is_running() || TorServer::is_starting() ||
TorServer::is_stopping() {
ui.ctx().request_repaint_after(Duration::from_millis(1000));
}
}
/// Draw Tor connection item content.
fn tor_transport_item_ui(ui: &mut egui::Ui) {
// Draw round background.
let mut rect = ui.available_rect_before_wrap();
rect.set_height(78.0);
let rounding = View::item_rounding(0, 1, false);
ui.painter().rect(rect, rounding, Colors::FILL, View::ITEM_STROKE);
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Draw button to show Tor connection settings.
View::item_button(ui, View::item_rounding(0, 1, true), GEAR_SIX, None, || {
AppConfig::toggle_show_connections_network_panel();
});
// Draw buttons to stop or start Tor server.
if !TorServer::is_stopping() && !TorServer::is_starting() {
if TorServer::is_running() {
View::item_button(ui, Rounding::default(), POWER, Some(Colors::RED), || {
TorServer::stop();
});
} else {
View::item_button(ui, Rounding::default(), POWER, Some(Colors::GREEN), || {
TorServer::start();
});
}
}
let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
ui.add_space(6.0);
ui.vertical(|ui| {
ui.add_space(3.0);
ui.label(RichText::new(t!("network.tor_network"))
.size(18.0)
.color(Colors::TITLE));
// Setup SOCKS server address.
let socks_port = TorServerConfig::socks_port();
let addr_text = format!("{} http://127.0.0.1:{}", COMPUTER_TOWER, socks_port);
ui.label(RichText::new(addr_text).size(15.0).color(Colors::TEXT));
ui.add_space(1.0);
// Setup server status text.
let (status_icon, status_text) = if TorServer::has_error() {
(WARNING_CIRCLE, t!("network.server_error"))
} else if TorServer::is_starting() {
(DOTS_THREE_CIRCLE, t!("network.server_starting"))
} else if TorServer::is_stopping() {
(DOTS_THREE_CIRCLE, t!("network.server_stopping"))
} else if TorServer::is_running() {
(CHECK_CIRCLE, t!("network.server_enabled"))
} else {
(X_CIRCLE, t!("network.server_disabled"))
};
let status_text = format!("{} {}", status_icon, status_text);
ui.label(RichText::new(status_text).size(15.0).color(Colors::GRAY));
})
});
});
}
/// Draw integrated node connection item content.
+10 -1
View File
@@ -83,7 +83,9 @@ pub struct TextEditOptions {
/// Flag to show copy button.
pub copy: bool,
/// Flag to show paste button.
pub paste: bool
pub paste: bool,
/// Flag to show button to scan QR code into text.
pub scan_qr: bool
}
impl TextEditOptions {
@@ -95,6 +97,7 @@ impl TextEditOptions {
password: false,
copy: false,
paste: false,
scan_qr: false,
}
}
@@ -127,4 +130,10 @@ impl TextEditOptions {
self.paste = true;
self
}
/// Show button to scan QR code to text.
pub fn scan_qr(mut self) -> Self {
self.scan_qr = true;
self
}
}
+10 -1
View File
@@ -25,7 +25,7 @@ use egui::text::{LayoutJob, TextFormat};
use egui::text_edit::TextEditState;
use crate::gui::Colors;
use crate::gui::icons::{CHECK_SQUARE, CLIPBOARD_TEXT, COPY, EYE, EYE_SLASH, SQUARE};
use crate::gui::icons::{CHECK_SQUARE, CLIPBOARD_TEXT, COPY, EYE, EYE_SLASH, SCAN, SQUARE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::types::TextEditOptions;
@@ -346,6 +346,15 @@ impl View {
ui.add_space(8.0);
}
// Setup scan QR code button.
if options.paste {
let scan_icon = SCAN.to_string();
View::button(ui, scan_icon, Colors::WHITE, || {
//TODO: open scanner
});
ui.add_space(8.0);
}
let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
// Setup text edit size.
+2 -2
View File
@@ -19,7 +19,7 @@ use grin_core::core::amount_to_hr_string;
use crate::AppConfig;
use crate::gui::Colors;
use crate::gui::icons::{BRIDGE, CHAT_CIRCLE_TEXT, CHECK, CHECK_FAT, FOLDER_USER, GEAR_FINE, GRAPH, PACKAGE, PATH, POWER, QR_CODE, REPEAT, USERS_THREE};
use crate::gui::icons::{BRIDGE, CHAT_CIRCLE_TEXT, CHECK, CHECK_FAT, FOLDER_USER, GEAR_FINE, GRAPH, PACKAGE, PATH, POWER, REPEAT, SCAN, USERS_THREE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, Root, View};
use crate::gui::views::types::{ModalPosition, TextEditOptions};
@@ -176,7 +176,7 @@ impl WalletContent {
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Draw button to scan QR code.
View::item_button(ui, View::item_rounding(0, 2, true), QR_CODE, None, || {
View::item_button(ui, View::item_rounding(0, 2, true), SCAN, None, || {
//TODO: Scan with QR code.
});
+58 -59
View File
@@ -53,9 +53,6 @@ impl MessageError {
/// Slatepacks messages interaction tab content.
pub struct WalletMessages {
/// Flag to check if send or invoice request was opened.
send_request: bool,
/// Slatepack message to create response message.
message_edit: String,
/// Parsed Slatepack message.
@@ -67,15 +64,17 @@ pub struct WalletMessages {
/// Flag to check if Dandelion is needed to finalize transaction.
dandelion: bool,
/// Amount to send or receive.
/// Flag to check if send or invoice request was opened for [`Modal`].
send_request: bool,
/// Amount to send or receive at [`Modal`].
amount_edit: String,
/// Generated Slatepack message as request to send or receive funds.
/// Generated Slatepack message as request to send or receive funds at [`Modal`].
request_edit: String,
/// Flag to check if there is an error happened on invoice creation.
/// Flag to check if there is an error happened on request creation at [`Modal`].
request_error: Option<MessageError>,
}
/// Identifier for invoice amount [`Modal`].
/// Identifier for amount input [`Modal`].
const AMOUNT_MODAL: &'static str = "amount_modal";
impl WalletMessages {
@@ -127,7 +126,7 @@ impl WalletTab for WalletMessages {
.show_inside(ui, |ui| {
ScrollArea::vertical()
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
.id_source(Id::from("wallet_manual").with(wallet.get_config().id))
.id_source(Id::from("wallet_messages").with(wallet.get_config().id))
.auto_shrink([false; 2])
.show(ui, |ui| {
ui.vertical_centered(|ui| {
@@ -181,6 +180,54 @@ impl WalletMessages {
}
}
/// Draw creation of request to send or receive funds.
fn request_ui(&mut self,
ui: &mut egui::Ui,
cb: &dyn PlatformCallbacks) {
ui.label(RichText::new(t!("wallets.create_request_desc"))
.size(16.0)
.color(Colors::INACTIVE_TEXT));
ui.add_space(7.0);
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
// Draw send request creation button.
let send_text = format!("{} {}", UPLOAD_SIMPLE, t!("wallets.send"));
View::button(ui, send_text, Colors::BUTTON, || {
// Setup modal values.
self.send_request = true;
self.amount_edit = "".to_string();
self.request_error = None;
// Show send amount modal.
Modal::new(AMOUNT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.send"))
.show();
cb.show_keyboard();
});
});
columns[1].vertical_centered_justified(|ui| {
// Draw invoice request creation button.
let receive_text = format!("{} {}", DOWNLOAD_SIMPLE, t!("wallets.receive"));
View::button(ui, receive_text, Colors::BUTTON, || {
// Setup modal values.
self.send_request = false;
self.amount_edit = "".to_string();
self.request_error = None;
// Show receive amount modal.
Modal::new(AMOUNT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.receive"))
.show();
cb.show_keyboard();
});
});
});
}
/// Draw Slatepack message input content.
fn input_slatepack_ui(&mut self,
ui: &mut egui::Ui,
@@ -509,55 +556,7 @@ impl WalletMessages {
}
}
/// Draw creation of request to send or receive funds.
fn request_ui(&mut self,
ui: &mut egui::Ui,
cb: &dyn PlatformCallbacks) {
ui.label(RichText::new(t!("wallets.create_request_desc"))
.size(16.0)
.color(Colors::INACTIVE_TEXT));
ui.add_space(7.0);
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
// Draw send request creation button.
let send_text = format!("{} {}", UPLOAD_SIMPLE, t!("wallets.send"));
View::button(ui, send_text, Colors::BUTTON, || {
// Setup modal values.
self.send_request = true;
self.amount_edit = "".to_string();
self.request_error = None;
// Show send amount modal.
Modal::new(AMOUNT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.send"))
.show();
cb.show_keyboard();
});
});
columns[1].vertical_centered_justified(|ui| {
// Draw invoice request creation button.
let receive_text = format!("{} {}", DOWNLOAD_SIMPLE, t!("wallets.receive"));
View::button(ui, receive_text, Colors::BUTTON, || {
// Setup modal values.
self.send_request = false;
self.amount_edit = "".to_string();
self.request_error = None;
// Show receive amount modal.
Modal::new(AMOUNT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.receive"))
.show();
cb.show_keyboard();
});
});
});
}
/// Draw invoice amount [`Modal`] content.
/// Draw amount input [`Modal`] content to create invoice or request to send funds.
fn amount_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
@@ -621,7 +620,7 @@ impl WalletMessages {
}
}
// Show invoice creation error.
// Show request creation error.
if self.request_error.is_some() {
ui.add_space(12.0);
ui.vertical_centered(|ui| {
@@ -658,7 +657,7 @@ impl WalletMessages {
wallet.issue_invoice(a)
};
match message {
Ok(message) => {
Ok((_, message)) => {
self.request_edit = message;
cb.hide_keyboard();
}
+514 -10
View File
@@ -12,17 +12,54 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use egui::Margin;
use std::sync::{Arc, RwLock};
use std::thread;
use egui::{Align, Id, Layout, Margin, RichText, Rounding, ScrollArea};
use egui::scroll_area::ScrollBarVisibility;
use grin_core::core::{amount_from_hr_string, amount_to_hr_string};
use grin_wallet_libwallet::SlatepackAddress;
use crate::gui::Colors;
use crate::gui::icons::{CHECK_CIRCLE, COMPUTER_TOWER, COPY, DOTS_THREE_CIRCLE, EXPORT, GEAR_SIX, GLOBE_SIMPLE, POWER, QR_CODE, UPLOAD, WARNING_CIRCLE, X_CIRCLE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::View;
use crate::gui::views::{Modal, Root, View};
use crate::gui::views::types::{ModalPosition, TextEditOptions};
use crate::gui::views::wallets::wallet::types::{WalletTab, WalletTabType};
use crate::gui::views::wallets::wallet::WalletContent;
use crate::tor::Tor;
use crate::wallet::Wallet;
/// Sending tab content.
#[derive(Default)]
pub struct WalletTransport;
/// Wallet transport tab content.
pub struct WalletTransport {
/// Flag to check if transaction is sending over Tor to show progress at [`Modal`].
tor_sending: Arc<RwLock<bool>>,
/// Flag to check if error occurred during sending of transaction over Tor at [`Modal`].
tor_send_error: Arc<RwLock<bool>>,
/// Flag to check if transaction sent successfully over Tor [`Modal`].
tor_success: Arc<RwLock<bool>>,
/// Entered amount value for [`Modal`].
amount_edit: String,
/// Entered address value for [`Modal`].
address_edit: String,
/// Flag to check if entered address is incorrect at [`Modal`].
address_error: bool,
/// Flag to check if [`Modal`] was just opened to focus on first field.
modal_just_opened: bool,
}
impl Default for WalletTransport {
fn default() -> Self {
Self {
tor_sending: Arc::new(RwLock::new(false)),
tor_send_error: Arc::new(RwLock::new(false)),
tor_success: Arc::new(RwLock::new(false)),
amount_edit: "".to_string(),
address_edit: "".to_string(),
address_error: false,
modal_just_opened: false,
}
}
}
impl WalletTab for WalletTransport {
fn get_type(&self) -> WalletTabType {
@@ -33,11 +70,14 @@ impl WalletTab for WalletTransport {
ui: &mut egui::Ui,
_: &mut eframe::Frame,
wallet: &mut Wallet,
_: &dyn PlatformCallbacks) {
cb: &dyn PlatformCallbacks) {
if WalletContent::sync_ui(ui, wallet) {
return;
}
// Show modal content for this ui container.
self.modal_content_ui(ui, wallet, cb);
// Show transport content panel.
egui::CentralPanel::default()
.frame(egui::Frame {
@@ -52,14 +92,478 @@ impl WalletTab for WalletTransport {
..Default::default()
})
.show_inside(ui, |ui| {
self.transport_ui(ui, wallet);
ScrollArea::vertical()
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
.id_source(Id::from("wallet_transport").with(wallet.get_config().id))
.auto_shrink([false; 2])
.show(ui, |ui| {
ui.vertical_centered(|ui| {
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
self.ui(ui, wallet, cb);
});
});
});
});
}
}
impl WalletTransport {
/// Draw transport content.
pub fn transport_ui(&self, ui: &mut egui::Ui, wallet: &mut Wallet) {
/// Identifier for [`Modal`] to send amount over Tor.
const SEND_TOR_MODAL: &'static str = "send_tor_modal";
impl WalletTransport {
/// Draw wallet transport content.
pub fn ui(&mut self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
ui.add_space(3.0);
ui.label(RichText::new(t!("transport.desc"))
.size(16.0)
.color(Colors::INACTIVE_TEXT));
ui.add_space(7.0);
// Draw Tor content.
self.tor_ui(ui, wallet, cb);
}
/// Draw [`Modal`] content for this ui container.
fn modal_content_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
cb: &dyn PlatformCallbacks) {
match Modal::opened() {
None => {}
Some(id) => {
match id {
SEND_TOR_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.send_tor_modal_ui(ui, wallet, modal, cb);
});
}
_ => {}
}
}
}
}
/// Draw Tor transport content.
fn tor_ui(&mut self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
// Draw header content.
self.tor_header_ui(ui, wallet, cb);
// Draw receive info content.
if wallet.slatepack_address().is_some() {
self.tor_receive_ui(ui, wallet, cb);
}
// Draw send content.
self.tor_send_ui(ui, wallet, cb);
}
/// Draw Tor transport header content.
fn tor_header_ui(&self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
// Setup layout size.
let mut rect = ui.available_rect_before_wrap();
rect.set_height(78.0);
// Draw round background.
let bg_rect = rect.clone();
let item_rounding = View::item_rounding(0, 2, false);
ui.painter().rect(bg_rect, item_rounding, Colors::BUTTON, View::ITEM_STROKE);
ui.vertical(|ui| {
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Draw button to setup Tor transport.
let button_rounding = View::item_rounding(0, 2, true);
View::item_button(ui, button_rounding, GEAR_SIX, None, || {
//TODO: tor settings
});
// Draw button to enable/disable Tor listener for current wallet.
let service_id = &wallet.identifier();
if !Tor::is_service_running(service_id) &&
wallet.foreign_api_port().is_some() {
View::item_button(ui, Rounding::default(), POWER, Some(Colors::GREEN), || {
if let Ok(key) = wallet.secret_key() {
Tor::start_service(wallet.foreign_api_port().unwrap(), key, service_id);
}
});
} else if !Tor::is_service_starting(service_id) {
View::item_button(ui, Rounding::default(), POWER, Some(Colors::RED), || {
Tor::stop_service(service_id);
});
}
let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
ui.add_space(6.0);
ui.vertical(|ui| {
ui.add_space(3.0);
ui.label(RichText::new(t!("transport.tor_network"))
.size(18.0)
.color(Colors::TITLE));
// Setup wallet API address text.
let port = wallet.foreign_api_port().unwrap();
let address_text = format!("{} http://127.0.0.1:{}", GLOBE_SIMPLE, port);
ui.label(RichText::new(address_text).size(15.0).color(Colors::TEXT));
ui.add_space(1.0);
// Setup Tor status text.
let is_running = Tor::is_service_running(service_id);
let is_starting = Tor::is_service_starting(service_id);
let has_error = Tor::is_service_failed(service_id);
let (icon, text) = if is_starting {
(DOTS_THREE_CIRCLE, t!("transport.connecting"))
} else if has_error {
(WARNING_CIRCLE, t!("transport.conn_error"))
} else if is_running {
(CHECK_CIRCLE, t!("transport.connected"))
} else {
(X_CIRCLE, t!("transport.disconnected"))
};
let status_text = format!("{} {}", icon, text);
ui.label(RichText::new(status_text).size(15.0).color(Colors::GRAY));
});
});
});
});
}
/// Draw Tor send content.
fn tor_receive_ui(&self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
let slatepack_addr = wallet.slatepack_address().unwrap();
let service_id = &wallet.identifier();
// Setup layout size.
let mut rect = ui.available_rect_before_wrap();
rect.set_height(52.0);
// Draw round background.
let bg_rect = rect.clone();
let item_rounding = View::item_rounding(1, 3, false);
ui.painter().rect(bg_rect, item_rounding, Colors::BUTTON, View::ITEM_STROKE);
ui.vertical(|ui| {
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Draw button to setup Tor transport.
let button_rounding = View::item_rounding(1, 3, true);
View::item_button(ui, button_rounding, QR_CODE, None, || {
//TODO: qr for address
});
// Show button to enable/disable Tor listener for current wallet.
View::item_button(ui, Rounding::default(), COPY, None, || {
cb.copy_string_to_buffer(slatepack_addr.clone());
});
let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
ui.add_space(6.0);
ui.vertical(|ui| {
ui.add_space(3.0);
// Show wallet Slatepack address.
let address_color = if Tor::is_service_starting(service_id) {
Colors::INACTIVE_TEXT
} else if Tor::is_service_running(service_id) {
Colors::GREEN
} else {
Colors::RED
};
View::ellipsize_text(ui, slatepack_addr, 15.0, address_color);
let address_label = format!("{} {}",
COMPUTER_TOWER,
t!("network_mining.address"));
ui.label(RichText::new(address_label).size(15.0).color(Colors::GRAY));
});
});
});
});
}
/// Draw Tor receive content.
fn tor_send_ui(&mut self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
// Setup layout size.
let mut rect = ui.available_rect_before_wrap();
rect.set_height(55.0);
// Draw round background.
let bg_rect = rect.clone();
let item_rounding = View::item_rounding(1, 2, false);
ui.painter().rect(bg_rect, item_rounding, Colors::FILL, View::ITEM_STROKE);
ui.vertical(|ui| {
ui.allocate_ui_with_layout(rect.size(), Layout::top_down(Align::Center), |ui| {
ui.add_space(7.0);
// Draw button to open sending modal.
let send_text = format!("{} {}", EXPORT, t!("wallets.send"));
View::button(ui, send_text, Colors::WHITE, || {
self.show_send_tor_modal(cb);
});
});
});
}
/// Show [`Modal`] to send over Tor.
fn show_send_tor_modal(&mut self, cb: &dyn PlatformCallbacks) {
// Setup modal values.
let mut w_send_err = self.tor_send_error.write().unwrap();
*w_send_err = false;
let mut w_sending = self.tor_sending.write().unwrap();
*w_sending = false;
let mut w_success = self.tor_success.write().unwrap();
*w_success = false;
self.modal_just_opened = true;
self.amount_edit = "".to_string();
self.address_edit = "".to_string();
self.address_error = false;
// Show modal.
Modal::new(SEND_TOR_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.send"))
.show();
cb.show_keyboard();
}
/// Check if error occurred during sending over Tor at [`Modal`].
fn has_tor_send_error(&self) -> bool {
let r_send_err = self.tor_send_error.read().unwrap();
r_send_err.clone()
}
/// Check if transaction is sending over Tor to show progress at [`Modal`].
fn tor_sending(&self) -> bool {
let r_sending = self.tor_sending.read().unwrap();
r_sending.clone()
}
/// Check if transaction sent over Tor with success at [`Modal`].
fn tor_success(&self) -> bool {
let r_success = self.tor_success.read().unwrap();
r_success.clone()
}
/// Draw amount input [`Modal`] content to send over Tor.
/// Draw amount input [`Modal`] content to send over Tor.
fn send_tor_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
let has_send_err = self.has_tor_send_error();
let sending = self.tor_sending();
if !has_send_err && !sending {
ui.vertical_centered(|ui| {
let data = wallet.get_data().unwrap();
let amount = amount_to_hr_string(data.info.amount_currently_spendable, true);
let enter_text = t!("wallets.enter_amount_send","amount" => amount);
ui.label(RichText::new(enter_text)
.size(17.0)
.color(Colors::GRAY));
});
ui.add_space(8.0);
// Draw amount text edit.
let amount_edit_id = Id::from(modal.id).with("amount").with(wallet.get_config().id);
let mut amount_edit_opts = TextEditOptions::new(amount_edit_id).h_center().no_focus();
let amount_edit_before = self.amount_edit.clone();
if self.modal_just_opened {
self.modal_just_opened = false;
amount_edit_opts.focus = true;
}
View::text_edit(ui, cb, &mut self.amount_edit, amount_edit_opts);
ui.add_space(8.0);
// Check value if input was changed.
if amount_edit_before != self.amount_edit {
if !self.amount_edit.is_empty() {
match amount_from_hr_string(self.amount_edit.as_str()) {
Ok(a) => {
if !self.amount_edit.contains(".") {
// To avoid input of several "0".
if a == 0 {
self.amount_edit = "0".to_string();
return;
}
} else {
// Check input after ".".
let parts = self.amount_edit.split(".").collect::<Vec<&str>>();
if parts.len() == 2 && parts[1].len() > 9 {
self.amount_edit = amount_edit_before;
return;
}
}
// Do not input amount more than balance in sending.
let b = wallet.get_data().unwrap().info.amount_currently_spendable;
if b < a {
self.amount_edit = amount_edit_before;
}
}
Err(_) => {
self.amount_edit = amount_edit_before;
}
}
}
}
// Show address error or input description.
ui.vertical_centered(|ui| {
if self.address_error {
ui.label(RichText::new(t!("transport.incorrect_addr_err"))
.size(17.0)
.color(Colors::RED));
} else {
ui.label(RichText::new(t!("transport.receiver_address"))
.size(17.0)
.color(Colors::GRAY));
}
});
ui.add_space(8.0);
// Draw address text edit.
let addr_edit_before = self.address_edit.clone();
let address_edit_id = Id::from(modal.id).with("address").with(wallet.get_config().id);
let address_edit_opts = TextEditOptions::new(address_edit_id)
.paste()
.scan_qr()
.no_focus();
View::text_edit(ui, cb, &mut self.address_edit, address_edit_opts);
ui.add_space(12.0);
// Check value if input was changed.
if addr_edit_before != self.address_edit {
self.address_error = false;
}
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
self.amount_edit = "".to_string();
self.address_edit = "".to_string();
cb.hide_keyboard();
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
View::button(ui, t!("continue"), Colors::WHITE, || {
if self.amount_edit.is_empty() {
return;
}
// Check entered address.
let addr_str = self.address_edit.as_str();
if let Ok(addr) = SlatepackAddress::try_from(addr_str) {
// Parse amount and send over Tor.
if let Ok(a) = amount_from_hr_string(self.amount_edit.as_str()) {
cb.hide_keyboard();
let mut w_sending = self.tor_sending.write().unwrap();
*w_sending = true;
{
let send_error = self.tor_send_error.clone();
let send_success = self.tor_success.clone();
let mut wallet = wallet.clone();
thread::spawn(move || {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
if wallet.send_tor(a, &addr).await.is_some() {
let mut w_send_success
= send_success.write().unwrap();
*w_send_success = true;
} else {
let mut w_send_error
= send_error.write().unwrap();
*w_send_error = true;
}
});
});
}
}
} else {
self.address_error = true;
}
});
});
});
ui.add_space(6.0);
} else if has_send_err {
ui.add_space(6.0);
ui.label(RichText::new(t!("transport.tor_send_error"))
.size(17.0)
.color(Colors::RED));
ui.add_space(12.0);
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
self.amount_edit = "".to_string();
self.address_edit = "".to_string();
cb.hide_keyboard();
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
View::button(ui, t!("repeat"), Colors::WHITE, || {
// Parse amount and send over Tor.
if let Ok(a) = amount_from_hr_string(self.amount_edit.as_str()) {
let mut w_send_error = self.tor_send_error.write().unwrap();
*w_send_error = false;
let mut w_sending = self.tor_sending.write().unwrap();
*w_sending = true;
{
let addr_text = self.address_edit.clone();
let send_error = self.tor_send_error.clone();
let send_success = self.tor_success.clone();
let mut wallet = wallet.clone();
thread::spawn(move || {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let addr_str = addr_text.as_str();
let addr = &SlatepackAddress::try_from(addr_str)
.unwrap();
if wallet.send_tor(a, &addr).await.is_some() {
let mut w_send_success
= send_success.write().unwrap();
*w_send_success = true;
} else {
let mut w_send_error
= send_error.write().unwrap();
*w_send_error = true;
}
});
});
}
}
});
});
});
} else {
ui.add_space(16.0);
ui.vertical_centered(|ui| {
View::small_loading_spinner(ui);
ui.add_space(12.0);
ui.label(RichText::new(t!("transport.tor_sending"))
.size(17.0)
.color(Colors::TEXT));
});
ui.add_space(10.0);
// Close modal on success sending.
if self.tor_success() {
modal.close();
}
}
}
}