From d757b6c5dd6b78841384c227e4d53ed5b12a9ade Mon Sep 17 00:00:00 2001 From: benk10 Date: Sun, 8 Sep 2024 11:04:57 +0300 Subject: [PATCH] Update hwiService.ts --- src/services/hwiService.ts | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/services/hwiService.ts b/src/services/hwiService.ts index 5a3f4b6..eeffe1f 100644 --- a/src/services/hwiService.ts +++ b/src/services/hwiService.ts @@ -1,21 +1,43 @@ import { invoke } from "@tauri-apps/api/tauri"; import { HWIDevice, HWIDeviceType } from "../helpers/devices"; +interface Result { + Ok: T; + Err: string; +} + +const emptyTrezorDevice: HWIDevice = { + device_type: "trezor", + needs_pin_sent: true, + model: "", + path: "", + needs_passphrase_sent: false, + fingerprint: null, +}; + const hwiService = { fetchDevices: async ( deviceType: HWIDeviceType | null = null, ): Promise => { - const devices = await invoke("hwi_enumerate"); - return devices - .filter((device) => !deviceType || device.device_type === deviceType) + const devices = await invoke[]>("hwi_enumerate"); + const updatedDevices = devices.map((device) => + device.Err && device.Err.includes("Trezor is locked") + ? { Ok: emptyTrezorDevice } + : device, + ); + return updatedDevices + .filter( + (device) => + !deviceType || (device.Ok && device.Ok.device_type === deviceType), + ) .map((device) => ({ - ...device, - device_type: device.device_type.toLowerCase() as HWIDeviceType, + ...device.Ok, + device_type: device.Ok.device_type.toLowerCase() as HWIDeviceType, })); }, setHWIClient: async ( - fingerprint: string, + fingerprint: string | null, deviceType: string, network: string, ): Promise => { @@ -59,6 +81,10 @@ const hwiService = { await invoke("emit_to_channel", { eventData }); }, + promptPin: async (): Promise => { + await invoke("hwi_prompt_pin"); + }, + sendPin: async (pin: string): Promise => { await invoke("hwi_send_pin", { pin }); },