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
2018-10-04 15:08:02 +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-10-04 15:08:02 +00:00
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 = {
2018-10-02 08:05:51 +00:00
initialized : false ,
2018-02-20 09:30:36 +00:00
error : null ,
2018-10-04 15:08:02 +00:00
transport : {
type : null ,
bridge : {
version : [ ] ,
directory : '' ,
packages : [ ] ,
2018-10-05 07:24:36 +00:00
changelog : [ ] ,
2018-10-04 15:08:02 +00:00
} ,
} ,
2018-05-23 09:46:57 +00:00
browserState : { } ,
2018-10-02 08:05:51 +00:00
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 ) {
2018-10-02 08:05:51 +00:00
// trezor-connect iframe didn't loaded properly
case CONNECT . INITIALIZATION _ERROR :
2018-05-23 09:46:57 +00:00
return {
... state ,
2018-10-02 08:05:51 +00:00
error : action . error ,
2018-07-30 10:52:13 +00:00
} ;
2018-10-02 08:05:51 +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 ,
2018-10-02 08:05:51 +00:00
initialized : true ,
browserState : action . payload . browser ,
2017-12-13 11:01:37 +00:00
} ;
2018-10-02 08:05:51 +00:00
// trezor-connect (trezor-link) initialized
2018-07-30 10:52:13 +00:00
case TRANSPORT . START :
2018-03-27 15:12:01 +00:00
return {
... state ,
2018-04-05 10:44:16 +00:00
transport : action . payload ,
2018-07-30 10:52:13 +00:00
error : null ,
} ;
2018-10-02 08:05:51 +00:00
// 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 ,
2018-10-04 15:08:02 +00:00
// 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' ,
2018-10-04 15:08:02 +00:00
transport : {
type : null ,
bridge : action . payload . bridge ,
} ,
2017-12-13 11:01:37 +00:00
} ;
2018-02-20 09:30:36 +00:00
2018-10-02 08:05:51 +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
}