1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-01-03 21:00:55 +00:00

Simplify input values, added verify

This commit is contained in:
Vladimir Volek 2018-10-18 14:18:48 +02:00
parent b89a825077
commit 18a424c214
4 changed files with 95 additions and 72 deletions

View File

@ -4,7 +4,11 @@ import type { GetState, Dispatch } from 'flowtype';
import * as NOTIFICATION from 'actions/constants/notification'; import * as NOTIFICATION from 'actions/constants/notification';
import * as SIGN_VERIFY from './constants/signVerify'; import * as SIGN_VERIFY from './constants/signVerify';
export const sign = (path: Array<number>, message: string, hex: boolean = false): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const sign = (
path: Array<number>,
message: string,
hex: boolean = false,
): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const selected = getState().wallet.selectedDevice; const selected = getState().wallet.selectedDevice;
const devicePath = selected.path; const devicePath = selected.path;
const device = { const device = {
@ -31,7 +35,47 @@ export const sign = (path: Array<number>, message: string, hex: boolean = false)
type: NOTIFICATION.ADD, type: NOTIFICATION.ADD,
payload: { payload: {
type: 'error', type: 'error',
title: 'Signing error', title: 'Sign error',
message: response.payload.error,
cancelable: true,
actions: [{
label: 'Try again',
callback: () => {
dispatch(() => {});
},
},
],
},
});
}
};
export const verify = (
address: string,
message: string,
signature: string,
hex: boolean = false,
): AsyncAction => async (dispatch: Dispatch): Promise<void> => {
const input = {
address,
message,
signature,
hex,
};
console.log('input', input);
const response = await TrezorConnect.ethereumVerifyMessage(input);
if (response && response.success) {
dispatch({
type: SIGN_VERIFY.VERIFY_SUCCESS,
signature: response.payload.success,
});
} else {
dispatch({
type: NOTIFICATION.ADD,
payload: {
type: 'error',
title: 'Verify error',
message: response.payload.error, message: response.payload.error,
cancelable: true, cancelable: true,
actions: [ actions: [

View File

@ -3,7 +3,7 @@
import type { Action } from 'flowtype'; import type { Action } from 'flowtype';
import type { NetworkToken } from './LocalStorageReducer'; import type { NetworkToken } from './LocalStorageReducer';
import { SIGN_SUCCESS } from '../actions/constants/signVerify'; import * as SIGN_VERIFY from '../actions/constants/signVerify';
export type State = { export type State = {
details: boolean; details: boolean;
@ -16,10 +16,16 @@ export const initialState: State = {
export default (state: State = initialState, action: Action): State => { export default (state: State = initialState, action: Action): State => {
switch (action.type) { switch (action.type) {
case SIGN_SUCCESS: case SIGN_VERIFY.SIGN_SUCCESS:
return { return {
...state, ...state,
signature: state.signature, signature: action.signature,
};
case SIGN_VERIFY.VERIFY_SUCCESS:
return {
...state,
signature: action.signature,
}; };
default: default:

View File

@ -12,20 +12,14 @@ type OwnProps = {}
export type StateProps = { export type StateProps = {
selectedAccount: $ElementType<State, 'selectedAccount'>, selectedAccount: $ElementType<State, 'selectedAccount'>,
sendForm: $ElementType<State, 'sendForm'>,
wallet: $ElementType<State, 'wallet'>,
fiat: $ElementType<State, 'fiat'>,
localStorage: $ElementType<State, 'localStorage'>,
} }
export type Props = StateProps & DispatchProps; export type Props = StateProps & DispatchProps;
const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: State): StateProps => ({ const mapStateToProps: MapStateToProps<State, OwnProps, StateProps> = (state: State): StateProps => ({
selectedAccount: state.selectedAccount, selectedAccount: state.selectedAccount,
signature: state.signature, signature: state.signVerifyReducer.signature,
wallet: state.wallet, isVerifySuccess: state.signVerifyReducer.isVerifySuccess,
fiat: state.fiat,
localStorage: state.localStorage,
}); });
const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps> = (dispatch: Dispatch): DispatchProps => ({ const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps> = (dispatch: Dispatch): DispatchProps => ({

View File

@ -52,16 +52,10 @@ class SignVerify extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
sign: { signMessage: '',
address: '', verifyAddress: '',
message: '', verifyMessage: '',
signature: '', verifySignature: '',
},
verify: {
address: '',
message: '',
signature: '',
},
}; };
} }
@ -78,36 +72,16 @@ class SignVerify extends Component {
return result || 'loading...'; return result || 'loading...';
} }
handleSignInput = (e) => { handleInputChange = (event) => {
this.setState({ sign: { [e.target.name]: e.target.value } }); this.setState({ [event.target.name]: event.target.value });
}
handleVerifyInput = (e) => {
this.setState({ verify: { [e.target.name]: e.target.value } });
}
clearSign = () => {
this.setState({
sign: {
address: '',
message: '',
signature: '',
},
});
}
clearVerify = () => {
this.setState({
verify: {
address: '',
message: '',
signature: '',
},
});
} }
render() { render() {
const { signVerifyActions } = this.props; const {
signVerifyActions,
signature,
isVerifySuccess,
} = this.props;
return ( return (
<Content> <Content>
<Title>Sign & Verify</Title> <Title>Sign & Verify</Title>
@ -116,9 +90,8 @@ class SignVerify extends Component {
<Row> <Row>
<Label>Address</Label> <Label>Address</Label>
<Input <Input
name="address" name="signAddress"
value={this.getAddress()} value={this.getAddress()}
onChange={this.handleSignInput}
height={50} height={50}
type="text" type="text"
isSmallText isSmallText
@ -128,9 +101,9 @@ class SignVerify extends Component {
<Row> <Row>
<Label>Message</Label> <Label>Message</Label>
<Textarea <Textarea
name="message" name="signMessage"
value={this.state.sign.message} value={this.state.signMessage}
onChange={this.handleSignInput} onChange={this.handleInputChange}
rows="2" rows="2"
maxLength="255" maxLength="255"
/> />
@ -138,9 +111,8 @@ class SignVerify extends Component {
<Row> <Row>
<Label>Signature</Label> <Label>Signature</Label>
<Textarea <Textarea
name="signature" name="signSignature"
value={this.state.sign.signature} value={this.props.signature}
onChange={this.handleSign}
rows="2" rows="2"
maxLength="255" maxLength="255"
isDisabled isDisabled
@ -148,12 +120,12 @@ class SignVerify extends Component {
</Row> </Row>
<RowButtons> <RowButtons>
<Button <Button
onClick={this.clearVerify} onClick={() => this.clearSign()}
isWhite isWhite
>Clear >Clear
</Button> </Button>
<StyledButton <StyledButton
onClick={() => signVerifyActions.sign(this.getPath(), this.state.sign.message)} onClick={() => signVerifyActions.sign(this.getPath(), this.state.signMessage)}
>Sign >Sign
</StyledButton> </StyledButton>
</RowButtons> </RowButtons>
@ -162,9 +134,9 @@ class SignVerify extends Component {
<Row> <Row>
<Label>Address</Label> <Label>Address</Label>
<Input <Input
name="address" name="verifyAddress"
value={this.state.verify.address} value={this.state.verifyAddress}
onChange={this.handleVerifyInput} onChange={this.handleInputChange}
type="text" type="text"
isSmallText isSmallText
/> />
@ -172,9 +144,9 @@ class SignVerify extends Component {
<Row> <Row>
<Label>Message</Label> <Label>Message</Label>
<Textarea <Textarea
name="message" name="verifyMessage"
value={this.state.verify.message} value={this.state.verifyMessage}
onChange={this.handleVerifyInput} onChange={this.handleInputChange}
rows="4" rows="4"
maxLength="255" maxLength="255"
/> />
@ -182,20 +154,27 @@ class SignVerify extends Component {
<Row> <Row>
<Label>Signature</Label> <Label>Signature</Label>
<Textarea <Textarea
name="signature" name="verifySignature"
value={this.state.verify.signature} value={this.state.verifySignature}
onChange={this.handleVerifyInput} onChange={this.handleInputChange}
rows="4" rows="4"
maxLength="255" maxLength="255"
/> />
</Row> </Row>
<RowButtons> <RowButtons>
<Button <Button
onClick={this.clearSign} onClick={() => this.clearVerify()}
isWhite isWhite
>Clear >Clear
</Button> </Button>
<StyledButton>Verify</StyledButton> <StyledButton
onClick={() => signVerifyActions.verify(
this.state.verifyAddress,
this.state.verifyMessage,
this.state.verifySignature,
)}
>Verify
</StyledButton>
</RowButtons> </RowButtons>
</Verify> </Verify>
</Wrapper> </Wrapper>
@ -205,7 +184,7 @@ class SignVerify extends Component {
} }
SignVerify.propTypes = { SignVerify.propTypes = {
sign: PropTypes.func.isRequired, isVerifySuccess: PropTypes.bool,
}; };
export default SignVerify; export default SignVerify;