|
|
@ -21,10 +21,7 @@ log.level = 'debug'; |
|
|
|
|
|
|
|
var io; |
|
|
|
var blockExplorerSockets = {}; |
|
|
|
var subscriptions = { |
|
|
|
wallets: {}, |
|
|
|
addresses: {}, |
|
|
|
}; |
|
|
|
var subscriptions = {}; |
|
|
|
|
|
|
|
var WsApp = function() {}; |
|
|
|
|
|
|
@ -34,11 +31,13 @@ WsApp._unauthorized = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
WsApp.subscribeAddresses = function(walletId, addresses) { |
|
|
|
$.checkArgument(walletId); |
|
|
|
|
|
|
|
if (!addresses || addresses.length == 0) return; |
|
|
|
|
|
|
|
function handlerFor(walletId, address, txid) { |
|
|
|
function handlerFor(address, txid) { |
|
|
|
var notification = Notification.create({ |
|
|
|
walletId: walletId, |
|
|
|
walletId: this, |
|
|
|
type: 'NewIncomingTx', |
|
|
|
data: { |
|
|
|
address: address, |
|
|
@ -48,24 +47,28 @@ WsApp.subscribeAddresses = function(walletId, addresses) { |
|
|
|
WsApp._sendNotification(notification); |
|
|
|
}; |
|
|
|
|
|
|
|
if (!subscriptions[walletId]) { |
|
|
|
subscriptions[walletId] = { |
|
|
|
addresses: [], |
|
|
|
}; |
|
|
|
}; |
|
|
|
var addresses = [].concat(addresses); |
|
|
|
var network = Bitcore.Address.fromString(addresses[0]).network; |
|
|
|
var socket = blockExplorerSockets[network]; |
|
|
|
_.each(addresses, function(address) { |
|
|
|
subscriptions[address] = walletId; |
|
|
|
subscriptions[walletId].addresses.push(address); |
|
|
|
socket.emit('subscribe', address); |
|
|
|
socket.on(address, _.bind(handlerFor, null, walletId, address)); |
|
|
|
socket.on(address, _.bind(handlerFor, walletId, address)); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WsApp.subscribeWallet = function(serviceInstance) { |
|
|
|
var walletId = serviceInstance.walletId; |
|
|
|
if (subscriptions.wallets[walletId]) return; |
|
|
|
if (subscriptions[walletId]) return; |
|
|
|
|
|
|
|
subscriptions.wallets[walletId] = true; |
|
|
|
serviceInstance.getMainAddresses({}, function(err, addresses) { |
|
|
|
if (err) { |
|
|
|
delete subscriptions.wallets[walletId]; |
|
|
|
delete subscriptions[walletId]; |
|
|
|
log.warn('Could not subscribe to addresses for wallet ' + serviceInstance.walletId); |
|
|
|
return; |
|
|
|
} |
|
|
|