1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 12:59:09 +00:00
trezor-wallet/src/js/actions/TrezorConnectActions.js

418 lines
14 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
import TrezorConnect, {
UI, DEVICE, DEVICE_EVENT, UI_EVENT, TRANSPORT_EVENT,
} from 'trezor-connect';
2018-04-11 10:13:38 +00:00
import * as TOKEN from './constants/token';
2018-02-20 09:30:36 +00:00
import * as CONNECT from './constants/TrezorConnect';
import * as NOTIFICATION from './constants/notification';
import * as WALLET from './constants/wallet';
2017-12-13 11:01:37 +00:00
import { push } from 'react-router-redux';
import * as DiscoveryActions from './DiscoveryActions';
2018-02-20 09:30:36 +00:00
import { resolveAfter } from '../utils/promiseUtils';
2018-04-16 21:19:50 +00:00
import type {
Device,
DeviceMessage,
UiMessage,
TransportMessage,
DeviceMessageType,
TransportMessageType,
2018-07-30 10:52:13 +00:00
UiMessageType,
2018-04-16 21:19:50 +00:00
} from 'trezor-connect';
2018-07-30 10:52:13 +00:00
import type {
2018-04-16 21:19:50 +00:00
Dispatch,
GetState,
Action,
2018-05-02 09:01:08 +00:00
ThunkAction,
2018-04-16 21:19:50 +00:00
AsyncAction,
TrezorDevice,
2018-07-30 10:52:13 +00:00
RouterLocationState,
} from '~/flowtype';
2018-04-16 21:19:50 +00:00
export type TrezorConnectAction = {
type: typeof CONNECT.INITIALIZATION_ERROR,
2018-04-23 10:20:15 +00:00
error: string
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.COIN_CHANGED,
payload: {
network: string
}
} | {
type: typeof CONNECT.AUTH_DEVICE,
2018-05-19 16:26:39 +00:00
device: TrezorDevice,
state: string
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.DUPLICATE,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.REMEMBER_REQUEST,
2018-04-23 10:20:15 +00:00
device: TrezorDevice,
instances: Array<TrezorDevice>
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.DISCONNECT_REQUEST,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.FORGET_REQUEST,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.FORGET,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.FORGET_SINGLE,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.REMEMBER,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.TRY_TO_DUPLICATE,
2018-04-23 10:20:15 +00:00
device: TrezorDevice
2018-04-16 21:19:50 +00:00
} | {
type: typeof CONNECT.DEVICE_FROM_STORAGE,
2018-04-23 10:20:15 +00:00
payload: Array<TrezorDevice>
2018-05-23 09:46:57 +00:00
} | {
type: typeof CONNECT.START_ACQUIRING,
} | {
type: typeof CONNECT.STOP_ACQUIRING,
2018-04-16 21:19:50 +00:00
};
2018-07-30 10:52:13 +00:00
export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
// set listeners
TrezorConnect.on(DEVICE_EVENT, (event: DeviceMessage): void => {
// post event to reducers
const type: DeviceMessageType = event.type; // assert flow type
dispatch({
type,
device: event.payload,
2018-02-20 09:30:36 +00:00
});
2018-07-30 10:52:13 +00:00
});
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
TrezorConnect.on(UI_EVENT, (event: UiMessage): void => {
// post event to reducers
const type: UiMessageType = event.type; // assert flow type
dispatch({
type,
payload: event.payload,
});
2018-07-30 10:52:13 +00:00
});
2018-07-30 10:52:13 +00:00
TrezorConnect.on(TRANSPORT_EVENT, (event: TransportMessage): void => {
// post event to reducers
const type: TransportMessageType = event.type; // assert flow type
dispatch({
type,
payload: event.payload,
});
2018-07-30 10:52:13 +00:00
});
2018-02-20 09:30:36 +00:00
2018-08-08 11:21:28 +00:00
// $FlowIssue LOCAL not declared
2018-08-08 12:39:43 +00:00
// window.__TREZOR_CONNECT_SRC = typeof LOCAL === 'string' ? LOCAL : 'https://connect.trezor.io/5/';
window.__TREZOR_CONNECT_SRC = 'https://sisyfos.trezor.io/connect/';
// window.__TREZOR_CONNECT_SRC = 'https://localhost:8088/';
2018-08-08 11:21:28 +00:00
2018-07-30 10:52:13 +00:00
try {
await TrezorConnect.init({
transportReconnect: true,
debug: true,
2018-07-30 10:52:13 +00:00
popup: false,
webusb: true,
pendingTransportEvent: (getState().devices.length < 1),
});
} catch (error) {
// dispatch({
// type: CONNECT.INITIALIZATION_ERROR,
// error
// })
2017-12-13 11:01:37 +00:00
}
2018-07-30 10:52:13 +00:00
};
2017-12-13 11:01:37 +00:00
2018-02-20 09:30:36 +00:00
// called after backend was initialized
// set listeners for connect/disconnect
2018-07-30 10:52:13 +00:00
export const postInit = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const handleDeviceConnect = (device: Device) => {
dispatch(initConnectedDevice(device));
};
2018-07-30 10:52:13 +00:00
TrezorConnect.off(DEVICE.CONNECT, handleDeviceConnect);
TrezorConnect.off(DEVICE.CONNECT_UNACQUIRED, handleDeviceConnect);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
TrezorConnect.on(DEVICE.CONNECT, handleDeviceConnect);
TrezorConnect.on(DEVICE.CONNECT_UNACQUIRED, handleDeviceConnect);
2018-07-30 10:52:13 +00:00
const { devices } = getState();
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const { initialPathname, initialParams } = getState().wallet;
2018-04-16 21:19:50 +00:00
2018-07-30 10:52:13 +00:00
if (initialPathname) {
dispatch({
type: WALLET.SET_INITIAL_URL,
// pathname: null,
// params: null
});
}
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
if (devices.length > 0) {
const unacquired: ?TrezorDevice = devices.find(d => d.features);
2018-07-30 10:52:13 +00:00
if (unacquired) {
dispatch(onSelectDevice(unacquired));
} else {
const latest: Array<TrezorDevice> = sortDevices(devices);
const firstConnected: ?TrezorDevice = latest.find(d => d.connected);
dispatch(onSelectDevice(firstConnected || latest[0]));
// TODO
if (initialParams) {
if (!initialParams.hasOwnProperty('network') && initialPathname !== getState().router.location.pathname) {
// dispatch( push(initialPathname) );
} else {
}
2018-02-20 09:30:36 +00:00
}
}
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const sortDevices = (devices: Array<TrezorDevice>): Array<TrezorDevice> => devices.sort((a, b) => {
if (!a.ts || !b.ts) {
return -1;
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
return a.ts > b.ts ? -1 : 1;
});
export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const selected = getState().wallet.selectedDevice;
// if (!selected || (selected && selected.state)) {
dispatch(onSelectDevice(device));
// }
// if (device.unacquired && selected && selected.path !== device.path && !selected.connected) {
// dispatch( onSelectDevice(device) );
// } else if (!selected) {
// dispatch( onSelectDevice(device) );
// }
};
2018-02-20 09:30:36 +00:00
// selection from Aside dropdown button
// after device_connect event
2018-02-20 09:30:36 +00:00
// or after acquiring device
// device type could be local TrezorDevice or Device (from trezor-connect device_connect event)
2018-07-30 10:52:13 +00:00
export const onSelectDevice = (device: TrezorDevice | Device): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
// || device.isUsedElsewhere
// switch to initial url and reset this value
if (!device.features) {
2018-08-08 12:39:43 +00:00
dispatch(push(`/device/${device.path}/${ device.type === 'unreadable' ? 'unreadable' : 'acquire' }`));
2018-07-30 10:52:13 +00:00
} else if (device.features.bootloader_mode) {
dispatch(push(`/device/${device.path}/bootloader`));
} else if (!device.features.initialized) {
dispatch(push(`/device/${device.features.device_id}/initialize`));
} else if (typeof device.instance === 'number') {
dispatch(push(`/device/${device.features.device_id}:${device.instance}`));
} else {
const deviceId: string = device.features.device_id;
const urlParams: RouterLocationState = getState().router.location.state;
// let url: string = `/device/${ device.features.device_id }/network/ethereum/account/0`;
let url: string = `/device/${deviceId}`;
let instance: ?number;
// check if device is not TrezorDevice type
if (!device.hasOwnProperty('ts')) {
// its device from trezor-connect (called in initConnectedDevice triggered by device_connect event)
// need to lookup if there are unavailable instances
const available: Array<TrezorDevice> = getState().devices.filter(d => d.path === device.path);
const latest: Array<TrezorDevice> = sortDevices(available);
if (latest.length > 0 && latest[0].instance) {
url += `:${latest[0].instance}`;
instance = latest[0].instance;
}
2018-07-30 10:52:13 +00:00
}
// check if current location is not set to this device
//dispatch( push(`/device/${ device.features.device_id }/network/etc/account/0`) );
2018-05-02 11:39:27 +00:00
2018-07-30 10:52:13 +00:00
if (urlParams.deviceInstance !== instance || urlParams.device !== deviceId) {
dispatch(push(url));
2018-02-20 09:30:36 +00:00
}
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const switchToFirstAvailableDevice = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { devices } = getState();
if (devices.length > 0) {
// TODO: Priority:
// 1. First Unacquired
// 2. First connected
// 3. Saved with latest timestamp
const unacquired = devices.find(d => !d.features);
2018-07-30 10:52:13 +00:00
if (unacquired) {
dispatch(initConnectedDevice(unacquired));
2018-02-20 09:30:36 +00:00
} else {
2018-07-30 10:52:13 +00:00
const latest: Array<TrezorDevice> = sortDevices(devices);
const firstConnected: ?TrezorDevice = latest.find(d => d.connected);
dispatch(onSelectDevice(firstConnected || latest[0]));
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
} else {
dispatch(push('/'));
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const getSelectedDeviceState = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected = getState().wallet.selectedDevice;
if (selected
2018-02-20 09:30:36 +00:00
&& selected.connected
2018-05-22 12:16:18 +00:00
&& (selected.features && !selected.features.bootloader_mode && selected.features.initialized)
&& !selected.state) {
2018-07-30 10:52:13 +00:00
const response = await TrezorConnect.getDeviceState({
device: {
path: selected.path,
instance: selected.instance,
state: selected.state,
},
useEmptyPassphrase: !selected.instance,
});
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
if (response && response.success) {
dispatch({
type: CONNECT.AUTH_DEVICE,
device: selected,
state: response.payload.state,
});
} else {
dispatch({
type: NOTIFICATION.ADD,
payload: {
devicePath: selected.path,
type: 'error',
title: 'Authentication error',
message: response.payload.error,
cancelable: false,
actions: [
{
label: 'Try again',
callback: () => {
dispatch({
type: NOTIFICATION.CLOSE,
payload: { devicePath: selected.path },
});
dispatch(getSelectedDeviceState());
},
},
],
},
});
2018-02-20 09:30:36 +00:00
}
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const deviceDisconnect = (device: Device): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
if (device && device.features) {
if (selected && selected.features && selected.features.device_id === device.features.device_id) {
dispatch(DiscoveryActions.stop(selected));
}
2018-02-20 09:30:36 +00:00
const instances = getState().devices.filter(d => d.features && device.features && d.state && !d.remember && d.features.device_id === device.features.device_id);
2018-07-30 10:52:13 +00:00
if (instances.length > 0) {
dispatch({
type: CONNECT.REMEMBER_REQUEST,
device: instances[0],
instances,
});
2018-03-08 16:10:53 +00:00
}
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const coinChanged = (network: ?string): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return;
2018-04-16 21:19:50 +00:00
2018-07-30 10:52:13 +00:00
dispatch(DiscoveryActions.stop(selected));
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
if (network) {
dispatch(DiscoveryActions.start(selected, network));
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
export function reload(): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-07-30 10:52:13 +00:00
};
}
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
export function acquire(): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = getState().wallet.selectedDevice;
2018-04-16 21:19:50 +00:00
if (!selected) return;
2018-02-20 09:30:36 +00:00
2018-05-23 09:46:57 +00:00
dispatch({
type: CONNECT.START_ACQUIRING,
2018-07-30 10:52:13 +00:00
});
2018-05-23 09:46:57 +00:00
2018-07-30 10:52:13 +00:00
const response = await TrezorConnect.getFeatures({
2018-02-20 09:30:36 +00:00
device: {
path: selected.path,
},
useEmptyPassphrase: !selected.instance,
2018-02-20 09:30:36 +00:00
});
2018-05-16 16:55:12 +00:00
if (!response.success) {
2018-02-20 09:30:36 +00:00
dispatch({
type: NOTIFICATION.ADD,
payload: {
type: 'error',
title: 'Acquire device error',
message: response.payload.error,
2018-02-20 09:30:36 +00:00
cancelable: true,
2018-05-23 09:46:57 +00:00
// actions: [
// {
// label: 'Try again',
// callback: () => {
// dispatch(acquire())
// }
// }
// ]
2018-07-30 10:52:13 +00:00
},
});
2018-02-20 09:30:36 +00:00
}
2018-05-23 09:46:57 +00:00
dispatch({
type: CONNECT.STOP_ACQUIRING,
2018-07-30 10:52:13 +00:00
});
};
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
export const gotoDeviceSettings = (device: TrezorDevice): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
if (device.features) {
const devUrl: string = `${device.features.device_id}${device.instance ? `:${device.instance}` : ''}`;
dispatch(push(`/device/${devUrl}/settings`));
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
// called from Aside - device menu (forget single instance)
2018-07-30 10:52:13 +00:00
export const forget = (device: TrezorDevice): Action => ({
type: CONNECT.FORGET_REQUEST,
device,
});
export const duplicateDevice = (device: TrezorDevice): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
dispatch({
type: CONNECT.TRY_TO_DUPLICATE,
device,
});
};
2017-12-13 11:01:37 +00:00
2018-05-16 16:30:46 +00:00
export function addAccount(): ThunkAction {
2018-04-16 21:19:50 +00:00
return (dispatch: Dispatch, getState: GetState): void => {
const selected = getState().wallet.selectedDevice;
2018-04-23 10:20:15 +00:00
if (!selected) return;
2018-07-30 10:52:13 +00:00
dispatch(DiscoveryActions.start(selected, getState().router.location.state.network, true)); // TODO: network nicer
};
2017-12-13 11:01:37 +00:00
}