Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

update SMS verification #3579

Merged
merged 10 commits into from
Nov 28, 2016
1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"es6-error": "^4.0.0",
"es6-promise": "^3.2.1",
"ethereumjs-tx": "^1.1.2",
"eventemitter3": "^2.0.2",
"file-saver": "^1.3.3",
"format-json": "^1.0.3",
"format-number": "^2.0.1",
Expand Down
5 changes: 3 additions & 2 deletions js/src/3rdparty/sms-verification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ export const termsOfService = (
</ul>
);

export const postToServer = (query) => {
export const postToServer = (query, isTestnet = false) => {
const port = isTestnet ? 8443 : 443;
query = stringify(query);
return fetch('https://sms-verification.parity.io/?' + query, {
return fetch(`https://sms-verification.parity.io:${port}/?` + query, {
method: 'POST', mode: 'cors', cache: 'no-store'
})
.then((res) => {
Expand Down
2 changes: 1 addition & 1 deletion js/src/contracts/abi/sms-verification.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"}],"name":"certify","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"request","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"},{"name":"_puzzle","type":"bytes32"}],"name":"puzzle","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"},{"name":"_field","type":"string"}],"name":"getAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"uint256"}],"name":"setFee","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"}],"name":"revoke","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_code","type":"bytes32"}],"name":"confirm","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"delegate","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"},{"name":"_field","type":"string"}],"name":"getUint","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setDelegate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"certified","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"},{"name":"_field","type":"string"}],"name":"get","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"}],"name":"Requested","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"},{"indexed":false,"name":"puzzle","type":"bytes32"}],"name":"Puzzled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"}],"name":"Confirmed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"}],"name":"Revoked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}]
[{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"}],"name":"certify","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"request","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"},{"name":"_puzzle","type":"bytes32"}],"name":"puzzle","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"},{"name":"_field","type":"string"}],"name":"getAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"uint256"}],"name":"setFee","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"}],"name":"revoke","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_code","type":"bytes32"}],"name":"confirm","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"delegate","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"},{"name":"_field","type":"string"}],"name":"getUint","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setDelegate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"certified","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"},{"name":"_field","type":"string"}],"name":"get","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"}],"name":"Requested","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"},{"indexed":false,"name":"puzzle","type":"bytes32"}],"name":"Puzzled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"}],"name":"Confirmed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"}],"name":"Revoked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}]
35 changes: 35 additions & 0 deletions js/src/contracts/sms-verification.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import subscribeToEvent from '../util/subscribe-to-event';

export const checkIfVerified = (contract, account) => {
return contract.instance.certified.call({}, [account]);
};
Expand Down Expand Up @@ -50,3 +52,36 @@ export const checkIfRequested = (contract, account) => {
});
});
};

const blockNumber = (api) => {
return new Promise((resolve, reject) => {
api.subscribe('eth_blockNumber', (err, block) => {
if (err) {
return reject(err);
}
resolve(block);
})
.then((subscription) => {
api.unsubscribe(subscription);
})
.catch(reject);
});
};

