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

165 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-10-16 22:31:33 +00:00
/* @flow */
import TrezorConnect from 'trezor-connect';
2018-10-20 10:12:29 +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';
import * as SIGN_VERIFY from './constants/signVerify';
2018-10-16 22:31:33 +00:00
2018-10-20 10:12:29 +00:00
export type SignVerifyAction = {
type: typeof SIGN_VERIFY.SIGN_SUCCESS,
2018-11-22 13:20:37 +00:00
signSignature: string
2018-10-20 10:12:29 +00:00
} | {
2018-11-22 13:20:37 +00:00
type: typeof SIGN_VERIFY.CLEAR_SIGN,
} | {
type: typeof SIGN_VERIFY.CLEAR_VERIFY,
} | {
type: typeof SIGN_VERIFY.INPUT_CHANGE,
2018-11-27 16:11:46 +00:00
inputName: string,
2018-11-22 13:20:37 +00:00
value: string
} | {
type: typeof SIGN_VERIFY.TOUCH,
2018-11-27 16:11:46 +00:00
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
}
const sign = (
2018-10-18 12:18:48 +00:00
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: {
type: 'error',
2018-10-18 12:18:48 +00:00
title: 'Sign error',
message: response.payload.error,
cancelable: true,
},
});
}
};
const verify = (
2018-10-18 12:18:48 +00:00
address: string,
message: string,
signature: string,
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: {
type: 'success',
title: 'Verify success',
message: 'signature is valid',
cancelable: true,
},
});
} else {
dispatch({
type: NOTIFICATION.ADD,
payload: {
type: 'error',
title: 'Verify error',
message: response.payload.error,
cancelable: true,
},
});
}
2018-10-17 15:57:21 +00:00
}
2018-10-18 22:55:16 +00:00
};
2018-11-27 16:11:46 +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
});
2018-11-27 16:11:46 +00:00
if (inputName === 'verifyAddress' && validateAddress(value) !== null) {
dispatch({
type: SIGN_VERIFY.ERROR,
inputName,
message: validateAddress(value),
});
}
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
});
};
export default {
sign,
verify,
2018-11-21 18:32:28 +00:00
clearSign,
clearVerify,
2018-11-21 18:19:14 +00:00
inputChange,
2018-10-16 22:31:33 +00:00
};