1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-12 11:28:56 +00:00
trezor-wallet/src/actions/ReceiveActions.js

108 lines
2.7 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
import TrezorConnect from 'trezor-connect';
2018-08-14 13:18:16 +00:00
import * as RECEIVE from 'actions/constants/receive';
import * as NOTIFICATION from 'actions/constants/notification';
2018-02-20 09:30:36 +00:00
2018-08-14 13:18:16 +00:00
import { initialState } from 'reducers/ReceiveReducer';
import type { State } from 'reducers/ReceiveReducer';
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
import type {
TrezorDevice, ThunkAction, AsyncAction, Action, GetState, Dispatch,
2018-08-14 12:56:47 +00:00
} 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.HIDE_ADDRESS
2018-04-16 21:19:50 +00:00
} | {
type: typeof RECEIVE.SHOW_UNVERIFIED_ADDRESS
}
2018-09-05 14:17:50 +00:00
export const init = (): ThunkAction => (dispatch: Dispatch): void => {
2018-07-30 10:52:13 +00:00
const state: State = {
...initialState,
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
dispatch({
type: RECEIVE.INIT,
state,
});
};
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const dispose = (): Action => ({
type: RECEIVE.DISPOSE,
});
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const showUnverifiedAddress = (): Action => ({
type: RECEIVE.SHOW_UNVERIFIED_ADDRESS,
});
2018-02-20 09:30:36 +00:00
2018-05-05 11:52:03 +00:00
//export const showAddress = (address_n: string): AsyncAction => {
2018-09-23 06:49:43 +00:00
export const showAddress = (path: Array<number>): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-07-30 10:52:13 +00:00
const selected = getState().wallet.selectedDevice;
if (!selected) return;
if (selected && (!selected.connected || !selected.available)) {
dispatch({
type: RECEIVE.REQUEST_UNVERIFIED,
device: selected,
});
return;
}
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const response = await TrezorConnect.ethereumGetAddress({
device: {
path: selected.path,
instance: selected.instance,
state: selected.state,
},
2018-09-23 06:49:43 +00:00
path,
// useEmptyPassphrase: !selected.instance,
useEmptyPassphrase: selected.useEmptyPassphrase,
2018-07-30 10:52:13 +00:00
});
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
if (response && response.success) {
dispatch({
type: RECEIVE.SHOW_ADDRESS,
});
} else {
dispatch({
type: RECEIVE.HIDE_ADDRESS,
});
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
dispatch({
type: NOTIFICATION.ADD,
payload: {
type: 'error',
title: 'Verifying address error',
message: response.payload.error,
cancelable: true,
actions: [
{
label: 'Try again',
callback: () => {
2018-09-23 06:49:43 +00:00
dispatch(showAddress(path));
2018-07-30 10:52:13 +00:00
},
},
],
2018-02-20 09:30:36 +00:00
},
});
}
2018-07-30 10:52:13 +00:00
};
2018-04-16 21:19:50 +00:00
export default {
init,
dispose,
showAddress,
2018-07-30 10:52:13 +00:00
showUnverifiedAddress,
};