You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/TrezorConnectReducer.js

109 lines
2.9 KiB

7 years ago
/* @flow */
import { TRANSPORT, UI } from 'trezor-connect';
import * as CONNECT from 'actions/constants/TrezorConnect';
6 years ago
6 years ago
import type { Action } from 'flowtype';
6 years ago
export type SelectedDevice = {
6 years ago
id: string; // could be device path if unacquired or features.device_id
6 years ago
instance: ?number;
}
7 years ago
export type LatestBridge = {
version: Array<number>;
directory: string;
packages: Array<{ name: string; url: string; signature?: string; preferred: boolean; }>;
changelog: Array<string>;
}
export type State = {
initialized: boolean;
error: ?string;
transport: {
6 years ago
type: string;
version: string;
outdated: boolean;
bridge: LatestBridge;
} | {
type: null,
bridge: LatestBridge;
6 years ago
};
// browserState: {
// name: string;
// osname: string;
// supported: boolean;
// outdated: boolean;
// mobile: boolean;
// } | {};
browserState: any;
acquiringDevice: boolean;
7 years ago
}
const initialState: State = {
initialized: false,
6 years ago
error: null,
transport: {
type: null,
bridge: {
version: [],
directory: '',
packages: [],
changelog: [],
},
},
browserState: {},
acquiringDevice: false,
7 years ago
};
export default function connect(state: State = initialState, action: Action): State {
7 years ago
switch (action.type) {
// trezor-connect iframe didn't loaded properly
case CONNECT.INITIALIZATION_ERROR:
return {
...state,
error: action.error,
};
// trezor-connect iframe loaded
case UI.IFRAME_HANDSHAKE:
7 years ago
return {
...state,
initialized: true,
browserState: action.payload.browser,
7 years ago
};
// trezor-connect (trezor-link) initialized
case TRANSPORT.START:
return {
...state,
transport: action.payload,
error: null,
};
// trezor-connect (trezor-link)
// will be called continuously in interval until connection (bridge/webusb) will be established
case TRANSPORT.ERROR:
7 years ago
return {
...state,
// error: action.payload.error, // message is wrapped in "device" field. It's dispatched from TrezorConnect.on(DEVICE_EVENT...) in TrezorConnectService
error: 'Transport is missing',
transport: {
type: null,
bridge: action.payload.bridge,
},
7 years ago
};
6 years ago
case CONNECT.START_ACQUIRING:
return {
...state,
acquiringDevice: true,
};
case CONNECT.STOP_ACQUIRING:
return {
...state,
acquiringDevice: false,
};
7 years ago
default:
return state;
}
}