1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/reducers/DevicesReducer.js

379 lines
13 KiB
JavaScript
Raw Normal View History

/* @flow */
2018-07-30 10:52:13 +00:00
2018-08-20 11:01:43 +00:00
import { DEVICE } from 'trezor-connect';
2018-07-30 10:52:13 +00:00
import type { Device } from 'trezor-connect';
2018-08-14 13:11:52 +00:00
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet';
2018-08-20 11:01:43 +00:00
import { getDuplicateInstanceNumber } from 'reducers/utils';
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
export type State = Array<TrezorDevice>;
const initialState: State = [];
const mergeDevices = (current: TrezorDevice, upcoming: Device | TrezorDevice): TrezorDevice => {
// do not merge if passphrase protection was changed
// if (upcoming.features && current.features) {
// if (upcoming.features.passphrase_protection !== current.features.passphrase_protection) {
// // device settings has been changed, reset state
// // dev.state = null;
// }
// }
2018-09-06 15:04:28 +00:00
let { instanceLabel } = current;
if (upcoming.label !== current.label) {
2018-07-30 10:52:13 +00:00
instanceLabel = upcoming.label;
if (typeof current.instance === 'number') {
instanceLabel += ` (${current.instanceName || current.instance})`;
}
}
const extended = {
connected: typeof upcoming.connected === 'boolean' ? upcoming.connected : current.connected,
available: typeof upcoming.available === 'boolean' ? upcoming.available : current.available,
remember: typeof upcoming.remember === 'boolean' ? upcoming.remember : current.remember,
instance: current.instance,
instanceLabel,
2019-03-04 12:33:02 +00:00
instanceName:
typeof upcoming.instanceName === 'string'
? upcoming.instanceName
: current.instanceName,
state: current.state,
ts: typeof upcoming.ts === 'number' ? upcoming.ts : current.ts,
2019-03-04 12:33:02 +00:00
useEmptyPassphrase:
typeof upcoming.useEmptyPassphrase === 'boolean'
? upcoming.useEmptyPassphrase
: current.useEmptyPassphrase,
2018-08-14 14:06:34 +00:00
};
if (upcoming.type === 'acquired') {
2018-08-14 14:06:34 +00:00
return { ...upcoming, ...extended };
2019-03-04 12:33:02 +00:00
}
if (upcoming.type === 'unacquired' && current.features && current.state) {
// corner-case: trying to merge unacquired device with acquired
// make sure that sensitive fields will not be changed and device will remain acquired
return {
type: 'acquired',
path: upcoming.path,
...current,
2018-08-14 14:06:34 +00:00
...extended,
};
}
2018-08-14 14:06:34 +00:00
return {
...upcoming,
features: null,
...extended,
};
2018-07-30 10:52:13 +00:00
};
const addDevice = (state: State, device: Device): State => {
let affectedDevices: Array<TrezorDevice> = [];
let otherDevices: Array<TrezorDevice> = [];
if (!device.features) {
// check if connected device is unacquired, and it's already exists
affectedDevices = state.filter(d => d.path === device.path);
// if so, ignore this action
if (affectedDevices.length > 0) {
return state;
}
otherDevices = state.filter(d => affectedDevices.indexOf(d) === -1);
} else {
2019-03-04 12:33:02 +00:00
affectedDevices = state.filter(
d => d.features && device.features && d.features.device_id === device.features.device_id
);
const unacquiredDevices = state.filter(d => d.path.length > 0 && d.path === device.path);
2019-03-04 12:33:02 +00:00
otherDevices = state.filter(
d => affectedDevices.indexOf(d) < 0 && unacquiredDevices.indexOf(d) < 0
);
}
const extended = {
remember: false,
connected: true,
available: true,
path: device.path,
label: device.label,
state: null,
instanceLabel: device.label,
instanceName: null,
ts: new Date().getTime(),
useEmptyPassphrase: true,
2018-08-14 14:06:34 +00:00
};
2019-03-04 12:33:02 +00:00
const newDevice: TrezorDevice =
device.type === 'acquired'
? {
...device,
...extended,
}
: {
...device,
features: null,
...extended,
};
2018-07-30 10:52:13 +00:00
if (affectedDevices.length > 0) {
// check if freshly added device has different "passphrase_protection" settings
// let hasDifferentPassphraseSettings: boolean = false;
// let hasInstancesWithPassphraseSettings: boolean = false;
// const changedDevices: Array<TrezorDevice> = affectedDevices.map(d => {
// if (d.features && d.features.passphrase_protection === device.features.passphrase_protection) {
// hasInstancesWithPassphraseSettings = true;
// return mergeDevices(d, { ...device, connected: true, available: true } );
// } else {
// hasDifferentPassphraseSettings = true;
// d.connected = true;
// d.available = false;
// return d;
// }
// });
// 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 = getDuplicateInstanceNumber(affectedDevices, device);
// newDevice.instance = instance;
// newDevice.instanceLabel = `${device.label} (${instance})`;
// changedDevices.push(newDevice);
// }
2019-03-04 12:33:02 +00:00
const changedDevices: Array<TrezorDevice> = affectedDevices
.filter(
d =>
d.features &&
device.features &&
d.features.passphrase_protection === device.features.passphrase_protection
)
.map(d => {
const extended2: Object = { connected: true, available: true };
return mergeDevices(d, { ...device, ...extended2 });
});
if (changedDevices.length !== affectedDevices.length) {
changedDevices.push(newDevice);
}
return otherDevices.concat(changedDevices);
}
2018-07-30 10:52:13 +00:00
return otherDevices.concat([newDevice]);
};
const duplicate = (state: State, device: TrezorDevice): State => {
if (!device.features) return state;
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
const instance: number = getDuplicateInstanceNumber(state, device);
const newDevice: TrezorDevice = {
...device,
remember: false,
state: null,
// instance, (instance is already part of device - added in modal)
2018-07-30 10:52:13 +00:00
instanceLabel: `${device.label} (${device.instanceName || instance})`,
ts: new Date().getTime(),
2018-07-30 10:52:13 +00:00
};
newState.push(newDevice);
return newState;
2018-07-30 10:52:13 +00:00
};
const changeDevice = (state: State, device: Device | TrezorDevice, extended: Object): State => {
// change only acquired devices
if (!device.features) return state;
// find devices with the same device_id and passphrase_protection settings
// or devices with the same path (TODO: should be that way?)
2019-03-04 12:33:02 +00:00
const affectedDevices: Array<TrezorDevice> = state.filter(
d =>
(d.features &&
device.features &&
d.features.device_id === device.features.device_id &&
d.features.passphrase_protection === device.features.passphrase_protection) ||
(d.features && d.path.length > 0 && d.path === device.path)
);
const otherDevices: Array<TrezorDevice> = state.filter(d => affectedDevices.indexOf(d) === -1);
if (affectedDevices.length > 0) {
// merge incoming device with State
2019-03-04 12:33:02 +00:00
const changedDevices = affectedDevices.map(d =>
mergeDevices(d, { ...device, ...extended })
);
return otherDevices.concat(changedDevices);
}
return state;
2018-07-30 10:52:13 +00:00
};
const authDevice = (state: State, device: TrezorDevice, deviceState: string): State => {
2019-03-04 12:33:02 +00:00
const affectedDevice: ?TrezorDevice = state.find(
d => d.path === device.path && d.instance === device.instance
);
// device could already have own state from trezor-connect, do not override it
if (affectedDevice && !affectedDevice.state && affectedDevice.type === 'acquired') {
const otherDevices: Array<TrezorDevice> = state.filter(d => d !== affectedDevice);
return otherDevices.concat([{ ...affectedDevice, state: deviceState }]);
}
return state;
2018-07-30 10:52:13 +00:00
};
// Transform JSON form local storage into State
2019-03-04 12:33:02 +00:00
const devicesFromStorage = ($devices: Array<TrezorDevice>): State =>
$devices.map((device: TrezorDevice) => {
const extended = {
connected: false,
available: false,
path: '',
};
2019-03-04 12:33:02 +00:00
return device.type === 'acquired'
? {
...device,
...extended,
}
: {
...device,
features: null,
...extended,
};
});
// Remove all device reference from State
2019-03-04 12:33:02 +00:00
const forgetDevice = (state: State, device: TrezorDevice): State =>
state.filter(
d =>
d.remember ||
(d.features && device.features && d.features.device_id !== device.features.device_id) ||
(!d.features && d.path !== device.path)
);
// Remove single device reference from State
const forgetSingleDevice = (state: State, device: TrezorDevice): State => {
// remove only one instance (called from Aside button)
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
newState.splice(newState.indexOf(device), 1);
return newState;
2018-07-30 10:52:13 +00:00
};
const disconnectDevice = (state: State, device: Device): State => {
2019-03-04 12:33:02 +00:00
const affectedDevices: State = state.filter(
d =>
d.path === device.path ||
(d.features && device.features && d.features.device_id === device.features.device_id)
);
const otherDevices: State = state.filter(d => affectedDevices.indexOf(d) === -1);
if (affectedDevices.length > 0) {
2019-03-04 12:33:02 +00:00
const acquiredDevices = affectedDevices
.filter(d => d.features && d.state)
.map(d => {
// eslint-disable-line arrow-body-style
return d.type === 'acquired'
? {
...d,
connected: false,
available: false,
status: 'available',
path: '',
}
: d;
});
return otherDevices.concat(acquiredDevices);
}
return state;
2018-07-30 10:52:13 +00:00
};
const onSelectedDevice = (state: State, device: ?TrezorDevice): State => {
2018-10-02 08:57:17 +00:00
if (!device) return state;
const otherDevices: Array<TrezorDevice> = state.filter(d => d !== device);
2019-03-04 12:33:02 +00:00
const extended =
device.type === 'acquired'
? {
...device,
ts: new Date().getTime(),
}
: {
...device,
features: null,
ts: new Date().getTime(),
};
2018-10-02 08:57:17 +00:00
return otherDevices.concat([extended]);
2018-07-30 10:52:13 +00:00
};
const onChangeWalletType = (state: State, device: TrezorDevice, hidden: boolean): State => {
2019-03-04 12:33:02 +00:00
const affectedDevices: State = state.filter(
d =>
d.path === device.path ||
(d.features && device.features && d.features.device_id === device.features.device_id)
);
const otherDevices: State = state.filter(d => affectedDevices.indexOf(d) === -1);
if (affectedDevices.length > 0) {
2019-03-04 12:33:02 +00:00
const changedDevices = affectedDevices.map(d => {
// eslint-disable-line arrow-body-style
return d.type === 'acquired'
? {
...d,
remember: false,
state: null,
useEmptyPassphrase: !hidden,
ts: new Date().getTime(),
}
: d;
});
return otherDevices.concat(changedDevices);
}
return state;
};
export default function devices(state: State = initialState, action: Action): State {
switch (action.type) {
2018-07-30 10:52:13 +00:00
case CONNECT.DEVICE_FROM_STORAGE:
return devicesFromStorage(action.payload);
2018-07-30 10:52:13 +00:00
case CONNECT.DUPLICATE:
return duplicate(state, action.device);
2018-07-30 10:52:13 +00:00
case CONNECT.AUTH_DEVICE:
return authDevice(state, action.device, action.state);
2018-07-30 10:52:13 +00:00
case CONNECT.REMEMBER:
// return changeDevice(state, { ...action.device, path: '', remember: true });
return changeDevice(state, action.device, { path: '', remember: true });
2018-07-30 10:52:13 +00:00
case CONNECT.FORGET:
return forgetDevice(state, action.device);
2018-07-30 10:52:13 +00:00
case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
return forgetSingleDevice(state, action.device);
2018-07-30 10:52:13 +00:00
case DEVICE.CONNECT:
case DEVICE.CONNECT_UNACQUIRED:
return addDevice(state, action.device);
2018-07-30 10:52:13 +00:00
case DEVICE.CHANGED:
// return changeDevice(state, { ...action.device, connected: true, available: true });
return changeDevice(state, action.device, { connected: true, available: true });
2019-03-04 12:33:02 +00:00
// TODO: check if available will propagate to unavailable
2018-07-30 10:52:13 +00:00
case DEVICE.DISCONNECT:
2019-03-04 12:33:02 +00:00
// case DEVICE.DISCONNECT_UNACQUIRED:
return disconnectDevice(state, action.device);
2018-07-30 10:52:13 +00:00
case WALLET.SET_SELECTED_DEVICE:
return onSelectedDevice(state, action.device);
case CONNECT.RECEIVE_WALLET_TYPE:
case CONNECT.UPDATE_WALLET_TYPE:
return onChangeWalletType(state, action.device, action.hidden);
default:
return state;
}
2019-03-04 12:33:02 +00:00
}