From 72ae82cc92544c8884046cab0beb186925f4be83 Mon Sep 17 00:00:00 2001 From: benk10 Date: Wed, 30 Oct 2024 15:23:14 +0400 Subject: [PATCH] Pass network on HWI enumerate --- .../hwi/implementations/binary_implementation.rs | 10 +++++++--- src-tauri/src/hwi/interface.rs | 8 ++++---- src-tauri/src/hwi/types.rs | 2 +- src-tauri/src/main.rs | 11 +++++++---- src/hooks/useDeviceActions.tsx | 14 ++++++++++++-- src/modals/DeviceActionModal/DeviceActionModal.tsx | 4 ++++ src/modals/ModalManager.tsx | 1 + src/modals/TrezorPinModal/TrezorPinModal.tsx | 5 ++++- src/services/hwiService.ts | 8 +++++++- 9 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/hwi/implementations/binary_implementation.rs b/src-tauri/src/hwi/implementations/binary_implementation.rs index 2dd3074..161dd13 100644 --- a/src-tauri/src/hwi/implementations/binary_implementation.rs +++ b/src-tauri/src/hwi/implementations/binary_implementation.rs @@ -26,9 +26,13 @@ pub struct BinaryHWIImplementation { } impl HWIImplementation for BinaryHWIImplementation { - fn enumerate() -> Result { - let output = - BinaryHWIImplementation::::run_hwi_command(None, false, None, vec!["enumerate"])?; + fn enumerate(chain: Option) -> Result { + let output = BinaryHWIImplementation::::run_hwi_command( + None, + false, + chain.as_ref(), + vec!["enumerate"], + )?; Ok(output.to_string()) } diff --git a/src-tauri/src/hwi/interface.rs b/src-tauri/src/hwi/interface.rs index f6b36db..e84667b 100644 --- a/src-tauri/src/hwi/interface.rs +++ b/src-tauri/src/hwi/interface.rs @@ -33,7 +33,7 @@ impl HWIClient { /// # use hwi::implementations::python_implementation::PythonHWIImplementation; /// # use hwi::error::Error; /// # fn main() -> Result<(), Error> { - /// let devices = HWIClient::::enumerate()?; + /// let devices = HWIClient::::enumerate(None)?; /// for device in devices { /// match device { /// Ok(d) => println!("I can see a {} here 😄", d.model), @@ -43,8 +43,8 @@ impl HWIClient { /// # Ok(()) /// # } /// ``` - pub fn enumerate() -> Result>, Error> { - let output = T::enumerate()?; + pub fn enumerate(chain: Option) -> Result>, Error> { + let output = T::enumerate(chain)?; let devices_internal: Vec = deserialize_obj!(&output)?; Ok(devices_internal.into_iter().map(|d| d.try_into()).collect()) } @@ -59,7 +59,7 @@ impl HWIClient { /// # use hwi::error::Error; /// # use hwi::implementations::python_implementation::PythonHWIImplementation; /// # fn main() -> Result<(), Error> { - /// let devices = HWIClient::::enumerate()?; + /// let devices = HWIClient::::enumerate(None)?; /// for device in devices { /// let device = device?; /// let client = HWIClient::::get_client( diff --git a/src-tauri/src/hwi/types.rs b/src-tauri/src/hwi/types.rs index 7f0013a..4f928cc 100644 --- a/src-tauri/src/hwi/types.rs +++ b/src-tauri/src/hwi/types.rs @@ -277,7 +277,7 @@ pub enum HWIWordCount { } pub trait HWIImplementation { - fn enumerate() -> Result; + fn enumerate(chain: Option) -> Result; fn get_client(device: &HWIDevice, expert: bool, chain: HWIChain) -> Result where Self: Sized; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 72a5c1d..270bde5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -11,7 +11,7 @@ use device::get_xpub; use hwi::error::Error; use hwi::implementations::binary_implementation::BinaryHWIImplementation; use hwi::interface::HWIClient; -use hwi::types::{HWIBinaryExecutor, HWIDevice, HWIDeviceType}; +use hwi::types::{HWIBinaryExecutor, HWIChain, HWIDevice, HWIDeviceType}; #[cfg(target_os = "linux")] use log::warn; use serde_json::{json, Value}; @@ -98,8 +98,11 @@ fn emit_to_channel(state: State<'_, AppState>, event_data: Value) -> Result<(), // ==================== HWI Commands ==================== #[tauri::command] -async fn hwi_enumerate(_: State<'_, AppState>) -> Result>, String> { - HWIAppClient::enumerate() +async fn hwi_enumerate( + _: State<'_, AppState>, + network: Option, +) -> Result>, String> { + HWIAppClient::enumerate(network.map(HWIChain::from)) .map(|devices| { devices .into_iter() @@ -312,7 +315,7 @@ pub struct HWIBinaryExecutorImpl; impl HWIBinaryExecutor for HWIBinaryExecutorImpl { fn execute_command(args: Vec) -> Result { let mut args = args; - // TODO: Downgrade binaries to HWI 2.x and remove this flag + args.insert(0, "--emulators".to_string()); let output = Command::new_sidecar("hwi") diff --git a/src/hooks/useDeviceActions.tsx b/src/hooks/useDeviceActions.tsx index 6707449..7b747ab 100644 --- a/src/hooks/useDeviceActions.tsx +++ b/src/hooks/useDeviceActions.tsx @@ -1,8 +1,14 @@ import { useState } from "react"; import hwiService from "../services/hwiService"; -import { HWI_ACTION, HWIDevice, HWIDeviceType } from "../helpers/devices"; +import { + HWI_ACTION, + HWIDevice, + HWIDeviceType, + NetworkType, +} from "../helpers/devices"; interface UseDeviceActionsProps { + network: NetworkType | null; deviceType: HWIDeviceType; actionType: HWI_ACTION; psbt: string | null; @@ -14,6 +20,7 @@ interface UseDeviceActionsProps { } export const useDeviceActions = ({ + network, deviceType, actionType, psbt, @@ -31,7 +38,10 @@ export const useDeviceActions = ({ try { switch (actionType) { case "connect": { - const devices = await hwiService.fetchDevices(deviceType); + const devices = await hwiService.fetchDevices( + deviceType, + network?.toLowerCase(), + ); await onConnectResult(devices); break; } diff --git a/src/modals/DeviceActionModal/DeviceActionModal.tsx b/src/modals/DeviceActionModal/DeviceActionModal.tsx index 52255b7..c870fd4 100644 --- a/src/modals/DeviceActionModal/DeviceActionModal.tsx +++ b/src/modals/DeviceActionModal/DeviceActionModal.tsx @@ -5,6 +5,7 @@ import { HWI_DEVICES, HWIDevice, HWIDeviceType, + NetworkType, } from "../../helpers/devices"; import styles from "./DeviceActionModal.module.css"; import baseStyles from "../BaseModal/BaseModal.module.css"; @@ -15,6 +16,7 @@ import { useDeviceActions } from "../../hooks/useDeviceActions"; interface DeviceActionModalProps { isOpen: boolean; onClose: () => void; + network: NetworkType | null; deviceType: HWIDeviceType; actionType: HWI_ACTION; psbt: string | null; @@ -37,6 +39,7 @@ const actionTitle = (deviceType: HWIDeviceType) => ({ const DeviceActionModal = ({ isOpen, onClose, + network, deviceType, actionType, psbt, @@ -47,6 +50,7 @@ const DeviceActionModal = ({ onError, }: DeviceActionModalProps) => { const { isLoading, handleContinue } = useDeviceActions({ + network, deviceType, actionType, psbt, diff --git a/src/modals/ModalManager.tsx b/src/modals/ModalManager.tsx index 1a74d5b..ab4787a 100644 --- a/src/modals/ModalManager.tsx +++ b/src/modals/ModalManager.tsx @@ -54,6 +54,7 @@ const ModalsManager = ({ => { - const devices = await invoke[]>("hwi_enumerate"); + if (network === "mainnet") { + network = "bitcoin"; + } + const devices = await invoke[]>("hwi_enumerate", { + network, + }); const updatedDevices = devices.map((device) => device.Err && device.Err.includes("Trezor is locked") ? { Ok: emptyTrezorDevice }