1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-13 20:08:56 +00:00
trezor-wallet/src/reducers/DiscoveryReducer.js

196 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
import HDKey from 'hdkey';
2018-08-14 13:11:52 +00:00
import * as DISCOVERY from 'actions/constants/discovery';
import * as ACCOUNT from 'actions/constants/account';
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet';
2018-02-20 09:30:36 +00:00
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
2018-07-30 10:52:13 +00:00
import type {
2018-04-23 10:20:15 +00:00
DiscoveryStartAction,
DiscoveryWaitingAction,
DiscoveryStopAction,
2018-07-30 10:52:13 +00:00
DiscoveryCompleteAction,
2018-08-14 13:11:52 +00:00
} from 'actions/DiscoveryActions';
2018-04-23 10:20:15 +00:00
import type {
2018-07-30 10:52:13 +00:00
AccountCreateAction,
2018-08-14 13:11:52 +00:00
} from 'actions/AccountsActions';
2018-04-23 10:20:15 +00:00
2018-02-20 09:30:36 +00:00
export type Discovery = {
network: string;
publicKey: string;
chainCode: string;
2018-04-23 10:20:15 +00:00
hdKey: HDKey;
basePath: Array<number>;
2018-04-16 21:19:50 +00:00
deviceState: string;
2018-02-20 09:30:36 +00:00
accountIndex: number;
interrupted: boolean;
completed: boolean;
waitingForDevice: boolean;
2018-05-07 11:25:46 +00:00
waitingForBackend: boolean;
2018-02-20 09:30:36 +00:00
}
2018-04-23 10:20:15 +00:00
export type State = Array<Discovery>;
const initialState: State = [];
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const findIndex = (state: State, network: string, deviceState: string): number => state.findIndex(d => d.network === network && d.deviceState === deviceState);
2018-04-23 10:20:15 +00:00
const start = (state: State, action: DiscoveryStartAction): State => {
const deviceState: string = action.device.state || '0';
2018-07-30 10:52:13 +00:00
const hdKey: HDKey = new HDKey();
hdKey.publicKey = new Buffer(action.publicKey, 'hex');
hdKey.chainCode = new Buffer(action.chainCode, 'hex');
2018-02-20 09:30:36 +00:00
const instance: Discovery = {
network: action.network,
publicKey: action.publicKey,
chainCode: action.chainCode,
hdKey,
2018-02-20 09:30:36 +00:00
basePath: action.basePath,
2018-04-23 10:20:15 +00:00
deviceState,
2018-02-20 09:30:36 +00:00
accountIndex: 0,
interrupted: false,
completed: false,
2018-05-07 11:25:46 +00:00
waitingForDevice: false,
waitingForBackend: false,
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-04-23 10:20:15 +00:00
const index: number = findIndex(state, action.network, deviceState);
2018-02-20 09:30:36 +00:00
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
const complete = (state: State, action: DiscoveryCompleteAction): State => {
const index: number = findIndex(state, action.network, action.device.state || '0');
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
newState[index] = { ...newState[index], completed: true };
2018-02-20 09:30:36 +00:00
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-05-16 16:30:46 +00:00
const accountCreate = (state: State, action: AccountCreateAction): State => {
2018-04-23 10:20:15 +00:00
const index: number = findIndex(state, action.network, action.device.state || '0');
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
newState[index].accountIndex++;
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const forgetDiscovery = (state: State, device: TrezorDevice): State => state.filter(d => d.deviceState !== device.state);
2018-02-20 09:30:36 +00:00
const clear = (state: State, devices: Array<TrezorDevice>): State => {
2018-07-30 10:52:13 +00:00
let newState: State = [...state];
devices.forEach((d) => {
newState = forgetDiscovery(newState, d);
});
return newState;
2018-07-30 10:52:13 +00:00
};
2018-04-23 10:20:15 +00:00
const stop = (state: State, action: DiscoveryStopAction): State => {
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
return newState.map((d: Discovery) => {
if (d.deviceState === action.device.state && !d.completed) {
2018-02-20 09:30:36 +00:00
d.interrupted = true;
d.waitingForDevice = false;
}
return d;
});
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-05-07 11:25:46 +00:00
const waitingForDevice = (state: State, action: DiscoveryWaitingAction): State => {
2018-04-23 10:20:15 +00:00
const deviceState: string = action.device.state || '0';
2018-02-20 09:30:36 +00:00
const instance: Discovery = {
network: action.network,
2018-04-23 10:20:15 +00:00
deviceState,
publicKey: '',
chainCode: '',
2018-04-16 21:19:50 +00:00
hdKey: null,
2018-04-23 10:20:15 +00:00
basePath: [],
2018-02-20 09:30:36 +00:00
accountIndex: 0,
interrupted: false,
completed: false,
2018-05-07 11:25:46 +00:00
waitingForDevice: true,
waitingForBackend: false,
2018-07-30 10:52:13 +00:00
};
2018-05-07 11:25:46 +00:00
const index: number = findIndex(state, action.network, deviceState);
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-05-07 11:25:46 +00:00
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
return newState;
2018-07-30 10:52:13 +00:00
};
2018-05-07 11:25:46 +00:00
const waitingForBackend = (state: State, action: DiscoveryWaitingAction): State => {
const deviceState: string = action.device.state || '0';
const instance: Discovery = {
network: action.network,
deviceState,
publicKey: '',
chainCode: '',
2018-05-07 11:25:46 +00:00
hdKey: null,
basePath: [],
accountIndex: 0,
interrupted: false,
completed: false,
waitingForDevice: false,
2018-07-30 10:52:13 +00:00
waitingForBackend: true,
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
const index: number = findIndex(state, action.network, deviceState);
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
export default function discovery(state: State = initialState, action: Action): State {
2018-02-20 09:30:36 +00:00
switch (action.type) {
2018-07-30 10:52:13 +00:00
case DISCOVERY.START:
2018-02-20 09:30:36 +00:00
return start(state, action);
2018-07-30 10:52:13 +00:00
case ACCOUNT.CREATE:
2018-05-16 16:30:46 +00:00
return accountCreate(state, action);
2018-07-30 10:52:13 +00:00
case DISCOVERY.STOP:
2018-02-20 09:30:36 +00:00
return stop(state, action);
2018-07-30 10:52:13 +00:00
case DISCOVERY.COMPLETE:
2018-02-20 09:30:36 +00:00
return complete(state, action);
2018-07-30 10:52:13 +00:00
case DISCOVERY.WAITING_FOR_DEVICE:
2018-05-07 11:25:46 +00:00
return waitingForDevice(state, action);
2018-07-30 10:52:13 +00:00
case DISCOVERY.WAITING_FOR_BACKEND:
2018-05-07 11:25:46 +00:00
return waitingForBackend(state, action);
2018-07-30 10:52:13 +00:00
case DISCOVERY.FROM_STORAGE:
return action.payload.map((d) => {
const hdKey: HDKey = new HDKey();
hdKey.publicKey = new Buffer(d.publicKey, 'hex');
hdKey.chainCode = new Buffer(d.chainCode, 'hex');
2018-02-20 09:30:36 +00:00
return {
...d,
hdKey,
2018-02-20 09:30:36 +00:00
interrupted: false,
2018-05-07 11:25:46 +00:00
waitingForDevice: false,
waitingForBackend: false,
2018-07-30 10:52:13 +00:00
};
});
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
2018-04-23 10:20:15 +00:00
return forgetDiscovery(state, action.device);
2018-07-30 10:52:13 +00:00
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices);
2018-02-20 09:30:36 +00:00
default:
return state;
}
}