sms verification: refactor checks & confirmation
This commit is contained in:
parent
89c1d9c25c
commit
ac80276ad8
@ -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 (
|
||||
<div className={ styles.container }>
|
||||
<ErrorIcon />
|
||||
@ -109,7 +111,7 @@ export default class GatherData extends Component {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (isCertified === false) {
|
||||
if (isVerified === false) {
|
||||
return (
|
||||
<div className={ styles.container }>
|
||||
<SuccessIcon />
|
||||
@ -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();
|
||||
|
@ -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
|
||||
|
@ -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, {
|
||||
|
32
js/src/modals/SMSVerification/check-if-requested.js
Normal file
32
js/src/modals/SMSVerification/check-if-requested.js
Normal 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;
|
21
js/src/modals/SMSVerification/check-if-verified.js
Normal file
21
js/src/modals/SMSVerification/check-if-verified.js
Normal 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;
|
42
js/src/modals/SMSVerification/wait-for-confirmations.js
Normal file
42
js/src/modals/SMSVerification/wait-for-confirmations.js
Normal 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;
|
Loading…
Reference in New Issue
Block a user