Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hwiService.ts #16

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/services/hwiService.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
import { invoke } from "@tauri-apps/api/tauri";
import { HWIDevice, HWIDeviceType } from "../helpers/devices";

interface Result<T> {
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<HWIDevice[]> => {
const devices = await invoke<HWIDevice[]>("hwi_enumerate");
return devices
.filter((device) => !deviceType || device.device_type === deviceType)
const devices = await invoke<Result<HWIDevice>[]>("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<void> => {
Expand Down Expand Up @@ -59,6 +81,10 @@ const hwiService = {
await invoke<void>("emit_to_channel", { eventData });
},

promptPin: async (): Promise<void> => {
await invoke<void>("hwi_prompt_pin");
},

sendPin: async (pin: string): Promise<void> => {
await invoke<void>("hwi_send_pin", { pin });
},
Expand Down
Loading