Skip to content

Commit

Permalink
add support for Hot wallet with trezor device
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Aug 12, 2024
1 parent 855cfce commit 2bfa44c
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 149 deletions.
72 changes: 69 additions & 3 deletions node-gui/src/backend/backend_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,37 @@ impl Backend {
(wallet_data, accounts_info, best_block)
}
#[cfg(feature = "trezor")]
(WalletType::Trezor, _) => {
(WalletType::Trezor, ColdHotNodeController::Hot(controller)) => {
let handles_client = WalletHandlesClient::new(
controller.chainstate.clone(),
controller.mempool.clone(),
controller.block_prod.clone(),
controller.p2p.clone(),
)
.await
.map_err(|e| BackendError::WalletError(e.to_string()))?;

let (wallet_rpc, command_handler, best_block, accounts_info, accounts_data) = self
.create_wallet(
handles_client,
file_path.clone(),
wallet_args,
import,
wallet_events,
)
.await?;

let wallet_data = WalletData {
controller: GuiHotColdController::Hot(wallet_rpc, command_handler),
accounts: accounts_data,
best_block,
updated: false,
};

(wallet_data, accounts_info, best_block)
}
#[cfg(feature = "trezor")]
(WalletType::Trezor, ColdHotNodeController::Cold) => {
let client = make_cold_wallet_rpc_client(Arc::clone(&self.chain_config));

let (wallet_rpc, command_handler, best_block, accounts_info, accounts_data) = self
Expand Down Expand Up @@ -398,7 +428,7 @@ impl Backend {
let wallet_rpc = WalletRpc::new(wallet_handle, node_rpc.clone(), chain_config.clone());

wallet_rpc
.create_wallet(file_path, wallet_args, import.skip_syncing())
.create_wallet(file_path, wallet_args, true, import.scan_blokchain())
.await
.map_err(|err| BackendError::WalletError(err.to_string()))?;
tokio::spawn(forward_events(
Expand Down Expand Up @@ -510,7 +540,43 @@ impl Backend {
(wallet_data, accounts_info, best_block, encryption_state)
}
#[cfg(feature = "trezor")]
(WalletType::Trezor, _) => {
(WalletType::Trezor, ColdHotNodeController::Hot(controller)) => {
let handles_client = WalletHandlesClient::new(
controller.chainstate.clone(),
controller.mempool.clone(),
controller.block_prod.clone(),
controller.p2p.clone(),
)
.await
.map_err(|e| BackendError::WalletError(e.to_string()))?;

let (
wallet_rpc,
command_handler,
encryption_state,
best_block,
accounts_info,
accounts_data,
) = self
.open_wallet(
handles_client,
file_path.clone(),
wallet_events,
Some(HardwareWalletType::Trezor),
)
.await?;

let wallet_data = WalletData {
controller: GuiHotColdController::Hot(wallet_rpc, command_handler),
accounts: accounts_data,
best_block,
updated: false,
};

(wallet_data, accounts_info, best_block, encryption_state)
}
#[cfg(feature = "trezor")]
(WalletType::Trezor, ColdHotNodeController::Cold) => {
let client = make_cold_wallet_rpc_client(Arc::clone(&self.chain_config));

let (
Expand Down
9 changes: 0 additions & 9 deletions node-gui/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,6 @@ pub async fn node_initialize(
});
(chain_config, chain_info)
}
#[cfg(feature = "trezor")]
WalletMode::Trezor => spawn_cold_backend(
network,
event_tx,
request_rx,
low_priority_event_tx,
wallet_updated_tx,
wallet_updated_rx,
),
WalletMode::Cold => spawn_cold_backend(
network,
event_tx,
Expand Down
21 changes: 0 additions & 21 deletions node-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ use tokio::sync::mpsc::UnboundedReceiver;
const COLD_WALLET_TOOLTIP_TEXT: &str =
"Start the wallet in Cold mode without connecting to the network or any nodes. The Cold mode is made to run the wallet on an air-gapped machine without internet connection for storage of keys of high-value. For example, pool decommission keys.";
const HOT_WALLET_TOOLTIP_TEXT: &str = "Start the wallet in Hot mode and connect to the network.";
#[cfg(feature = "trezor")]
const TREZOR_WALLET_TOOLTIP_TEXT: &str =
"Start the wallet in Trezor mode and connect to a Trezor hardware wallet.";

const MAIN_NETWORK_TOOLTIP: &str = "The 'Mainnet' is the main network that has coins with value.";
const TEST_NETWORK_TOOLTIP: &str = "The 'Testnet' is the network with coins that have no value, but is used for testing various applications before deploying them on Mainnet.";
Expand Down Expand Up @@ -73,8 +70,6 @@ pub enum InitNetwork {
pub enum WalletMode {
Cold,
Hot,
#[cfg(feature = "trezor")]
Trezor,
}

#[derive(Debug)]
Expand Down Expand Up @@ -365,22 +360,6 @@ impl Application for MintlayerNodeGUI {
.align_items(iced::Alignment::Center)
.spacing(5);

#[cfg(feature = "trezor")]
let error_box = {
let trezor_row = row![
iced::widget::button(text("Trezor")).on_press(WalletMode::Trezor),
tooltip(
Text::new(iced_aw::Bootstrap::Question.to_string())
.font(iced_aw::BOOTSTRAP_FONT),
TREZOR_WALLET_TOOLTIP_TEXT,
tooltip::Position::Bottom
)
.gap(10)
.style(iced::theme::Container::Box)
];
error_box.push(trezor_row)
};

let res: Element<WalletMode> = container(error_box)
.width(Length::Fill)
.height(Length::Fill)
Expand Down
58 changes: 36 additions & 22 deletions node-gui/src/main_window/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn make_menu_file<'a>(wallet_mode: WalletMode) -> Item<'a, MenuMessage, Theme, i
labeled_button("File", MenuMessage::NoOp),
Menu::new(match wallet_mode {
WalletMode::Hot => {
vec![
let menu = vec![
menu_item(
"Create new Hot wallet",
MenuMessage::CreateNewWallet {
Expand All @@ -130,7 +130,41 @@ fn make_menu_file<'a>(wallet_mode: WalletMode) -> Item<'a, MenuMessage, Theme, i
// TODO: enable setting when needed
// menu_item("Settings", MenuMessage::NoOp),
menu_item("Exit", MenuMessage::Exit),
]
];
#[cfg(feature = "trezor")]
{
let mut menu = menu;
menu.insert(
1,
menu_item(
"Create new Trezor wallet",
MenuMessage::CreateNewWallet {
wallet_type: WalletType::Trezor,
},
),
);
menu.insert(
3,
menu_item(
"Recover from Trezor wallet",
MenuMessage::RecoverWallet {
wallet_type: WalletType::Trezor,
},
),
);
menu.insert(
5,
menu_item(
"Open Trezor wallet",
MenuMessage::OpenWallet {
wallet_type: WalletType::Trezor,
},
),
);
menu
}
#[cfg(not(feature = "trezor"))]
menu
}
WalletMode::Cold => {
vec![
Expand All @@ -157,26 +191,6 @@ fn make_menu_file<'a>(wallet_mode: WalletMode) -> Item<'a, MenuMessage, Theme, i
menu_item("Exit", MenuMessage::Exit),
]
}
#[cfg(feature = "trezor")]
WalletMode::Trezor => {
vec![
menu_item(
"Create new Trezor wallet",
MenuMessage::CreateNewWallet {
wallet_type: WalletType::Trezor,
},
),
menu_item(
"Open Trezor wallet",
MenuMessage::OpenWallet {
wallet_type: WalletType::Trezor,
},
),
// TODO: enable setting when needed
// menu_item("Settings", MenuMessage::NoOp),
menu_item("Exit", MenuMessage::Exit),
]
}
})
.width(260),
);
Expand Down
6 changes: 0 additions & 6 deletions node-gui/src/main_window/main_widget/tabs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ impl TabsWidget {
self.cold_wallet_tab.tab_label(),
self.cold_wallet_tab.view(node_state),
),
#[cfg(feature = "trezor")]
WalletMode::Trezor => tabs.push(
TabIndex::Summary as usize,
self.cold_wallet_tab.tab_label(),
self.cold_wallet_tab.view(node_state),
),
};
// TODO: enable settings tab when needed
//.push(
Expand Down
24 changes: 22 additions & 2 deletions node-gui/src/main_window/main_widget/tabs/wallet/left_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ pub fn view_left_panel(

// `next_height` is used to prevent flickering when a new block is found
let show_scan_progress = match wallet_info.wallet_type {
#[cfg(feature = "trezor")]
WalletType::Trezor => false,
WalletType::Cold => false,
#[cfg(feature = "trezor")]
WalletType::Trezor => {
wallet_info.best_block.1.next_height() < node_state.chain_info.best_block_height
}
WalletType::Hot => {
wallet_info.best_block.1.next_height() < node_state.chain_info.best_block_height
}
Expand Down Expand Up @@ -180,12 +182,30 @@ pub fn view_left_panel(
#[cfg(feature = "trezor")]
WalletType::Trezor => {
column![
panel_button(
"Transactions",
SelectedPanel::Transactions,
selected_panel,
TRANSACTIONS_TOOLTIP_TEXT
),
panel_button(
"Addresses",
SelectedPanel::Addresses,
selected_panel,
ADDRESSES_TOOLTIP_TEXT
),
panel_button(
"Send",
SelectedPanel::Send,
selected_panel,
SEND_TOOLTIP_TEXT
),
panel_button(
"Delegation",
SelectedPanel::Delegation,
selected_panel,
DELEGATION_TOOLTIP_TEXT
),
panel_button(
"Console",
SelectedPanel::Console,
Expand Down
41 changes: 28 additions & 13 deletions node-gui/src/main_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ pub enum ImportOrCreate {
}

impl ImportOrCreate {
pub fn skip_syncing(&self) -> bool {
pub fn scan_blokchain(&self) -> bool {
match self {
Self::Create => true,
Self::Import => false,
Self::Create => false,
Self::Import => true,
}
}
}
Expand Down Expand Up @@ -824,23 +824,38 @@ impl MainWindow {
wallet_type,
}),
Box::new(|| MainWindowMessage::CloseDialog),
ImportOrCreate::Create,
)
.into(),
}
}

ActiveDialog::WalletRecover { wallet_type } => {
let wallet_type = *wallet_type;
wallet_mnemonic_dialog(
None,
Box::new(move |mnemonic| MainWindowMessage::ImportWalletMnemonic {
args: WalletArgs::Software { mnemonic },
import: ImportOrCreate::Import,
wallet_type,
}),
Box::new(|| MainWindowMessage::CloseDialog),
)
.into()
// FIXME
match wallet_type {
WalletType::Hot | WalletType::Cold => wallet_mnemonic_dialog(
None,
Box::new(move |mnemonic| MainWindowMessage::ImportWalletMnemonic {
args: WalletArgs::Software { mnemonic },
import: ImportOrCreate::Import,
wallet_type,
}),
Box::new(|| MainWindowMessage::CloseDialog),
)
.into(),
#[cfg(feature = "trezor")]
WalletType::Trezor => hw_wallet_create_dialog(
Box::new(move || MainWindowMessage::ImportWalletMnemonic {
args: WalletArgs::Trezor,
import: ImportOrCreate::Import,
wallet_type,
}),
Box::new(|| MainWindowMessage::CloseDialog),
ImportOrCreate::Import,
)
.into(),
}
}

ActiveDialog::WalletSetPassword { wallet_id } => {
Expand Down
Loading

0 comments on commit 2bfa44c

Please sign in to comment.