2018-10-04 20:56:50 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2019-03-18 17:23:40 +00:00
|
|
|
import { colors } from 'trezor-ui-components';
|
2018-08-21 15:17:13 +00:00
|
|
|
|
2018-10-11 11:07:29 +00:00
|
|
|
import type { Device } from 'trezor-connect';
|
2019-03-04 12:33:02 +00:00
|
|
|
import type { TrezorDevice, State } from 'flowtype';
|
2018-10-04 20:56:50 +00:00
|
|
|
|
|
|
|
type Transport = $ElementType<$ElementType<State, 'connect'>, 'transport'>;
|
|
|
|
|
|
|
|
export const getStatus = (device: TrezorDevice): string => {
|
|
|
|
if (!device.connected) {
|
|
|
|
return 'disconnected';
|
|
|
|
}
|
|
|
|
if (device.type === 'acquired') {
|
|
|
|
if (device.mode === 'bootloader') {
|
|
|
|
return 'bootloader';
|
|
|
|
}
|
|
|
|
if (device.mode === 'initialize') {
|
|
|
|
return 'initialize';
|
|
|
|
}
|
2018-11-01 09:53:56 +00:00
|
|
|
if (device.mode === 'seedless') {
|
|
|
|
return 'seedless';
|
|
|
|
}
|
2018-10-04 20:56:50 +00:00
|
|
|
if (device.firmware === 'required') {
|
|
|
|
return 'firmware-required';
|
|
|
|
}
|
2018-08-21 15:56:30 +00:00
|
|
|
if (device.status === 'occupied') {
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'used-in-other-window';
|
|
|
|
}
|
|
|
|
if (device.status === 'used') {
|
|
|
|
return 'used-in-other-window';
|
|
|
|
}
|
|
|
|
if (device.firmware === 'outdated') {
|
|
|
|
return 'firmware-recommended';
|
2018-08-21 15:56:30 +00:00
|
|
|
}
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'connected';
|
2018-08-21 15:02:06 +00:00
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
if (!device.available) {
|
|
|
|
// deprecated
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'unavailable';
|
|
|
|
}
|
|
|
|
if (device.type === 'unacquired') {
|
|
|
|
return 'unacquired';
|
|
|
|
}
|
|
|
|
if (device.type === 'unreadable') {
|
|
|
|
return 'unreadable';
|
|
|
|
}
|
|
|
|
return 'unknown';
|
2018-08-21 15:02:06 +00:00
|
|
|
};
|
|
|
|
|
2018-10-04 20:56:50 +00:00
|
|
|
export const getStatusName = (deviceStatus: string): string => {
|
2018-08-21 15:17:13 +00:00
|
|
|
switch (deviceStatus) {
|
2018-08-21 15:02:06 +00:00
|
|
|
case 'connected':
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'Connected';
|
2018-08-21 15:02:06 +00:00
|
|
|
case 'disconnected':
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'Disconnected';
|
|
|
|
case 'bootloader':
|
|
|
|
return 'Connected (bootloader mode)';
|
|
|
|
case 'initialize':
|
|
|
|
return 'Connected (not initialized)';
|
2018-11-01 09:53:56 +00:00
|
|
|
case 'seedless':
|
|
|
|
return 'Connected (seedless mode)';
|
2018-10-04 20:56:50 +00:00
|
|
|
case 'firmware-required':
|
|
|
|
return 'Connected (update required)';
|
|
|
|
case 'firmware-recommended':
|
|
|
|
return 'Connected (update recommended)';
|
|
|
|
case 'used-in-other-window':
|
|
|
|
return 'Used in other window';
|
2018-08-23 09:52:55 +00:00
|
|
|
case 'unacquired':
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'Used in other window';
|
2018-08-21 15:02:06 +00:00
|
|
|
case 'unavailable':
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'Unavailable';
|
|
|
|
case 'unreadable':
|
|
|
|
return 'Unreadable';
|
2018-08-21 15:02:06 +00:00
|
|
|
default:
|
2018-10-04 20:56:50 +00:00
|
|
|
return 'Status unknown';
|
2018-08-21 15:02:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export const isWebUSB = (transport: Transport) => !!(transport.type && transport.type === 'webusb');
|
2018-08-21 15:17:13 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export const isDisabled = (
|
|
|
|
selectedDevice: TrezorDevice,
|
|
|
|
devices: Array<TrezorDevice>,
|
|
|
|
transport: Transport
|
|
|
|
) => {
|
2018-09-21 08:37:19 +00:00
|
|
|
if (isWebUSB(transport)) return false; // always enabled if webusb
|
|
|
|
if (devices.length < 1) return true; // no devices
|
|
|
|
if (devices.length === 1) {
|
|
|
|
if (!selectedDevice.features) return true; // unacquired, unreadable
|
2018-11-01 09:53:56 +00:00
|
|
|
if (selectedDevice.mode !== 'normal') return true; // bootloader, not initialized, seedless
|
|
|
|
if (selectedDevice.firmware === 'required') return true;
|
2018-09-21 08:37:19 +00:00
|
|
|
}
|
|
|
|
return false; // default
|
2018-09-27 13:51:33 +00:00
|
|
|
};
|
2018-08-21 15:17:13 +00:00
|
|
|
|
2018-10-04 20:56:50 +00:00
|
|
|
export const isDeviceAccessible = (device: ?TrezorDevice): boolean => {
|
|
|
|
if (!device || !device.features) return false;
|
|
|
|
return device.mode === 'normal' && device.firmware !== 'required';
|
|
|
|
};
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export const isSelectedDevice = (
|
|
|
|
selected: ?TrezorDevice,
|
|
|
|
device: ?(TrezorDevice | Device)
|
|
|
|
): boolean =>
|
|
|
|
!!(
|
|
|
|
selected &&
|
|
|
|
device &&
|
|
|
|
(selected.path === device.path && (device.ts && selected.instance === device.instance))
|
|
|
|
);
|
2018-10-04 20:56:50 +00:00
|
|
|
|
|
|
|
export const getVersion = (device: TrezorDevice): string => {
|
2018-08-22 10:59:22 +00:00
|
|
|
let version;
|
2018-08-21 15:02:06 +00:00
|
|
|
if (device.features && device.features.major_version > 1) {
|
|
|
|
version = 'T';
|
|
|
|
} else {
|
2019-02-20 11:17:28 +00:00
|
|
|
version = 'One';
|
2018-08-21 15:02:06 +00:00
|
|
|
}
|
|
|
|
return version;
|
|
|
|
};
|
|
|
|
|
2018-10-04 20:56:50 +00:00
|
|
|
export const getStatusColor = (deviceStatus: string): string => {
|
2018-08-21 15:17:13 +00:00
|
|
|
switch (deviceStatus) {
|
|
|
|
case 'connected':
|
2018-10-04 20:56:50 +00:00
|
|
|
return colors.GREEN_PRIMARY;
|
2018-08-21 15:17:13 +00:00
|
|
|
case 'disconnected':
|
2018-10-04 20:56:50 +00:00
|
|
|
return colors.ERROR_PRIMARY;
|
|
|
|
case 'bootloader':
|
|
|
|
case 'initialize':
|
2018-11-01 09:53:56 +00:00
|
|
|
case 'seedless':
|
2018-10-04 20:56:50 +00:00
|
|
|
case 'firmware-recommended':
|
|
|
|
case 'used-in-other-window':
|
|
|
|
case 'unacquired':
|
|
|
|
return colors.WARNING_PRIMARY;
|
|
|
|
case 'firmware-required':
|
2018-08-21 15:17:13 +00:00
|
|
|
case 'unavailable':
|
2018-10-04 20:56:50 +00:00
|
|
|
case 'unreadable':
|
|
|
|
return colors.ERROR_PRIMARY;
|
2018-08-21 15:17:13 +00:00
|
|
|
default:
|
2018-10-04 20:56:50 +00:00
|
|
|
return colors.TEXT_PRIMARY;
|
2018-08-21 15:17:13 +00:00
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|