Skip to content

Commit

Permalink
♻️ Update middleware for hwWallet remove login moddleware and add hwW…
Browse files Browse the repository at this point in the history
…allet
  • Loading branch information
Osvaldo Vega committed Apr 24, 2019
1 parent 149e750 commit 6c20e0d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 86 deletions.
2 changes: 1 addition & 1 deletion app/src/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const ledgerObserver = {
const liskAccount = await getLiskAccount(device.path);
const ledgerDevice = createLedgerHWDevice(liskAccount, device.path);
addConnectedDevices(ledgerDevice);
hwDevice = device;
hwDevice = ledgerDevice;
win.send({ event: 'hwConnected', value: { model: ledgerDevice.model } });
}
} else if (type === 'remove') {
Expand Down
74 changes: 74 additions & 0 deletions src/store/middlewares/hwWallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import actionTypes from '../../constants/actions';
import { accountLoggedOut } from '../../actions/account';
import { dialogDisplayed, dialogHidden } from '../../actions/dialog';
import { successToastDisplayed, errorToastDisplayed, infoToastDisplayed } from '../../actions/toaster';
import { HW_MSG } from '../../constants/hwConstants';
import Alert from '../../components/dialog/alert';

const hwWalletMiddleware = store => next => (action) => {
const { ipc } = window;

if (action.type === actionTypes.storeCreated && ipc) {
const util = require('util');

store.dispatch({
type: actionTypes.settingsUpdated,
data: { isHarwareWalletConnected: false },
});

ipc.on('hwConnected', (event, { model }) => {
store.dispatch({
type: actionTypes.settingsUpdated,
data: { isHarwareWalletConnected: true },
});

store.dispatch(successToastDisplayed({ label: `${model} connected` }));
});

ipc.on('hwDisconnected', (event, { model }) => {
const state = store.getState();
const { account } = state;

if (account.address) {
store.dispatch(dialogDisplayed({
childComponent: Alert,
childComponentProps: {
title: 'You are disconnected',
text: `There is no connection to the ${model}. Please check the cables if it happened by accident.`,
closeDialog: () => {
store.dispatch(dialogHidden());
location.reload(); // eslint-disable-line
},
},
}));

store.dispatch(accountLoggedOut());
}

store.dispatch({
type: actionTypes.settingsUpdated,
data: { isHarwareWalletConnected: false },
});

store.dispatch(successToastDisplayed({ label: `${model} disconnected` }));
});

ipc.on('trezorButtonCallback', (event, data) => {
store.dispatch(infoToastDisplayed({
label: util.format(HW_MSG.TREZOR_ASK_FOR_CONFIRMATION, data),
}));
});

ipc.on('trezorParamMessage', (event, data) => {
store.dispatch(infoToastDisplayed({ label: HW_MSG[data] }));
});

ipc.on('trezorError', () => {
store.dispatch(errorToastDisplayed({ label: HW_MSG.ERROR_OR_DEVICE_IS_NOT_CONNECTED }));
});
}

next(action);
};

export default hwWalletMiddleware;
14 changes: 7 additions & 7 deletions src/store/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import peersMiddleware from './peers';
import accountMiddleware from './account';
import loadingBarMiddleware from './loadingBar';
import offlineMiddleware from './offline';
import loginMiddleware from './login';
import hwWalletMiddleware from './hwWallet';
// ToDo : enable this one when you solve the problem with multi account management
// import notificationMiddleware from './notification';
import votingMiddleware from './voting';
import followedAccountsMiddleware from './followedAccounts';
import socketMiddleware from './socket';

export default [
thunk,
peersMiddleware,
socketMiddleware,
// notificationMiddleware,
accountMiddleware,
followedAccountsMiddleware,
hwWalletMiddleware,
loadingBarMiddleware,
offlineMiddleware,
loginMiddleware,
// notificationMiddleware,
peersMiddleware,
socketMiddleware,
thunk,
votingMiddleware,
followedAccountsMiddleware,
];
54 changes: 0 additions & 54 deletions src/store/middlewares/login.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/utils/hwWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,12 @@ import i18next from 'i18next';
import { LedgerAccount, SupportedCoin, DposLedger } from 'dpos-ledger-api';
import { HW_CMD, HW_MSG, calculateSecondPassphraseIndex } from '../constants/hwConstants';
import { loadingStarted, loadingFinished } from './loading';
import { infoToastDisplayed, errorToastDisplayed } from '../actions/toaster';
import { getTransactionBytes, calculateTxId, getBufferToHex } from './rawTransactionWrapper';
import { PLATFORM_TYPES, getPlatformType } from './platform';
import store from '../store';
import loginTypes from '../constants/loginTypes';

const util = require('util');

const { ipc } = window;

if (ipc) { // On browser-mode is undefined
ipc.on('ledgerButtonCallback', () => {
// store.dispatch(infoToastDisplayed({ label: HW_MSG.LEDGER_ASK_FOR_CONFIRMATION }));
});

ipc.on('trezorButtonCallback', (event, data) => {
store.dispatch(infoToastDisplayed({
label: util.format(HW_MSG.TREZOR_ASK_FOR_CONFIRMATION, data),
}));
});

ipc.on('trezorParamMessage', (event, data) => {
store.dispatch(errorToastDisplayed({ label: HW_MSG[data] }));
});

ipc.on('trezorError', () => {
store.dispatch(errorToastDisplayed({ label: HW_MSG.ERROR_OR_DEVICE_IS_NOT_CONNECTED }));
});
}

const getLedgerTransportU2F = async () => TransportU2F.create();

export const getLedgerAccount = (index = 0) => {
Expand Down

0 comments on commit 6c20e0d

Please sign in to comment.