sms verification: refactor checks & confirmation

This commit is contained in:
Jannis R 2016-11-10 13:37:32 +01:00
parent 89c1d9c25c
commit ac80276ad8
No known key found for this signature in database
GPG Key ID: 0FE83946296A88A5
6 changed files with 113 additions and 71 deletions

View File

@ -24,6 +24,8 @@ import phone from 'phoneformat.js';
import { fromWei } from '../../../api/util/wei'; import { fromWei } from '../../../api/util/wei';
import { Form, Input } from '../../../ui'; import { Form, Input } from '../../../ui';
import checkIfVerified from '../check-if-verified';
import checkIfRequested from '../check-if-requested';
import styles from './gatherData.css'; import styles from './gatherData.css';
@ -43,7 +45,7 @@ export default class GatherData extends Component {
state = { state = {
init: true, init: true,
isCertified: null, isVerified: null,
hasRequested: null, hasRequested: null,
numberIsValid: null, numberIsValid: null,
consentGiven: false consentGiven: false
@ -99,9 +101,9 @@ export default class GatherData extends Component {
} }
renderCertified () { renderCertified () {
const { isCertified } = this.props.data; const { isVerified } = this.props.data;
if (isCertified) { if (isVerified) {
return ( return (
<div className={ styles.container }> <div className={ styles.container }>
<ErrorIcon /> <ErrorIcon />
@ -109,7 +111,7 @@ export default class GatherData extends Component {
</div> </div>
); );
} }
if (isCertified === false) { if (isVerified === false) {
return ( return (
<div className={ styles.container }> <div className={ styles.container }>
<SuccessIcon /> <SuccessIcon />
@ -159,9 +161,9 @@ export default class GatherData extends Component {
checkIfCertified = () => { checkIfCertified = () => {
const { account, contract, onData } = this.props; const { account, contract, onData } = this.props;
contract.instance.certified.call({}, [account]) checkIfVerified(contract, account)
.then((isCertified) => { .then((isVerified) => {
onData({ isCertified }); onData({ isVerified });
this.onChange(); this.onChange();
}) })
.catch((err) => { .catch((err) => {
@ -172,18 +174,13 @@ export default class GatherData extends Component {
checkIfRequested = () => { checkIfRequested = () => {
const { account, contract, onData } = this.props; const { account, contract, onData } = this.props;
contract.subscribe('Requested', { checkIfRequested(contract, account)
fromBlock: 0, toBlock: 'pending', .then((hasRequested) => {
// limit: 1
}, (err, logs) => {
if (err) {
return console.error('error checking if requested', err);
}
const hasRequested = logs.some((l) => {
return l.type === 'mined' && l.params.who && l.params.who.value === account;
});
onData({ hasRequested }); onData({ hasRequested });
this.onChange(); this.onChange();
})
.catch((err) => {
console.error('error checking if requested', err);
}); });
} }
@ -206,10 +203,10 @@ export default class GatherData extends Component {
} }
onChange = () => { onChange = () => {
const { fee, isCertified, hasRequested } = this.props.data; const { fee, isVerified, hasRequested } = this.props.data;
const { numberIsValid, consentGiven } = this.state; const { numberIsValid, consentGiven } = this.state;
if (fee && numberIsValid && consentGiven && isCertified === false) { if (fee && numberIsValid && consentGiven && isVerified === false) {
this.props.onDataIsValid(); this.props.onDataIsValid();
} else { } else {
this.props.onDataIsInvalid(); this.props.onDataIsInvalid();

View File

@ -18,35 +18,10 @@ import React, { Component, PropTypes } from 'react';
import TxHash from '../../../ui/TxHash'; import TxHash from '../../../ui/TxHash';
import { sha3 } from '../../../api/util/sha3'; import { sha3 } from '../../../api/util/sha3';
import waitForConfirmations from '../wait-for-confirmations';
import styles from './sendConfirmation.css'; import styles from './sendConfirmation.css';
const isValidReceipt = (receipt) => {
return receipt && receipt.blockNumber && receipt.blockNumber.gt(0);
};
// TODO: DRY up with ../SendRequest
const waitForConfirmations = (api, tx, confirmations) => {
return new Promise((resolve, reject) => {
api.pollMethod('eth_getTransactionReceipt', tx, isValidReceipt)
.then((receipt) => {
let subscription;
api.subscribe('eth_blockNumber', (err, block) => {
if (err) {
reject(err);
} else if (block.minus(confirmations - 1).gte(receipt.blockNumber)) {
api.unsubscribe(subscription);
resolve();
}
})
.then((_subscription) => {
subscription = _subscription;
})
.catch(reject);
});
});
};
export default class SendConfirmation extends Component { export default class SendConfirmation extends Component {
static contextTypes = { static contextTypes = {
api: PropTypes.object.isRequired api: PropTypes.object.isRequired

View File

@ -18,35 +18,10 @@ import React, { Component, PropTypes } from 'react';
import qs from 'querystring'; import qs from 'querystring';
import TxHash from '../../../ui/TxHash'; import TxHash from '../../../ui/TxHash';
import waitForConfirmations from '../wait-for-confirmations';
import styles from './sendRequest.css'; import styles from './sendRequest.css';
const isValidReceipt = (receipt) => {
return receipt && receipt.blockNumber && receipt.blockNumber.gt(0);
};
// TODO: DRY up with ../SendConfirmation
const waitForConfirmations = (api, tx, confirmations) => {
return new Promise((resolve, reject) => {
api.pollMethod('eth_getTransactionReceipt', tx, isValidReceipt)
.then((receipt) => {
let subscription;
api.subscribe('eth_blockNumber', (err, block) => {
if (err) {
reject(err);
} else if (block.minus(confirmations - 1).gte(receipt.blockNumber)) {
api.unsubscribe(subscription);
resolve();
}
})
.then((_subscription) => {
subscription = _subscription;
})
.catch(reject);
});
});
};
const postToVerificationServer = (query) => { const postToVerificationServer = (query) => {
query = qs.stringify(query); query = qs.stringify(query);
return fetch('https://sms-verification.parity.io/?' + query, { return fetch('https://sms-verification.parity.io/?' + query, {

View File

@ -0,0 +1,32 @@
// 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/>.
const checkIfRequested = (contract, account) => {
return new Promise((resolve, reject) => {
contract.subscribe('Requested', {
fromBlock: 0, toBlock: 'pending'
}, (err, logs) => {
if (err) {
return reject(err);
}
resolve(logs.some((l) => {
return l.type === 'mined' && l.params.who && l.params.who.value === account;
}));
});
});
};
export default checkIfRequested;

View File

@ -0,0 +1,21 @@
// 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/>.
const checkIfVerified = (contract, account) => {
return contract.instance.certified.call({}, [account]);
};
export default checkIfVerified;

View File

@ -0,0 +1,42 @@
// 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/>.
const isValidReceipt = (receipt) => {
return receipt && receipt.blockNumber && receipt.blockNumber.gt(0);
};
const waitForConfirmations = (api, tx, confirmations) => {
return new Promise((resolve, reject) => {
api.pollMethod('eth_getTransactionReceipt', tx, isValidReceipt)
.then((receipt) => {
let subscription;
api.subscribe('eth_blockNumber', (err, block) => {
if (err) {
reject(err);
} else if (block.minus(confirmations - 1).gte(receipt.blockNumber)) {
api.unsubscribe(subscription);
resolve();
}
})
.then((_subscription) => {
subscription = _subscription;
})
.catch(reject);
});
});
};
export default waitForConfirmations;