1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 17:28:10 +00:00

rename "getNewInstance" util to "getDuplicateInstanceNumber" and move it to ./utils dir

This commit is contained in:
Szymon Lesisz 2018-08-17 15:20:42 +02:00
parent e8109350bb
commit 4093240af0
4 changed files with 29 additions and 25 deletions

View File

@ -8,7 +8,7 @@ import * as TOKEN from './constants/token';
import * as CONNECT from './constants/TrezorConnect'; import * as CONNECT from './constants/TrezorConnect';
import * as NOTIFICATION from './constants/notification'; import * as NOTIFICATION from './constants/notification';
import * as WALLET from './constants/wallet'; import * as WALLET from './constants/wallet';
import { getNewInstance } from '../reducers/DevicesReducer'; import { getDuplicateInstanceNumber } from '../reducers/utils';
import { push } from 'react-router-redux'; import { push } from 'react-router-redux';
import * as DiscoveryActions from './DiscoveryActions'; import * as DiscoveryActions from './DiscoveryActions';
@ -113,15 +113,16 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS
}); });
// $FlowIssue LOCAL not declared // $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 = 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/'; // window.__TREZOR_CONNECT_SRC = 'https://localhost:8088/';
try { try {
await TrezorConnect.init({ await TrezorConnect.init({
transportReconnect: true, transportReconnect: true,
debug: true, debug: true,
popup: false, popup: true,
webusb: true, webusb: true,
pendingTransportEvent: (getState().devices.length < 1), pendingTransportEvent: (getState().devices.length < 1),
}); });
@ -407,7 +408,7 @@ export const duplicateDevice = (device: TrezorDevice): AsyncAction => async (dis
// device, // device,
// }); // });
const instance: number = getNewInstance(getState().devices, device); const instance: number = getDuplicateInstanceNumber(getState().devices, device);
const extended: Object = { instance }; const extended: Object = { instance };
dispatch({ dispatch({
type: CONNECT.DUPLICATE, type: CONNECT.DUPLICATE,

View File

@ -2,7 +2,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { getNewInstance } from '~/js/reducers/DevicesReducer'; import { getDuplicateInstanceNumber } from '~/js/reducers/utils';
import type { Props } from './index'; import type { Props } from './index';
type State = { type State = {
@ -25,7 +25,7 @@ export default class DuplicateDevice extends Component<Props, State> {
const device = props.modal.opened ? props.modal.device : null; const device = props.modal.opened ? props.modal.device : null;
if (!device) return; if (!device) return;
const instance = getNewInstance(props.devices, device); const instance = getDuplicateInstanceNumber(props.devices, device);
this.state = { this.state = {
defaultName: `${device.label} (${instance.toString()})`, defaultName: `${device.label} (${instance.toString()})`,

View File

@ -5,6 +5,7 @@ import { TRANSPORT, DEVICE } from 'trezor-connect';
import type { Device } from 'trezor-connect'; import type { Device } from 'trezor-connect';
import * as CONNECT from '../actions/constants/TrezorConnect'; import * as CONNECT from '../actions/constants/TrezorConnect';
import * as WALLET from '../actions/constants/wallet'; import * as WALLET from '../actions/constants/wallet';
import { getDuplicateInstanceNumber } from './utils';
import type { Action, TrezorDevice } from '~/flowtype'; 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 // edge case: freshly connected device has different "passphrase_protection" than saved instances
// need to automatically create another instance with default instance name // need to automatically create another instance with default instance name
// if (hasDifferentPassphraseSettings && !hasInstancesWithPassphraseSettings) { // if (hasDifferentPassphraseSettings && !hasInstancesWithPassphraseSettings) {
// const instance = getNewInstance(affectedDevices, device); // const instance = getDuplicateInstanceNumber(affectedDevices, device);
// newDevice.instance = instance; // newDevice.instance = instance;
// newDevice.instanceLabel = `${device.label} (${instance})`; // newDevice.instanceLabel = `${device.label} (${instance})`;
@ -152,7 +153,7 @@ const duplicate = (state: State, device: TrezorDevice): State => {
const newState: State = [...state]; const newState: State = [...state];
const instance: number = getNewInstance(state, device); const instance: number = getDuplicateInstanceNumber(state, device);
const newDevice: TrezorDevice = { const newDevice: TrezorDevice = {
...device, ...device,
@ -300,19 +301,3 @@ export default function devices(state: State = initialState, action: Action): St
return state; 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;
};

View File

@ -15,6 +15,7 @@ import type {
Action, Action,
AsyncAction, AsyncAction,
GetState, GetState,
Device,
TrezorDevice, TrezorDevice,
Account, Account,
Coin, Coin,
@ -53,6 +54,23 @@ export const findDevice = (devices: Array<TrezorDevice>, deviceId: string, devic
return false; return false;
}); });
// get next instance number
export const getDuplicateInstanceNumber = (devices: Array<TrezorDevice>, device: Device | TrezorDevice): number => {
// find device(s) with the same features.device_id
// and sort them by instance number
const affectedDevices: Array<TrezorDevice> = 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 => { export const getSelectedAccount = (state: State): ?Account => {
const device = state.wallet.selectedDevice; const device = state.wallet.selectedDevice;
const locationState = state.router.location.state; const locationState = state.router.location.state;