2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import TrezorConnect from 'trezor-connect';
|
|
|
|
import * as RECEIVE from './constants/receive';
|
|
|
|
import * as NOTIFICATION from './constants/notification';
|
|
|
|
|
|
|
|
import { initialState } from '../reducers/ReceiveReducer';
|
|
|
|
import type { State } from '../reducers/ReceiveReducer';
|
|
|
|
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
|
|
|
|
|
2018-05-02 09:01:08 +00:00
|
|
|
import type { TrezorDevice, ThunkAction, AsyncAction, Action, GetState, Dispatch } from '../flowtype';
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
export type ReceiveAction = {
|
|
|
|
type: typeof RECEIVE.INIT,
|
|
|
|
state: State
|
|
|
|
} | {
|
|
|
|
type: typeof RECEIVE.DISPOSE,
|
|
|
|
} | {
|
|
|
|
type: typeof RECEIVE.REQUEST_UNVERIFIED,
|
|
|
|
device: TrezorDevice
|
|
|
|
} | {
|
|
|
|
type: typeof RECEIVE.SHOW_ADDRESS
|
|
|
|
} | {
|
|
|
|
type: typeof RECEIVE.SHOW_UNVERIFIED_ADDRESS
|
|
|
|
}
|
|
|
|
|
2018-05-02 09:01:08 +00:00
|
|
|
export const init = (): ThunkAction => {
|
2018-04-16 21:19:50 +00:00
|
|
|
return (dispatch: Dispatch, getState: GetState): void => {
|
2018-04-11 13:21:43 +00:00
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
const state: State = {
|
|
|
|
...initialState,
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: RECEIVE.INIT,
|
|
|
|
state: state
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 09:01:08 +00:00
|
|
|
export const update = (): ThunkAction => {
|
2018-04-16 21:19:50 +00:00
|
|
|
return (dispatch: Dispatch, getState: GetState): void => {
|
2018-02-20 09:30:36 +00:00
|
|
|
const {
|
2018-04-11 13:21:43 +00:00
|
|
|
abstractAccount,
|
2018-02-20 09:30:36 +00:00
|
|
|
router
|
|
|
|
} = getState();
|
|
|
|
|
2018-04-11 13:21:43 +00:00
|
|
|
const isLocationChanged: boolean = router.location.pathname !== abstractAccount.location;
|
2018-02-20 09:30:36 +00:00
|
|
|
if (isLocationChanged) {
|
|
|
|
dispatch( init() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
export const dispose = (): Action => {
|
2018-02-20 09:30:36 +00:00
|
|
|
return {
|
|
|
|
type: RECEIVE.DISPOSE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
export const showUnverifiedAddress = (): Action => {
|
2018-02-20 09:30:36 +00:00
|
|
|
return {
|
|
|
|
type: RECEIVE.SHOW_UNVERIFIED_ADDRESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
export const showAddress = (address_n: string): AsyncAction => {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
2018-02-20 09:30:36 +00:00
|
|
|
|
|
|
|
const selected = findSelectedDevice(getState().connect);
|
2018-03-08 16:10:53 +00:00
|
|
|
if (!selected) return;
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-04-11 10:06:46 +00:00
|
|
|
if (selected && (!selected.connected || !selected.available)) {
|
2018-02-20 09:30:36 +00:00
|
|
|
dispatch({
|
|
|
|
type: RECEIVE.REQUEST_UNVERIFIED,
|
2018-04-11 10:06:46 +00:00
|
|
|
device: selected
|
2018-02-20 09:30:36 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await TrezorConnect.ethereumGetAddress({
|
|
|
|
device: {
|
|
|
|
path: selected.path,
|
|
|
|
instance: selected.instance,
|
2018-03-29 11:38:09 +00:00
|
|
|
state: selected.state
|
2018-02-20 09:30:36 +00:00
|
|
|
},
|
2018-03-27 15:12:01 +00:00
|
|
|
path: address_n,
|
2018-02-20 09:30:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (response && response.success) {
|
|
|
|
dispatch({
|
|
|
|
type: RECEIVE.SHOW_ADDRESS
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// TODO: handle invalid pin?
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
|
|
|
type: 'error',
|
|
|
|
title: 'Veryfying address error',
|
2018-03-27 15:12:01 +00:00
|
|
|
message: response.payload.error,
|
2018-02-20 09:30:36 +00:00
|
|
|
cancelable: true,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: 'Try again',
|
|
|
|
callback: () => {
|
|
|
|
dispatch(showAddress(address_n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 21:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
init,
|
|
|
|
update,
|
|
|
|
dispose,
|
|
|
|
showAddress,
|
|
|
|
showUnverifiedAddress
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|