diff --git a/src/js/actions/TrezorConnectActions.js b/src/js/actions/TrezorConnectActions.js index 59998dbf..7b25dc9c 100644 --- a/src/js/actions/TrezorConnectActions.js +++ b/src/js/actions/TrezorConnectActions.js @@ -8,7 +8,7 @@ import * as TOKEN from './constants/token'; import * as CONNECT from './constants/TrezorConnect'; import * as NOTIFICATION from './constants/notification'; import * as WALLET from './constants/wallet'; -import { getNewInstance } from '../reducers/DevicesReducer'; +import { getDuplicateInstanceNumber } from '../reducers/utils'; import { push } from 'react-router-redux'; import * as DiscoveryActions from './DiscoveryActions'; @@ -113,15 +113,16 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS }); // $FlowIssue LOCAL not declared + window.__TREZOR_CONNECT_SRC = typeof LOCAL === 'string' ? LOCAL : 'https://sisyfos.trezor.io/connect/'; // window.__TREZOR_CONNECT_SRC = typeof LOCAL === 'string' ? LOCAL : 'https://connect.trezor.io/5/'; - window.__TREZOR_CONNECT_SRC = 'https://sisyfos.trezor.io/connect/'; + //window.__TREZOR_CONNECT_SRC = 'https://sisyfos.trezor.io/connect/'; // window.__TREZOR_CONNECT_SRC = 'https://localhost:8088/'; try { await TrezorConnect.init({ transportReconnect: true, debug: true, - popup: false, + popup: true, webusb: true, pendingTransportEvent: (getState().devices.length < 1), }); @@ -407,7 +408,7 @@ export const duplicateDevice = (device: TrezorDevice): AsyncAction => async (dis // device, // }); - const instance: number = getNewInstance(getState().devices, device); + const instance: number = getDuplicateInstanceNumber(getState().devices, device); const extended: Object = { instance }; dispatch({ type: CONNECT.DUPLICATE, diff --git a/src/js/components/modal/DuplicateDevice.js b/src/js/components/modal/DuplicateDevice.js index c047b9fe..dfb2d00a 100644 --- a/src/js/components/modal/DuplicateDevice.js +++ b/src/js/components/modal/DuplicateDevice.js @@ -2,7 +2,7 @@ import React, { Component } from 'react'; -import { getNewInstance } from '~/js/reducers/DevicesReducer'; +import { getDuplicateInstanceNumber } from '~/js/reducers/utils'; import type { Props } from './index'; type State = { @@ -25,7 +25,7 @@ export default class DuplicateDevice extends Component { const device = props.modal.opened ? props.modal.device : null; if (!device) return; - const instance = getNewInstance(props.devices, device); + const instance = getDuplicateInstanceNumber(props.devices, device); this.state = { defaultName: `${device.label} (${instance.toString()})`, diff --git a/src/js/reducers/DevicesReducer.js b/src/js/reducers/DevicesReducer.js index b62e9c86..171143bb 100644 --- a/src/js/reducers/DevicesReducer.js +++ b/src/js/reducers/DevicesReducer.js @@ -5,6 +5,7 @@ import { TRANSPORT, DEVICE } from 'trezor-connect'; import type { Device } from 'trezor-connect'; import * as CONNECT from '../actions/constants/TrezorConnect'; import * as WALLET from '../actions/constants/wallet'; +import { getDuplicateInstanceNumber } from './utils'; import type { Action, TrezorDevice } from '~/flowtype'; @@ -123,7 +124,7 @@ const addDevice = (state: State, device: Device): State => { // edge case: freshly connected device has different "passphrase_protection" than saved instances // need to automatically create another instance with default instance name // if (hasDifferentPassphraseSettings && !hasInstancesWithPassphraseSettings) { - // const instance = getNewInstance(affectedDevices, device); + // const instance = getDuplicateInstanceNumber(affectedDevices, device); // newDevice.instance = instance; // newDevice.instanceLabel = `${device.label} (${instance})`; @@ -152,7 +153,7 @@ const duplicate = (state: State, device: TrezorDevice): State => { const newState: State = [...state]; - const instance: number = getNewInstance(state, device); + const instance: number = getDuplicateInstanceNumber(state, device); const newDevice: TrezorDevice = { ...device, @@ -299,20 +300,4 @@ export default function devices(state: State = initialState, action: Action): St default: return state; } -} - -// UTILS - -export const getNewInstance = (devices: State, device: Device | TrezorDevice): number => { - const affectedDevices: State = devices.filter(d => d.features && device.features && d.features.device_id === device.features.device_id) - .sort((a, b) => { - if (!a.instance) { - return -1; - } - return !b.instance || a.instance > b.instance ? 1 : -1; - }); - - const instance: number = affectedDevices.reduce((inst, dev) => (dev.instance ? dev.instance + 1 : inst + 1), 0); - - return instance; -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/js/reducers/utils/index.js b/src/js/reducers/utils/index.js index 17068816..142bf178 100644 --- a/src/js/reducers/utils/index.js +++ b/src/js/reducers/utils/index.js @@ -15,6 +15,7 @@ import type { Action, AsyncAction, GetState, + Device, TrezorDevice, Account, Coin, @@ -53,6 +54,23 @@ export const findDevice = (devices: Array, deviceId: string, devic return false; }); +// get next instance number +export const getDuplicateInstanceNumber = (devices: Array, device: Device | TrezorDevice): number => { + // find device(s) with the same features.device_id + // and sort them by instance number + const affectedDevices: Array = devices.filter(d => d.features && device.features && d.features.device_id === device.features.device_id) + .sort((a, b) => { + if (!a.instance) { + return -1; + } + return !b.instance || a.instance > b.instance ? 1 : -1; + }); + + // calculate new instance number + const instance: number = affectedDevices.reduce((inst, dev) => (dev.instance ? dev.instance + 1 : inst + 1), 0); + return instance; +}; + export const getSelectedAccount = (state: State): ?Account => { const device = state.wallet.selectedDevice; const locationState = state.router.location.state;