openethereum/js/src/modals/SMSVerification/SendConfirmation/sendConfirmation.js

118 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-11-09 17:34:21 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import React, { Component, PropTypes } from 'react';
import TxHash from '../../../ui/TxHash';
import { sha3 } from '../../../api/util/sha3';
import waitForConfirmations from '../wait-for-confirmations';
2016-11-09 17:34:21 +01:00
import styles from './sendConfirmation.css';
export default class SendConfirmation extends Component {
static contextTypes = {
api: PropTypes.object.isRequired
}
static propTypes = {
account: PropTypes.string.isRequired,
contract: PropTypes.object.isRequired,
data: PropTypes.object.isRequired,
onData: PropTypes.func.isRequired,
onSuccess: PropTypes.func.isRequired,
onError: PropTypes.func.isRequired,
nextStep: PropTypes.func.isRequired
2016-11-09 17:34:21 +01:00
}
state = {
init: true,
step: 'init'
};
componentWillMount () {
const { init } = this.state;
if (init) {
this.send();
this.setState({ init: false });
}
}
render () {
const { step } = this.state;
if (step === 'error') {
return (<p>{ this.state.error }</p>);
}
if (step === 'pending') {
return (<p>The verification code will be sent to the contract. Please authorize this using the Parity Signer.</p>);
2016-11-09 17:34:21 +01:00
}
if (step === 'posted') {
return (
<div className={ styles.centered }>
<TxHash hash={ this.props.data.txHash } maxConfirmations={ 2 } />
2016-11-09 17:34:21 +01:00
<p>Please keep this window open.</p>
</div>);
}
return null;
}
send = () => {
const { api } = this.context;
const { account, contract, onData, onError, onSuccess, nextStep } = this.props;
2016-11-09 17:34:21 +01:00
const { code } = this.props.data;
const token = sha3(code);
const confirm = contract.functions.find((fn) => fn.name === 'confirm');
const options = { from: account };
const values = [ token ];
confirm.estimateGas(options, values)
.then((gas) => {
options.gas = gas.mul(1.2).toFixed(0);
// TODO: show message
this.setState({ step: 'pending' });
return confirm.postTransaction(options, values);
})
.then((handle) => {
// TODO: The "request rejected" error doesn't have any property to
// distinguish it from other errors, so we can't give a meaningful error here.
return api.pollMethod('parity_checkRequest', handle);
})
.then((txHash) => {
onData({ txHash: txHash });
this.setState({ step: 'posted' });
return waitForConfirmations(api, txHash, 2);
2016-11-09 17:34:21 +01:00
})
.then(() => {
onSuccess();
nextStep();
2016-11-09 17:34:21 +01:00
})
.catch((err) => {
console.error('failed to confirm sms verification', err);
onError(err);
this.setState({
step: 'error',
error: 'Failed to send the verification code: ' + err.message
});
// TODO: show message in SnackBar
});
}
}