Skip to content

Commit

Permalink
wip - misc fixes, mainly to match rusty-kaspa updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Dec 14, 2023
1 parent 5654fd9 commit f0baf5a
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 101 deletions.
17 changes: 12 additions & 5 deletions core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use cfg_if::cfg_if;
use kaspa_ng_core::runtime;
use kaspa_ng_core::settings::Settings;
use kaspa_wallet_core::api::WalletApi;
use std::iter::once;
use std::sync::Arc;
use workflow_i18n::*;
use workflow_log::*;

// #[allow(unused)]
pub const KASPA_NG_ICON_SVG: &[u8] = include_bytes!("../../resources/images/kaspa.svg");
pub const KASPA_NG_LOGO_SVG: &[u8] = include_bytes!("../../resources/images/kaspa-ng.svg");
pub const I18N_EMBEDDED: &str = include_str!("../../resources/i18n/i18n.json");
Expand All @@ -32,7 +32,6 @@ cfg_if! {
MINIMUM_DAEMON_SOFT_FD_LIMIT
};
use kaspad_lib::args::Args as NodeArgs;
use kaspad_lib::args::parse_args as parse_kaspad_args;
use kaspa_utils::fd_budget;
use kaspa_core::signals::Signals;
use clap::ArgAction;
Expand Down Expand Up @@ -60,16 +59,24 @@ cfg_if! {
fn parse_args() -> Args {
#[allow(unused)]
use clap::{arg, command, Arg, Command};
use std::env::{args,var};

if std::env::args().any(|arg| arg == "--daemon") || std::env::var("KASPA_NG_DAEMON").is_ok() {
Args::Kaspad { args : Box::new(parse_kaspad_args()) }
if args().any(|arg| arg == "--daemon") || var("KASPA_NG_DAEMON").is_ok() {
let args = once("kaspad".to_string()).chain(args().skip(1).filter(|arg| arg != "--daemon"));//.collect::<Vec<String>>();
match NodeArgs::parse(args) {
Ok(args) => Args::Kaspad { args : Box::new(args) },
Err(err) => {
println!("{err}");
std::process::exit(1);
}
}
} else {

let cmd = Command::new("kaspa-ng")

.about(format!("kaspa-ng v{VERSION}-{GIT_DESCRIBE} (rusty-kaspa v{})", kaspa_wallet_core::version()))
.arg(arg!(--disable "Disable node services when starting"))
.arg(arg!(--daemon "Run as Rusty Kaspa p2p daemon"))
.arg(arg!(--daemon "Run as Rusty Kaspa p2p daemon (kaspad)"))
.arg(
Arg::new("reset-settings")
.long("reset-settings")
Expand Down
41 changes: 24 additions & 17 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::mobile::MobileMenu;
use egui::load::Bytes;
use egui_notify::Toasts;
use kaspa_metrics_core::MetricsSnapshot;
use kaspa_wallet_core::api::TransactionDataGetResponse;
use kaspa_wallet_core::api::TransactionsDataGetResponse;
use kaspa_wallet_core::events::Events as CoreWallet;
use kaspa_wallet_core::storage::{Binding, Hint, PrvKeyDataInfo};
use std::borrow::Cow;
Expand All @@ -25,7 +25,7 @@ pub struct Core {
application_events_channel: ApplicationEventsChannel,
deactivation: Option<Module>,
module: Module,
stack: VecDeque<Module>,
pub stack: VecDeque<Module>,
modules: HashMap<TypeId, Module>,
pub settings: Settings,
pub toasts: Toasts,
Expand Down Expand Up @@ -189,17 +189,6 @@ impl Core {

this.wallet_update_list();

#[cfg(not(target_arch = "wasm32"))]
spawn(async move {
use workflow_core::task::sleep;

loop {
println!("Checking version...");
let _ = check_version().await;
sleep(Duration::from_secs(60 * 60 * 6)).await;
}
});

this
}

Expand Down Expand Up @@ -234,11 +223,24 @@ impl Core {
}

pub fn back(&mut self) {
if let Some(module) = self.stack.pop_back() {
self.module = module;
if self.state().is_open() {
if let Some(module) = self.stack.pop_back() {
self.module = module;
}
} else {
while let Some(module) = self.stack.pop_back() {
if !module.secure() {
self.module = module;
return;
}
}
}
}

pub fn purge_secure_stack(&mut self) {
self.stack.retain(|module| !module.secure());
}

pub fn sender(&self) -> crate::runtime::channel::Sender<Events> {
self.application_events_channel.sender.clone()
}
Expand Down Expand Up @@ -725,6 +727,7 @@ impl Core {
self.prv_key_data_map = Some(prv_key_data_map);
}
}
CoreWallet::AccountDeactivation { ids: _ } => {}
CoreWallet::AccountActivation { ids: _ } => {}
CoreWallet::AccountCreate { account_descriptor } => {
let account = Account::from(account_descriptor);
Expand Down Expand Up @@ -762,6 +765,8 @@ impl Core {
self.modules.clone().into_iter().for_each(|(_, module)| {
module.reset(self);
});

self.purge_secure_stack();
}
CoreWallet::AccountSelection { id: _ } => {}
CoreWallet::DAAScoreChange { current_daa_score } => {
Expand Down Expand Up @@ -890,6 +895,7 @@ impl Core {
.into_iter()
.map(|prv_key_data_info| (*prv_key_data_info.id(), prv_key_data_info))
.collect::<HashMap<_, _>>();

application_events_sender
.send(Events::PrvKeyDataInfo {
prv_key_data_info_map,
Expand All @@ -900,6 +906,7 @@ impl Core {
.iter()
.map(|account| account.id())
.collect::<Vec<_>>();

let account_map: HashMap<AccountId, Account> = account_list
.clone()
.into_iter()
Expand All @@ -911,7 +918,7 @@ impl Core {
.map(|account_id| {
runtime
.wallet()
.transaction_data_get_range(account_id, network_id, 0..128)
.transactions_data_get_range(account_id, network_id, 0..128)
})
.collect::<Vec<_>>();

Expand All @@ -922,7 +929,7 @@ impl Core {
.collect::<Result<Vec<_>>>()?;

transaction_data.into_iter().for_each(|data| {
let TransactionDataGetResponse {
let TransactionsDataGetResponse {
account_id,
transactions,
start: _,
Expand Down
1 change: 1 addition & 0 deletions core/src/egui/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl<'panel, Context> Panel<'panel, Context> {
ui.horizontal(|ui| {
match self.back {
Some(back) if self.back_enabled => {
println!("Back is enabled");
let icon = CompositeIcon::new(egui_phosphor::bold::ARROW_BEND_UP_LEFT)
.icon_size(icon_size.inner.x)
.padding(Some(icon_padding));
Expand Down
3 changes: 3 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub enum Error {

#[error(transparent)]
NetworkType(#[from] kaspa_consensus_core::network::NetworkTypeError),

#[error("Account creation error")]
AccountCreationError,
}

impl Error {
Expand Down
6 changes: 3 additions & 3 deletions core/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use kaspa_utils::{hashmap::GroupExtension, networking::ContextualNetAddress}
pub use kaspa_wallet_core::api;
pub use kaspa_wallet_core::api::WalletApi;
pub use kaspa_wallet_core::events::SyncState;
pub use kaspa_wallet_core::prelude::Address;
pub use kaspa_wallet_core::rpc::DynRpcApi;
pub use kaspa_wallet_core::runtime::{Account as KaspaAccount, Wallet as KaspaWallet};
pub use kaspa_wallet_core::runtime::{AccountDescriptor, AccountId, Balance};
Expand All @@ -17,7 +18,6 @@ pub use kaspa_wallet_core::storage::{
IdT, PrvKeyDataId, TransactionId, TransactionRecord, WalletDescriptor,
};
pub use kaspa_wallet_core::utils::*;
pub use kaspa_wallet_core::Address;
pub use kaspa_wrpc_client::{KaspaRpcClient, WrpcEncoding};

pub use async_trait::async_trait;
Expand Down Expand Up @@ -78,8 +78,8 @@ pub use crate::primitives::{
pub use crate::result::Result;
pub use crate::runtime::{runtime, spawn, spawn_with_result, Payload, Runtime, Service};
pub use crate::settings::{
KaspadNodeKind, NetworkInterfaceConfig, NetworkInterfaceKind, NodeSettings, PluginSettings,
PluginSettingsMap, RpcConfig, Settings, UserInterfaceSettings,
KaspadNodeKind, NetworkInterfaceConfig, NetworkInterfaceKind, NodeSettings, RpcConfig,
Settings, UserInterfaceSettings,
};
pub use crate::state::State;
pub use crate::status::Status;
Expand Down
21 changes: 17 additions & 4 deletions core/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,27 @@ impl<'core> Menu<'core> {
ui.close_menu();
}
ui.separator();
if ui.button("Wallet").clicked() {
if self.core.state().is_open() {

#[allow(clippy::collapsible_else_if)]
if self.core.state().is_open() {
if ui.button("Wallet").clicked() {
self.select::<modules::AccountManager>();
} else {
ui.close_menu();
}
} else {
if ui.button("Wallet").clicked() {
self.select::<modules::WalletOpen>();
ui.close_menu();
}
ui.close_menu();
}
// if ui.button("Wallet").clicked() {
// if self.core.state().is_open() {
// self.select::<modules::AccountManager>();
// } else {
// self.select::<modules::WalletOpen>();
// }
// ui.close_menu();
// }

ui.separator();
if ui.button("Metrics").clicked() {
Expand Down
30 changes: 17 additions & 13 deletions core/src/modules/account_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use crate::imports::*;
use kaspa_bip32::Mnemonic;
use kaspa_wallet_core::api::AccountsCreateRequest;
use kaspa_wallet_core::runtime::{AccountCreateArgs, PrvKeyDataCreateArgs, WalletCreateArgs};
use kaspa_wallet_core::storage::interface::AccessContext;
use kaspa_wallet_core::storage::{AccessContextT, AccountKind, PrvKeyDataInfo};
use kaspa_wallet_core::runtime::wallet::AccountCreateArgsBip32;
use kaspa_wallet_core::runtime::{AccountCreateArgs, PrvKeyDataCreateArgs, WalletCreateArgs, PrvKeyDataArgs};
use kaspa_wallet_core::storage::{AccountKind, PrvKeyDataInfo};


#[derive(Clone)]
Expand Down Expand Up @@ -347,30 +347,34 @@ impl ModuleT for AccountCreate {


// let prv_key_data_info = args.prv_key_data_info.clone().unwrap();
let account_create_result = Payload::<Result<AccountDescriptor>>::new("wallet_create_result");
let account_create_result = Payload::<Result<AccountDescriptor>>::new("account_create_result");
if !account_create_result.is_pending() {

let wallet = self.runtime.wallet().clone();
spawn_with_result(&account_create_result, async move {

let account_name = args.account_name.trim();
let account_name = (!account_name.is_empty()).then_some(account_name.to_string());
let account_kind = AccountKind::Bip32;
// let account_kind = AccountKind::Bip32;
let wallet_secret = Secret::from(args.wallet_secret);
let payment_secret = args.prv_key_data_info.as_ref().and_then(|secret| {
secret.requires_bip39_passphrase().then_some(Secret::from(args.payment_secret))
});

let prv_key_data_id = args.prv_key_data_info.as_ref().unwrap().id();
let prv_key_data_id = *args.prv_key_data_info.as_ref().unwrap().id();

let account_args = AccountCreateArgs {
account_name,
account_kind,
wallet_secret,
payment_secret,
};
let prv_key_data_args = PrvKeyDataArgs { prv_key_data_id, payment_secret };
let account_args = AccountCreateArgsBip32 { account_name, account_index: None };
let account_create_args = AccountCreateArgs::Bip32 { prv_key_data_args, account_args };
// {
// account_name,
// account_kind,
// wallet_secret,
// payment_secret,
// };

Ok(wallet.accounts_create(*prv_key_data_id, account_args).await?)
let account_descriptor = wallet.accounts_create(wallet_secret, account_create_args).await?;
Ok(account_descriptor)
});
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/modules/account_manager/menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl WalletMenu {
.auto_shrink([true; 2])
.show(ui, |ui| {

let wallet_list = core.wallet_list().clone();

let mut wallet_list = core.wallet_list().clone();
wallet_list.sort();
wallet_list.into_iter().for_each(|wallet_descriptor| {

let title = if let Some(title) = wallet_descriptor.title.clone() {
Expand Down
17 changes: 17 additions & 0 deletions core/src/modules/account_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ impl ModuleT for AccountManager {
self.state = AccountManagerState::Select;
}

fn secure(&self) -> bool {
true
}

// fn reload(&mut self, core : &mut Core) {
// if let AccountManagerState::Overview { account } = self.state.clone() {
// let account_id = account.id();
Expand Down Expand Up @@ -280,6 +284,8 @@ impl AccountManager {
match self.state.clone() {
AccountManagerState::Select => {

core.apply_mobile_style(ui);

if !core.state().is_open() {
core.select::<modules::WalletOpen>();
} else if let Some(account_collection) = core.account_collection() {
Expand Down Expand Up @@ -357,6 +363,17 @@ impl AccountManager {
ui.separator();
AccountMenu::new().render(core,ui,self,rc, screen_rect_height * 0.8);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {

if ui.add(Label::new(RichText::new(egui_phosphor::light::LOCK).size(18.)).sense(Sense::click())).clicked() {
let wallet = core.wallet().clone();
spawn(async move {
wallet.wallet_close().await?;
Ok(())
});
}

ui.separator();

ToolsMenu::new().render(core,ui,self, rc, screen_rect_height * 0.8);

ui.separator();
Expand Down
14 changes: 14 additions & 0 deletions core/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub trait ModuleT: Downcast {
false
}

fn secure(&self) -> bool {
false
}

fn style(&self) -> ModuleStyle {
// ModuleStyle::Large
ModuleStyle::Default
Expand Down Expand Up @@ -152,6 +156,10 @@ impl Module {
self.inner.module.borrow_mut().modal()
}

pub fn secure(&self) -> bool {
self.inner.module.borrow_mut().secure()
}

pub fn type_id(&self) -> TypeId {
self.inner.type_id
}
Expand Down Expand Up @@ -179,6 +187,12 @@ impl Module {
}
}

impl std::fmt::Debug for Module {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner.name)
}
}

impl<T> From<Rc<RefCell<T>>> for Module
where
T: ModuleT + 'static,
Expand Down
3 changes: 1 addition & 2 deletions core/src/modules/private_key_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use crate::imports::*;
use kaspa_bip32::Mnemonic;
use kaspa_wallet_core::runtime::{PrvKeyDataCreateArgs, WalletCreateArgs};
use kaspa_wallet_core::storage::interface::AccessContext;
use kaspa_wallet_core::storage::{AccessContextT, AccountKind};
use kaspa_wallet_core::storage::AccountKind;

pub enum MnemonicSize {
Words12,
Expand Down
Loading

0 comments on commit f0baf5a

Please sign in to comment.