|
|
@ -7,16 +7,13 @@ var log = require('npmlog'); |
|
|
|
log.debug = log.verbose; |
|
|
|
var Uuid = require('uuid'); |
|
|
|
|
|
|
|
var WalletUtils = require('bitcore-wallet-utils'); |
|
|
|
var Bitcore = WalletUtils.Bitcore; |
|
|
|
var WalletService = require('./server'); |
|
|
|
var BlockchainMonitor = require('./blockchainmonitor') |
|
|
|
|
|
|
|
var Notification = require('./model/notification'); |
|
|
|
|
|
|
|
log.level = 'debug'; |
|
|
|
|
|
|
|
var io, bcMonitor; |
|
|
|
var io, messageQueue; |
|
|
|
|
|
|
|
var WsApp = function() {}; |
|
|
|
|
|
|
@ -25,31 +22,41 @@ WsApp._unauthorized = function(socket) { |
|
|
|
socket.disconnect(); |
|
|
|
}; |
|
|
|
|
|
|
|
WsApp.handleNotification = function(service, notification) { |
|
|
|
if (notification.type == 'NewAddress') { |
|
|
|
self.subscribeAddresses(notification.walletId, notification.data.address); |
|
|
|
} |
|
|
|
io.to(notification.walletId).emit('notification', notification); |
|
|
|
}; |
|
|
|
// WsApp._handleNotification = function(notification) {
|
|
|
|
// console.log('*** [wsapp.js ln26] notification:', notification); // TODO
|
|
|
|
|
|
|
|
WsApp.start = function(server, config) { |
|
|
|
io = require('socket.io')(server); |
|
|
|
bcMonitor = new BlockchainMonitor(config.blockchainExplorerOpts); |
|
|
|
// io.to(notification.walletId).emit('notification', notification);
|
|
|
|
// };
|
|
|
|
|
|
|
|
WsApp._initMessageQueue = function(cb) { |
|
|
|
function handleNotification(notification) { |
|
|
|
if (notification.type == 'NewAddress') { |
|
|
|
bcMonitor.subscribeAddresses(notification.walletId, notification.data.address); |
|
|
|
} |
|
|
|
io.to(notification.walletId).emit('notification', notification); |
|
|
|
}; |
|
|
|
|
|
|
|
bcMonitor.on('notification', handleNotification); |
|
|
|
WalletService.onNotification(handleNotification); |
|
|
|
messageQueue = require('socket.io-client').connect('http://localhost:3380', { |
|
|
|
'force new connection': true, |
|
|
|
}); |
|
|
|
messageQueue.on('connect_error', function(err) { |
|
|
|
log.warn('Could not connect to message queue server'); |
|
|
|
}); |
|
|
|
messageQueue.on('notification', handleNotification); |
|
|
|
|
|
|
|
messageQueue.on('connect', function() { |
|
|
|
return cb(); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WsApp.start = function(server, config, cb) { |
|
|
|
io = require('socket.io')(server); |
|
|
|
|
|
|
|
async.series([ |
|
|
|
|
|
|
|
function(done) { |
|
|
|
WsApp._initMessageQueue(done); |
|
|
|
}, |
|
|
|
function(done) { |
|
|
|
io.on('connection', function(socket) { |
|
|
|
socket.nonce = Uuid.v4(); |
|
|
|
socket.emit('challenge', socket.nonce); |
|
|
|
|
|
|
|
socket.on('authorize', function(data) { |
|
|
|
if (data.message != socket.nonce) return WsApp._unauthorized(socket); |
|
|
|
|
|
|
@ -58,12 +65,15 @@ WsApp.start = function(server, config) { |
|
|
|
|
|
|
|
socket.join(service.walletId); |
|
|
|
socket.emit('authorized'); |
|
|
|
|
|
|
|
bcMonitor.subscribeWallet(service, function(err) { |
|
|
|
if (err) log.warn(err.message); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
socket.emit('challenge', socket.nonce); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}, |
|
|
|
], function(err) { |
|
|
|
if (cb) return cb(err); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|