Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed May 28, 2024
1 parent 728b813 commit 3502b2b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
24 changes: 17 additions & 7 deletions app/actions/TrezorActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
};

Expand All @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions app/reducers/trezor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3502b2b

Please sign in to comment.