Ivan Socolsky
10 years ago
5 changed files with 133 additions and 100 deletions
@ -0,0 +1,90 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var $ = require('preconditions').singleton(); |
||||
|
var _ = require('lodash'); |
||||
|
var async = require('async'); |
||||
|
var log = require('npmlog'); |
||||
|
log.debug = log.verbose; |
||||
|
var Uuid = require('uuid'); |
||||
|
var inherits = require('inherits'); |
||||
|
var events = require('events'); |
||||
|
var nodeutil = require('util'); |
||||
|
|
||||
|
var WalletUtils = require('bitcore-wallet-utils'); |
||||
|
var Bitcore = WalletUtils.Bitcore; |
||||
|
var WalletService = require('./server'); |
||||
|
var BlockExplorer = require('./blockexplorer'); |
||||
|
|
||||
|
var Notification = require('./model/notification'); |
||||
|
|
||||
|
function BlockchainMonitor() { |
||||
|
this.subscriptions = {}; |
||||
|
this.sockets = {}; |
||||
|
|
||||
|
this._initBlockExplorerSocket('insight', 'livenet'); |
||||
|
this._initBlockExplorerSocket('insight', 'testnet'); |
||||
|
}; |
||||
|
|
||||
|
nodeutil.inherits(BlockchainMonitor, events.EventEmitter); |
||||
|
|
||||
|
BlockchainMonitor.prototype._initBlockExplorerSocket = function(provider, network) { |
||||
|
var explorer = new BlockExplorer({ |
||||
|
provider: provider, |
||||
|
network: network, |
||||
|
}); |
||||
|
|
||||
|
this.sockets[network] = explorer.initSocket(); |
||||
|
}; |
||||
|
|
||||
|
BlockchainMonitor.prototype.subscribeAddresses = function(walletId, addresses) { |
||||
|
$.checkArgument(walletId); |
||||
|
|
||||
|
var self = this; |
||||
|
|
||||
|
if (!addresses || addresses.length == 0) return; |
||||
|
|
||||
|
function handlerFor(address, txid) { |
||||
|
var notification = Notification.create({ |
||||
|
walletId: this, |
||||
|
type: 'NewIncomingTx', |
||||
|
data: { |
||||
|
address: address, |
||||
|
txid: txid, |
||||
|
}, |
||||
|
}); |
||||
|
self.emit('notification', notification); |
||||
|
}; |
||||
|
|
||||
|
if (!self.subscriptions[walletId]) { |
||||
|
self.subscriptions[walletId] = { |
||||
|
addresses: [], |
||||
|
}; |
||||
|
}; |
||||
|
var addresses = [].concat(addresses); |
||||
|
var network = Bitcore.Address.fromString(addresses[0]).network.name; |
||||
|
var socket = self.sockets[network]; |
||||
|
_.each(addresses, function(address) { |
||||
|
self.subscriptions[walletId].addresses.push(address); |
||||
|
socket.emit('subscribe', address); |
||||
|
socket.on(address, _.bind(handlerFor, walletId, address)); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
BlockchainMonitor.prototype.subscribeWallet = function(walletService) { |
||||
|
var self = this; |
||||
|
|
||||
|
var walletId = walletService.walletId; |
||||
|
if (self.subscriptions[walletId]) return; |
||||
|
|
||||
|
walletService.getMainAddresses({}, function(err, addresses) { |
||||
|
if (err) { |
||||
|
delete self.subscriptions[walletId]; |
||||
|
log.warn('Could not subscribe to addresses for wallet ' + walletId); |
||||
|
return; |
||||
|
} |
||||
|
self.subscribeAddresses(walletService.walletId, _.pluck(addresses, 'address')); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
module.exports = BlockchainMonitor; |
@ -1,25 +0,0 @@ |
|||||
'use strict'; |
|
||||
|
|
||||
var log = require('npmlog'); |
|
||||
log.debug = log.verbose; |
|
||||
var inherits = require('inherits'); |
|
||||
var events = require('events'); |
|
||||
var nodeutil = require('util'); |
|
||||
|
|
||||
function EventBroadcaster() {}; |
|
||||
|
|
||||
nodeutil.inherits(EventBroadcaster, events.EventEmitter); |
|
||||
|
|
||||
EventBroadcaster.prototype.broadcast = function(eventName, serviceInstance, args) { |
|
||||
this.emit(eventName, serviceInstance, args); |
|
||||
}; |
|
||||
|
|
||||
var _eventBroadcasterInstance; |
|
||||
EventBroadcaster.singleton = function() { |
|
||||
if (!_eventBroadcasterInstance) { |
|
||||
_eventBroadcasterInstance = new EventBroadcaster(); |
|
||||
} |
|
||||
return _eventBroadcasterInstance; |
|
||||
}; |
|
||||
|
|
||||
module.exports = EventBroadcaster.singleton(); |
|
@ -0,0 +1,25 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var log = require('npmlog'); |
||||
|
log.debug = log.verbose; |
||||
|
var inherits = require('inherits'); |
||||
|
var events = require('events'); |
||||
|
var nodeutil = require('util'); |
||||
|
|
||||
|
function NotificationBroadcaster() {}; |
||||
|
|
||||
|
nodeutil.inherits(NotificationBroadcaster, events.EventEmitter); |
||||
|
|
||||
|
NotificationBroadcaster.prototype.broadcast = function(eventName, notification) { |
||||
|
this.emit(eventName, notification); |
||||
|
}; |
||||
|
|
||||
|
var _instance; |
||||
|
NotificationBroadcaster.singleton = function() { |
||||
|
if (!_instance) { |
||||
|
_instance = new NotificationBroadcaster(); |
||||
|
} |
||||
|
return _instance; |
||||
|
}; |
||||
|
|
||||
|
module.exports = NotificationBroadcaster.singleton(); |
Loading…
Reference in new issue