Skip to content

Commit

Permalink
Pass network on HWI enumerate
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-kaufman committed Oct 30, 2024
1 parent 163530d commit 72ae82c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src-tauri/src/hwi/implementations/binary_implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ pub struct BinaryHWIImplementation<T: HWIBinaryExecutor> {
}

impl<T: HWIBinaryExecutor> HWIImplementation for BinaryHWIImplementation<T> {
fn enumerate() -> Result<String, Error> {
let output =
BinaryHWIImplementation::<T>::run_hwi_command(None, false, None, vec!["enumerate"])?;
fn enumerate(chain: Option<HWIChain>) -> Result<String, Error> {
let output = BinaryHWIImplementation::<T>::run_hwi_command(
None,
false,
chain.as_ref(),
vec!["enumerate"],
)?;
Ok(output.to_string())
}

Expand Down
8 changes: 4 additions & 4 deletions src-tauri/src/hwi/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<T: HWIImplementation> HWIClient<T> {
/// # use hwi::implementations::python_implementation::PythonHWIImplementation;
/// # use hwi::error::Error;
/// # fn main() -> Result<(), Error> {
/// let devices = HWIClient::<PythonHWIImplementation>::enumerate()?;
/// let devices = HWIClient::<PythonHWIImplementation>::enumerate(None)?;
/// for device in devices {
/// match device {
/// Ok(d) => println!("I can see a {} here 😄", d.model),
Expand All @@ -43,8 +43,8 @@ impl<T: HWIImplementation> HWIClient<T> {
/// # Ok(())
/// # }
/// ```
pub fn enumerate() -> Result<Vec<Result<HWIDevice, Error>>, Error> {
let output = T::enumerate()?;
pub fn enumerate(chain: Option<HWIChain>) -> Result<Vec<Result<HWIDevice, Error>>, Error> {
let output = T::enumerate(chain)?;
let devices_internal: Vec<HWIDeviceInternal> = deserialize_obj!(&output)?;
Ok(devices_internal.into_iter().map(|d| d.try_into()).collect())
}
Expand All @@ -59,7 +59,7 @@ impl<T: HWIImplementation> HWIClient<T> {
/// # use hwi::error::Error;
/// # use hwi::implementations::python_implementation::PythonHWIImplementation;
/// # fn main() -> Result<(), Error> {
/// let devices = HWIClient::<PythonHWIImplementation>::enumerate()?;
/// let devices = HWIClient::<PythonHWIImplementation>::enumerate(None)?;
/// for device in devices {
/// let device = device?;
/// let client = HWIClient::<PythonHWIImplementation>::get_client(
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/hwi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub enum HWIWordCount {
}

pub trait HWIImplementation {
fn enumerate() -> Result<String, Error>;
fn enumerate(chain: Option<HWIChain>) -> Result<String, Error>;
fn get_client(device: &HWIDevice, expert: bool, chain: HWIChain) -> Result<Self, Error>
where
Self: Sized;
Expand Down
11 changes: 7 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Vec<Result<HWIDevice, String>>, String> {
HWIAppClient::enumerate()
async fn hwi_enumerate(
_: State<'_, AppState>,
network: Option<bitcoin::Network>,
) -> Result<Vec<Result<HWIDevice, String>>, String> {
HWIAppClient::enumerate(network.map(HWIChain::from))
.map(|devices| {
devices
.into_iter()
Expand Down Expand Up @@ -312,7 +315,7 @@ pub struct HWIBinaryExecutorImpl;
impl HWIBinaryExecutor for HWIBinaryExecutorImpl {
fn execute_command(args: Vec<String>) -> Result<String, Error> {
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")
Expand Down
14 changes: 12 additions & 2 deletions src/hooks/useDeviceActions.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +20,7 @@ interface UseDeviceActionsProps {
}

export const useDeviceActions = ({
network,
deviceType,
actionType,
psbt,
Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/modals/DeviceActionModal/DeviceActionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -37,6 +39,7 @@ const actionTitle = (deviceType: HWIDeviceType) => ({
const DeviceActionModal = ({
isOpen,
onClose,
network,
deviceType,
actionType,
psbt,
Expand All @@ -47,6 +50,7 @@ const DeviceActionModal = ({
onError,
}: DeviceActionModalProps) => {
const { isLoading, handleContinue } = useDeviceActions({
network,
deviceType,
actionType,
psbt,
Expand Down
1 change: 1 addition & 0 deletions src/modals/ModalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const ModalsManager = ({
<DeviceActionModal
isOpen={openModal === "deviceAction"}
onClose={closeModalHandler}
network={network}
deviceType={deviceType as HWIDeviceType}
actionType={currentAction}
psbt={psbt}
Expand Down
5 changes: 4 additions & 1 deletion src/modals/TrezorPinModal/TrezorPinModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const TrezorPinModal = ({
setIsLoading(true);
try {
await hwiService.sendPin(pin);
const devices = await hwiService.fetchDevices("trezor");
const devices = await hwiService.fetchDevices(
"trezor",
network?.toLowerCase(),
);
await hwiService.setHWIClient(
devices[0].fingerprint,
"trezor",
Expand Down
8 changes: 7 additions & 1 deletion src/services/hwiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ const emptyTrezorDevice: HWIDevice = {
const hwiService = {
fetchDevices: async (
deviceType: HWIDeviceType | null = null,
network: string | null = null,
): Promise<HWIDevice[]> => {
const devices = await invoke<Result<HWIDevice>[]>("hwi_enumerate");
if (network === "mainnet") {
network = "bitcoin";
}
const devices = await invoke<Result<HWIDevice>[]>("hwi_enumerate", {
network,
});
const updatedDevices = devices.map((device) =>
device.Err && device.Err.includes("Trezor is locked")
? { Ok: emptyTrezorDevice }
Expand Down

0 comments on commit 72ae82c

Please sign in to comment.