diff --git a/src/actions/TrezorConnectActions.js b/src/actions/TrezorConnectActions.js index b54a97a0..8f0bd2e8 100644 --- a/src/actions/TrezorConnectActions.js +++ b/src/actions/TrezorConnectActions.js @@ -71,6 +71,13 @@ export type TrezorConnectAction = { payload: Array } | { type: typeof CONNECT.START_ACQUIRING | typeof CONNECT.STOP_ACQUIRING, +} | { + type: typeof CONNECT.REQUEST_WALLET_TYPE, + device: TrezorDevice +} | { + type: typeof CONNECT.RECEIVE_WALLET_TYPE, + device: TrezorDevice, + hidden: boolean, }; export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { @@ -152,7 +159,19 @@ export const postInit = (): ThunkAction => (dispatch: Dispatch): void => { } }; -export const getSelectedDeviceState = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { +export const requestWalletType = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { + const selected = getState().wallet.selectedDevice; + if (!selected) return; + const isDeviceReady = selected.connected && selected.features && !selected.state && selected.mode === 'normal' && selected.firmware !== 'required'; + if (!isDeviceReady) return; + + dispatch({ + type: CONNECT.REQUEST_WALLET_TYPE, + device: selected, + }); +}; + +export const authorizeDevice = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { const selected = getState().wallet.selectedDevice; if (!selected) return; const isDeviceReady = selected.connected && selected.features && !selected.state && selected.mode === 'normal' && selected.firmware !== 'required'; @@ -164,7 +183,7 @@ export const getSelectedDeviceState = (): AsyncAction => async (dispatch: Dispat instance: selected.instance, state: selected.state, }, - useEmptyPassphrase: !selected.instance, + useEmptyPassphrase: selected.useEmptyPassphrase, }); if (response && response.success) { @@ -190,14 +209,13 @@ export const getSelectedDeviceState = (): AsyncAction => async (dispatch: Dispat type: NOTIFICATION.CLOSE, payload: { devicePath: selected.path }, }); - dispatch(getSelectedDeviceState()); + dispatch(authorizeDevice()); }, }, ], }, }); } - }; export const deviceDisconnect = (device: Device): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { @@ -231,11 +249,14 @@ export function acquire(): AsyncAction { type: CONNECT.START_ACQUIRING, }); + // this is the only place where useEmptyPassphrase should be used every time + // the goal here is to acquire device and get his features + // authentication (passphrase) is not needed here yet const response = await TrezorConnect.getFeatures({ device: { path: selected.path, }, - useEmptyPassphrase: !selected.instance, + useEmptyPassphrase: true, }); if (!response.success) { @@ -270,7 +291,7 @@ export const forget = (device: TrezorDevice): Action => ({ device, }); -export const duplicateDevice = (device: TrezorDevice): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { +export const duplicateDevice1 = (device: TrezorDevice): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { // dispatch({ // type: CONNECT.TRY_TO_DUPLICATE, // device, @@ -283,3 +304,15 @@ export const duplicateDevice = (device: TrezorDevice): AsyncAction => async (dis device: { ...device, ...extended }, }); }; + +export const duplicateDevice = (device: TrezorDevice): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { + // dispatch({ + // type: CONNECT.TRY_TO_DUPLICATE, + // device, + // }); + + dispatch({ + type: CONNECT.REQUEST_WALLET_TYPE, + device, + }); +}; diff --git a/src/actions/WalletActions.js b/src/actions/WalletActions.js index a7325d25..b477ce14 100644 --- a/src/actions/WalletActions.js +++ b/src/actions/WalletActions.js @@ -87,6 +87,7 @@ export const clearUnavailableDevicesData = (prevState: State, device: Device): T const actions = [ LOCATION_CHANGE, CONNECT.AUTH_DEVICE, + CONNECT.RECEIVE_WALLET_TYPE, ...Object.values(DEVICE).filter(v => typeof v === 'string'), ]; diff --git a/src/actions/constants/TrezorConnect.js b/src/actions/constants/TrezorConnect.js index a49371c5..7a6a446d 100644 --- a/src/actions/constants/TrezorConnect.js +++ b/src/actions/constants/TrezorConnect.js @@ -25,4 +25,7 @@ export const DUPLICATE: 'connect__duplicate' = 'connect__duplicate'; export const DEVICE_STATE_EXCEPTION: 'connect__device_state_exception' = 'connect__device_state_exception'; export const START_ACQUIRING: 'connect__start_acquiring' = 'connect__start_acquiring'; -export const STOP_ACQUIRING: 'connect__stop_acquiring' = 'connect__stop_acquiring'; \ No newline at end of file +export const STOP_ACQUIRING: 'connect__stop_acquiring' = 'connect__stop_acquiring'; + +export const REQUEST_WALLET_TYPE: 'connect__request_wallet_type' = 'connect__request_wallet_type'; +export const RECEIVE_WALLET_TYPE: 'connect__receive_wallet_type' = 'connect__receive_wallet_type'; \ No newline at end of file diff --git a/src/services/WalletService.js b/src/services/WalletService.js index 3cfe4ddb..0e2b87d2 100644 --- a/src/services/WalletService.js +++ b/src/services/WalletService.js @@ -49,10 +49,9 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa api.dispatch(LocalStorageActions.loadData()); break; case WALLET.SET_SELECTED_DEVICE: - if (action.device) { - // try to authorize device - api.dispatch(TrezorConnectActions.getSelectedDeviceState()); - } + // try to authorize device + // api.dispatch(TrezorConnectActions.authorizeDevice()); + api.dispatch(TrezorConnectActions.requestWalletType()); break; case DEVICE.CONNECT: api.dispatch(WalletActions.clearUnavailableDevicesData(prevState, action.device)); @@ -102,10 +101,18 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa // if "selectedAccount" didn't change observe send form props changes api.dispatch(SendFormActionActions.observe(prevState, action)); } - } else if (action.type === CONNECT.AUTH_DEVICE) { - // selected device did changed - // try to restore discovery after device authentication - api.dispatch(DiscoveryActions.restore()); + } else { + switch (action.type) { + case CONNECT.AUTH_DEVICE: + // selected device did changed + // try to restore discovery after device authentication + api.dispatch(DiscoveryActions.restore()); + break; + case CONNECT.RECEIVE_WALLET_TYPE: + api.dispatch(TrezorConnectActions.authorizeDevice()); + break; + default: break; + } } // even if "selectedDevice" didn't change because it was updated on DEVICE.CHANGED before DEVICE.CONNECT action