1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-28 03:08:30 +00:00

update utils

This commit is contained in:
Szymon Lesisz 2018-10-04 22:56:50 +02:00
parent 5a6dedb314
commit e9e55263a9
2 changed files with 86 additions and 69 deletions

View File

@ -21,7 +21,7 @@ export const getSelectedDevice = (state: State): ?TrezorDevice => {
return state.devices.find((d) => { return state.devices.find((d) => {
if (!d.features && d.path === locationState.device) { if (!d.features && d.path === locationState.device) {
return true; return true;
} if (d.features && d.features.bootloader_mode && d.path === locationState.device) { } if (d.mode === 'bootloader' && d.path === locationState.device) {
return true; return true;
} if (d.features && d.features.device_id === locationState.device && d.instance === instance) { } if (d.features && d.features.device_id === locationState.device && d.instance === instance) {
return true; return true;
@ -30,9 +30,6 @@ export const getSelectedDevice = (state: State): ?TrezorDevice => {
}); });
}; };
//
export const isSelectedDevice = (current: ?TrezorDevice, device: ?TrezorDevice): boolean => !!((current && device && (current.path === device.path && current.instance === device.instance)));
// find device by id and state // find device by id and state
export const findDevice = (devices: Array<TrezorDevice>, deviceId: string, deviceState: string /*, instance: ?number*/): ?TrezorDevice => devices.find((d) => { export const findDevice = (devices: Array<TrezorDevice>, deviceId: string, deviceState: string /*, instance: ?number*/): ?TrezorDevice => devices.find((d) => {
// TODO: && (instance && d.instance === instance) // TODO: && (instance && d.instance === instance)

View File

@ -1,64 +1,99 @@
/* @flow */
import colors from 'config/colors'; import colors from 'config/colors';
const getStatus = (device) => { import type {
let status = 'connected'; TrezorDevice,
if (device.features && device.features.bootloader_mode) { State,
status = 'connected-bootloader'; } from 'flowtype';
} else if (!device.connected) {
status = 'disconnected'; type Transport = $ElementType<$ElementType<State, 'connect'>, 'transport'>;
} else if (!device.available) {
status = 'unavailable'; export const getStatus = (device: TrezorDevice): string => {
} else if (device.type === 'acquired') { if (!device.connected) {
if (device.status === 'occupied') { return 'disconnected';
status = 'used-in-other-window'; }
if (device.type === 'acquired') {
if (device.mode === 'bootloader') {
return 'bootloader';
} }
} else if (device.type === 'unacquired') { if (device.mode === 'initialize') {
status = 'unacquired'; return 'initialize';
}
if (device.firmware === 'required') {
return 'firmware-required';
}
if (device.status === 'occupied') {
return 'used-in-other-window';
}
if (device.status === 'used') {
return 'used-in-other-window';
}
if (device.firmware === 'outdated') {
return 'firmware-recommended';
}
return 'connected';
} }
if (!device.available) { // deprecated
return status; return 'unavailable';
}
if (device.type === 'unacquired') {
return 'unacquired';
}
if (device.type === 'unreadable') {
return 'unreadable';
}
return 'unknown';
}; };
const getStatusName = (deviceStatus) => { export const getStatusName = (deviceStatus: string): string => {
let statusName;
switch (deviceStatus) { switch (deviceStatus) {
case 'used-in-other-window':
statusName = 'Used in other window';
break;
case 'connected': case 'connected':
statusName = 'Connected'; return 'Connected';
break;
case 'connected-bootloader':
statusName = 'Connected (bootloader mode)';
break;
case 'disconnected': case 'disconnected':
statusName = 'Disconnected'; return 'Disconnected';
break; case 'bootloader':
return 'Connected (bootloader mode)';
case 'initialize':
return 'Connected (not initialized)';
case 'firmware-required':
return 'Connected (update required)';
case 'firmware-recommended':
return 'Connected (update recommended)';
case 'used-in-other-window':
return 'Used in other window';
case 'unacquired': case 'unacquired':
statusName = 'Used in other window'; return 'Used in other window';
break;
case 'unavailable': case 'unavailable':
statusName = 'Unavailable'; return 'Unavailable';
break; case 'unreadable':
return 'Unreadable';
default: default:
statusName = 'Status unknown'; return 'Status unknown';
} }
return statusName;
}; };
const isWebUSB = transport => !!((transport && transport.version.indexOf('webusb') >= 0)); export const isWebUSB = (transport: Transport) => !!((transport.type && transport.version.indexOf('webusb') >= 0));
const isDisabled = (selectedDevice, devices, transport) => { export const isDisabled = (selectedDevice: TrezorDevice, devices: Array<TrezorDevice>, transport: Transport) => {
if (isWebUSB(transport)) return false; // always enabled if webusb if (isWebUSB(transport)) return false; // always enabled if webusb
if (devices.length < 1) return true; // no devices if (devices.length < 1) return true; // no devices
if (devices.length === 1) { if (devices.length === 1) {
if (!selectedDevice.features) return true; // unacquired, unreadable if (!selectedDevice.features) return true; // unacquired, unreadable
if (selectedDevice.features.bootloader_mode || !selectedDevice.features.initialized) return true; // bootlader, not initialized if (selectedDevice.mode !== 'normal') return true; // bootloader, not initialized
if (selectedDevice.firmware === 'required') return true; // bootloader, not initialized
} }
return false; // default return false; // default
}; };
const getVersion = (device) => { export const isDeviceAccessible = (device: ?TrezorDevice): boolean => {
if (!device || !device.features) return false;
return device.mode === 'normal' && device.firmware !== 'required';
};
export const isSelectedDevice = (current: ?TrezorDevice, device: ?TrezorDevice): boolean => !!((current && device && (current.path === device.path && current.instance === device.instance)));
export const getVersion = (device: TrezorDevice): string => {
let version; let version;
if (device.features && device.features.major_version > 1) { if (device.features && device.features.major_version > 1) {
version = 'T'; version = 'T';
@ -68,38 +103,23 @@ const getVersion = (device) => {
return version; return version;
}; };
const getStatusColor = (deviceStatus) => { export const getStatusColor = (deviceStatus: string): string => {
let color;
switch (deviceStatus) { switch (deviceStatus) {
case 'used-in-other-window':
color = colors.WARNING_PRIMARY;
break;
case 'connected': case 'connected':
color = colors.GREEN_PRIMARY; return colors.GREEN_PRIMARY;
break;
case 'connected-bootloader':
color = colors.WARNING_PRIMARY;
break;
case 'unacquired':
color = colors.WARNING_PRIMARY;
break;
case 'disconnected': case 'disconnected':
color = colors.ERROR_PRIMARY; return colors.ERROR_PRIMARY;
break; case 'bootloader':
case 'initialize':
case 'firmware-recommended':
case 'used-in-other-window':
case 'unacquired':
return colors.WARNING_PRIMARY;
case 'firmware-required':
case 'unavailable': case 'unavailable':
color = colors.ERROR_PRIMARY; case 'unreadable':
break; return colors.ERROR_PRIMARY;
default: default:
color = colors.TEXT_PRIMARY; return colors.TEXT_PRIMARY;
} }
return color;
};
export {
isWebUSB,
getStatus,
isDisabled,
getStatusName,
getVersion,
getStatusColor,
}; };