From f507bfcec21e9df443c5db76a9c12fa2f618ca2f Mon Sep 17 00:00:00 2001 From: Osvaldo Vega Date: Thu, 18 Apr 2019 16:05:22 +0200 Subject: [PATCH 1/6] :: --- src/components/hwWallet/trezorLogin.js | 11 ++--- src/store/middlewares/login.js | 41 +----------------- src/utils/api/hwWallet.js | 4 -- src/utils/hwWallet.js | 58 +++++++++++++++++--------- src/utils/ledger.js | 1 + 5 files changed, 47 insertions(+), 68 deletions(-) diff --git a/src/components/hwWallet/trezorLogin.js b/src/components/hwWallet/trezorLogin.js index bc26fe9e9f..4128d7bf6e 100644 --- a/src/components/hwWallet/trezorLogin.js +++ b/src/components/hwWallet/trezorLogin.js @@ -34,6 +34,7 @@ class TrezorLogin extends React.Component { loginType: null, publicKey: null, address: null, + deviceId: null, }; if (ipc) { @@ -66,6 +67,7 @@ class TrezorLogin extends React.Component { loginType, publicKey, address: extractAddress(publicKey), + deviceId }); // Retrieve Address with verification @@ -91,7 +93,7 @@ class TrezorLogin extends React.Component { network: this.props.network, hwInfo: { device: this.props.device, - deviceId: this.props.device.deviceId, + deviceId: this.state.deviceId, derivationIndex: 0, }, }); @@ -102,6 +104,7 @@ class TrezorLogin extends React.Component { async componentDidMount() { this.setState({ isLoading: true }); const devices = await getDeviceList(); + setTimeout(async () => { const output = await displayAccounts({ liskAPIClient: this.props.liskAPIClient, @@ -124,12 +127,11 @@ class TrezorLogin extends React.Component { selectAccount(ledgerAccount, index) { Piwik.trackingEvent('TrezorLogin', 'button', 'Select account'); - // set active peer this.props.liskAPIClientSet({ publicKey: ledgerAccount.publicKey, network: this.props.network, - hwInfo: { // Use pubKey[0] first 10 char as device id - deviceId: ledgerAccount.publicKey.substring(0, 10), + hwInfo: { + deviceId: this.state.deviceId, derivationIndex: index, }, }); @@ -137,7 +139,6 @@ class TrezorLogin extends React.Component { async addAccount() { Piwik.trackingEvent('TrezorLogin', 'button', 'Add account'); - console.log(this.state.hwAccounts); const devices = await getDeviceList(); if (this.state.hwAccounts[this.state.hwAccounts.length - 1].isInitialized) { diff --git a/src/store/middlewares/login.js b/src/store/middlewares/login.js index 117feefeac..f23f749480 100644 --- a/src/store/middlewares/login.js +++ b/src/store/middlewares/login.js @@ -1,14 +1,7 @@ - -import { accountLoggedOut } from '../../actions/account'; import actionTypes from '../../constants/actions'; -import { HW_MSG } from '../../constants/hwConstants'; -import { errorToastDisplayed } from '../../actions/toaster'; -import { dialogDisplayed, dialogHidden } from '../../actions/dialog'; -import Alert from '../../components/dialog/alert'; -const loginMiddleware = store => next => (action) => { // eslint-disable-line max-statements +const loginMiddleware = store => next => (action) => { const { ipc } = window; - switch (action.type) { case actionTypes.storeCreated: if (ipc) { // On browser-mode is undefined @@ -16,38 +9,6 @@ const loginMiddleware = store => next => (action) => { // eslint-disable-line ma type: actionTypes.settingsUpdated, data: { isHarwareWalletConnected: false }, }); - - ipc.on('ledgerConnected', () => { - store.dispatch({ - type: actionTypes.settingsUpdated, - data: { isHarwareWalletConnected: true }, - }); - store.dispatch(errorToastDisplayed({ label: HW_MSG.LEDGER_CONNECTED })); - }); - - ipc.on('ledgerDisconnected', () => { - const state = store.getState(); - const { account } = state; - if (account.address) { - store.dispatch( // eslint-disable-line - dialogDisplayed({ - childComponent: Alert, - childComponentProps: { - title: 'You are disconnected', - text: 'There is no connection to the Ledger Nano S. 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 }, - }); - }); } break; default: break; diff --git a/src/utils/api/hwWallet.js b/src/utils/api/hwWallet.js index bda10edd79..559a7ded2a 100644 --- a/src/utils/api/hwWallet.js +++ b/src/utils/api/hwWallet.js @@ -287,19 +287,15 @@ export const getHWAccountInfo = async (activePeer, deviceId, loginType, accountI export const sendWithHW = (activePeer, account, recipientId, amount, pin = null, data = null) => new Promise(async (resolve, reject) => { - console.log(account.publicKey, recipientId, amount, data); const rawTx = createSendTX(account.publicKey, recipientId, amount, data); let error; let signedTx; - console.log('signTransactionWithHW', rawTx); [error, signedTx] = await to(signTransactionWithHW(rawTx, account, pin)); if (error) { reject(error); } else { activePeer.transactions.broadcast(signedTx).then(() => { - console.log('signedTx', signedTx); - resolve(signedTx); }).catch(reject); } diff --git a/src/utils/hwWallet.js b/src/utils/hwWallet.js index be1dc991c4..e4d6982422 100644 --- a/src/utils/hwWallet.js +++ b/src/utils/hwWallet.js @@ -10,37 +10,57 @@ import { getTransactionBytes, calculateTxId, getBufferToHex } from './rawTransac import { PLATFORM_TYPES, getPlatformType } from './platform'; import store from '../store'; import loginTypes from '../constants/loginTypes'; +import { accountLoggedOut } from '../actions/account'; +import { dialogDisplayed, dialogHidden } from '../actions/dialog'; +import Alert from '../components/dialog/alert'; const util = require('util'); const { ipc } = window; +const handleConnect = () => { + store.dispatch({ + type: actionTypes.settingsUpdated, + data: { isHarwareWalletConnected: true }, + }); + store.dispatch(errorToastDisplayed({ label: HW_MSG.LEDGER_CONNECTED })); +}; + +const handleDisconnect = (hwName) => { + const state = store.getState(); + const { account } = state; + if (account.address) { + store.dispatch( // eslint-disable-line + dialogDisplayed({ + childComponent: Alert, + childComponentProps: { + title: 'You are disconnected', + text: `There is no connection to the ${hwName}. 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 }, + }); +}; + if (ipc) { // On browser-mode is undefined - // ipc.on('ledgerConnected', () => { - // store.dispatch(infoToastDisplayed({ label: HW_MSG.LEDGER_CONNECTED })); - // }); + ipc.on('ledgerConnected', handleConnect); + ipc.on('ledgerDisconnected', () => handleDisconnect('Ledger Nano S')); - // ipc.on('ledgerDisconnected', () => { - // console.log('ledgerDisconnected'); - // store.dispatch(errorToastDisplayed({ label: HW_MSG.LEDGER_DISCONNECTED })); - // }); + ipc.on('trezorConnected', handleConnect); + ipc.on('trezorDisconnected', () => handleDisconnect('Trezor Model T')); ipc.on('ledgerButtonCallback', () => { // store.dispatch(infoToastDisplayed({ label: HW_MSG.LEDGER_ASK_FOR_CONFIRMATION })); }); - ipc.on('trezorConnected', (event, data) => { - store.dispatch(infoToastDisplayed({ - label: util.format(HW_MSG.TREZOR_CONNECTED, data.model, data.label), - })); - }); - - ipc.on('trezorDisconnected', (event, data) => { - store.dispatch(errorToastDisplayed({ - label: util.format(HW_MSG.TREZOR_DISCONNECTED, data.model, data.label), - })); - }); - ipc.on('trezorButtonCallback', (event, data) => { store.dispatch(infoToastDisplayed({ label: util.format(HW_MSG.TREZOR_ASK_FOR_CONFIRMATION, data), diff --git a/src/utils/ledger.js b/src/utils/ledger.js index 627afe7945..0d40b9d738 100644 --- a/src/utils/ledger.js +++ b/src/utils/ledger.js @@ -37,6 +37,7 @@ export const displayAccounts = async ({ liskAPIClient, loginType, hwAccounts, t, while (accountInfo.isInitialized || index === 0); /* eslint-disable-next-line */ return { + deviceId, hwAccounts: accounts, isLoading: false, showNextAvailable: (index === 1), From 29871a8a23f32391ac9f06adb4a9ab7492feaebe Mon Sep 17 00:00:00 2001 From: Osvaldo Vega Date: Thu, 18 Apr 2019 17:12:35 +0200 Subject: [PATCH 2/6] :recycle: Update send confirm component when using HW --- i18n/locales/en/common.json | 2 -- src/components/hwWallet/trezorLogin.js | 4 +++- src/components/sendV2/summary/summary.js | 5 +++-- src/components/sendV2/summary/summary.test.js | 3 +++ src/utils/ledger.js | 2 ++ 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/i18n/locales/en/common.json b/i18n/locales/en/common.json index d8feb2bbee..67540b8043 100644 --- a/i18n/locales/en/common.json +++ b/i18n/locales/en/common.json @@ -88,7 +88,6 @@ "Confirm": "Confirm", "Confirm (Fee: 1 LSK)": "Confirm (Fee: 1 LSK)", "Confirm (Fee: {{fee}} LSK)": "Confirm (Fee: {{fee}} LSK)", - "Confirm on Ledger": "Confirm on Ledger", "Confirm to register your second passphrase on the blockchain.": "Confirm to register your second passphrase on the blockchain.", "Confirm transaction on Ledger Nano S": "Confirm transaction on Ledger Nano S", "Confirm transfer": "Confirm transfer", @@ -552,7 +551,6 @@ "Your Trezor Device is not initialized. Please do it with trezor software.": "Your Trezor Device is not initialized. Please do it with trezor software.", "Your Trezor Model T has an old Firmware. Please update it.": "Your Trezor Model T has an old Firmware. Please update it.", "Your Trezor One has an old Firmware. Please update it.": "Your Trezor One has an old Firmware. Please update it.", - "Your account has been created!": "Your account has been created!", "Your delegate name is being registered": "Your delegate name is being registered", "Your passphrase is both your login and passphrase to your Lisk Hub. It is provided during account registration.": "Your passphrase is both your login and passphrase to your Lisk Hub. It is provided during account registration.", "Your passphrase is used to access your Lisk ID.": "Your passphrase is used to access your Lisk ID.", diff --git a/src/components/hwWallet/trezorLogin.js b/src/components/hwWallet/trezorLogin.js index 4128d7bf6e..7e041f54db 100644 --- a/src/components/hwWallet/trezorLogin.js +++ b/src/components/hwWallet/trezorLogin.js @@ -67,7 +67,8 @@ class TrezorLogin extends React.Component { loginType, publicKey, address: extractAddress(publicKey), - deviceId + deviceId, + deviceModel: devices[0].model, }); // Retrieve Address with verification @@ -133,6 +134,7 @@ class TrezorLogin extends React.Component { hwInfo: { deviceId: this.state.deviceId, derivationIndex: index, + deviceModel: this.state.deviceModel, }, }); } diff --git a/src/components/sendV2/summary/summary.js b/src/components/sendV2/summary/summary.js index 6401ee8b60..8ec6510f00 100644 --- a/src/components/sendV2/summary/summary.js +++ b/src/components/sendV2/summary/summary.js @@ -144,16 +144,17 @@ class Summary extends React.Component { } render() { + const { account } = this.props; const { secondPassphrase, isHardwareWalletConnected } = this.state; let isBtnDisabled = secondPassphrase.hasSecondPassphrase && secondPassphrase.isValid !== '' && !secondPassphrase.isValid; isBtnDisabled = !isBtnDisabled && isHardwareWalletConnected; const confirmBtnMessage = isHardwareWalletConnected - ? this.props.t('Confirm on Ledger') + ? this.props.t(`Confirm on ${account.hwInfo.deviceModel}`) : this.props.t('Send {{amount}} LSK', { amount: this.props.fields.amount.value }); const title = isHardwareWalletConnected - ? this.props.t('Confirm transaction on Ledger Nano S') + ? this.props.t(`Confirm transaction on ${account.hwInfo.deviceModel}`) : this.props.t('Transaction summary'); return ( diff --git a/src/components/sendV2/summary/summary.test.js b/src/components/sendV2/summary/summary.test.js index d9ba56b81d..7b3d2cc2a6 100644 --- a/src/components/sendV2/summary/summary.test.js +++ b/src/components/sendV2/summary/summary.test.js @@ -51,6 +51,9 @@ describe('Summary', () => { account: { address: accounts['second passphrase account'].address, secondPublicKey: accounts['second passphrase account'].secondPublicKey, + hwInfo: { + deviceModel: 'Ledger Nano S', + }, }, failedTransactions: '', pendingTransactions: [], diff --git a/src/utils/ledger.js b/src/utils/ledger.js index 0d40b9d738..65eb642b18 100644 --- a/src/utils/ledger.js +++ b/src/utils/ledger.js @@ -7,6 +7,7 @@ export const displayAccounts = async ({ liskAPIClient, loginType, hwAccounts, t, let index = unInitializedAdded ? hwAccounts.length : 0; let accountInfo; const deviceId = device && device.deviceId; + const deviceModel = device && device.model; const accounts = []; do { @@ -37,6 +38,7 @@ export const displayAccounts = async ({ liskAPIClient, loginType, hwAccounts, t, while (accountInfo.isInitialized || index === 0); /* eslint-disable-next-line */ return { + deviceModel, deviceId, hwAccounts: accounts, isLoading: false, From bd35f741a177a5f51a6dbd1ce136b90d9ad6200c Mon Sep 17 00:00:00 2001 From: Osvaldo Vega Date: Sun, 21 Apr 2019 22:53:08 +0200 Subject: [PATCH 3/6] :recycle: Update ledger and trezor connect and disconnect functions --- app/src/ledger.js | 20 +++++------ app/src/main.js | 6 ---- app/src/trezor.js | 4 +-- i18n/locales/en/common.json | 2 ++ src/components/sendV2/summary/summary.js | 42 ++++++++++++------------ src/utils/hwWallet.js | 19 +++++------ 6 files changed, 43 insertions(+), 50 deletions(-) diff --git a/app/src/ledger.js b/app/src/ledger.js index 7386d27739..609517beb8 100644 --- a/app/src/ledger.js +++ b/app/src/ledger.js @@ -32,7 +32,7 @@ TransportNodeHid.setListenDevicesDebug((msg, ...args) => { */ let busy = false; TransportNodeHid.setListenDevicesPollingSkip(() => busy); -let ledgerPath = null; +let hwDevice = null; const getLedgerAccount = (index = 0) => { const ledgerAccount = new LedgerAccount(); ledgerAccount.coinIndex(SupportedCoin.LISK); @@ -85,14 +85,14 @@ const ledgerObserver = { const liskAccount = await getLiskAccount(device.path); const ledgerDevice = createLedgerHWDevice(liskAccount, device.path); addConnectedDevices(ledgerDevice); - ledgerPath = device.path; - win.send({ event: 'ledgerConnected', value: null }); + hwDevice = device; + win.send({ event: 'hwConnected', value: { model: ledgerDevice.model } }); } } else if (type === 'remove') { - if (ledgerPath) { - removeConnectedDeviceByPath(ledgerPath); - ledgerPath = null; - win.send({ event: 'ledgerDisconnected', value: null }); + if (hwDevice) { + removeConnectedDeviceByPath(hwDevice.path); + win.send({ event: 'hwDisconnected', value: { model: hwDevice.model } }); + hwDevice = null; } } } @@ -104,7 +104,7 @@ const syncDevices = () => { try { observableListen = TransportNodeHid.listen(ledgerObserver); } catch (e) { - ledgerPath = null; + hwDevice = null; syncDevices(); } }; @@ -174,8 +174,8 @@ export const executeLedgerCommand = (device, command) => // // eslint-disable-next-line arrow-body-style createCommand('ledgerCommand', (command) => { - if (ledgerPath) { - return TransportNodeHid.open(ledgerPath) + if (hwDevice) { + return TransportNodeHid.open(hwDevice.path) .then(async (transport) => { // eslint-disable-line max-statements busy = true; try { diff --git a/app/src/main.js b/app/src/main.js index a481a6c3f2..7e993c2195 100644 --- a/app/src/main.js +++ b/app/src/main.js @@ -101,12 +101,6 @@ ipcMain.on('proxyCredentialsEntered', (event, username, password) => { global.myTempFunction(username, password); }); - -// ipcMain.on('ledgerConnected', () => { -// console.log('ledgerConnected'); -// store.dispatch({ type: actionTypes.connectHardwareWallet }); -// }); - // ToDo - enable this feature when it is implemented in the new design // ipcMain.on('set-locale', (event, locale) => { // const langCode = locale.substr(0, 2); diff --git a/app/src/trezor.js b/app/src/trezor.js index 856be9be90..583610cbbb 100644 --- a/app/src/trezor.js +++ b/app/src/trezor.js @@ -110,7 +110,7 @@ list.on('connect', (device) => { logDebug(`Disconnected device ${device.features.label}`); const trezorDevice = getDeviceById(deviceId); if (trezorDevice) { - win.send({ event: 'trezorDisconnected', value: trezorDevice }); + win.send({ event: 'hwDisconnected', value: { model: trezorDevice.model } }); removeConnectedDeviceByID(trezorDevice.deviceId); } }); @@ -144,7 +144,7 @@ list.on('connect', (device) => { logDebug(`Adding Device ${device.features.label} to connected devices.`); const trezorDevice = createTrezorHWDevice(device.features); addConnectedDevices(trezorDevice); - win.send({ event: 'trezorConnected', value: trezorDevice }); + win.send({ event: 'hwConnected', value: { model: trezorDevice.model } }); }); // This gets called on general error of the devicelist (no transport, etc) diff --git a/i18n/locales/en/common.json b/i18n/locales/en/common.json index 67540b8043..77eac9a50b 100644 --- a/i18n/locales/en/common.json +++ b/i18n/locales/en/common.json @@ -88,8 +88,10 @@ "Confirm": "Confirm", "Confirm (Fee: 1 LSK)": "Confirm (Fee: 1 LSK)", "Confirm (Fee: {{fee}} LSK)": "Confirm (Fee: {{fee}} LSK)", + "Confirm on {{deviceModel}}": "Confirm on {{deviceModel}}", "Confirm to register your second passphrase on the blockchain.": "Confirm to register your second passphrase on the blockchain.", "Confirm transaction on Ledger Nano S": "Confirm transaction on Ledger Nano S", + "Confirm transaction on {{deviceModel}}": "Confirm transaction on {{deviceModel}}", "Confirm transfer": "Confirm transfer", "Confirm vote on Ledger Nano S": "Confirm vote on Ledger Nano S", "Confirm your name": "Confirm your name", diff --git a/src/components/sendV2/summary/summary.js b/src/components/sendV2/summary/summary.js index 8ec6510f00..8032211dd9 100644 --- a/src/components/sendV2/summary/summary.js +++ b/src/components/sendV2/summary/summary.js @@ -144,18 +144,18 @@ class Summary extends React.Component { } render() { - const { account } = this.props; + const { account, fields, t } = this.props; const { secondPassphrase, isHardwareWalletConnected } = this.state; let isBtnDisabled = secondPassphrase.hasSecondPassphrase && secondPassphrase.isValid !== '' && !secondPassphrase.isValid; isBtnDisabled = !isBtnDisabled && isHardwareWalletConnected; const confirmBtnMessage = isHardwareWalletConnected - ? this.props.t(`Confirm on ${account.hwInfo.deviceModel}`) - : this.props.t('Send {{amount}} LSK', { amount: this.props.fields.amount.value }); + ? t('Confirm on {{deviceModel}}', { deviceModel: account.hwInfo.deviceModel }) + : t('Send {{amount}} LSK', { amount: fields.amount.value }); const title = isHardwareWalletConnected - ? this.props.t(`Confirm transaction on ${account.hwInfo.deviceModel}`) - : this.props.t('Transaction summary'); + ? t('Confirm transaction on {{deviceModel}}', { deviceModel: account.hwInfo.deviceModel }) + : t('Transaction summary'); return (
@@ -165,60 +165,60 @@ class Summary extends React.Component {
- +
- + - {this.props.fields.recipient.address} + {fields.recipient.address}
- +
- -

{this.props.fields.reference.value}

+ +

{fields.reference.value}

- {this.props.t('{{fee}} LSK', { fee: fromRawLsk(fees.send) })} + {t('{{fee}} LSK', { fee: fromRawLsk(fees.send) })}
{ secondPassphrase.hasSecondPassphrase ?
- + - {this.props.t('Edit transaction')} + {t('Edit transaction')}
diff --git a/src/utils/hwWallet.js b/src/utils/hwWallet.js index e4d6982422..6c80850fe4 100644 --- a/src/utils/hwWallet.js +++ b/src/utils/hwWallet.js @@ -18,27 +18,27 @@ const util = require('util'); const { ipc } = window; -const handleConnect = () => { +const handleConnect = (deviceModel) => { store.dispatch({ type: actionTypes.settingsUpdated, data: { isHarwareWalletConnected: true }, }); - store.dispatch(errorToastDisplayed({ label: HW_MSG.LEDGER_CONNECTED })); + store.dispatch(errorToastDisplayed({ label: `${deviceModel} connected` })); }; -const handleDisconnect = (hwName) => { +const handleDisconnect = (deviceModel) => { const state = store.getState(); const { account } = state; if (account.address) { - store.dispatch( // eslint-disable-line + store.dispatch( dialogDisplayed({ childComponent: Alert, childComponentProps: { title: 'You are disconnected', - text: `There is no connection to the ${hwName}. Please check the cables if it happened by accident.`, + text: `There is no connection to the ${deviceModel}. Please check the cables if it happened by accident.`, closeDialog: () => { store.dispatch(dialogHidden()); - location.reload(); // eslint-disable-line + location.reload(); }, }, })); @@ -51,11 +51,8 @@ const handleDisconnect = (hwName) => { }; if (ipc) { // On browser-mode is undefined - ipc.on('ledgerConnected', handleConnect); - ipc.on('ledgerDisconnected', () => handleDisconnect('Ledger Nano S')); - - ipc.on('trezorConnected', handleConnect); - ipc.on('trezorDisconnected', () => handleDisconnect('Trezor Model T')); + ipc.on('hwConnected', (event, { model }) => handleConnect(model)); + ipc.on('hwDisconnected', (event, { model }) => handleDisconnect(model)); ipc.on('ledgerButtonCallback', () => { // store.dispatch(infoToastDisplayed({ label: HW_MSG.LEDGER_ASK_FOR_CONFIRMATION })); From 149e750a79ac6e215d68c2e7b6c2abbbac1267ca Mon Sep 17 00:00:00 2001 From: Osvaldo Vega Date: Tue, 23 Apr 2019 17:41:49 +0200 Subject: [PATCH 4/6] :recycle: Fix login middleware file after code review --- src/store/middlewares/login.js | 35 +++++++++++++++++++++++++++++++ src/utils/hwWallet.js | 38 ---------------------------------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/store/middlewares/login.js b/src/store/middlewares/login.js index f23f749480..b2c070d79e 100644 --- a/src/store/middlewares/login.js +++ b/src/store/middlewares/login.js @@ -1,4 +1,8 @@ import actionTypes from '../../constants/actions'; +import { accountLoggedOut } from '../../actions/account'; +import { dialogDisplayed, dialogHidden } from '../../actions/dialog'; +import { errorToastDisplayed } from '../../actions/toaster'; +import Alert from '../../components/dialog/alert'; const loginMiddleware = store => next => (action) => { const { ipc } = window; @@ -9,6 +13,37 @@ const loginMiddleware = store => next => (action) => { type: actionTypes.settingsUpdated, data: { isHarwareWalletConnected: false }, }); + + ipc.on('hwConnected', (event, { model }) => { + store.dispatch({ + type: actionTypes.settingsUpdated, + data: { isHarwareWalletConnected: true }, + }); + store.dispatch(errorToastDisplayed({ 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 }, + }); + }); } break; default: break; diff --git a/src/utils/hwWallet.js b/src/utils/hwWallet.js index 6c80850fe4..8cfe1d3fc2 100644 --- a/src/utils/hwWallet.js +++ b/src/utils/hwWallet.js @@ -10,50 +10,12 @@ import { getTransactionBytes, calculateTxId, getBufferToHex } from './rawTransac import { PLATFORM_TYPES, getPlatformType } from './platform'; import store from '../store'; import loginTypes from '../constants/loginTypes'; -import { accountLoggedOut } from '../actions/account'; -import { dialogDisplayed, dialogHidden } from '../actions/dialog'; -import Alert from '../components/dialog/alert'; const util = require('util'); const { ipc } = window; -const handleConnect = (deviceModel) => { - store.dispatch({ - type: actionTypes.settingsUpdated, - data: { isHarwareWalletConnected: true }, - }); - store.dispatch(errorToastDisplayed({ label: `${deviceModel} connected` })); -}; - -const handleDisconnect = (deviceModel) => { - 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 ${deviceModel}. Please check the cables if it happened by accident.`, - closeDialog: () => { - store.dispatch(dialogHidden()); - location.reload(); - }, - }, - })); - store.dispatch(accountLoggedOut()); - } - store.dispatch({ - type: actionTypes.settingsUpdated, - data: { isHarwareWalletConnected: false }, - }); -}; - if (ipc) { // On browser-mode is undefined - ipc.on('hwConnected', (event, { model }) => handleConnect(model)); - ipc.on('hwDisconnected', (event, { model }) => handleDisconnect(model)); - ipc.on('ledgerButtonCallback', () => { // store.dispatch(infoToastDisplayed({ label: HW_MSG.LEDGER_ASK_FOR_CONFIRMATION })); }); From 6c20e0d22aafd29d25b228ec88a6a39af54f9947 Mon Sep 17 00:00:00 2001 From: Osvaldo Vega Date: Wed, 24 Apr 2019 11:00:54 +0200 Subject: [PATCH 5/6] :recycle: Update middleware for hwWallet remove login moddleware and add hwWallet --- app/src/ledger.js | 2 +- src/store/middlewares/hwWallet.js | 74 +++++++++++++++++++++++++++++++ src/store/middlewares/index.js | 14 +++--- src/store/middlewares/login.js | 54 ---------------------- src/utils/hwWallet.js | 24 ---------- 5 files changed, 82 insertions(+), 86 deletions(-) create mode 100644 src/store/middlewares/hwWallet.js delete mode 100644 src/store/middlewares/login.js diff --git a/app/src/ledger.js b/app/src/ledger.js index 609517beb8..eb44e2a5cf 100644 --- a/app/src/ledger.js +++ b/app/src/ledger.js @@ -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') { diff --git a/src/store/middlewares/hwWallet.js b/src/store/middlewares/hwWallet.js new file mode 100644 index 0000000000..15f3bc759a --- /dev/null +++ b/src/store/middlewares/hwWallet.js @@ -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; diff --git a/src/store/middlewares/index.js b/src/store/middlewares/index.js index 31d23436d2..2bbf56c066 100644 --- a/src/store/middlewares/index.js +++ b/src/store/middlewares/index.js @@ -3,7 +3,7 @@ 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'; @@ -11,14 +11,14 @@ 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, ]; diff --git a/src/store/middlewares/login.js b/src/store/middlewares/login.js deleted file mode 100644 index b2c070d79e..0000000000 --- a/src/store/middlewares/login.js +++ /dev/null @@ -1,54 +0,0 @@ -import actionTypes from '../../constants/actions'; -import { accountLoggedOut } from '../../actions/account'; -import { dialogDisplayed, dialogHidden } from '../../actions/dialog'; -import { errorToastDisplayed } from '../../actions/toaster'; -import Alert from '../../components/dialog/alert'; - -const loginMiddleware = store => next => (action) => { - const { ipc } = window; - switch (action.type) { - case actionTypes.storeCreated: - if (ipc) { // On browser-mode is undefined - store.dispatch({ - type: actionTypes.settingsUpdated, - data: { isHarwareWalletConnected: false }, - }); - - ipc.on('hwConnected', (event, { model }) => { - store.dispatch({ - type: actionTypes.settingsUpdated, - data: { isHarwareWalletConnected: true }, - }); - store.dispatch(errorToastDisplayed({ 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 }, - }); - }); - } - break; - default: break; - } - next(action); -}; - -export default loginMiddleware; diff --git a/src/utils/hwWallet.js b/src/utils/hwWallet.js index 8cfe1d3fc2..68449c902e 100644 --- a/src/utils/hwWallet.js +++ b/src/utils/hwWallet.js @@ -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) => { From ecd54e941ae598e2431edf9f56f594c8df5c4ca5 Mon Sep 17 00:00:00 2001 From: Osvaldo Vega Date: Wed, 24 Apr 2019 11:23:59 +0200 Subject: [PATCH 6/6] :recycle: Update hwWallet middelware file, exclude file from jest/coverage --- jest.config.js | 1 - src/store/middlewares/hwWallet.js | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index a0ec428ad8..f2c9116a06 100644 --- a/jest.config.js +++ b/jest.config.js @@ -16,7 +16,6 @@ module.exports = { 'src/components/register/register.test.js', 'src/components/transactions/votedDelegates.test.js', 'src/components/voteUrlProcessor/index.test.js', - 'src/store/middlewares/login.test.js', 'src/store/reducers/liskService.test.js', 'src/store/middlewares/socket.test.js', 'src/store/middlewares/peers.test.js', diff --git a/src/store/middlewares/hwWallet.js b/src/store/middlewares/hwWallet.js index 15f3bc759a..f35edc0b7e 100644 --- a/src/store/middlewares/hwWallet.js +++ b/src/store/middlewares/hwWallet.js @@ -1,3 +1,4 @@ +// istanbul ignore file import actionTypes from '../../constants/actions'; import { accountLoggedOut } from '../../actions/account'; import { dialogDisplayed, dialogHidden } from '../../actions/dialog';