1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/actions/RouterActions.js

459 lines
15 KiB
JavaScript
Raw Normal View History

2018-09-20 16:24:28 +00:00
/* @flow */
import { push, LOCATION_CHANGE } from 'connected-react-router';
import { CONTEXT_NONE } from 'actions/constants/modal';
import { SET_INITIAL_URL, SET_FIRST_LOCATION_CHANGE } from 'actions/constants/wallet';
import { routes } from 'support/routes';
import * as deviceUtils from 'utils/device';
2018-09-20 16:24:28 +00:00
import type {
RouterLocationState,
Device,
TrezorDevice,
ThunkAction,
PayloadAction,
Dispatch,
GetState,
} from 'flowtype';
import type { RouterAction } from 'connected-react-router';
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Parse url string to RouterLocationState object (key/value)
*/
export const pathToParams = (
path: string
): PayloadAction<RouterLocationState> => (): RouterLocationState => {
2018-09-20 16:24:28 +00:00
// split url into parts
const parts: Array<string> = path.split('/').slice(1);
const params: RouterLocationState = {};
// return empty params
if (parts.length < 1 || path === '/') return params;
// map parts to params by key/value
// assuming that url is in format: "/key/value"
for (let i = 0, len = parts.length; i < len; i += 2) {
params[parts[i]] = parts[i + 1] || parts[i];
}
// check for special case: /device/device-id:instance-id
if (params.hasOwnProperty('device')) {
const isClonedDevice: Array<string> = params.device.split(':');
if (isClonedDevice.length > 1) {
2018-09-21 12:01:41 +00:00
const [device, instance] = isClonedDevice;
params.device = device;
params.deviceInstance = instance;
2018-09-20 16:24:28 +00:00
}
}
return params;
};
/*
2019-03-04 12:33:02 +00:00
* RouterLocationState validation
* Check if requested device or network exists in reducers
*/
export const paramsValidation = (params: RouterLocationState): PayloadAction<boolean> => (
dispatch: Dispatch,
getState: GetState
): boolean => {
2018-09-20 16:24:28 +00:00
// validate requested device
2018-12-20 14:20:02 +00:00
2018-09-20 16:24:28 +00:00
if (params.hasOwnProperty('device')) {
const { devices } = getState();
let device: ?TrezorDevice;
if (params.hasOwnProperty('deviceInstance')) {
2019-03-04 12:33:02 +00:00
device = devices.find(
d =>
d.features &&
d.features.device_id === params.device &&
d.instance === parseInt(params.deviceInstance, 10)
);
2018-09-20 16:24:28 +00:00
} else {
2019-03-04 12:33:02 +00:00
device = devices.find(
d =>
((!d.features || d.mode === 'bootloader') && d.path === params.device) ||
(d.features && d.features.device_id === params.device)
);
2018-09-20 16:24:28 +00:00
}
if (!device) return false;
if (!deviceUtils.isDeviceAccessible(device)) {
2018-11-01 09:53:56 +00:00
// no access to deep links if device has incorrect mode/firmware
if (params.hasOwnProperty('network') || params.hasOwnProperty('account')) return false;
}
2018-09-20 16:24:28 +00:00
}
// validate requested network
if (params.hasOwnProperty('network')) {
const { config } = getState().localStorage;
2018-10-15 13:44:10 +00:00
const network = config.networks.find(c => c.shortcut === params.network);
if (!network) return false;
2018-09-20 16:24:28 +00:00
if (!params.account) return false;
}
// validate requested account
2018-09-20 21:05:48 +00:00
// TODO: only if discovery on this network is completed
2018-09-20 16:24:28 +00:00
// if (params.hasOwnProperty('account')) {
// }
2018-09-20 18:25:34 +00:00
return true;
2018-09-21 12:01:41 +00:00
};
2018-09-20 18:25:34 +00:00
/*
2019-03-04 12:33:02 +00:00
* Composing url string from given RouterLocationState object
* Filters unrecognized fields and sorting in correct order
*/
export const paramsToPath = (
params: RouterLocationState
): PayloadAction<?string> => (): ?string => {
// get patterns (fields) from routes and sort them by complexity
2019-03-04 12:33:02 +00:00
const patterns: Array<Array<string>> = routes
.map(r => r.fields)
.sort((a, b) => (a.length > b.length ? -1 : 1));
2018-09-20 21:05:48 +00:00
// find pattern
const keys: Array<string> = Object.keys(params);
let patternToUse: ?Array<string>;
2018-09-21 12:01:41 +00:00
let i: number;
for (i = 0; i < patterns.length; i++) {
const pattern = patterns[i];
2018-09-20 21:05:48 +00:00
const match: Array<string> = keys.filter(key => pattern.indexOf(key) >= 0);
if (match.length === pattern.length) {
patternToUse = pattern;
break;
}
}
// pattern not found, redirect back
if (!patternToUse) return null;
2018-09-21 12:01:41 +00:00
2018-09-20 21:05:48 +00:00
// compose url string from pattern
let url: string = '';
2019-03-04 12:33:02 +00:00
patternToUse.forEach(field => {
2018-09-20 21:05:48 +00:00
if (field === params[field]) {
// standalone (odd) fields
2018-09-21 12:01:41 +00:00
url += `/${field}`;
2018-09-20 21:05:48 +00:00
} else {
2018-09-21 12:01:41 +00:00
url += `/${field}/${params[field]}`;
2018-09-20 21:05:48 +00:00
if (field === 'device') {
if (params.hasOwnProperty('deviceInstance')) {
2018-09-21 12:01:41 +00:00
url += `:${params.deviceInstance}`;
2018-09-20 21:05:48 +00:00
}
}
}
});
return url;
2018-09-21 12:01:41 +00:00
};
2018-09-20 21:05:48 +00:00
2019-03-04 12:33:02 +00:00
export const getValidUrl = (action: RouterAction): PayloadAction<string> => (
dispatch: Dispatch,
getState: GetState
): string => {
2018-09-20 21:05:48 +00:00
const { location } = getState().router;
2018-12-18 16:05:06 +00:00
const { firstLocationChange } = getState().wallet;
2018-09-20 21:05:48 +00:00
// redirect to landing page (loading screen)
// and wait until application is ready
if (firstLocationChange) {
dispatch({
type: SET_FIRST_LOCATION_CHANGE,
});
return '/';
}
2018-12-18 16:05:06 +00:00
const requestedUrl = action.payload.location.pathname;
2018-09-20 21:05:48 +00:00
// Corner case: LOCATION_CHANGE was called but pathname didn't changed (redirect action from RouterService)
if (requestedUrl === location.pathname) return requestedUrl;
2018-09-21 12:01:41 +00:00
2018-09-20 21:05:48 +00:00
// Modal is opened
// redirect to previous url
if (getState().modal.context !== CONTEXT_NONE) {
2018-09-20 21:05:48 +00:00
// Corner case: modal is opened and currentParams are still valid
// example 1 (valid blocking): url changed while passphrase modal opened but device is still connected (we want user to finish this action)
// example 2 (invalid blocking): url changes while passphrase modal opened because device disconnect
2018-09-21 12:01:41 +00:00
const currentParams = dispatch(pathToParams(location.pathname));
const currentParamsAreValid = dispatch(paramsValidation(currentParams));
2019-03-04 12:33:02 +00:00
if (currentParamsAreValid) {
return location.pathname;
}
2018-09-20 21:05:48 +00:00
}
// there are no connected devices or application isn't ready or initialization error occurred
// redirect to landing page
2019-03-04 12:33:02 +00:00
const shouldBeLandingPage =
getState().devices.length < 1 ||
!getState().wallet.ready ||
getState().connect.error !== null;
2018-09-21 12:01:41 +00:00
const landingPageUrl = dispatch(isLandingPageUrl(requestedUrl));
2018-09-20 21:05:48 +00:00
if (shouldBeLandingPage) {
const landingPageRoute = dispatch(isLandingPageUrl(requestedUrl, getState().wallet.ready));
return !landingPageRoute ? '/' : requestedUrl;
2018-09-20 21:05:48 +00:00
}
// Disallow displaying landing page
// redirect to previous url
if (!shouldBeLandingPage && landingPageUrl) {
2018-10-02 15:33:49 +00:00
return dispatch(getFirstAvailableDeviceUrl()) || location.pathname;
2018-09-20 21:05:48 +00:00
}
// Regular url change during application live cycle
2018-09-21 12:01:41 +00:00
const requestedParams = dispatch(pathToParams(requestedUrl));
const requestedParamsAreValid: boolean = dispatch(paramsValidation(requestedParams));
2018-09-20 21:05:48 +00:00
// Requested params are not valid
// Neither device or network doesn't exists
if (!requestedParamsAreValid) {
return location.pathname;
}
// Compose valid url from requested params
2018-09-21 12:01:41 +00:00
const composedUrl = dispatch(paramsToPath(requestedParams));
2018-09-20 21:05:48 +00:00
return composedUrl || location.pathname;
2018-09-21 12:01:41 +00:00
};
2018-09-20 21:05:48 +00:00
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Compose url from requested device object and returns url
*/
const getDeviceUrl = (device: TrezorDevice | Device): PayloadAction<?string> => (
dispatch: Dispatch,
getState: GetState
): ?string => {
2018-09-20 16:24:28 +00:00
let url: ?string;
if (!device.features) {
url = `/device/${device.path}/${device.type === 'unreadable' ? 'unreadable' : 'acquire'}`;
2019-03-04 12:33:02 +00:00
} else if (device.mode === 'bootloader') {
// device in bootloader doesn't have device_id
2018-09-20 16:24:28 +00:00
url = `/device/${device.path}/bootloader`;
} else if (device.mode === 'initialize') {
2018-09-20 16:24:28 +00:00
url = `/device/${device.features.device_id}/initialize`;
2018-11-01 09:53:56 +00:00
} else if (device.mode === 'seedless') {
url = `/device/${device.features.device_id}/seedless`;
} else if (device.firmware === 'required') {
url = `/device/${device.features.device_id}/firmware-update`;
2018-09-20 16:24:28 +00:00
} else if (typeof device.instance === 'number') {
url = `/device/${device.features.device_id}:${device.instance}`;
} else {
url = `/device/${device.features.device_id}`;
// make sure that device is not TrezorDevice type
if (!device.hasOwnProperty('ts')) {
// it is device from trezor-connect triggered by DEVICE.CONNECT event
// need to lookup if there are unavailable instances
2019-03-04 12:33:02 +00:00
const available: Array<TrezorDevice> = getState().devices.filter(
d => d.path === device.path
);
2018-09-20 16:24:28 +00:00
const latest: Array<TrezorDevice> = sortDevices(available);
if (latest.length > 0 && latest[0].instance) {
url += `:${latest[0].instance}`;
}
}
}
2018-10-02 15:33:49 +00:00
return url;
2018-09-21 12:01:41 +00:00
};
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Try to find first available device using order:
* 1. First unacquired
* 2. First connected
* 3. Saved with latest timestamp
* OR redirect to landing page
*/
export const getFirstAvailableDeviceUrl = (): PayloadAction<?string> => (
dispatch: Dispatch,
getState: GetState
): ?string => {
2018-09-20 16:24:28 +00:00
const { devices } = getState();
2018-10-02 15:33:49 +00:00
let url: ?string;
2018-09-20 16:24:28 +00:00
if (devices.length > 0) {
const unacquired = devices.find(d => !d.features);
if (unacquired) {
2018-10-02 15:33:49 +00:00
url = dispatch(getDeviceUrl(unacquired));
2018-09-20 16:24:28 +00:00
} else {
const latest: Array<TrezorDevice> = sortDevices(devices);
const firstConnected: ?TrezorDevice = latest.find(d => d.connected);
2018-10-02 15:33:49 +00:00
url = dispatch(getDeviceUrl(firstConnected || latest[0]));
2018-09-20 16:24:28 +00:00
}
2018-10-02 15:33:49 +00:00
}
return url;
};
/*
2019-03-04 12:33:02 +00:00
* Utility used in "getDeviceUrl" and "getFirstAvailableDeviceUrl"
* sorting device array by "ts" (timestamp) field
*/
const sortDevices = (devices: Array<TrezorDevice>): Array<TrezorDevice> =>
devices.sort((a, b) => {
if (!a.ts || !b.ts) {
return -1;
}
return a.ts > b.ts ? -1 : 1;
});
2018-10-02 15:33:49 +00:00
/*
2019-03-04 12:33:02 +00:00
* Redirect to requested device
*/
export const selectDevice = (device: TrezorDevice | Device): ThunkAction => (
dispatch: Dispatch,
getState: GetState
): void => {
if (dispatch(setInitialUrl())) return;
2018-10-02 15:33:49 +00:00
const url: ?string = dispatch(getDeviceUrl(device));
if (!url) return;
const currentParams: RouterLocationState = getState().router.location.state;
const requestedParams = dispatch(pathToParams(url));
2019-03-04 12:33:02 +00:00
if (
currentParams.device !== requestedParams.device ||
currentParams.deviceInstance !== requestedParams.deviceInstance
) {
2018-10-02 15:33:49 +00:00
dispatch(goto(url));
}
};
/*
2019-03-04 12:33:02 +00:00
* Redirect to first device or landing page
*/
export const selectFirstAvailableDevice = (gotoRoot: boolean = false): ThunkAction => (
dispatch: Dispatch,
getState: GetState
): void => {
2018-10-02 15:33:49 +00:00
const url = dispatch(getFirstAvailableDeviceUrl());
if (url) {
const currentParams = getState().router.location.state;
const requestedParams = dispatch(pathToParams(url));
2019-03-04 12:33:02 +00:00
if (
gotoRoot ||
currentParams.device !== requestedParams.device ||
currentParams.deviceInstance !== requestedParams.deviceInstance
) {
dispatch(goto(url));
}
2018-09-20 16:24:28 +00:00
} else {
2018-09-21 12:01:41 +00:00
dispatch(gotoLandingPage());
2018-09-20 16:24:28 +00:00
}
2018-09-21 12:01:41 +00:00
};
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Internal method. redirect to given url
*/
2018-09-20 16:24:28 +00:00
const goto = (url: string): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
if (getState().router.location.pathname !== url) {
2018-09-21 12:01:41 +00:00
dispatch(push(url));
2018-09-20 16:24:28 +00:00
}
2018-09-21 12:01:41 +00:00
};
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Check if requested OR current url is landing page
*/
export const isLandingPageUrl = (
$url?: string,
checkRoutes: boolean = false
): PayloadAction<boolean> => (dispatch: Dispatch, getState: GetState): boolean => {
2018-09-21 12:01:41 +00:00
let url: ?string = $url;
2018-09-20 16:24:28 +00:00
if (typeof url !== 'string') {
url = getState().router.location.pathname;
}
if (checkRoutes) {
2019-03-04 12:33:02 +00:00
const isLandingRoute = routes.find(
r => r.pattern === url && r.name.indexOf('landing') >= 0
);
if (isLandingRoute) {
return true;
}
}
2018-09-25 10:19:09 +00:00
return url === '/';
2018-09-21 12:01:41 +00:00
};
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Try to redirect to landing page
*/
2018-09-21 12:01:41 +00:00
export const gotoLandingPage = (): ThunkAction => (dispatch: Dispatch): void => {
const isLandingPage = dispatch(isLandingPageUrl());
2018-09-20 16:24:28 +00:00
if (!isLandingPage) {
2018-09-21 12:01:41 +00:00
dispatch(goto('/'));
2018-09-20 16:24:28 +00:00
}
2018-09-21 12:01:41 +00:00
};
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Go to given device settings page
*/
export const gotoDeviceSettings = (device: TrezorDevice): ThunkAction => (
dispatch: Dispatch
): void => {
2018-09-20 16:24:28 +00:00
if (device.features) {
2019-03-04 12:33:02 +00:00
const devUrl: string = `${device.features.device_id}${
device.instance ? `:${device.instance}` : ''
}`;
2018-09-21 12:01:41 +00:00
dispatch(goto(`/device/${devUrl}/settings`));
2018-09-20 16:24:28 +00:00
}
};
2018-10-04 17:47:10 +00:00
/*
2019-03-04 12:33:02 +00:00
* Go to UpdateBridge page
*/
2018-10-04 17:47:10 +00:00
export const gotoBridgeUpdate = (): ThunkAction => (dispatch: Dispatch): void => {
dispatch(goto('/bridge'));
};
/*
2019-03-04 12:33:02 +00:00
* Go to UpdateFirmware page
* Called from App notification
*/
export const gotoFirmwareUpdate = (): ThunkAction => (
dispatch: Dispatch,
getState: GetState
): void => {
const { selectedDevice } = getState().wallet;
if (!selectedDevice || !selectedDevice.features) return;
2019-03-04 12:33:02 +00:00
const devUrl: string = `${selectedDevice.features.device_id}${
selectedDevice.instance ? `:${selectedDevice.instance}` : ''
}`;
dispatch(goto(`/device/${devUrl}/firmware-update`));
2018-10-04 17:47:10 +00:00
};
2019-02-25 12:28:41 +00:00
/*
2019-03-04 12:33:02 +00:00
* Go to NoBackup page
*/
2019-02-25 12:28:41 +00:00
export const gotoBackup = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const { selectedDevice } = getState().wallet;
if (!selectedDevice || !selectedDevice.features) return;
2019-03-04 12:33:02 +00:00
const devUrl: string = `${selectedDevice.features.device_id}${
selectedDevice.instance ? `:${selectedDevice.instance}` : ''
}`;
2019-02-25 12:28:41 +00:00
dispatch(goto(`/device/${devUrl}/backup`));
};
2018-09-20 16:24:28 +00:00
/*
2019-03-04 12:33:02 +00:00
* Try to redirect to initial url
*/
export const setInitialUrl = (): PayloadAction<boolean> => (
dispatch: Dispatch,
getState: GetState
): boolean => {
const { initialPathname } = getState().wallet;
if (typeof initialPathname === 'string' && !dispatch(isLandingPageUrl(initialPathname, true))) {
2019-03-04 12:33:02 +00:00
const valid = dispatch(
getValidUrl({
type: LOCATION_CHANGE,
payload: {
location: {
pathname: initialPathname,
hash: '',
search: '',
state: {},
},
2018-12-18 16:05:06 +00:00
},
2019-03-04 12:33:02 +00:00
})
);
if (valid === initialPathname) {
// reset initial url
dispatch({
type: SET_INITIAL_URL,
});
2018-09-21 12:01:41 +00:00
dispatch(goto(valid));
return true;
}
}
2018-09-20 16:24:28 +00:00
return false;
2018-09-21 12:01:41 +00:00
};