2018-10-16 22:31:33 +00:00
|
|
|
/* @flow */
|
2019-04-29 16:49:16 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2018-10-16 22:31:33 +00:00
|
|
|
import TrezorConnect from 'trezor-connect';
|
2019-03-04 12:33:02 +00:00
|
|
|
import type { GetState, Dispatch, ThunkAction, AsyncAction } from 'flowtype';
|
2018-11-27 16:11:46 +00:00
|
|
|
import { validateAddress } from 'utils/ethUtils';
|
2018-10-17 15:57:21 +00:00
|
|
|
import * as NOTIFICATION from 'actions/constants/notification';
|
2019-04-29 16:49:16 +00:00
|
|
|
import l10nMessages from 'components/notifications/Context/actions.messages';
|
2018-10-17 15:57:21 +00:00
|
|
|
import * as SIGN_VERIFY from './constants/signVerify';
|
2018-10-16 22:31:33 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export type SignVerifyAction =
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.SIGN_SUCCESS,
|
|
|
|
signSignature: string,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.CLEAR_SIGN,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.CLEAR_VERIFY,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.INPUT_CHANGE,
|
|
|
|
inputName: string,
|
|
|
|
value: string,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.TOUCH,
|
|
|
|
inputName: string,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.ERROR,
|
|
|
|
inputName: string,
|
|
|
|
message: ?string,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof SIGN_VERIFY.ERROR,
|
|
|
|
inputName: string,
|
|
|
|
message: ?string,
|
|
|
|
};
|
2018-10-20 10:12:29 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
const sign = (path: Array<number>, message: string, hex: boolean = false): AsyncAction => async (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
): Promise<void> => {
|
2018-10-16 22:31:33 +00:00
|
|
|
const selected = getState().wallet.selectedDevice;
|
2018-11-02 15:38:27 +00:00
|
|
|
if (!selected) return;
|
|
|
|
|
|
|
|
const response = await TrezorConnect.ethereumSignMessage({
|
2018-10-20 00:11:24 +00:00
|
|
|
device: {
|
2018-11-02 15:38:27 +00:00
|
|
|
path: selected.path,
|
2018-10-20 00:11:24 +00:00
|
|
|
instance: selected.instance,
|
|
|
|
state: selected.state,
|
|
|
|
},
|
2018-10-17 15:57:21 +00:00
|
|
|
path,
|
|
|
|
hex,
|
|
|
|
message,
|
|
|
|
useEmptyPassphrase: selected.useEmptyPassphrase,
|
2018-11-02 15:38:27 +00:00
|
|
|
});
|
2018-10-16 22:31:33 +00:00
|
|
|
|
2018-10-17 15:57:21 +00:00
|
|
|
if (response && response.success) {
|
|
|
|
dispatch({
|
|
|
|
type: SIGN_VERIFY.SIGN_SUCCESS,
|
2018-11-22 13:20:37 +00:00
|
|
|
signSignature: response.payload.signature,
|
2018-10-17 15:57:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
2019-05-02 12:00:24 +00:00
|
|
|
variant: 'error',
|
2019-04-29 16:49:16 +00:00
|
|
|
title: <FormattedMessage {...l10nMessages.TR_SIGN_MESSAGE_ERROR} />,
|
2018-10-18 12:18:48 +00:00
|
|
|
message: response.payload.error,
|
|
|
|
cancelable: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-21 13:47:04 +00:00
|
|
|
const verify = (
|
2018-10-18 12:18:48 +00:00
|
|
|
address: string,
|
|
|
|
message: string,
|
|
|
|
signature: string,
|
2019-03-04 12:33:02 +00:00
|
|
|
hex: boolean = false
|
2018-10-19 23:12:17 +00:00
|
|
|
): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
|
|
|
const selected = getState().wallet.selectedDevice;
|
2018-11-02 15:38:27 +00:00
|
|
|
if (!selected) return;
|
2018-11-27 16:11:46 +00:00
|
|
|
const hasError = validateAddress(address);
|
2018-11-05 13:57:23 +00:00
|
|
|
|
2018-11-27 16:11:46 +00:00
|
|
|
if (hasError) {
|
2018-10-18 12:18:48 +00:00
|
|
|
dispatch({
|
2018-11-27 16:11:46 +00:00
|
|
|
type: SIGN_VERIFY.ERROR,
|
|
|
|
inputName: 'verifyAddress',
|
|
|
|
message: validateAddress(address),
|
2018-10-18 12:18:48 +00:00
|
|
|
});
|
2018-11-27 16:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasError) {
|
|
|
|
const response = await TrezorConnect.ethereumVerifyMessage({
|
|
|
|
device: {
|
|
|
|
path: selected.path,
|
|
|
|
instance: selected.instance,
|
|
|
|
state: selected.state,
|
2018-10-17 15:57:21 +00:00
|
|
|
},
|
2018-11-27 16:11:46 +00:00
|
|
|
address,
|
|
|
|
message,
|
|
|
|
signature,
|
|
|
|
hex,
|
|
|
|
useEmptyPassphrase: selected.useEmptyPassphrase,
|
2018-10-17 15:57:21 +00:00
|
|
|
});
|
2018-11-27 16:11:46 +00:00
|
|
|
|
|
|
|
if (response && response.success) {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
2019-05-02 12:00:24 +00:00
|
|
|
variant: 'success',
|
2019-04-29 16:49:16 +00:00
|
|
|
title: <FormattedMessage {...l10nMessages.TR_VERIFY_MESSAGE_SUCCESS} />,
|
|
|
|
message: <FormattedMessage {...l10nMessages.TR_SIGNATURE_IS_VALID} />,
|
2018-11-27 16:11:46 +00:00
|
|
|
cancelable: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
2019-05-02 12:00:24 +00:00
|
|
|
variant: 'error',
|
2019-04-29 16:49:16 +00:00
|
|
|
title: <FormattedMessage {...l10nMessages.TR_VERIFY_MESSAGE_ERROR} />,
|
2018-11-27 16:11:46 +00:00
|
|
|
message: response.payload.error,
|
|
|
|
cancelable: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2018-10-17 15:57:21 +00:00
|
|
|
}
|
2018-10-18 22:55:16 +00:00
|
|
|
};
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
const inputChange = (inputName: string, value: string): ThunkAction => (
|
|
|
|
dispatch: Dispatch
|
|
|
|
): void => {
|
2018-11-21 18:19:14 +00:00
|
|
|
dispatch({
|
|
|
|
type: SIGN_VERIFY.INPUT_CHANGE,
|
2018-11-27 16:11:46 +00:00
|
|
|
inputName,
|
2018-11-21 18:19:14 +00:00
|
|
|
value,
|
|
|
|
});
|
|
|
|
dispatch({
|
|
|
|
type: SIGN_VERIFY.TOUCH,
|
2018-11-27 16:11:46 +00:00
|
|
|
inputName,
|
2018-11-21 18:19:14 +00:00
|
|
|
});
|
2019-03-29 08:47:05 +00:00
|
|
|
|
|
|
|
if (inputName === 'verifyAddress') {
|
|
|
|
const error = validateAddress(value);
|
|
|
|
if (error) {
|
|
|
|
dispatch({
|
|
|
|
type: SIGN_VERIFY.ERROR,
|
|
|
|
inputName,
|
|
|
|
message: error,
|
|
|
|
});
|
|
|
|
}
|
2018-11-27 16:11:46 +00:00
|
|
|
}
|
2018-11-21 18:19:14 +00:00
|
|
|
};
|
|
|
|
|
2018-11-21 18:32:28 +00:00
|
|
|
const clearSign = (): ThunkAction => (dispatch: Dispatch): void => {
|
2018-10-18 22:55:16 +00:00
|
|
|
dispatch({
|
2018-11-21 18:32:28 +00:00
|
|
|
type: SIGN_VERIFY.CLEAR_SIGN,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearVerify = (): ThunkAction => (dispatch: Dispatch): void => {
|
|
|
|
dispatch({
|
|
|
|
type: SIGN_VERIFY.CLEAR_VERIFY,
|
2018-10-18 22:55:16 +00:00
|
|
|
});
|
2018-11-21 13:47:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
sign,
|
|
|
|
verify,
|
2018-11-21 18:32:28 +00:00
|
|
|
clearSign,
|
|
|
|
clearVerify,
|
2018-11-21 18:19:14 +00:00
|
|
|
inputChange,
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|