From ac80276ad84e7649ce67e5aa8e201adf30f7c5c6 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 10 Nov 2016 13:37:32 +0100 Subject: [PATCH] sms verification: refactor checks & confirmation --- .../SMSVerification/GatherData/gatherData.js | 35 +++++++--------- .../SendConfirmation/sendConfirmation.js | 27 +----------- .../SendRequest/sendRequest.js | 27 +----------- .../SMSVerification/check-if-requested.js | 32 ++++++++++++++ .../SMSVerification/check-if-verified.js | 21 ++++++++++ .../SMSVerification/wait-for-confirmations.js | 42 +++++++++++++++++++ 6 files changed, 113 insertions(+), 71 deletions(-) create mode 100644 js/src/modals/SMSVerification/check-if-requested.js create mode 100644 js/src/modals/SMSVerification/check-if-verified.js create mode 100644 js/src/modals/SMSVerification/wait-for-confirmations.js diff --git a/js/src/modals/SMSVerification/GatherData/gatherData.js b/js/src/modals/SMSVerification/GatherData/gatherData.js index 9eb6e6883..9b55fbfe5 100644 --- a/js/src/modals/SMSVerification/GatherData/gatherData.js +++ b/js/src/modals/SMSVerification/GatherData/gatherData.js @@ -24,6 +24,8 @@ import phone from 'phoneformat.js'; import { fromWei } from '../../../api/util/wei'; import { Form, Input } from '../../../ui'; +import checkIfVerified from '../check-if-verified'; +import checkIfRequested from '../check-if-requested'; import styles from './gatherData.css'; @@ -43,7 +45,7 @@ export default class GatherData extends Component { state = { init: true, - isCertified: null, + isVerified: null, hasRequested: null, numberIsValid: null, consentGiven: false @@ -99,9 +101,9 @@ export default class GatherData extends Component { } renderCertified () { - const { isCertified } = this.props.data; + const { isVerified } = this.props.data; - if (isCertified) { + if (isVerified) { return (
@@ -109,7 +111,7 @@ export default class GatherData extends Component {
); } - if (isCertified === false) { + if (isVerified === false) { return (
@@ -159,9 +161,9 @@ export default class GatherData extends Component { checkIfCertified = () => { const { account, contract, onData } = this.props; - contract.instance.certified.call({}, [account]) - .then((isCertified) => { - onData({ isCertified }); + checkIfVerified(contract, account) + .then((isVerified) => { + onData({ isVerified }); this.onChange(); }) .catch((err) => { @@ -172,18 +174,13 @@ export default class GatherData extends Component { checkIfRequested = () => { const { account, contract, onData } = this.props; - contract.subscribe('Requested', { - fromBlock: 0, toBlock: 'pending', - // 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; - }); + checkIfRequested(contract, account) + .then((hasRequested) => { onData({ hasRequested }); this.onChange(); + }) + .catch((err) => { + console.error('error checking if requested', err); }); } @@ -206,10 +203,10 @@ export default class GatherData extends Component { } onChange = () => { - const { fee, isCertified, hasRequested } = this.props.data; + const { fee, isVerified, hasRequested } = this.props.data; const { numberIsValid, consentGiven } = this.state; - if (fee && numberIsValid && consentGiven && isCertified === false) { + if (fee && numberIsValid && consentGiven && isVerified === false) { this.props.onDataIsValid(); } else { this.props.onDataIsInvalid(); diff --git a/js/src/modals/SMSVerification/SendConfirmation/sendConfirmation.js b/js/src/modals/SMSVerification/SendConfirmation/sendConfirmation.js index da0380965..7e7880929 100644 --- a/js/src/modals/SMSVerification/SendConfirmation/sendConfirmation.js +++ b/js/src/modals/SMSVerification/SendConfirmation/sendConfirmation.js @@ -18,35 +18,10 @@ import React, { Component, PropTypes } from 'react'; import TxHash from '../../../ui/TxHash'; import { sha3 } from '../../../api/util/sha3'; +import waitForConfirmations from '../wait-for-confirmations'; 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 { static contextTypes = { api: PropTypes.object.isRequired diff --git a/js/src/modals/SMSVerification/SendRequest/sendRequest.js b/js/src/modals/SMSVerification/SendRequest/sendRequest.js index e43464949..0594ebbf1 100644 --- a/js/src/modals/SMSVerification/SendRequest/sendRequest.js +++ b/js/src/modals/SMSVerification/SendRequest/sendRequest.js @@ -18,35 +18,10 @@ import React, { Component, PropTypes } from 'react'; import qs from 'querystring'; import TxHash from '../../../ui/TxHash'; +import waitForConfirmations from '../wait-for-confirmations'; 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) => { query = qs.stringify(query); return fetch('https://sms-verification.parity.io/?' + query, { diff --git a/js/src/modals/SMSVerification/check-if-requested.js b/js/src/modals/SMSVerification/check-if-requested.js new file mode 100644 index 000000000..8f1d8cc57 --- /dev/null +++ b/js/src/modals/SMSVerification/check-if-requested.js @@ -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 . + +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; diff --git a/js/src/modals/SMSVerification/check-if-verified.js b/js/src/modals/SMSVerification/check-if-verified.js new file mode 100644 index 000000000..f45b0caba --- /dev/null +++ b/js/src/modals/SMSVerification/check-if-verified.js @@ -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 . + +const checkIfVerified = (contract, account) => { + return contract.instance.certified.call({}, [account]); +}; + +export default checkIfVerified; diff --git a/js/src/modals/SMSVerification/wait-for-confirmations.js b/js/src/modals/SMSVerification/wait-for-confirmations.js new file mode 100644 index 000000000..e99d3d173 --- /dev/null +++ b/js/src/modals/SMSVerification/wait-for-confirmations.js @@ -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 . + +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;