1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-03 13:48:15 +00:00
trezor-wallet/src/reducers/TrezorConnectReducer.js

111 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2019-04-11 14:00:57 +00:00
import { TRANSPORT, UI } from 'trezor-connect';
2018-08-14 13:11:52 +00:00
import * as CONNECT from 'actions/constants/TrezorConnect';
2018-02-20 09:30:36 +00:00
2018-08-20 11:01:43 +00:00
import type { Action } from 'flowtype';
2018-02-20 09:30:36 +00:00
export type SelectedDevice = {
2019-03-04 12:33:02 +00:00
id: string, // could be device path if unacquired or features.device_id
instance: ?number,
};
2017-12-13 11:01:37 +00:00
export type LatestBridge = {
2019-03-04 12:33:02 +00:00
version: Array<number>,
directory: string,
packages: Array<{ name: string, url: string, signature?: string, preferred: boolean }>,
changelog: Array<string>,
};
2018-04-16 21:19:50 +00:00
export type State = {
2019-03-04 12:33:02 +00:00
initialized: boolean,
error: ?string,
transport:
| {
type: string,
version: string,
outdated: boolean,
bridge: LatestBridge,
}
| {
type: null,
bridge: LatestBridge,
},
2018-05-05 11:52:03 +00:00
// browserState: {
// name: string;
// osname: string;
// supported: boolean;
// outdated: boolean;
// mobile: boolean;
// } | {};
2019-03-04 12:33:02 +00:00
browserState: any,
acquiringDevice: boolean,
};
2017-12-13 11:01:37 +00:00
const initialState: State = {
initialized: false,
2018-02-20 09:30:36 +00:00
error: null,
transport: {
type: null,
bridge: {
version: [],
directory: '',
packages: [],
changelog: [],
},
},
2018-05-23 09:46:57 +00:00
browserState: {},
acquiringDevice: false,
2017-12-13 11:01:37 +00:00
};
2018-04-16 21:19:50 +00:00
export default function connect(state: State = initialState, action: Action): State {
2017-12-13 11:01:37 +00:00
switch (action.type) {
// trezor-connect iframe didn't loaded properly
case CONNECT.INITIALIZATION_ERROR:
2018-05-23 09:46:57 +00:00
return {
...state,
error: action.error,
2018-07-30 10:52:13 +00:00
};
// trezor-connect iframe loaded
2019-04-11 14:00:57 +00:00
case UI.IFRAME_HANDSHAKE:
2017-12-13 11:01:37 +00:00
return {
...state,
initialized: true,
browserState: action.payload.browser,
2017-12-13 11:01:37 +00:00
};
// trezor-connect (trezor-link) initialized
2018-07-30 10:52:13 +00:00
case TRANSPORT.START:
return {
...state,
transport: action.payload,
2018-07-30 10:52:13 +00:00
error: null,
};
// trezor-connect (trezor-link)
// will be called continuously in interval until connection (bridge/webusb) will be established
2018-07-30 10:52:13 +00:00
case TRANSPORT.ERROR:
2017-12-13 11:01:37 +00:00
return {
...state,
// error: action.payload.error, // message is wrapped in "device" field. It's dispatched from TrezorConnect.on(DEVICE_EVENT...) in TrezorConnectService
2018-07-30 10:52:13 +00:00
error: 'Transport is missing',
transport: {
type: null,
bridge: action.payload.bridge,
},
2017-12-13 11:01:37 +00:00
};
2018-02-20 09:30:36 +00:00
case CONNECT.START_ACQUIRING:
return {
...state,
acquiringDevice: true,
};
case CONNECT.STOP_ACQUIRING:
return {
...state,
acquiringDevice: false,
};
2017-12-13 11:01:37 +00:00
default:
return state;
}
2019-03-04 12:33:02 +00:00
}