1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

findSelectedDevice > WalletReducer.selectedDevice

This commit is contained in:
Szymon Lesisz 2018-05-22 22:07:25 +02:00
parent 751a96ccc1
commit 60c4390925
13 changed files with 35 additions and 49 deletions

View File

@ -2,7 +2,6 @@
'use strict'; 'use strict';
import TrezorConnect from 'trezor-connect'; import TrezorConnect from 'trezor-connect';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import * as DISCOVERY from './constants/discovery'; import * as DISCOVERY from './constants/discovery';
import * as ACCOUNT from './constants/account'; import * as ACCOUNT from './constants/account';
import * as TOKEN from './constants/token'; import * as TOKEN from './constants/token';
@ -54,7 +53,7 @@ export type DiscoveryCompleteAction = {
export const start = (device: TrezorDevice, network: string, ignoreCompleted?: boolean): ThunkAction => { export const start = (device: TrezorDevice, network: string, ignoreCompleted?: boolean): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
if (!selected) { if (!selected) {
// TODO: throw error // TODO: throw error
console.error("Start discovery: no selected device", device) console.error("Start discovery: no selected device", device)
@ -330,7 +329,7 @@ const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): Asy
export const restore = (): ThunkAction => { export const restore = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
if (selected && selected.connected && selected.features) { if (selected && selected.connected && selected.features) {
const discoveryProcess: ?Discovery = getState().discovery.find(d => d.deviceState === selected.state && d.waitingForDevice); const discoveryProcess: ?Discovery = getState().discovery.find(d => d.deviceState === selected.state && d.waitingForDevice);
@ -347,7 +346,7 @@ export const restore = (): ThunkAction => {
// try to start discovery after CONNECT.AUTH_DEVICE action // try to start discovery after CONNECT.AUTH_DEVICE action
export const check = (): ThunkAction => { export const check = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
const urlParams = getState().router.location.state; const urlParams = getState().router.location.state;

View File

@ -7,7 +7,6 @@ import * as NOTIFICATION from './constants/notification';
import { initialState } from '../reducers/ReceiveReducer'; import { initialState } from '../reducers/ReceiveReducer';
import type { State } from '../reducers/ReceiveReducer'; import type { State } from '../reducers/ReceiveReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { TrezorDevice, ThunkAction, AsyncAction, Action, GetState, Dispatch } from '~/flowtype'; import type { TrezorDevice, ThunkAction, AsyncAction, Action, GetState, Dispatch } from '~/flowtype';
@ -55,7 +54,7 @@ export const showUnverifiedAddress = (): Action => {
export const showAddress = (address_n: Array<number>): AsyncAction => { export const showAddress = (address_n: Array<number>): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => { return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
if (selected && (!selected.connected || !selected.available)) { if (selected && (!selected.connected || !selected.available)) {

View File

@ -4,7 +4,6 @@
import * as ACCOUNT from './constants/account'; import * as ACCOUNT from './constants/account';
import { initialState } from '../reducers/SelectedAccountReducer'; import { initialState } from '../reducers/SelectedAccountReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { AsyncAction, ThunkAction, Action, GetState, Dispatch, TrezorDevice } from '~/flowtype'; import type { AsyncAction, ThunkAction, Action, GetState, Dispatch, TrezorDevice } from '~/flowtype';
import type { State } from '../reducers/SelectedAccountReducer'; import type { State } from '../reducers/SelectedAccountReducer';
@ -23,7 +22,7 @@ export const init = (): ThunkAction => {
const { location } = getState().router; const { location } = getState().router;
const urlParams = location.state; const urlParams = location.state;
const selected: ?TrezorDevice = findSelectedDevice( getState().connect ); const selected: ?TrezorDevice = getState().wallet.selectedDevice;;
if (!selected) return; if (!selected) return;
if (!selected.state || !selected.features) return; if (!selected.state || !selected.features) return;

View File

@ -17,7 +17,7 @@ import BigNumber from 'bignumber.js';
import { initialState } from '../reducers/SendFormReducer'; import { initialState } from '../reducers/SendFormReducer';
import { findAccount } from '../reducers/AccountsReducer'; import { findAccount } from '../reducers/AccountsReducer';
import { findToken } from '../reducers/TokensReducer'; import { findToken } from '../reducers/TokensReducer';
import { findSelectedDevice, findDevice } from '../reducers/TrezorConnectReducer'; import { findDevice } from '../reducers/TrezorConnectReducer';
import type { import type {
Dispatch, Dispatch,
@ -203,7 +203,7 @@ export const init = (): ThunkAction => {
const { location } = getState().router; const { location } = getState().router;
const urlParams: RouterLocationState = location.state; const urlParams: RouterLocationState = location.state;
const selected: ?TrezorDevice = findSelectedDevice( getState().connect ); const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
const web3instance: ?Web3Instance = getState().web3.find(w3 => w3.network === urlParams.network); const web3instance: ?Web3Instance = getState().web3.find(w3 => w3.network === urlParams.network);
@ -893,7 +893,7 @@ export const onSend = (): AsyncAction => {
v: '' v: ''
} }
const selected: ?TrezorDevice = findSelectedDevice(getState().connect); const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
let signedTransaction = await TrezorConnect.ethereumSignTransaction({ let signedTransaction = await TrezorConnect.ethereumSignTransaction({

View File

@ -6,7 +6,6 @@ import * as SUMMARY from './constants/summary';
import * as TOKEN from './constants/token'; import * as TOKEN from './constants/token';
import { resolveAfter } from '../utils/promiseUtils'; import { resolveAfter } from '../utils/promiseUtils';
import { initialState } from '../reducers/SummaryReducer'; import { initialState } from '../reducers/SummaryReducer';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { ThunkAction, AsyncAction, Action, GetState, Dispatch } from '~/flowtype'; import type { ThunkAction, AsyncAction, Action, GetState, Dispatch } from '~/flowtype';
import type { State } from '../reducers/SummaryReducer'; import type { State } from '../reducers/SummaryReducer';

View File

@ -10,7 +10,6 @@ import * as WALLET from './constants/wallet';
import { push } from 'react-router-redux'; import { push } from 'react-router-redux';
import * as DiscoveryActions from './DiscoveryActions'; import * as DiscoveryActions from './DiscoveryActions';
import { resolveAfter } from '../utils/promiseUtils'; import { resolveAfter } from '../utils/promiseUtils';
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
import type { import type {
@ -194,7 +193,7 @@ const sortDevices = (devices: Array<TrezorDevice>): Array<TrezorDevice> => {
export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => { export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
// if (!selected || (selected && selected.state)) { // if (!selected || (selected && selected.state)) {
dispatch( onSelectDevice(device) ); dispatch( onSelectDevice(device) );
// } // }
@ -283,7 +282,7 @@ export const switchToFirstAvailableDevice = (): AsyncAction => {
export const getSelectedDeviceState = (): AsyncAction => { export const getSelectedDeviceState = (): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => { return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
if (selected if (selected
&& selected.connected && selected.connected
&& (selected.features && !selected.features.bootloader_mode && selected.features.initialized) && (selected.features && !selected.features.bootloader_mode && selected.features.initialized)
@ -335,7 +334,7 @@ export const getSelectedDeviceState = (): AsyncAction => {
export const deviceDisconnect = (device: Device): AsyncAction => { export const deviceDisconnect = (device: Device): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => { return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect); const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (device && device.features) { if (device && device.features) {
if (selected && selected.features && selected.features.device_id === device.features.device_id) { if (selected && selected.features && selected.features.device_id === device.features.device_id) {
@ -361,7 +360,7 @@ export const deviceDisconnect = (device: Device): AsyncAction => {
export const coinChanged = (network: ?string): ThunkAction => { export const coinChanged = (network: ?string): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect); const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
dispatch( DiscoveryActions.stop(selected) ); dispatch( DiscoveryActions.stop(selected) );
@ -380,7 +379,7 @@ export function reload(): AsyncAction {
export function acquire(): AsyncAction { export function acquire(): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => { return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect); const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
const response = await TrezorConnect.getFeatures({ const response = await TrezorConnect.getFeatures({
@ -438,18 +437,10 @@ export const duplicateDevice = (device: TrezorDevice): AsyncAction => {
} }
} }
export const selectDuplicatedDevice = (): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
if (selected)
dispatch(onSelectDevice(selected));
}
}
export function addAccount(): ThunkAction { export function addAccount(): ThunkAction {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect); const selected = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
dispatch( DiscoveryActions.start(selected, getState().router.location.state.network, true) ); // TODO: network nicer dispatch( DiscoveryActions.start(selected, getState().router.location.state.network, true) ); // TODO: network nicer
} }

View File

@ -3,7 +3,6 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { findAccount } from '~/js/reducers/AccountsReducer'; import { findAccount } from '~/js/reducers/AccountsReducer';
import { findSelectedDevice } from '~/js/reducers/TrezorConnectReducer';
import type { Props } from './index'; import type { Props } from './index';

View File

@ -7,6 +7,7 @@ import type { Props } from './index';
type State = { type State = {
defaultName: string; defaultName: string;
instance: number;
instanceName: ?string; instanceName: ?string;
isUsed: boolean; isUsed: boolean;
} }
@ -27,6 +28,7 @@ export default class DuplicateDevice extends Component<Props, State> {
this.state = { this.state = {
defaultName: `${device.label} (${instance.toString()})`, defaultName: `${device.label} (${instance.toString()})`,
instance,
instanceName: null, instanceName: null,
isUsed: false, isUsed: false,
} }
@ -66,7 +68,7 @@ export default class DuplicateDevice extends Component<Props, State> {
submit() { submit() {
if (!this.props.modal.opened) return; if (!this.props.modal.opened) return;
this.props.modalActions.onDuplicateDevice( { ...this.props.modal.device, instanceName: this.state.instanceName } ); this.props.modalActions.onDuplicateDevice( { ...this.props.modal.device, instanceName: this.state.instanceName, instance: this.state.instance } );
} }
render() { render() {

View File

@ -3,7 +3,6 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import raf from 'raf'; import raf from 'raf';
import { findSelectedDevice } from '~/js/reducers/TrezorConnectReducer';
import type { Props } from './index'; import type { Props } from './index';
type State = { type State = {
@ -33,7 +32,7 @@ export default class PinModal extends Component<Props, State> {
// check if this device is already known // check if this device is already known
// const isSavedDevice = props.devices.find(d => d.path === props.modal.device.path && d.remember); // const isSavedDevice = props.devices.find(d => d.path === props.modal.device.path && d.remember);
const selected = findSelectedDevice(props.connect); const selected = props.wallet.selectedDevice;
let deviceLabel =device.label; let deviceLabel =device.label;
let singleInput = false; let singleInput = false;
if (selected && selected.path === device.path) { if (selected && selected.path === device.path) {

View File

@ -40,6 +40,7 @@ type StateProps = {
sendForm: $ElementType<State, 'sendForm'>, sendForm: $ElementType<State, 'sendForm'>,
receive: $ElementType<State, 'receive'>, receive: $ElementType<State, 'receive'>,
localStorage: $ElementType<State, 'localStorage'>, localStorage: $ElementType<State, 'localStorage'>,
wallet: $ElementType<State, 'wallet'>,
} }
type DispatchProps = { type DispatchProps = {
@ -131,7 +132,8 @@ const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: St
selectedAccount: state.selectedAccount, selectedAccount: state.selectedAccount,
sendForm: state.sendForm, sendForm: state.sendForm,
receive: state.receive, receive: state.receive,
localStorage: state.localStorage localStorage: state.localStorage,
wallet: state.wallet
}; };
} }

View File

@ -323,18 +323,6 @@ const devicesFromLocalStorage = (devices: Array<TrezorDevice>): Array<TrezorDevi
const duplicate = (state: State, device: TrezorDevice): State => { const duplicate = (state: State, device: TrezorDevice): State => {
const newState: State = { ...state }; const newState: State = { ...state };
// const affectedDevices: Array<TrezorDevice> = state.devices.filter(d => d.features && device.features && d.features.device_id === device.features.device_id)
// .sort((a, b) => {
// if (!a.instance) {
// return -1;
// } else {
// return !b.instance || a.instance > b.instance ? 1 : -1;
// }
// });
// const instance: number = affectedDevices.reduce((inst, dev) => {
// return dev.instance ? dev.instance + 1 : inst + 1;
// }, 0);
const instance: number = getNewInstance(state.devices, device); const instance: number = getNewInstance(state.devices, device);
@ -347,7 +335,7 @@ const duplicate = (state: State, device: TrezorDevice): State => {
// path: device.path, // path: device.path,
// label: device.label, // label: device.label,
state: null, state: null,
instance, // instance,
// instanceLabel: `${device.label} (${instance})`, // instanceLabel: `${device.label} (${instance})`,
instanceLabel: `${device.label} (${ device.instanceName || instance })`, instanceLabel: `${device.label} (${ device.instanceName || instance })`,
ts: new Date().getTime(), ts: new Date().getTime(),

View File

@ -80,9 +80,9 @@ const TrezorConnectService: Middleware = (api: MiddlewareAPI) => (next: Middlewa
} else if (action.type === CONNECT.AUTH_DEVICE) { } else if (action.type === CONNECT.AUTH_DEVICE) {
api.dispatch( DiscoveryActions.check() ); api.dispatch( DiscoveryActions.check() );
} else if (action.type === CONNECT.DUPLICATE) { } else if (action.type === CONNECT.DUPLICATE) {
api.dispatch( TrezorConnectActions.selectDuplicatedDevice() ); api.dispatch( TrezorConnectActions.onSelectDevice( action.device ) );
} else if (action.type === CONNECT.SELECT_DEVICE) { // } else if (action.type === CONNECT.SELECT_DEVICE) {
api.dispatch( TrezorConnectActions.getSelectedDeviceState() ); // api.dispatch( TrezorConnectActions.getSelectedDeviceState() );
} else if (action.type === CONNECT.COIN_CHANGED) { } else if (action.type === CONNECT.COIN_CHANGED) {
api.dispatch( TrezorConnectActions.coinChanged( action.payload.network ) ); api.dispatch( TrezorConnectActions.coinChanged( action.payload.network ) );
} }

View File

@ -6,6 +6,7 @@ import * as WALLET from '../actions/constants/wallet';
import * as WalletActions from '../actions/WalletActions'; import * as WalletActions from '../actions/WalletActions';
import * as LocalStorageActions from '../actions/LocalStorageActions'; import * as LocalStorageActions from '../actions/LocalStorageActions';
import * as TrezorConnectActions from '../actions/TrezorConnectActions';
import type { import type {
Middleware, Middleware,
@ -62,11 +63,19 @@ const WalletService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispa
// listening devices state change // listening devices state change
if (locationChange || prevState.connect.devices !== state.connect.devices) { if (locationChange || prevState.connect.devices !== state.connect.devices) {
const device = getSelectedDevice(state); const device = getSelectedDevice(state);
const currentDevice = state.wallet.selectedDevice;
if (state.wallet.selectedDevice !== device) { if (state.wallet.selectedDevice !== device) {
api.dispatch({ api.dispatch({
type: WALLET.SET_SELECTED_DEVICE, type: WALLET.SET_SELECTED_DEVICE,
device device
}) })
if (!locationChange && currentDevice && device && (currentDevice.path === device.path || currentDevice.instance === device.instance)) {
// console.warn("but ONLY UPDATE!")
} else {
api.dispatch( TrezorConnectActions.getSelectedDeviceState() );
}
} }
} }