2018-04-11 10:59:44 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import TrezorConnect from 'trezor-connect';
|
2018-07-30 10:52:13 +00:00
|
|
|
import EthereumjsUtil from 'ethereumjs-util';
|
2018-08-14 13:18:16 +00:00
|
|
|
import * as DISCOVERY from 'actions/constants/discovery';
|
|
|
|
import * as ACCOUNT from 'actions/constants/account';
|
|
|
|
import * as NOTIFICATION from 'actions/constants/notification';
|
2018-08-14 14:06:34 +00:00
|
|
|
import type {
|
2018-09-13 12:40:41 +00:00
|
|
|
ThunkAction, AsyncAction, PromiseAction, Action, GetState, Dispatch, TrezorDevice,
|
2018-08-14 14:06:34 +00:00
|
|
|
} from 'flowtype';
|
|
|
|
import type { Discovery, State } from 'reducers/DiscoveryReducer';
|
2018-09-12 11:25:21 +00:00
|
|
|
import * as BlockchainActions from './BlockchainActions';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-04-23 10:20:15 +00:00
|
|
|
export type DiscoveryStartAction = {
|
2018-04-16 21:19:50 +00:00
|
|
|
type: typeof DISCOVERY.START,
|
2018-04-23 10:20:15 +00:00
|
|
|
device: TrezorDevice,
|
|
|
|
network: string,
|
2018-05-07 14:02:10 +00:00
|
|
|
publicKey: string,
|
|
|
|
chainCode: string,
|
2018-04-23 10:20:15 +00:00
|
|
|
basePath: Array<number>,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DiscoveryWaitingAction = {
|
2018-09-13 12:40:41 +00:00
|
|
|
type: typeof DISCOVERY.WAITING_FOR_DEVICE | typeof DISCOVERY.WAITING_FOR_BLOCKCHAIN,
|
2018-04-23 10:20:15 +00:00
|
|
|
device: TrezorDevice,
|
|
|
|
network: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DiscoveryStopAction = {
|
2018-04-16 21:19:50 +00:00
|
|
|
type: typeof DISCOVERY.STOP,
|
2018-04-23 10:20:15 +00:00
|
|
|
device: TrezorDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DiscoveryCompleteAction = {
|
2018-04-16 21:19:50 +00:00
|
|
|
type: typeof DISCOVERY.COMPLETE,
|
2018-04-23 10:20:15 +00:00
|
|
|
device: TrezorDevice,
|
|
|
|
network: string
|
|
|
|
}
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-09-05 14:17:50 +00:00
|
|
|
export type DiscoveryAction = {
|
|
|
|
type: typeof DISCOVERY.FROM_STORAGE,
|
|
|
|
payload: State
|
|
|
|
} | DiscoveryStartAction
|
2018-09-12 11:25:21 +00:00
|
|
|
| DiscoveryWaitingAction
|
|
|
|
| DiscoveryStopAction
|
|
|
|
| DiscoveryCompleteAction;
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
export const start = (device: TrezorDevice, network: string, ignoreCompleted?: boolean): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
const selected = getState().wallet.selectedDevice;
|
|
|
|
if (!selected) {
|
|
|
|
// TODO: throw error
|
|
|
|
console.error('Start discovery: no selected device', device);
|
|
|
|
return;
|
|
|
|
} if (selected.path !== device.path) {
|
|
|
|
console.error('Start discovery: requested device is not selected', device, selected);
|
|
|
|
return;
|
|
|
|
} if (!selected.state) {
|
|
|
|
console.warn("Start discovery: Selected device wasn't authenticated yet...");
|
|
|
|
return;
|
|
|
|
} if (selected.connected && !selected.available) {
|
|
|
|
console.warn('Start discovery: Selected device is unavailable...');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-23 07:13:09 +00:00
|
|
|
const { discovery } = getState();
|
2018-09-12 11:25:21 +00:00
|
|
|
const discoveryProcess: ?Discovery = discovery.find(d => d.deviceState === device.state && d.network === network);
|
|
|
|
|
|
|
|
if (!selected.connected && (!discoveryProcess || !discoveryProcess.completed)) {
|
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.WAITING_FOR_DEVICE,
|
|
|
|
device,
|
|
|
|
network,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const blockchain = getState().blockchain.find(b => b.name === network);
|
2018-09-13 12:40:41 +00:00
|
|
|
if (blockchain && !blockchain.connected && (!discoveryProcess || !discoveryProcess.completed)) {
|
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.WAITING_FOR_BLOCKCHAIN,
|
|
|
|
device,
|
|
|
|
network,
|
|
|
|
});
|
2018-09-12 11:25:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!discoveryProcess) {
|
2018-09-23 07:13:09 +00:00
|
|
|
dispatch(begin(device, network));
|
2018-09-12 11:25:21 +00:00
|
|
|
} else if (discoveryProcess.completed && !ignoreCompleted) {
|
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.COMPLETE,
|
|
|
|
device,
|
|
|
|
network,
|
|
|
|
});
|
2018-09-13 12:40:41 +00:00
|
|
|
} else if (discoveryProcess.interrupted || discoveryProcess.waitingForDevice || discoveryProcess.waitingForBlockchain) {
|
2018-09-12 11:25:21 +00:00
|
|
|
// discovery cycle was interrupted
|
|
|
|
// start from beginning
|
|
|
|
dispatch(begin(device, network));
|
|
|
|
} else {
|
|
|
|
dispatch(discoverAccount(device, discoveryProcess));
|
|
|
|
}
|
|
|
|
};
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
// first iteration
|
|
|
|
// generate public key for this account
|
|
|
|
// start discovery process
|
|
|
|
const begin = (device: TrezorDevice, network: string): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
|
|
|
const { config } = getState().localStorage;
|
|
|
|
const coinToDiscover = config.coins.find(c => c.network === network);
|
|
|
|
if (!coinToDiscover) return;
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.WAITING_FOR_DEVICE,
|
|
|
|
device,
|
|
|
|
network,
|
|
|
|
});
|
|
|
|
|
|
|
|
// get xpub from TREZOR
|
|
|
|
const response = await TrezorConnect.getPublicKey({
|
|
|
|
device: {
|
|
|
|
path: device.path,
|
|
|
|
instance: device.instance,
|
|
|
|
state: device.state,
|
|
|
|
},
|
|
|
|
path: coinToDiscover.bip44,
|
|
|
|
keepSession: true, // acquire and hold session
|
|
|
|
useEmptyPassphrase: !device.instance,
|
|
|
|
});
|
|
|
|
|
|
|
|
// handle TREZOR response error
|
|
|
|
if (!response.success) {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
|
|
|
type: 'error',
|
|
|
|
title: 'Discovery error',
|
|
|
|
message: response.payload.error,
|
|
|
|
cancelable: true,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: 'Try again',
|
|
|
|
callback: () => {
|
|
|
|
dispatch(start(device, network));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-12 14:59:31 +00:00
|
|
|
// check for interruption
|
|
|
|
const discoveryProcess: ?Discovery = getState().discovery.find(d => d.deviceState === device.state && d.network === network);
|
|
|
|
if (discoveryProcess && discoveryProcess.interrupted) return;
|
|
|
|
|
|
|
|
const basePath: Array<number> = response.payload.path;
|
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
// send data to reducer
|
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.START,
|
|
|
|
network: coinToDiscover.network,
|
|
|
|
device,
|
|
|
|
publicKey: response.payload.publicKey,
|
|
|
|
chainCode: response.payload.chainCode,
|
|
|
|
basePath,
|
|
|
|
});
|
|
|
|
|
|
|
|
// next iteration
|
|
|
|
dispatch(start(device, network));
|
|
|
|
};
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-09-23 07:13:09 +00:00
|
|
|
const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction => async (dispatch: Dispatch): Promise<void> => {
|
2018-09-05 14:17:50 +00:00
|
|
|
const { completed } = discoveryProcess;
|
2018-07-30 10:52:13 +00:00
|
|
|
|
|
|
|
const derivedKey = discoveryProcess.hdKey.derive(`m/${discoveryProcess.accountIndex}`);
|
|
|
|
const path = discoveryProcess.basePath.concat(discoveryProcess.accountIndex);
|
|
|
|
const publicAddress: string = EthereumjsUtil.publicToAddress(derivedKey.publicKey, true).toString('hex');
|
|
|
|
const ethAddress: string = EthereumjsUtil.toChecksumAddress(publicAddress);
|
2018-09-05 14:17:50 +00:00
|
|
|
const { network } = discoveryProcess;
|
2018-07-30 10:52:13 +00:00
|
|
|
|
|
|
|
// TODO: check if address was created before
|
2018-09-12 11:25:21 +00:00
|
|
|
try {
|
2018-09-23 07:13:09 +00:00
|
|
|
const account = await dispatch(BlockchainActions.discoverAccount(device, ethAddress, network));
|
2018-09-12 11:25:21 +00:00
|
|
|
if (discoveryProcess.interrupted) return;
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
// const accountIsEmpty = account.transactions <= 0 && account.nonce <= 0 && account.balance === '0';
|
|
|
|
const accountIsEmpty = account.nonce <= 0 && account.balance === '0';
|
|
|
|
if (!accountIsEmpty || (accountIsEmpty && completed) || (accountIsEmpty && discoveryProcess.accountIndex === 0)) {
|
2018-04-11 10:59:44 +00:00
|
|
|
dispatch({
|
2018-09-12 11:25:21 +00:00
|
|
|
type: ACCOUNT.CREATE,
|
2018-04-11 10:59:44 +00:00
|
|
|
payload: {
|
2018-09-12 11:25:21 +00:00
|
|
|
index: discoveryProcess.accountIndex,
|
|
|
|
loaded: true,
|
|
|
|
network,
|
|
|
|
deviceID: device.features ? device.features.device_id : '0',
|
|
|
|
deviceState: device.state || '0',
|
|
|
|
addressPath: path,
|
|
|
|
address: ethAddress,
|
|
|
|
balance: account.balance,
|
|
|
|
nonce: account.nonce,
|
|
|
|
block: account.block,
|
2018-09-23 07:13:09 +00:00
|
|
|
transactions: account.transactions,
|
|
|
|
},
|
2018-07-30 10:52:13 +00:00
|
|
|
});
|
2018-04-11 10:59:44 +00:00
|
|
|
}
|
2018-09-12 11:25:21 +00:00
|
|
|
|
|
|
|
if (accountIsEmpty) {
|
2018-09-23 07:13:09 +00:00
|
|
|
dispatch(finish(device, discoveryProcess));
|
|
|
|
} else if (!completed) {
|
|
|
|
dispatch(discoverAccount(device, discoveryProcess));
|
2018-09-12 11:25:21 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.STOP,
|
2018-09-23 07:13:09 +00:00
|
|
|
device,
|
2018-09-12 11:25:21 +00:00
|
|
|
});
|
|
|
|
|
2018-04-11 10:59:44 +00:00
|
|
|
dispatch({
|
2018-07-30 10:52:13 +00:00
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
|
|
|
type: 'error',
|
2018-09-12 11:25:21 +00:00
|
|
|
title: 'Account discovery error',
|
|
|
|
message: error.message,
|
2018-07-30 10:52:13 +00:00
|
|
|
cancelable: true,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: 'Try again',
|
|
|
|
callback: () => {
|
|
|
|
dispatch(start(device, discoveryProcess.network));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-04-11 10:59:44 +00:00
|
|
|
});
|
|
|
|
}
|
2018-09-12 11:25:21 +00:00
|
|
|
};
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-09-23 07:13:09 +00:00
|
|
|
const finish = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction => async (dispatch: Dispatch): Promise<void> => {
|
2018-09-12 11:25:21 +00:00
|
|
|
await TrezorConnect.getFeatures({
|
|
|
|
device: {
|
|
|
|
path: device.path,
|
|
|
|
instance: device.instance,
|
|
|
|
state: device.state,
|
|
|
|
},
|
|
|
|
keepSession: false,
|
|
|
|
useEmptyPassphrase: !device.instance,
|
|
|
|
});
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-09-23 07:13:09 +00:00
|
|
|
await dispatch(BlockchainActions.subscribe(discoveryProcess.network));
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
if (discoveryProcess.interrupted) return;
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-09-12 11:25:21 +00:00
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.COMPLETE,
|
|
|
|
device,
|
|
|
|
network: discoveryProcess.network,
|
|
|
|
});
|
2018-09-23 07:13:09 +00:00
|
|
|
};
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-09-23 07:13:09 +00:00
|
|
|
export const reconnect = (network: string): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
|
2018-09-13 12:40:41 +00:00
|
|
|
await dispatch(BlockchainActions.subscribe(network));
|
|
|
|
dispatch(restore());
|
2018-09-23 07:13:09 +00:00
|
|
|
};
|
2018-09-13 12:40:41 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
export const restore = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
const selected = getState().wallet.selectedDevice;
|
|
|
|
if (selected && selected.connected && selected.features) {
|
2018-09-13 12:40:41 +00:00
|
|
|
const discoveryProcess: ?Discovery = getState().discovery.find(d => d.deviceState === selected.state && (d.interrupted || d.waitingForDevice || d.waitingForBlockchain));
|
2018-07-30 10:52:13 +00:00
|
|
|
if (discoveryProcess) {
|
|
|
|
dispatch(start(selected, discoveryProcess.network));
|
2018-04-11 10:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-05-11 19:01:43 +00:00
|
|
|
// TODO: rename method to something intuitive
|
2018-04-11 10:59:44 +00:00
|
|
|
// there is no discovery process but it should be
|
|
|
|
// this is possible race condition when "network" was changed in url but device was not authenticated yet
|
|
|
|
// try to start discovery after CONNECT.AUTH_DEVICE action
|
2018-07-30 10:52:13 +00:00
|
|
|
export const check = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
const selected = getState().wallet.selectedDevice;
|
|
|
|
if (!selected) return;
|
|
|
|
|
|
|
|
const urlParams = getState().router.location.state;
|
|
|
|
if (urlParams.network) {
|
|
|
|
const discoveryProcess: ?Discovery = getState().discovery.find(d => d.deviceState === selected.state && d.network === urlParams.network);
|
|
|
|
if (!discoveryProcess) {
|
|
|
|
dispatch(start(selected, urlParams.network));
|
2018-04-11 10:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
2018-04-11 10:59:44 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
export const stop = (device: TrezorDevice): Action => ({
|
|
|
|
type: DISCOVERY.STOP,
|
|
|
|
device,
|
|
|
|
});
|