export const awaitPuzzle = (api, contract, account) => {
return blockNumber(api)
.then((block) => {
return new Promise((resolve, reject) => {
const subscription = subscribeToEvent(contract, 'Puzzled', {
from: block.toNumber(),
filter: (log) => log.params.who.value === account
});
subscription.once('error', reject);
subscription.once('log', subscription.unsubscribe);
subscription.once('log', resolve);
subscription.once('timeout', () => {
reject(new Error('Timed out waiting for the puzzle.'));
});
});
});
};
2 changes: 1 addition & 1 deletion js/src/modals/SMSVerification/GatherData/gatherData.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class GatherData extends Component {
{ this.renderCertified() }
{ this.renderRequested() }
<Input
label={ 'phone number' }
label={ 'phone number in international format' }
hint={ 'the SMS will be sent to this number' }
error={ isNumberValid ? null : 'invalid number' }
disabled={ isVerified }
Expand Down
4 changes: 2 additions & 2 deletions js/src/modals/SMSVerification/SMSVerification.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
LOADING,
QUERY_DATA,
POSTING_REQUEST, POSTED_REQUEST,
REQUESTING_SMS, REQUESTED_SMS,
REQUESTING_SMS, QUERY_CODE,
POSTING_CONFIRMATION, POSTED_CONFIRMATION,
DONE
} from './store';
Expand All @@ -48,7 +48,7 @@ export default class SMSVerification extends Component {
[LOADING]: 0,
[QUERY_DATA]: 1,
[POSTING_REQUEST]: 2, [POSTED_REQUEST]: 2, [REQUESTING_SMS]: 2,
[REQUESTED_SMS]: 3,
[QUERY_CODE]: 3,
[POSTING_CONFIRMATION]: 4, [POSTED_CONFIRMATION]: 4,
[DONE]: 5
}
Expand Down
2 changes: 1 addition & 1 deletion js/src/modals/SMSVerification/SendRequest/sendRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class SendRequest extends Component {

case REQUESTING_SMS:
return (
<p>Requesting an SMS from the Parity server.</p>
<p>Requesting an SMS from the Parity server and waiting for the puzzle to be put into the contract.</p>
);

default:
Expand Down
45 changes: 32 additions & 13 deletions js/src/modals/SMSVerification/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ import { sha3 } from '../../api/util/sha3';

import Contracts from '../../contracts';

import { checkIfVerified, checkIfRequested } from '../../contracts/sms-verification';
import { checkIfVerified, checkIfRequested, awaitPuzzle } from '../../contracts/sms-verification';
import { postToServer } from '../../3rdparty/sms-verification';
import checkIfTxFailed from '../../util/check-if-tx-failed';
import waitForConfirmations from '../../util/wait-for-block-confirmations';

const validCode = /^[A-Z\s]+$/i;
import isTestnet from '../../util/is-testnet';

export const LOADING = 'fetching-contract';
export const QUERY_DATA = 'query-data';
export const POSTING_REQUEST = 'posting-request';
export const POSTED_REQUEST = 'posted-request';
export const REQUESTING_SMS = 'requesting-sms';
export const REQUESTED_SMS = 'requested-sms';
export const QUERY_CODE = 'query-code';
export const POSTING_CONFIRMATION = 'posting-confirmation';
export const POSTED_CONFIRMATION = 'posted-confirmation';
Expand All @@ -50,11 +48,9 @@ export default class VerificationStore {
@observable number = '';
@observable requestTx = null;
@observable code = '';
@observable isCodeValid = null;
@observable confirmationTx = null;

@computed get isCodeValid () {
return validCode.test(this.code);
}
@computed get isNumberValid () {
return phone.isValidNumber(this.number);
}
Expand All @@ -72,12 +68,10 @@ export default class VerificationStore {
return this.contract && this.fee && this.isVerified !== null && this.hasRequested !== null;
case QUERY_DATA:
return this.isNumberValid && this.consentGiven;
case REQUESTED_SMS:
return this.requestTx;
case QUERY_CODE:
return this.isCodeValid;
return this.requestTx && this.isCodeValid === true;
case POSTED_CONFIRMATION:
return this.confirmationTx;
return !!this.confirmationTx;
default:
return false;
}
Expand Down Expand Up @@ -151,7 +145,26 @@ export default class VerificationStore {
}

@action setCode = (code) => {
const { contract, account } = this;
if (!contract || !account || code.length === 0) return;

const confirm = contract.functions.find((fn) => fn.name === 'confirm');
const options = { from: account };
const values = [ sha3(code) ];

this.code = code;
this.isCodeValid = null;
confirm.estimateGas(options, values)
.then((gas) => {
options.gas = gas.mul(1.2).toFixed(0);
return confirm.call(options, values);
})
.then((result) => {
this.isCodeValid = result === true;
})
.catch((err) => {
this.error = 'Failed to check if the code is valid: ' + err.message;
});
}

@action sendRequest = () => {
Expand Down Expand Up @@ -188,11 +201,17 @@ export default class VerificationStore {

chain
.then(() => {
return api.parity.netChain();
})
.then((chain) => {
const isTest = isTestnet(chain);

this.step = REQUESTING_SMS;
return postToServer({ number, address: account });
return postToServer({ number, address: account }, isTest);
})
.then(() => awaitPuzzle(api, contract, account))
.then(() => {
this.step = REQUESTED_SMS;
this.step = QUERY_CODE;
})
.catch((err) => {
this.error = 'Failed to request a confirmation SMS: ' + err.message;
Expand Down
3 changes: 2 additions & 1 deletion js/src/redux/providers/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { statusBlockNumber, statusCollection, statusLogs } from './statusActions';
import isTestnet from '../../util/is-testnet';
import { isEqual } from 'lodash';

export default class Status {
Expand Down Expand Up @@ -247,7 +248,7 @@ export default class Status {
.then(([
clientVersion, defaultExtraData, netChain, netPort, rpcSettings, enode
]) => {
const isTest = netChain === 'morden' || netChain === 'ropsten' || netChain === 'testnet';
const isTest = isTestnet(netChain);

const longStatus = {
clientVersion,
Expand Down
19 changes: 19 additions & 0 deletions js/src/util/is-testnet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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/>.

export default (chain) => {
return chain === 'morden' || chain === 'ropsten' || chain === 'testnet';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? Redux has isTest, don't want to maintain it in yet another place, we just simplified and got it down to 1. (And had to make changes.)

In addition, we are really starting to clutter /utils now with all these single function files. There is no reason why things can't be grouped, e.g. had to move nullable to protypes once we added another one. (General grumble)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as of 88cb1df.

};
77 changes: 77 additions & 0 deletions js/src/util/subscribe-to-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 EventEmitter from 'eventemitter3';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would have really preferred a general solution that (a) doesn't add another way of doing things, (b) is available to everybody. But suppose it solves the immediate issue, so once we have a general solution in-place will have to come back.


const defaults = {
from: 0, // TODO
to: 'latest',
timeout: null,
filter: () => true
};

const subscribeToEvent = (contract, name, opt = {}) => {
opt = Object.assign({}, defaults, opt);

let subscription = null;
let timeout = null;

const unsubscribe = () => {
if (subscription) {
contract.unsubscribe(subscription);
subscription = null;
}
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};

const emitter = new EventEmitter();
emitter.unsubscribe = unsubscribe;

if (typeof opt.timeout === 'number') {
timeout = setTimeout(() => {
unsubscribe();
emitter.emit('timeout');
}, opt.timeout);
}

const callback = (err, logs) => {
if (err) {
return emitter.emit('error', err);
}
for (let log of logs) {
if (opt.filter(log)) {
emitter.emit('log', log);
}
}
};

contract.subscribe(name, {
fromBlock: opt.from, toBlock: opt.to
}, callback)
.then((_subscription) => {
subscription = _subscription;
})
.catch((err) => {
emitter.emit('error', err);
});

return emitter;
};

export default subscribeToEvent;