2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-12-18 13:33:01 +00:00
|
|
|
import { LOCATION_CHANGE } from 'connected-react-router';
|
2018-10-01 09:59:31 +00:00
|
|
|
import { DEVICE } from 'trezor-connect';
|
2018-10-03 17:54:57 +00:00
|
|
|
import * as CONNECT from 'actions/constants/TrezorConnect';
|
2018-08-14 13:18:16 +00:00
|
|
|
import * as WALLET from 'actions/constants/wallet';
|
2018-09-26 12:11:37 +00:00
|
|
|
import * as reducerUtils from 'reducers/utils';
|
2018-10-04 20:59:39 +00:00
|
|
|
import * as deviceUtils from 'utils/device';
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-09-03 15:43:17 +00:00
|
|
|
import type {
|
2018-08-17 13:24:47 +00:00
|
|
|
Device,
|
2018-07-30 10:52:13 +00:00
|
|
|
TrezorDevice,
|
|
|
|
RouterLocationState,
|
2018-05-25 09:26:36 +00:00
|
|
|
ThunkAction,
|
2018-10-01 09:59:31 +00:00
|
|
|
PayloadAction,
|
2018-05-25 09:26:36 +00:00
|
|
|
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-08-14 12:56:47 +00:00
|
|
|
} from 'flowtype';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export type WalletAction =
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_INITIAL_URL,
|
|
|
|
state?: RouterLocationState,
|
2019-04-12 15:13:42 +00:00
|
|
|
pathname?: string,
|
2019-03-04 12:33:02 +00:00
|
|
|
}
|
2019-04-17 15:13:23 +00:00
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_HIDDEN_COINS,
|
2019-04-17 17:14:06 +00:00
|
|
|
hiddenCoins: Array<string>,
|
2019-04-17 15:13:23 +00:00
|
|
|
}
|
2019-04-24 12:05:07 +00:00
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_HIDDEN_COINS_EXTERNAL,
|
|
|
|
hiddenCoinsExternal: Array<string>,
|
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
| {
|
|
|
|
type: typeof WALLET.TOGGLE_DEVICE_DROPDOWN,
|
|
|
|
opened: boolean,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.ONLINE_STATUS,
|
|
|
|
online: boolean,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_SELECTED_DEVICE,
|
|
|
|
device: ?TrezorDevice,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.UPDATE_SELECTED_DEVICE,
|
|
|
|
device: TrezorDevice,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA,
|
|
|
|
devices: Array<TrezorDevice>,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type:
|
|
|
|
| typeof WALLET.SHOW_BETA_DISCLAIMER
|
|
|
|
| typeof WALLET.HIDE_BETA_DISCLAIMER
|
|
|
|
| typeof WALLET.SET_FIRST_LOCATION_CHANGE,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.TOGGLE_SIDEBAR,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_LANGUAGE,
|
|
|
|
locale: string,
|
|
|
|
messages: { [string]: string },
|
2019-03-11 16:45:35 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_LOCAL_CURRENCY,
|
|
|
|
localCurrency: string,
|
2019-03-14 22:31:19 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof WALLET.SET_HIDE_BALANCE,
|
2019-03-15 09:46:54 +00:00
|
|
|
toggled: boolean,
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|
2018-05-17 14:03:11 +00:00
|
|
|
|
2018-09-06 15:04:28 +00:00
|
|
|
export const init = (): ThunkAction => (dispatch: Dispatch): void => {
|
|
|
|
const updateOnlineStatus = () => {
|
2018-07-30 10:52:13 +00:00
|
|
|
dispatch({
|
|
|
|
type: WALLET.ONLINE_STATUS,
|
|
|
|
online: navigator.onLine,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
window.addEventListener('online', updateOnlineStatus);
|
|
|
|
window.addEventListener('offline', updateOnlineStatus);
|
|
|
|
};
|
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
export const hideBetaDisclaimer = (): WalletAction => ({
|
|
|
|
type: WALLET.HIDE_BETA_DISCLAIMER,
|
|
|
|
});
|
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
export const toggleDeviceDropdown = (opened: boolean): WalletAction => ({
|
|
|
|
type: WALLET.TOGGLE_DEVICE_DROPDOWN,
|
|
|
|
opened,
|
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
|
2019-01-24 17:49:33 +00:00
|
|
|
export const toggleSidebar = (): WalletAction => ({
|
|
|
|
type: WALLET.TOGGLE_SIDEBAR,
|
|
|
|
});
|
|
|
|
|
2019-02-15 19:52:56 +00:00
|
|
|
export const fetchLocale = (locale: string): ThunkAction => (dispatch: Dispatch): void => {
|
2019-03-04 13:37:27 +00:00
|
|
|
fetch(`./l10n/${locale}.json`)
|
2019-03-04 18:03:07 +00:00
|
|
|
.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
throw Error(response.statusText);
|
|
|
|
})
|
2019-03-04 12:33:02 +00:00
|
|
|
.then(messages => {
|
2019-02-15 19:52:56 +00:00
|
|
|
dispatch({
|
|
|
|
type: WALLET.SET_LANGUAGE,
|
|
|
|
locale,
|
|
|
|
messages,
|
|
|
|
});
|
2019-03-04 18:03:07 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2019-03-05 10:53:33 +00:00
|
|
|
console.error(error);
|
2019-02-15 19:52:56 +00:00
|
|
|
});
|
|
|
|
};
|
2019-02-06 17:01:09 +00:00
|
|
|
|
2019-03-11 16:45:35 +00:00
|
|
|
export const setLocalCurrency = (localCurrency: string): WalletAction => ({
|
|
|
|
type: WALLET.SET_LOCAL_CURRENCY,
|
|
|
|
localCurrency: localCurrency.toLowerCase(),
|
|
|
|
});
|
|
|
|
|
2019-03-14 22:31:19 +00:00
|
|
|
export const setHideBalance = (toggled: boolean): WalletAction => ({
|
|
|
|
type: WALLET.SET_HIDE_BALANCE,
|
|
|
|
toggled,
|
|
|
|
});
|
|
|
|
|
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)
|
2019-03-04 12:33:02 +00:00
|
|
|
export const clearUnavailableDevicesData = (prevState: State, device: Device): ThunkAction => (
|
|
|
|
dispatch: Dispatch
|
|
|
|
): void => {
|
2018-07-30 10:52:13 +00:00
|
|
|
if (!device.features) return;
|
2018-05-28 17:42:03 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
const affectedDevices = prevState.devices.filter(
|
|
|
|
d =>
|
|
|
|
d.features &&
|
|
|
|
device.features &&
|
|
|
|
d.features.device_id === device.features.device_id &&
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-10-01 09:59:31 +00:00
|
|
|
// list of all actions which has influence on "selectedDevice" field in "wallet" reducer
|
|
|
|
// other actions will be ignored
|
|
|
|
const actions = [
|
|
|
|
LOCATION_CHANGE,
|
2018-10-03 17:54:57 +00:00
|
|
|
CONNECT.AUTH_DEVICE,
|
2018-10-05 13:47:37 +00:00
|
|
|
CONNECT.RECEIVE_WALLET_TYPE,
|
2018-10-01 09:59:31 +00:00
|
|
|
...Object.values(DEVICE).filter(v => typeof v === 'string'),
|
|
|
|
];
|
|
|
|
|
|
|
|
/*
|
2019-03-04 12:33:02 +00:00
|
|
|
* Called from WalletService
|
|
|
|
*/
|
|
|
|
export const observe = (prevState: State, action: Action): PayloadAction<boolean> => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
): boolean => {
|
2018-10-01 09:59:31 +00:00
|
|
|
// ignore not listed actions
|
|
|
|
if (actions.indexOf(action.type) < 0) return false;
|
2018-07-30 10:52:13 +00:00
|
|
|
|
|
|
|
const state: State = getState();
|
|
|
|
|
2018-09-26 12:11:37 +00:00
|
|
|
const device = reducerUtils.getSelectedDevice(state);
|
|
|
|
const selectedDeviceChanged = reducerUtils.observeChanges(state.wallet.selectedDevice, device);
|
2018-10-01 09:59:31 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
// handle devices state change (from trezor-connect events or location change)
|
2019-04-15 09:34:49 +00:00
|
|
|
if (selectedDeviceChanged) {
|
2018-10-04 20:59:39 +00:00
|
|
|
if (device && deviceUtils.isSelectedDevice(state.wallet.selectedDevice, device)) {
|
2018-09-26 12:11:37 +00:00
|
|
|
dispatch({
|
|
|
|
type: WALLET.UPDATE_SELECTED_DEVICE,
|
|
|
|
device,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: WALLET.SET_SELECTED_DEVICE,
|
|
|
|
device,
|
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
}
|
2018-10-01 09:59:31 +00:00
|
|
|
return true;
|
2018-05-25 09:26:36 +00:00
|
|
|
}
|
2018-10-01 09:59:31 +00:00
|
|
|
return false;
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|