diff --git a/app/actions/TrezorActions.js b/app/actions/TrezorActions.js index 76d1f2d7e8..1d0f950912 100644 --- a/app/actions/TrezorActions.js +++ b/app/actions/TrezorActions.js @@ -46,6 +46,7 @@ let setListeners = false; export const TRZ_WALLET_CLOSED = "TRZ_WALLET_CLOSED"; export const TRZ_TREZOR_ENABLED = "TRZ_TREZOR_ENABLED"; +export const TRZ_TREZORD_STARTED = "TRZ_TREZORD_STARTED"; // enableTrezor attepts to start a connection with connect if none exist and // connect to a trezor device. @@ -56,7 +57,11 @@ export const enableTrezor = () => async (dispatch, getState) => { setDeviceListeners(dispatch, getState); setListeners = true; } - await trezord.start(); + const trezordStarted = getState().trezor.trezordStarted; + if (!trezordStarted) { + await trezord.start(); + dispatch({ type: TRZ_TREZORD_STARTED }); + } connect()(dispatch, getState); }; @@ -83,22 +88,27 @@ export const initTransport = async (session, debug) => { export const TRZ_CONNECT_ATTEMPT = "TRZ_CONNECT_ATTEMPT"; export const TRZ_CONNECT_FAILED = "TRZ_CONNECT_FAILED"; export const TRZ_CONNECT_SUCCESS = "TRZ_CONNECT_SUCCESS"; +export const TRZ_INIT_SUCCESS = "TRZ_INIT_SUCCESS"; export const TRZ_UDEV_ERROR = "TREZOR_UDEV_ERROR"; export const connect = () => async (dispatch, getState) => { const { - trezor: { connected, connectAttempt } + trezor: { connected, connectAttempt, initted } } = getState(); if (connected || connectAttempt) return; dispatch({ type: TRZ_CONNECT_ATTEMPT }); wallet.allowExternalRequest(EXTERNALREQUEST_TREZOR_BRIDGE); - const debug = getState().trezor.debug; - await initTransport(session, debug).catch((error) => { - dispatch({ error, type: TRZ_CONNECT_FAILED }); - return; - }); + // We can only ever init transport once. + if (!initted) { + const debug = getState().trezor.debug; + await initTransport(session, debug).catch((error) => { + dispatch({ error, type: TRZ_CONNECT_FAILED }); + return; + }); + dispatch({ type: TRZ_INIT_SUCCESS }); + } try { await dispatch(getFeatures()); } catch (err) { diff --git a/app/reducers/trezor.js b/app/reducers/trezor.js index 4ab930fa77..7375f758a6 100644 --- a/app/reducers/trezor.js +++ b/app/reducers/trezor.js @@ -48,6 +48,8 @@ import { TRZ_INITDEVICE_ATTEMPT, TRZ_INITDEVICE_FAILED, TRZ_INITDEVICE_SUCCESS, + TRZ_INIT_SUCCESS, + TRZ_TREZORD_STARTED, TRZ_UPDATEFIRMWARE_ATTEMPT, TRZ_UPDATEFIRMWARE_FAILED, TRZ_UPDATEFIRMWARE_SUCCESS, @@ -69,7 +71,7 @@ export default function trezor(state = {}, action) { case TRZ_TREZOR_ENABLED: return { ...state, enabled: true }; case TRZ_TREZOR_DISABLED: - return { ...state, enabled: false }; + return { ...state }; case TRZ_CONNECT_ATTEMPT: return { ...state, @@ -224,6 +226,10 @@ export default function trezor(state = {}, action) { case TRZ_WIPEDEVICE_ATTEMPT: case TRZ_INITDEVICE_ATTEMPT: return { ...state, performingOperation: true }; + case TRZ_INIT_SUCCESS: + return { ...state, initted: true }; + case TRZ_TREZORD_STARTED: + return { ...state, trezordStarted: true }; case TRZ_TOGGLEPINPROTECTION_ATTEMPT: return { ...state, @@ -328,7 +334,7 @@ export default function trezor(state = {}, action) { performingTogglePassphraseOnDeviceProtection: false }; case CLOSEWALLET_SUCCESS: - return { ...state, enabled: false }; + return { ...state }; case TRZ_UDEV_ERROR: return { ...state,