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
2018-10-04 15:08:02 +00:00
export type LatestBridge = {
version : Array < number > ;
directory : string ;
packages : Array < { name : string ; url : string ; signature ? : string ; preferred : boolean ; } > ;
2018-10-05 07:24:36 +00:00
changelog : Array < string > ;
2018-10-04 15:08:02 +00:00
}
2018-04-16 21:19:50 +00:00
export type State = {
2018-10-02 08:05:51 +00:00
initialized : boolean ;
2018-04-16 21:19:50 +00:00
error : ? string ;
2018-10-04 15:08:02 +00:00
transport : {
2018-05-05 11:52:03 +00:00
type : string ;
version : string ;
2018-10-04 15:08:02 +00:00
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;
// } | {};
2018-03-27 15:12:01 +00:00
browserState : any ;
2018-10-02 08:05:51 +00:00
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-11 10:06:46 +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
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 ;
}
}