-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
152 lines (132 loc) · 3.26 KB
/
server.js
File metadata and controls
152 lines (132 loc) · 3.26 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
const Hapi = require('hapi');
const Inert = require('inert');
const Path = require('path');
var Web3 = require('web3');
var locus = require('locus');
var fs = require('fs');
var config = require('config');
var nodehost = config.get('nodehost');
var rocport = config.get('rocport');
var apphost = config.get('apphost');
var appport = config.get('appport');
var aadhar_data = JSON.parse(fs.readFileSync('mocked_aadhar_api.json', 'utf8'));
const server = new Hapi.Server({
connections: {
routes: {
files: {
relativeTo: Path.join(__dirname, 'uploads')
},
cors: {
origin: ['*']
}
}
}
});
var ethRpcNode = "http://"+nodehost+":"+rocport;
var web3 = new Web3(new Web3.providers.HttpProvider(ethRpcNode));
var coinbase = web3.eth.coinbase;
var balance = web3.eth.getBalance(coinbase);
server.connection({
host: apphost,
port: appport
});
server.register(Inert, () => {});
server.route({
method: 'GET',
path: '/ping',
config: {
cors: {
origin: ['*'],
additionalHeaders: ['cache-control', 'x-requested-with']
}
},
handler: function (request, reply) {
reply('pong');
}
});
server.route({
method: 'GET',
path: '/',
config: {
cors: {
origin: ['*'],
additionalHeaders: ['cache-control', 'x-requested-with']
}
},
handler: function (request, reply) {
reply('Hello there');
}
});
function createServeWallet(password) {
return web3.personal.newAccount(password);
}
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: true
}
}
});
server.route({
method: 'POST',
path: '/validateaadhaar',
config: {
cors: {
origin: ['*'],
additionalHeaders: ['cache-control', 'x-requested-with']
},
payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data'
},
handler: function (request, reply) {
var data = request.payload;
if (data.file) {
var name = data.file.hapi.filename;
var path = __dirname + "/uploads/" + name;
var file = fs.createWriteStream(path);
file.on('error', function (err) {
console.error(err)
});
data.file.pipe(file);
data.file.on('end', function (err) {
var ret = {
filename: data.file.hapi.filename,
headers: data.file.hapi.headers
}
var filename = file.path;
fs.readFile(filename, 'utf8', function(err, content) {
if (err) throw err;
var aadhar_id = request.payload.aadhar_id
if(aadhar_data[aadhar_id] == undefined){
reply({
"status": "error",
"message": "aadhar_id invalid"
}).code(402)
}else if(content == aadhar_data[aadhar_id]["fingerprint"]){
var wallet = createServeWallet('123');
reply(wallet);
}else{
reply({
"status": "error",
"message": "aadhar_id and fingerprint do not match"
}).code(402)
}
});
})
}
}
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});