This repository has been archived by the owner on Nov 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
verify.js
73 lines (62 loc) · 1.99 KB
/
verify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
const co = require('co-express')
const boom = require('boom')
const sha3 = require('web3/lib/utils/sha3')
const web3 = require('./lib/web3')
const normalizeNumber = require('./lib/normalize-number')
const storage = require('./lib/storage')
const generateCode = require('./lib/generate-code')
const postToContract = require('./lib/post-to-contract')
const sendSMS = require('./lib/send-sms')
function internal (msg, err = null) {
console.info(msg, err)
return boom.internal(msg)
}
module.exports = co(function* (req, res) {
let number = req.query.number
try {
number = yield normalizeNumber(number)
} catch (err) {
console.error(err)
throw boom.badRequest('Phone number is invalid.')
}
const address = req.query.address && req.query.address.toLowerCase()
if (!web3.isAddress(address)) throw boom.badRequest('Address is invalid.')
let code
try {
code = yield generateCode()
} catch (err) {
throw internal('An error occured while generating a code.', err)
}
const anonymized = '0x' + sha3(number)
try {
if (yield storage.has(anonymized)) {
throw boom.badRequest('This number has already been verified.')
}
} catch (err) {
if (err.isBoom) throw err
throw internal('An error occured while querying the database.', err)
}
try {
yield sendSMS(number, code)
console.info(`Verification code sent to ${anonymized}.`)
} catch (err) {
throw internal('An error occured while sending the SMS.', err)
}
try {
const txHash = yield postToContract(address, code)
console.info(`Challenge sent to contract (tx ${txHash}).`)
} catch (err) {
throw internal('An error occured while querying Parity.', err)
}
try {
yield storage.put(anonymized, code)
console.info(`Hash of phone number (${anonymized}) put into DB.`)
} catch (err) {
throw internal('An error occured while querying the database.', err)
}
return res.status(202).json({
status: 'ok',
message: `Verification code sent to ${number}.`
})
})