2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-05-25 09:26:36 +00:00
|
|
|
import { LOCATION_CHANGE } from 'react-router-redux';
|
2018-04-11 10:06:46 +00:00
|
|
|
import * as WALLET from './constants/wallet';
|
2018-05-28 17:42:03 +00:00
|
|
|
import * as CONNECT from './constants/TrezorConnect';
|
2018-05-25 09:26:36 +00:00
|
|
|
import * as stateUtils from '../reducers/utils';
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-05-28 17:42:03 +00:00
|
|
|
import type { Device } from 'trezor-connect';
|
2018-05-25 09:26:36 +00:00
|
|
|
import type
|
2018-07-30 10:52:13 +00:00
|
|
|
{
|
2018-05-25 09:26:36 +00:00
|
|
|
Account,
|
|
|
|
Coin,
|
|
|
|
Discovery,
|
|
|
|
Token,
|
|
|
|
Web3Instance,
|
2018-07-30 10:52:13 +00:00
|
|
|
TrezorDevice,
|
|
|
|
RouterLocationState,
|
2018-05-25 09:26:36 +00:00
|
|
|
ThunkAction,
|
|
|
|
AsyncAction,
|
|
|
|
Action,
|
2018-07-30 10:52:13 +00:00
|
|
|
Dispatch,
|
2018-05-25 09:26:36 +00:00
|
|
|
GetState,
|
2018-07-30 10:52:13 +00:00
|
|
|
State,
|
2018-05-25 09:26:36 +00:00
|
|
|
} from '~/flowtype';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
|
|
|
export type WalletAction = {
|
|
|
|
type: typeof WALLET.SET_INITIAL_URL,
|
|
|
|
state?: RouterLocationState,
|
|
|
|
pathname?: string
|
|
|
|
} | {
|
|
|
|
type: typeof WALLET.TOGGLE_DEVICE_DROPDOWN,
|
|
|
|
opened: boolean
|
|
|
|
} | {
|
|
|
|
type: typeof WALLET.ON_BEFORE_UNLOAD
|
2018-05-17 14:03:11 +00:00
|
|
|
} | {
|
|
|
|
type: typeof WALLET.ONLINE_STATUS,
|
|
|
|
online: boolean
|
2018-05-22 17:41:10 +00:00
|
|
|
} | {
|
|
|
|
type: typeof WALLET.SET_SELECTED_DEVICE,
|
|
|
|
device: ?TrezorDevice
|
2018-05-23 08:00:43 +00:00
|
|
|
} | {
|
|
|
|
type: typeof WALLET.UPDATE_SELECTED_DEVICE,
|
|
|
|
device: TrezorDevice
|
2018-05-28 17:42:03 +00:00
|
|
|
} | {
|
|
|
|
type: typeof WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA,
|
|
|
|
devices: Array<TrezorDevice>
|
2018-05-17 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
export const init = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
const updateOnlineStatus = (event) => {
|
|
|
|
dispatch({
|
|
|
|
type: WALLET.ONLINE_STATUS,
|
|
|
|
online: navigator.onLine,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
window.addEventListener('online', updateOnlineStatus);
|
|
|
|
window.addEventListener('offline', updateOnlineStatus);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const onBeforeUnload = (): WalletAction => ({
|
|
|
|
type: WALLET.ON_BEFORE_UNLOAD,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const toggleDeviceDropdown = (opened: boolean): WalletAction => ({
|
|
|
|
type: WALLET.TOGGLE_DEVICE_DROPDOWN,
|
|
|
|
opened,
|
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
|
2018-05-28 17:42:03 +00:00
|
|
|
// This method will be called after each DEVICE.CONNECT action
|
|
|
|
// if connected device has different "passphrase_protection" settings than saved instances
|
|
|
|
// all saved instances will be removed immediately inside DevicesReducer
|
|
|
|
// This method will clear leftovers associated with removed instances from reducers.
|
|
|
|
// (DiscoveryReducer, AccountReducer, TokensReducer)
|
2018-07-30 10:52:13 +00:00
|
|
|
export const clearUnavailableDevicesData = (prevState: State, device: Device): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
if (!device.features) return;
|
2018-05-28 17:42:03 +00:00
|
|
|
|
2018-08-08 09:42:12 +00:00
|
|
|
const affectedDevices = prevState.devices.filter(d =>
|
|
|
|
d.features && device.features
|
2018-05-28 17:42:03 +00:00
|
|
|
&& d.features.device_id === device.features.device_id
|
2018-07-30 10:52:13 +00:00
|
|
|
&& d.features.passphrase_protection !== device.features.passphrase_protection);
|
2018-05-28 17:42:03 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
if (affectedDevices.length > 0) {
|
|
|
|
dispatch({
|
|
|
|
type: WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA,
|
|
|
|
devices: affectedDevices,
|
|
|
|
});
|
2018-05-28 17:42:03 +00:00
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const updateSelectedValues = (prevState: State, action: Action): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
|
|
|
const locationChange: boolean = action.type === LOCATION_CHANGE;
|
|
|
|
const state: State = getState();
|
|
|
|
|
|
|
|
// handle devices state change (from trezor-connect events or location change)
|
|
|
|
if (locationChange || prevState.devices !== state.devices) {
|
|
|
|
const device = stateUtils.getSelectedDevice(state);
|
|
|
|
if (state.wallet.selectedDevice !== device) {
|
|
|
|
if (device && stateUtils.isSelectedDevice(state.wallet.selectedDevice, device)) {
|
|
|
|
dispatch({
|
|
|
|
type: WALLET.UPDATE_SELECTED_DEVICE,
|
|
|
|
device,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: WALLET.SET_SELECTED_DEVICE,
|
|
|
|
device,
|
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|