diff --git a/lib/error.js b/lib/error.js new file mode 100644 index 00000000..de7ca437 --- /dev/null +++ b/lib/error.js @@ -0,0 +1,19 @@ +const E = function generateError (name) { + var CustomErr = function (msg) { + this.message = msg + this.name = name + Error.captureStackTrace(this, CustomErr) + } + CustomErr.prototype = Object.create(Error.prototype) + CustomErr.prototype.constructor = CustomErr + + return CustomErr +} + +module.exports = E + +function register (errorName) { + E[errorName] = E(errorName) +} + +register('BadNumberError') diff --git a/lib/plugins.js b/lib/plugins.js index 7da1e03e..f9b88546 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -4,6 +4,7 @@ var _ = require('lodash') var async = require('async') var BigNumber = require('bignumber.js') +BigNumber.config({CRYPTO: true}) var logger = require('./logger') var notifier = require('./notifier') @@ -715,3 +716,16 @@ exports.startCheckingNotification = function startCheckingNotification () { checkNotification() setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL) } + +exports.getPhoneCode = function getPhoneCode (phone) { + const code = BigNumber.random().toFixed(6).slice(2) + const rec = { + sms: { + toNumber: phone, + body: 'Your cryptomat code: ' + code + } + } + + return smsPlugin.sendMessage(rec) + .then(() => code) +} diff --git a/lib/routes.js b/lib/routes.js index ae21e6ae..c2f42474 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -213,15 +213,15 @@ function pair (req, res) { ) } -function raqia (req, res) { - var raqiaCreds - try { - var raqiaRec = require('../raqia.json') - raqiaCreds = raqiaRec[getFingerprint(req)].apiKeys[0] - } catch (ex) { - raqiaCreds = null - } - res.json(raqiaCreds || {}) +function phoneCode (req, res) { + var phone = req.body.phone + + return plugins.getPhoneCode(phone) + .then(code => res.json({code: code})) + .catch(err => { + if (err.name === 'BadNumberError') return res.send(410) + return res.send(500) + }) } function init (localConfig) { @@ -246,7 +246,8 @@ function init (localConfig) { app.post('/verify_user', authMiddleware, verifyUser) app.post('/verify_transaction', authMiddleware, verifyTx) app.post('/pair', pair) - app.get('/raqia', raqia) + + app.post('/phone_code', authMiddleware, phoneCode) localApp.get('/pid', function (req, res) { var machineFingerprint = req.query.fingerprint diff --git a/migrations/007-add-phone.js b/migrations/007-add-phone.js new file mode 100644 index 00000000..8cb3c13f --- /dev/null +++ b/migrations/007-add-phone.js @@ -0,0 +1,13 @@ +var db = require('./db') + +exports.up = function (next) { + var sql = [ + 'alter table transactions add phone text', + 'create index on transactions (phone)' + ] + db.multi(sql, next) +} + +exports.down = function (next) { + next() +}