You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/DiscoveryReducer.js

196 lines
5.9 KiB

6 years ago
/* @flow */
6 years ago
6 years ago
import HDKey from 'hdkey';
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';
6 years ago
import type { Action, TrezorDevice } from 'flowtype';
import type {
6 years ago
DiscoveryStartAction,
DiscoveryWaitingAction,
DiscoveryStopAction,
DiscoveryCompleteAction,
} from 'actions/DiscoveryActions';
6 years ago
import type {
AccountCreateAction,
} from 'actions/AccountsActions';
6 years ago
6 years ago
export type Discovery = {
network: string;
publicKey: string;
chainCode: string;
6 years ago
hdKey: HDKey;
basePath: Array<number>;
deviceState: string;
6 years ago
accountIndex: number;
interrupted: boolean;
completed: boolean;
waitingForDevice: boolean;
waitingForBackend: boolean;
6 years ago
}
6 years ago
export type State = Array<Discovery>;
const initialState: State = [];
6 years ago
const findIndex = (state: State, network: string, deviceState: string): number => state.findIndex(d => d.network === network && d.deviceState === deviceState);
6 years ago
const start = (state: State, action: DiscoveryStartAction): State => {
const deviceState: string = action.device.state || '0';
const hdKey: HDKey = new HDKey();
hdKey.publicKey = new Buffer(action.publicKey, 'hex');
hdKey.chainCode = new Buffer(action.chainCode, 'hex');
6 years ago
const instance: Discovery = {
network: action.network,
publicKey: action.publicKey,
chainCode: action.chainCode,
hdKey,
6 years ago
basePath: action.basePath,
6 years ago
deviceState,
6 years ago
accountIndex: 0,
interrupted: false,
completed: false,
waitingForDevice: false,
waitingForBackend: false,
};
6 years ago
const newState: State = [...state];
6 years ago
const index: number = findIndex(state, action.network, deviceState);
6 years ago
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
return newState;
};
6 years ago
6 years ago
const complete = (state: State, action: DiscoveryCompleteAction): State => {
const index: number = findIndex(state, action.network, action.device.state || '0');
const newState: State = [...state];
newState[index] = { ...newState[index], completed: true };
6 years ago
return newState;
};
6 years ago
const accountCreate = (state: State, action: AccountCreateAction): State => {
6 years ago
const index: number = findIndex(state, action.network, action.device.state || '0');
const newState: State = [...state];
6 years ago
newState[index].accountIndex++;
return newState;
};
6 years ago
const forgetDiscovery = (state: State, device: TrezorDevice): State => state.filter(d => d.deviceState !== device.state);
6 years ago
const clear = (state: State, devices: Array<TrezorDevice>): State => {
let newState: State = [...state];
devices.forEach((d) => {
newState = forgetDiscovery(newState, d);
});
return newState;
};
6 years ago
const stop = (state: State, action: DiscoveryStopAction): State => {
const newState: State = [...state];
return newState.map((d: Discovery) => {
if (d.deviceState === action.device.state && !d.completed) {
6 years ago
d.interrupted = true;
d.waitingForDevice = false;
}
return d;
});
};
6 years ago
const waitingForDevice = (state: State, action: DiscoveryWaitingAction): State => {
6 years ago
const deviceState: string = action.device.state || '0';
6 years ago
const instance: Discovery = {
network: action.network,
6 years ago
deviceState,
publicKey: '',
chainCode: '',
hdKey: null,
6 years ago
basePath: [],
6 years ago
accountIndex: 0,
interrupted: false,
completed: false,
waitingForDevice: true,
waitingForBackend: false,
};
const index: number = findIndex(state, action.network, deviceState);
const newState: State = [...state];
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
return newState;
};
const waitingForBackend = (state: State, action: DiscoveryWaitingAction): State => {
const deviceState: string = action.device.state || '0';
const instance: Discovery = {
network: action.network,
deviceState,
publicKey: '',
chainCode: '',
hdKey: null,
basePath: [],
accountIndex: 0,
interrupted: false,
completed: false,
waitingForDevice: false,
waitingForBackend: true,
};
6 years ago
6 years ago
const index: number = findIndex(state, action.network, deviceState);
const newState: State = [...state];
6 years ago
if (index >= 0) {
newState[index] = instance;
} else {
newState.push(instance);
}
return newState;
};
6 years ago
6 years ago
export default function discovery(state: State = initialState, action: Action): State {
6 years ago
switch (action.type) {
case DISCOVERY.START:
6 years ago
return start(state, action);
case ACCOUNT.CREATE:
return accountCreate(state, action);
case DISCOVERY.STOP:
6 years ago
return stop(state, action);
case DISCOVERY.COMPLETE:
6 years ago
return complete(state, action);
case DISCOVERY.WAITING_FOR_DEVICE:
return waitingForDevice(state, action);
case DISCOVERY.WAITING_FOR_BACKEND:
return waitingForBackend(state, action);
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');
6 years ago
return {
...d,
hdKey,
6 years ago
interrupted: false,
waitingForDevice: false,
waitingForBackend: false,
};
});
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
6 years ago
return forgetDiscovery(state, action.device);
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices);
6 years ago
default:
return state;
}
}