-
Notifications
You must be signed in to change notification settings - Fork 50
/
wallet.js
325 lines (296 loc) · 11.7 KB
/
wallet.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* YourCoin App
*/
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true
});
////////// Config //////////
// coin Object - Localization settings and wallet daemon api calls.
var coin = require('./lib/coinapi');
module.exports = coin;
var initWallet = require('./lib/init-wallet');
var fs = require('fs');
var path = require('path');
var atob = require('atob');
var btoa = require('btoa');
var privateKey = coin.settings.ssl ? fs.readFileSync(coin.settings.sslKey, 'utf8') : null;
var certificate = coin.settings.ssl ? fs.readFileSync(coin.settings.sslCrt, 'utf8') : null;
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
var async = require('async');
var cors = require('cors');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var uuid = require('uuid');
var session = require('express-session');
var passport = require('passport');
var flash = require('express-flash');
// All environments
app.set('env', coin.settings.env || 'production');
app.set('status', ''); // Public status message (Important: Init to "")
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('host', coin.settings.appHost);
app.set('port', coin.settings.ssl ? coin.settings.sslPort : coin.settings.port);
app.use(cors());
app.use(express.static(path.join(__dirname, 'public' + coin.settings.chRoot)));
app.use(favicon(path.join(__dirname, coin.settings.favicon)));
app.use(morgan('dev'));
app.use(cookieParser(coin.settings.supersecret));
app.use(bodyParser.urlencoded({extended: false, limit: '2mb'})); // TODO: Put this limit in settings.json
app.use(bodyParser.json({limit: '2mb'})); // TODO: Put this limit in settings.json
app.use(session({name: coin.settings.appTitle,
secret: coin.settings.supersecret,
genid: function(req) {
return uuid.v4(); // use UUIDs
},
// Cookie expires in 30 days
cookie: {secure: coin.settings.ssl, maxAge: 30 * 24 * 60 * 60 * 1000, domain: coin.settings.appHost},
saveUninitialized: false,
resave: true}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use express-flash for flash messages stored in session
// DB Functions
var mdb = require('./lib/database');
var dbString = 'mongodb://' + coin.settings.mdb.user +
':' + coin.settings.mdb.password +
'@' + coin.settings.mdb.host +
':' + coin.settings.mdb.port +
'/' + coin.settings.mdb.database;
console.log('Database: ' + coin.settings.mdb.host + ':' + coin.settings.mdb.port + '/' + coin.settings.mdb.database);
coin.settings.mdb.password = "XXXXXXXX";
coin.settings.mdb = null; // garbage collection
// Connect to Database
function databaseConnect() {
mdb.connect(dbString, function(err) {
if (err){
app.set('status', coin.settings.appTitle + ' Maintenance');
} else {
app.set('status', '');
}
});
}
databaseConnect();
// Localizations for EJS rendering [MUST COME AFTER DB FUNCTIONS AND BEFORE AUTH ROUTES]
for (var s in coin.settings){
if (coin.settings.hasOwnProperty(s)){
if (s === "chRoot") {
// Need to flip relativeness for routes/auth.js.
if (coin.settings[s] === "") {
// We chrooted to public
coin.settings[s] = "/wallet";
} else {
// We chrooted to public/wallet
coin.settings[s] = "";
}
}
// Don't overwrite!
if (app.get(s) === undefined)
app.set(s, coin.settings[s]);
}
}
var chRoot = app.get('chRoot') || '';
////////// Routes //////////
// Add CORS headers to all requests
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-AUTHENTICATION, X-IP, Content-Type, Accept');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
next();
});
// Auth routes (includes: '/', '/signup', '/login', '/logout', '/profile', '/password', + oauth routes)
require('./routes/auth.js')(app, passport, coin);
// Passport.use functions
require('./lib/passport')(passport);
// Coin RPC routes / functions
require('./routes/coin.js')(app, coin);
// User routes //
require('./routes/user.js')(app, coin, mdb);
// Misc routes //
// Returns the wallet's rpc node info and localization settings.
app.get(chRoot + '/getnodeinfo', function(req, res) {
// Re-init if wallet is down.
initWallet();
var response = {
error: null,
result: {
settings: coin.settings
}
};
res.send(JSON.stringify(response));
});
// This goes last.
app.get('*', function(req, res) {
var err = 'The document you requested was not found.';
if (req.accepts('html')) {
res.render('error', {
message: err,
error: 404
});
return;
}
if (req.accepts('json')) {
res.send({ error: err });
return;
}
res.type('txt').send(err);
});
// *** Express 4.x requires these app.use calls to be after any app.get or app.post routes.
// *** "Your code should move any calls to app.use that came after app.use(app.router) after any routes (HTTP verbs)."
app.use(function(err, req, res, next) {
// Handle error message display
var msg = app.get('env') === 'production' ? 'An internal error occurred. Please try again later.': err;
if (req.accepts('html')) {
res.render('error', {
message: msg,
error: 500
});
return;
}
if (req.accepts('json')) {
res.send({ error: msg });
return;
}
res.type('txt').send(msg);
next();
});
// Catch session timeout
app.use(function(req, res, next) {
if (req.session && Date.now() <= req.session.cookie.expires){
next();
} else {
res.redirect(app.get('chRoot') + '/');
}
});
// Start it up!
function startApp(app) {
// Start the Express server
console.log("Express " + (coin.settings.ssl ? "Secure " : "") + "Server starting...");
var protocol = coin.settings.ssl ? require('https') : require('http');
var server = coin.settings.ssl ? protocol.createServer(credentials, app) : protocol.createServer(app);
var port = app.get('port'); // 8181 or 8383
var host = app.get('host');
var listener = server.listen(port, function() {
console.log('Server is listening on: ' + (coin.settings.ssl ? 'https://' : 'http://') + host + ':' + port);
console.log('Wallet is listening on: ' + (coin.settings.wallet.ssl ? 'https://' : 'http://') + coin.settings.wallet.rpchost + ':' + coin.settings.wallet.rpcport);
// Init MASTER_ACCOUNT in wallet and database for this node_id.
initWallet();
var io = require('socket.io')(server);
//Allow Cross Domain Requests
io.set('transports', [ 'websocket' ]);
io.on('connection', function (socket) {
socket.on('news', function (data) {
console.log(data);
});
socket.on('abort', function (data) {
console.log('Told clients to abort for: ' + data);
});
socket.on('continue', function (data) {
console.log('Told clients to continue to: /' + data);
app.set('status', '');
});
socket.on('connect_error', function (err) {
console.log("Socket connection error: " + err);
});
// Send a message to newly connected client
socket.emit('news', 'Socket connected!');
});
process.on('rpc_error', function (err) {
console.log(err);
app.set('status', coin.settings.appTitle + ' Wallet daemon is down.');
// Send abort message to all clients
io.sockets.emit('news', 'Wallet is down for maintenance...');
io.sockets.emit('wallet', 'down');
});
process.on('rpc_connected', function (err) {
console.log(err);
app.set('status', '');
// Send continue message to all clients
io.sockets.emit('news', 'Wallet is connected...'); // DEBUG: This is not working!
io.sockets.emit('wallet', 'up');
});
process.on('database_error', function (err) {
console.log(err);
app.set('status', coin.settings.appTitle + ' Database error.');
// Send abort message to all clients
io.sockets.emit('news', 'Database error...');
io.sockets.emit('abort', 'maintenance');
});
process.on('database_disconnected', function (err) {
console.log(err);
app.set('status', coin.settings.appTitle + ' Database disconnected.');
// Send abort message to all clients
io.sockets.emit('news', 'Database is down for maintenance...');
io.sockets.emit('abort', 'maintenance');
});
process.on('database_reconnected', function (err) {
console.log(err);
app.set('status', '');
// Send continue message to all clients
io.sockets.emit('news', 'Database is reconnected...');
io.sockets.emit('continue', '');
});
process.on('database_connected', function (err) {
console.log(err);
app.set('status', '');
// Send continue message to all clients
io.sockets.emit('news', 'Database is connected...');
io.sockets.emit('continue', '');
});
process.on('SIGINT', function(err){
console.log('SIGINT Received: ' + err);
app.set('status', coin.settings.appTitle + ' Down for Maintenance');
// Send abort message to all clients
io.sockets.emit('news', 'Going down for maintenance...');
io.sockets.emit('abort', 'maintenance');
mdb.close(function() {
listener.close(function(err) {
if (err) throw err;
console.log('Exiting App.');
process.exit(2);
});
});
});
process.on('uncaughtException', function (err) {
console.log('Caught unknown exception: ' + err);
});
process.on('unhandledRejection', function (reason, p) {
console.log('Caught unhandled rejection: ' + reason + ' (' + JSON.stringify(p) + ')');
});
process.on('rejectionHandled', function (p) {
console.log('Caught rejection handled: ' + JSON.stringify(p));
});
// Poll Status
setInterval(function(){
var status = app.get('status');
if (status.length > 0){
if (status.indexOf('Wallet') > 0){
// Wallet daemon down.
console.log(status);
// Re-init if wallet is down.
initWallet();
} else {
if (status.indexOf('Database') > 0){
// Reconnect DB
console.log(status);
console.log('Attempting to reconnect database...');
databaseConnect();
}
}
}
}, 10000);
});
}
startApp(app);