1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-01-23 06:21:06 +00:00
trezor-wallet/src/reducers/TrezorConnectReducer.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
2018-09-06 15:04:28 +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 = {
2018-03-08 16:10:53 +00:00
id: string; // could be device path if unacquired or features.device_id
2018-02-20 09:30:36 +00:00
instance: ?number;
}
2017-12-13 11:01:37 +00:00
export type LatestBridge = {
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 = {
initialized: boolean;
2018-04-16 21:19:50 +00:00
error: ?string;
transport: {
2018-05-05 11:52:03 +00:00
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;
// } | {};
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
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;
}
}