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

66 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2019-03-04 12:33:02 +00:00
import { TRANSPORT, DEVICE, BLOCKCHAIN } from 'trezor-connect';
2018-08-14 13:11:52 +00:00
import * as TrezorConnectActions from 'actions/TrezorConnectActions';
import * as BlockchainActions from 'actions/BlockchainActions';
import * as RouterActions from 'actions/RouterActions';
2018-08-14 13:11:52 +00:00
import * as ModalActions from 'actions/ModalActions';
import * as STORAGE from 'actions/constants/localStorage';
import * as CONNECT from 'actions/constants/TrezorConnect';
2018-09-13 12:40:41 +00:00
import { READY as BLOCKCHAIN_READY } from 'actions/constants/blockchain';
2017-12-13 11:01:37 +00:00
2019-03-04 12:33:02 +00:00
import type { Middleware, MiddlewareAPI, MiddlewareDispatch, State, Action } from 'flowtype';
2017-12-13 11:01:37 +00:00
2019-03-04 12:33:02 +00:00
const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (
action: Action
): Action => {
2018-09-06 15:04:28 +00:00
// const prevState: $ElementType<State, 'connect'> = api.getState().connect;
2018-04-16 21:19:50 +00:00
const prevModalState: $ElementType<State, 'modal'> = api.getState().modal;
2018-09-06 15:04:28 +00:00
// const prevRouterState: $ElementType<State, 'router'> = api.getState().router;
2018-02-20 09:30:36 +00:00
2017-12-13 11:01:37 +00:00
next(action);
2018-02-20 09:30:36 +00:00
if (action.type === STORAGE.READY) {
2018-07-30 10:52:13 +00:00
api.dispatch(TrezorConnectActions.init());
2018-02-20 09:30:36 +00:00
} else if (action.type === TRANSPORT.ERROR) {
// TODO: check if modal is open
// api.dispatch( RouterActions.gotoLandingPage() );
2018-09-03 15:43:17 +00:00
} else if (action.type === TRANSPORT.START) {
2018-09-13 12:40:41 +00:00
api.dispatch(BlockchainActions.init());
} else if (action.type === BLOCKCHAIN_READY) {
2018-07-30 10:52:13 +00:00
api.dispatch(TrezorConnectActions.postInit());
2018-02-20 09:30:36 +00:00
} else if (action.type === DEVICE.DISCONNECT) {
2018-07-30 10:52:13 +00:00
api.dispatch(TrezorConnectActions.deviceDisconnect(action.device));
2018-03-08 16:10:53 +00:00
} else if (action.type === CONNECT.REMEMBER_REQUEST) {
2018-07-30 10:52:13 +00:00
api.dispatch(ModalActions.onRememberRequest(prevModalState));
2018-02-20 09:30:36 +00:00
} else if (action.type === CONNECT.FORGET) {
2018-04-16 21:19:50 +00:00
//api.dispatch( TrezorConnectActions.forgetDevice(action.device) );
2018-09-21 12:01:41 +00:00
api.dispatch(RouterActions.selectFirstAvailableDevice());
2018-02-20 09:30:36 +00:00
} else if (action.type === CONNECT.FORGET_SINGLE) {
if (api.getState().devices.length < 1 && action.device.connected) {
2018-05-05 11:52:03 +00:00
// prompt disconnect device info in LandingPage
2018-04-16 21:19:50 +00:00
api.dispatch({
2018-02-20 09:30:36 +00:00
type: CONNECT.DISCONNECT_REQUEST,
2018-07-30 10:52:13 +00:00
device: action.device,
2017-12-13 11:01:37 +00:00
});
2018-09-21 12:01:41 +00:00
api.dispatch(RouterActions.gotoLandingPage());
2018-02-20 09:30:36 +00:00
} else {
2018-09-21 12:01:41 +00:00
api.dispatch(RouterActions.selectFirstAvailableDevice());
2018-02-20 09:30:36 +00:00
}
} else if (action.type === DEVICE.CONNECT || action.type === DEVICE.CONNECT_UNACQUIRED) {
2018-07-30 10:52:13 +00:00
api.dispatch(ModalActions.onDeviceConnect(action.device));
2018-02-20 09:30:36 +00:00
} else if (action.type === CONNECT.DUPLICATE) {
api.dispatch(RouterActions.selectDevice(action.device));
} else if (action.type === BLOCKCHAIN.BLOCK) {
2018-11-29 20:09:31 +00:00
api.dispatch(BlockchainActions.onBlockMined(action.payload));
} else if (action.type === BLOCKCHAIN.NOTIFICATION) {
2018-11-29 20:09:31 +00:00
api.dispatch(BlockchainActions.onNotification(action.payload));
2018-09-13 12:40:41 +00:00
} else if (action.type === BLOCKCHAIN.ERROR) {
api.dispatch(BlockchainActions.onError(action.payload));
2017-12-13 11:01:37 +00:00
}
2018-04-16 21:19:50 +00:00
return action;
2018-07-30 10:52:13 +00:00
};
2017-12-13 11:01:37 +00:00
2019-03-04 12:33:02 +00:00
export default TrezorConnectService;