2016-11-15 12:43:50 +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 { observable, computed, autorun, action } from 'mobx';
|
|
|
|
import phone from 'phoneformat.js';
|
2016-12-05 11:47:13 +01:00
|
|
|
import { sha3 } from '~/api/util/sha3';
|
2016-11-15 12:43:50 +01:00
|
|
|
|
2016-12-05 11:47:13 +01:00
|
|
|
import Contracts from '~/contracts';
|
2016-11-15 12:43:50 +01:00
|
|
|
|
2016-12-05 11:47:13 +01:00
|
|
|
import { checkIfVerified, checkIfRequested, awaitPuzzle } from '~/contracts/sms-verification';
|
2016-11-21 20:45:47 +01:00
|
|
|
import { postToServer } from '../../3rdparty/sms-verification';
|
2016-11-17 12:57:30 +01:00
|
|
|
import checkIfTxFailed from '../../util/check-if-tx-failed';
|
|
|
|
import waitForConfirmations from '../../util/wait-for-block-confirmations';
|
2016-11-15 12:43:50 +01:00
|
|
|
|
2016-11-17 14:21:05 +01:00
|
|
|
export const LOADING = 'fetching-contract';
|
|
|
|
export const QUERY_DATA = 'query-data';
|
2016-11-15 16:59:10 +01:00
|
|
|
export const POSTING_REQUEST = 'posting-request';
|
|
|
|
export const POSTED_REQUEST = 'posted-request';
|
|
|
|
export const REQUESTING_SMS = 'requesting-sms';
|
|
|
|
export const QUERY_CODE = 'query-code';
|
|
|
|
export const POSTING_CONFIRMATION = 'posting-confirmation';
|
|
|
|
export const POSTED_CONFIRMATION = 'posted-confirmation';
|
|
|
|
export const DONE = 'done';
|
2016-11-15 12:43:50 +01:00
|
|
|
|
2016-11-15 16:59:10 +01:00
|
|
|
export default class VerificationStore {
|
2016-11-15 12:43:50 +01:00
|
|
|
@observable step = null;
|
|
|
|
@observable error = null;
|
|
|
|
|
2016-11-17 14:21:05 +01:00
|
|
|
@observable contract = null;
|
2016-11-15 12:43:50 +01:00
|
|
|
@observable fee = null;
|
|
|
|
@observable isVerified = null;
|
|
|
|
@observable hasRequested = null;
|
|
|
|
@observable consentGiven = false;
|
|
|
|
@observable number = '';
|
|
|
|
@observable requestTx = null;
|
|
|
|
@observable code = '';
|
2016-11-28 17:39:55 +01:00
|
|
|
@observable isCodeValid = null;
|
2016-11-15 12:43:50 +01:00
|
|
|
@observable confirmationTx = null;
|
|
|
|
|
|
|
|
@computed get isNumberValid () {
|
|
|
|
return phone.isValidNumber(this.number);
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:21:53 +01:00
|
|
|
@computed get isStepValid () {
|
2016-11-15 16:59:10 +01:00
|
|
|
if (this.step === DONE) {
|
2016-11-15 16:21:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-17 12:26:59 +01:00
|
|
|
switch (this.step) {
|
2016-11-17 14:21:05 +01:00
|
|
|
case LOADING:
|
|
|
|
return this.contract && this.fee && this.isVerified !== null && this.hasRequested !== null;
|
|
|
|
case QUERY_DATA:
|
|
|
|
return this.isNumberValid && this.consentGiven;
|
2016-11-17 12:26:59 +01:00
|
|
|
case QUERY_CODE:
|
2016-11-28 17:39:55 +01:00
|
|
|
return this.requestTx && this.isCodeValid === true;
|
2016-11-17 12:26:59 +01:00
|
|
|
case POSTED_CONFIRMATION:
|
2016-11-28 17:39:55 +01:00
|
|
|
return !!this.confirmationTx;
|
2016-11-17 12:26:59 +01:00
|
|
|
default:
|
|
|
|
return false;
|
2016-11-15 16:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 17:39:55 +01:00
|
|
|
constructor (api, account, isTestnet) {
|
2016-11-15 12:43:50 +01:00
|
|
|
this.api = api;
|
|
|
|
this.account = account;
|
2016-11-28 17:39:55 +01:00
|
|
|
this.isTestnet = isTestnet;
|
2016-11-17 14:21:05 +01:00
|
|
|
|
|
|
|
this.step = LOADING;
|
2016-11-21 20:45:47 +01:00
|
|
|
Contracts.create(api).registry.getContract('smsverification')
|
2016-11-17 14:21:05 +01:00
|
|
|
.then((contract) => {
|
|
|
|
this.contract = contract;
|
|
|
|
this.load();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Failed to fetch the contract: ' + err.message;
|
|
|
|
});
|
2016-11-15 12:43:50 +01:00
|
|
|
|
|
|
|
autorun(() => {
|
|
|
|
if (this.error) {
|
|
|
|
console.error('sms verification: ' + this.error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-17 14:21:05 +01:00
|
|
|
@action load = () => {
|
2016-11-15 12:43:50 +01:00
|
|
|
const { contract, account } = this;
|
2016-11-17 14:21:05 +01:00
|
|
|
this.step = LOADING;
|
2016-11-15 12:43:50 +01:00
|
|
|
|
|
|
|
const fee = contract.instance.fee.call()
|
|
|
|
.then((fee) => {
|
|
|
|
this.fee = fee;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Failed to fetch the fee: ' + err.message;
|
|
|
|
});
|
|
|
|
|
|
|
|
const isVerified = checkIfVerified(contract, account)
|
|
|
|
.then((isVerified) => {
|
|
|
|
this.isVerified = isVerified;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Failed to check if verified: ' + err.message;
|
|
|
|
});
|
|
|
|
|
|
|
|
const hasRequested = checkIfRequested(contract, account)
|
2016-11-15 19:05:16 +01:00
|
|
|
.then((txHash) => {
|
|
|
|
this.hasRequested = !!txHash;
|
|
|
|
if (txHash) {
|
|
|
|
this.requestTx = txHash;
|
|
|
|
}
|
2016-11-15 12:43:50 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Failed to check if requested: ' + err.message;
|
|
|
|
});
|
|
|
|
|
2016-11-17 12:26:59 +01:00
|
|
|
Promise
|
|
|
|
.all([ fee, isVerified, hasRequested ])
|
|
|
|
.then(() => {
|
2016-11-17 14:21:05 +01:00
|
|
|
this.step = QUERY_DATA;
|
2016-11-17 12:26:59 +01:00
|
|
|
});
|
2016-11-15 12:43:50 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 14:21:05 +01:00
|
|
|
@action setNumber = (number) => {
|
|
|
|
this.number = number;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setConsentGiven = (consentGiven) => {
|
|
|
|
this.consentGiven = consentGiven;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setCode = (code) => {
|
2016-11-28 17:39:55 +01:00
|
|
|
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) ];
|
|
|
|
|
2016-11-17 14:21:05 +01:00
|
|
|
this.code = code;
|
2016-11-28 17:39:55 +01:00
|
|
|
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;
|
|
|
|
});
|
2016-11-17 14:21:05 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 12:43:50 +01:00
|
|
|
@action sendRequest = () => {
|
|
|
|
const { api, account, contract, fee, number, hasRequested } = this;
|
|
|
|
|
|
|
|
const request = contract.functions.find((fn) => fn.name === 'request');
|
|
|
|
const options = { from: account, value: fee.toString() };
|
|
|
|
|
|
|
|
let chain = Promise.resolve();
|
|
|
|
if (!hasRequested) {
|
2016-11-15 16:59:10 +01:00
|
|
|
this.step = POSTING_REQUEST;
|
2016-11-15 12:43:50 +01:00
|
|
|
chain = request.estimateGas(options, [])
|
|
|
|
.then((gas) => {
|
|
|
|
options.gas = gas.mul(1.2).toFixed(0);
|
|
|
|
return request.postTransaction(options, []);
|
|
|
|
})
|
|
|
|
.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) => {
|
|
|
|
this.requestTx = txHash;
|
2016-11-16 13:28:51 +01:00
|
|
|
return checkIfTxFailed(api, txHash, options.gas)
|
2016-11-17 12:26:59 +01:00
|
|
|
.then((hasFailed) => {
|
|
|
|
if (hasFailed) {
|
|
|
|
throw new Error('Transaction failed, all gas used up.');
|
|
|
|
}
|
|
|
|
this.step = POSTED_REQUEST;
|
|
|
|
return waitForConfirmations(api, txHash, 1);
|
|
|
|
});
|
2016-11-15 12:43:50 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
chain
|
|
|
|
.then(() => {
|
2016-11-28 17:39:55 +01:00
|
|
|
return api.parity.netChain();
|
|
|
|
})
|
|
|
|
.then((chain) => {
|
2016-11-15 16:59:10 +01:00
|
|
|
this.step = REQUESTING_SMS;
|
2016-11-28 17:39:55 +01:00
|
|
|
return postToServer({ number, address: account }, this.isTestnet);
|
2016-11-15 12:43:50 +01:00
|
|
|
})
|
2016-11-28 17:39:55 +01:00
|
|
|
.then(() => awaitPuzzle(api, contract, account))
|
2016-11-15 12:43:50 +01:00
|
|
|
.then(() => {
|
2016-11-28 17:39:55 +01:00
|
|
|
this.step = QUERY_CODE;
|
2016-11-15 12:43:50 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Failed to request a confirmation SMS: ' + err.message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:21:53 +01:00
|
|
|
@action queryCode = () => {
|
2016-11-15 16:59:10 +01:00
|
|
|
this.step = QUERY_CODE;
|
2016-11-15 12:43:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action sendConfirmation = () => {
|
|
|
|
const { api, account, contract, code } = this;
|
|
|
|
const token = sha3(code);
|
|
|
|
|
|
|
|
const confirm = contract.functions.find((fn) => fn.name === 'confirm');
|
|
|
|
const options = { from: account };
|
|
|
|
const values = [ token ];
|
|
|
|
|
2016-11-15 16:59:10 +01:00
|
|
|
this.step = POSTING_CONFIRMATION;
|
2016-11-15 12:43:50 +01:00
|
|
|
confirm.estimateGas(options, values)
|
|
|
|
.then((gas) => {
|
|
|
|
options.gas = gas.mul(1.2).toFixed(0);
|
|
|
|
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) => {
|
|
|
|
this.confirmationTx = txHash;
|
2016-11-16 13:28:51 +01:00
|
|
|
return checkIfTxFailed(api, txHash, options.gas)
|
2016-11-17 12:26:59 +01:00
|
|
|
.then((hasFailed) => {
|
|
|
|
if (hasFailed) {
|
|
|
|
throw new Error('Transaction failed, all gas used up.');
|
|
|
|
}
|
|
|
|
this.step = POSTED_CONFIRMATION;
|
|
|
|
return waitForConfirmations(api, txHash, 1);
|
|
|
|
});
|
2016-11-15 12:43:50 +01:00
|
|
|
})
|
|
|
|
.then(() => {
|
2016-11-15 16:59:10 +01:00
|
|
|
this.step = DONE;
|
2016-11-15 12:43:50 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.error = 'Failed to send the verification code: ' + err.message;
|
|
|
|
});
|
|
|
|
}
|
2016-11-15 16:21:53 +01:00
|
|
|
|
|
|
|
@action done = () => {
|
2016-11-15 16:59:10 +01:00
|
|
|
this.step = DONE;
|
2016-11-15 16:21:53 +01:00
|
|
|
}
|
2016-11-15 12:43:50 +01:00
|
|
|
}
|