Skip to content

Commit

Permalink
Merge #331: Make config editabe in Settings panel
Browse files Browse the repository at this point in the history
5408a5a Add load_config to Daemon trait (edouard)
392cdc8 Make config editable in settings panel (edouard)

Pull request description:

  close #21

ACKs for top commit:
  edouardparis:
    Self-ACK 5408a5a

Tree-SHA512: e868d810da0f4549624fa0024752bd25949cffba3f9b2dbeea4dff580ae4e80f67e885729222ccffc8fe38295a5bd2fd7a793260313d0554743caf0552442f01
  • Loading branch information
edouardparis committed Mar 25, 2022
2 parents c434c3a + 5408a5a commit c8c1318
Show file tree
Hide file tree
Showing 15 changed files with 1,554 additions and 501 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions src/app/context.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
use std::fs::OpenOptions;
use std::future::Future;
use std::io::Write;
use std::pin::Pin;
use std::sync::Arc;

use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;

use super::menu::Menu;
use crate::{app::config, conversion::Converter, daemon::Daemon, revault::Role};
use revaultd::config::Config as DaemonConfig;
use revaultd::revault_tx::miniscript::DescriptorPublicKey;

use revault_hwi::{app::revault::RevaultHWI, HWIError};

use crate::{
app::{config, error::Error, menu::Menu},
conversion::Converter,
daemon::Daemon,
revault::Role,
};

pub type HardwareWallet =
Box<dyn Future<Output = Result<Box<dyn RevaultHWI + Send>, HWIError>> + Send + Sync>;

Expand Down Expand Up @@ -113,6 +120,32 @@ impl Context {
false
}
}

pub fn load_daemon_config(&mut self, cfg: DaemonConfig) -> Result<(), Error> {
loop {
if let Some(daemon) = Arc::get_mut(&mut self.revaultd) {
daemon.load_config(cfg.clone())?;
break;
}
}

let mut daemon_config_file = OpenOptions::new()
.write(true)
.open(&self.config.gui.revaultd_config_path)
.map_err(|e| Error::ConfigError(e.to_string()))?;

let content =
toml::to_string(&self.config.daemon).map_err(|e| Error::ConfigError(e.to_string()))?;

daemon_config_file
.write_all(content.as_bytes())
.map_err(|e| {
log::warn!("failed to write to file: {:?}", e);
Error::ConfigError(e.to_string())
})?;

Ok(())
}
}

pub struct ConfigContext {
Expand Down
20 changes: 18 additions & 2 deletions src/app/message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use bitcoin::{util::psbt::PartiallySignedTransaction as Psbt, OutPoint};
use revault_hwi::{app::revault::RevaultHWI, HWIError};
use std::sync::Arc;

use bitcoin::{util::psbt::PartiallySignedTransaction as Psbt, OutPoint};
use tokio::sync::Mutex;

use revault_hwi::{app::revault::RevaultHWI, HWIError};
use revaultd::config::Config as DaemonConfig;

use crate::{
app::{error::Error, menu::Menu},
daemon::{
Expand Down Expand Up @@ -58,6 +61,19 @@ pub enum Message {
Close,
Revault,
Revaulted(Result<(), RevaultDError>),
Settings(usize, SettingsMessage),
AddWatchtower,
LoadDaemonConfig(DaemonConfig),
DaemonConfigLoaded(Result<(), Error>),
}

#[derive(Debug, Clone)]
pub enum SettingsMessage {
Remove,
Edit,
FieldEdited(&'static str, String),
CancelEdit,
ConfirmEdit,
}

#[derive(Debug, Clone)]
Expand Down
9 changes: 6 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use iced::{time, Clipboard, Command, Element, Subscription};
use iced_native::{window, Event};

pub use config::Config;
pub use message::Message;
pub use message::{Message, SettingsMessage};

use menu::Menu;
use state::{
Expand All @@ -31,14 +31,13 @@ pub struct App {
context: Context,
}

#[allow(unreachable_patterns)]
pub fn new_state(context: &Context) -> Box<dyn State> {
match (context.role, &context.menu) {
(_, Menu::Deposit) => DepositState::new().into(),
(_, Menu::History) => HistoryState::new().into(),
(_, Menu::Vaults(menu)) => VaultsState::new(menu).into(),
(_, Menu::RevaultVaults) => RevaultVaultsState::default().into(),
(_, Menu::Settings) => SettingsState::new(context.config.gui.clone()).into(),
(_, Menu::Settings) => SettingsState::new(context).into(),
(Role::Stakeholder, Menu::Home) => StakeholderHomeState::new().into(),
(Role::Stakeholder, Menu::CreateVaults) => StakeholderCreateVaultsState::new().into(),
(Role::Stakeholder, Menu::DelegateFunds) => StakeholderDelegateVaultsState::new().into(),
Expand Down Expand Up @@ -107,6 +106,10 @@ impl App {
}
Command::none()
}
Message::LoadDaemonConfig(cfg) => {
let res = self.context.load_daemon_config(cfg);
self.update(Message::DaemonConfigLoaded(res), clipboard)
}
Message::ChangeRole(role) => {
self.context.role = role;
self.state = new_state(&self.context);
Expand Down
Loading

0 comments on commit c8c1318

Please sign in to comment.