2017-10-30 19:10:25 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import translate from 'translations';
|
|
|
|
import { showNotification, TShowNotification } from 'actions/notifications';
|
|
|
|
import { verifySignedMessage, ISignedMessage } from 'libs/signing';
|
|
|
|
import './index.scss';
|
2018-03-01 17:53:29 +00:00
|
|
|
import { TextArea } from 'components/ui';
|
2017-10-30 19:10:25 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
showNotification: TShowNotification;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
signature: string;
|
|
|
|
verifiedAddress?: string;
|
|
|
|
verifiedMessage?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: State = {
|
|
|
|
signature: ''
|
|
|
|
};
|
|
|
|
|
2018-01-22 20:27:37 +00:00
|
|
|
const signatureExample: ISignedMessage = {
|
|
|
|
address: '0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8',
|
|
|
|
msg: 'asdfasdfasdf',
|
|
|
|
sig: '0x4771d78f13ba...',
|
|
|
|
version: '2'
|
|
|
|
};
|
|
|
|
const signaturePlaceholder = JSON.stringify(signatureExample, null, 2);
|
2017-10-30 19:10:25 +00:00
|
|
|
|
|
|
|
export class VerifyMessage extends Component<Props, State> {
|
|
|
|
public state: State = initialState;
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const { verifiedAddress, verifiedMessage, signature } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="Tab-content-pane">
|
2018-03-01 17:53:29 +00:00
|
|
|
<div className="input-group-wrapper ">
|
|
|
|
<label className="input-group">
|
2018-03-22 03:50:25 +00:00
|
|
|
<div className="input-group-header">{translate('MSG_SIGNATURE')}</div>
|
2018-03-01 17:53:29 +00:00
|
|
|
<TextArea
|
|
|
|
className={`VerifyMessage-inputBox ${signature ? 'is-valid' : 'is-invalid'}`}
|
|
|
|
placeholder={signaturePlaceholder}
|
|
|
|
value={signature}
|
|
|
|
onChange={this.handleSignatureChange}
|
|
|
|
onPaste={this.handleSignaturePaste}
|
|
|
|
/>
|
|
|
|
</label>
|
2017-10-30 19:10:25 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<button
|
|
|
|
className="VerifyMessage-sign btn btn-primary btn-lg"
|
|
|
|
onClick={this.handleVerifySignedMessage}
|
|
|
|
disabled={false}
|
|
|
|
>
|
2018-03-22 03:50:25 +00:00
|
|
|
{translate('MSG_VERIFY')}
|
2017-10-30 19:10:25 +00:00
|
|
|
</button>
|
|
|
|
|
|
|
|
{!!verifiedAddress &&
|
|
|
|
!!verifiedMessage && (
|
|
|
|
<div className="VerifyMessage-success alert alert-success">
|
2018-03-22 03:50:25 +00:00
|
|
|
<strong>{verifiedAddress}</strong> {translate('SIGNED')}
|
2017-10-30 19:10:25 +00:00
|
|
|
<strong>{verifiedMessage}</strong>.
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private clearVerifiedData = () =>
|
|
|
|
this.setState({
|
|
|
|
verifiedAddress: '',
|
|
|
|
verifiedMessage: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
private handleVerifySignedMessage = () => {
|
|
|
|
try {
|
|
|
|
const parsedSignature: ISignedMessage = JSON.parse(this.state.signature);
|
|
|
|
|
|
|
|
if (!verifySignedMessage(parsedSignature)) {
|
|
|
|
throw Error();
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:27:37 +00:00
|
|
|
const { address, msg } = parsedSignature;
|
2017-10-30 19:10:25 +00:00
|
|
|
this.setState({
|
|
|
|
verifiedAddress: address,
|
2018-01-22 20:27:37 +00:00
|
|
|
verifiedMessage: msg
|
2017-10-30 19:10:25 +00:00
|
|
|
});
|
|
|
|
this.props.showNotification('success', translate('SUCCESS_7'));
|
|
|
|
} catch (err) {
|
|
|
|
this.clearVerifiedData();
|
2018-04-26 00:36:29 +00:00
|
|
|
this.props.showNotification('danger', translate('ERROR_38'));
|
2017-10-30 19:10:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleSignatureChange = (e: React.FormEvent<HTMLTextAreaElement>) => {
|
|
|
|
const signature = e.currentTarget.value;
|
|
|
|
this.setState({ signature });
|
|
|
|
};
|
2018-02-01 22:35:26 +00:00
|
|
|
|
|
|
|
private handleSignaturePaste = (e: React.ClipboardEvent<HTMLTextAreaElement>) => {
|
|
|
|
const text = e.clipboardData.getData('Text');
|
|
|
|
if (text) {
|
|
|
|
try {
|
|
|
|
const signature = JSON.stringify(JSON.parse(text), null, 2);
|
|
|
|
this.setState({ signature });
|
|
|
|
e.preventDefault();
|
|
|
|
} catch (err) {
|
|
|
|
// Do nothing, it wasn't json they pasted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-10-30 19:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(null, {
|
|
|
|
showNotification
|
|
|
|
})(VerifyMessage);
|