add socket connection to block explorer
This commit is contained in:
parent
2075357d16
commit
ff29691cd2
|
@ -7,6 +7,7 @@ log.debug = log.verbose;
|
||||||
|
|
||||||
var Explorers = require('bitcore-explorers');
|
var Explorers = require('bitcore-explorers');
|
||||||
var request = require('request');
|
var request = require('request');
|
||||||
|
var io = require('socket.io-client');
|
||||||
|
|
||||||
|
|
||||||
function BlockExplorer(opts) {
|
function BlockExplorer(opts) {
|
||||||
|
@ -15,7 +16,6 @@ function BlockExplorer(opts) {
|
||||||
var network = opts.network || 'livenet';
|
var network = opts.network || 'livenet';
|
||||||
|
|
||||||
var url;
|
var url;
|
||||||
|
|
||||||
switch (provider) {
|
switch (provider) {
|
||||||
case 'insight':
|
case 'insight':
|
||||||
switch (network) {
|
switch (network) {
|
||||||
|
@ -29,7 +29,8 @@ function BlockExplorer(opts) {
|
||||||
}
|
}
|
||||||
var bc = new Explorers.Insight(url, network);
|
var bc = new Explorers.Insight(url, network);
|
||||||
bc.getTransactions = _.bind(getTransactionsInsight, bc, url);
|
bc.getTransactions = _.bind(getTransactionsInsight, bc, url);
|
||||||
this.blockExplorer = bc;
|
bc.initSocket = _.bind(initSocketInsight, bc, url);
|
||||||
|
return bc;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('Provider ' + provider + ' not supported');
|
throw new Error('Provider ' + provider + ' not supported');
|
||||||
|
@ -51,4 +52,9 @@ function getTransactionsInsight(url, addresses, cb) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function initSocketInsight(url) {
|
||||||
|
var socket = io.connect(url, {});
|
||||||
|
return socket;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = BlockExplorer;
|
module.exports = BlockExplorer;
|
||||||
|
|
|
@ -12,8 +12,8 @@ var Uuid = require('uuid');
|
||||||
* txProposalFinallyRejected - txProposalId
|
* txProposalFinallyRejected - txProposalId
|
||||||
* txProposalFinallyAccepted - txProposalId
|
* txProposalFinallyAccepted - txProposalId
|
||||||
*
|
*
|
||||||
* newIncommingTx (amount)
|
* NewIncommingTx (address, txid)
|
||||||
* newOutgoingTx - (txProposalId, txid)
|
* NewOutgoingTx - (txProposalId, txid)
|
||||||
*
|
*
|
||||||
* data Examples:
|
* data Examples:
|
||||||
* { amount: 'xxx', address: 'xxx'}
|
* { amount: 'xxx', address: 'xxx'}
|
||||||
|
|
|
@ -337,7 +337,7 @@ WalletService.prototype.verifyMessageSignature = function(opts, cb) {
|
||||||
|
|
||||||
WalletService.prototype._getBlockExplorer = function(provider, network) {
|
WalletService.prototype._getBlockExplorer = function(provider, network) {
|
||||||
if (!this.blockExplorer) {
|
if (!this.blockExplorer) {
|
||||||
this.blockexplorer = new BlockExplorer({
|
this.blockExplorer = new BlockExplorer({
|
||||||
provider: provider,
|
provider: provider,
|
||||||
network: network,
|
network: network,
|
||||||
});
|
});
|
||||||
|
|
84
lib/wsapp.js
84
lib/wsapp.js
|
@ -9,12 +9,22 @@ var querystring = require('querystring');
|
||||||
var bodyParser = require('body-parser')
|
var bodyParser = require('body-parser')
|
||||||
var Uuid = require('uuid');
|
var Uuid = require('uuid');
|
||||||
|
|
||||||
|
var WalletUtils = require('bitcore-wallet-utils');
|
||||||
|
var Bitcore = WalletUtils.Bitcore;
|
||||||
var WalletService = require('./server');
|
var WalletService = require('./server');
|
||||||
|
var BlockExplorer = require('./blockexplorer');
|
||||||
|
|
||||||
|
var Notification = require('./model/notification');
|
||||||
|
|
||||||
log.debug = log.verbose;
|
log.debug = log.verbose;
|
||||||
log.level = 'debug';
|
log.level = 'debug';
|
||||||
|
|
||||||
var subscriptions = {};
|
var io;
|
||||||
|
var blockExplorerSockets = {};
|
||||||
|
var subscriptions = {
|
||||||
|
wallets: {},
|
||||||
|
addresses: {},
|
||||||
|
};
|
||||||
|
|
||||||
var WsApp = function() {};
|
var WsApp = function() {};
|
||||||
|
|
||||||
|
@ -24,37 +34,71 @@ WsApp._unauthorized = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
WsApp.subscribeAddresses = function(walletId, addresses) {
|
WsApp.subscribeAddresses = function(walletId, addresses) {
|
||||||
console.log('*** [wsapp.js ln27] subscribing:', addresses, walletId); // TODO
|
if (!addresses || addresses.length == 0) return;
|
||||||
|
|
||||||
_.each([].concat(addresses), function(address) {
|
function handlerFor(walletId, address, txid) {
|
||||||
|
var notification = Notification.create({
|
||||||
|
walletId: walletId,
|
||||||
|
type: 'NewIncommingTx',
|
||||||
|
data: {
|
||||||
|
address: address,
|
||||||
|
txid: txid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
WsApp._sendNotification(notification);
|
||||||
|
};
|
||||||
|
|
||||||
|
var addresses = [].concat(addresses);
|
||||||
|
var network = Bitcore.Address.fromString(addresses[0]).network;
|
||||||
|
var socket = blockExplorerSockets[network];
|
||||||
|
_.each(addresses, function(address) {
|
||||||
subscriptions[address] = walletId;
|
subscriptions[address] = walletId;
|
||||||
|
socket.emit('subscribe', address);
|
||||||
|
socket.on(address, _.bind(handlerFor, null, walletId, address));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WsApp.subscribeWallet = function(serviceInstance) {
|
WsApp.subscribeWallet = function(serviceInstance) {
|
||||||
// TODO: optimize!
|
var walletId = serviceInstance.walletId;
|
||||||
|
if (subscriptions.wallets[walletId]) return;
|
||||||
|
|
||||||
|
subscriptions.wallets[walletId] = true;
|
||||||
serviceInstance.getMainAddresses({}, function(err, addresses) {
|
serviceInstance.getMainAddresses({}, function(err, addresses) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
delete subscriptions.wallets[walletId];
|
||||||
log.warn('Could not subscribe to addresses for wallet ' + serviceInstance.walletId);
|
log.warn('Could not subscribe to addresses for wallet ' + serviceInstance.walletId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
WsApp.subscribeAddress(_.pluck(addresses, 'address'), serviceInstance.walletId);
|
WsApp.subscribeAddresses(serviceInstance.walletId, _.pluck(addresses, 'address'));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WsApp.start = function(server) {
|
WsApp._sendNotification = function(notification) {
|
||||||
var self = this;
|
if (notification.type == 'NewAddress') {
|
||||||
|
WsApp.subscribeAddresses(notification.walletId, notification.data.address);
|
||||||
var io = require('socket.io')(server);
|
|
||||||
|
|
||||||
WalletService.onNotification(function(serviceInstance, args) {
|
|
||||||
var walletId = serviceInstance.walletId || args.walletId;
|
|
||||||
if (!walletId) return;
|
|
||||||
|
|
||||||
if (args.type == 'NewAddress') {
|
|
||||||
WsApp.subscribeAddress(walletId, args.address);
|
|
||||||
}
|
}
|
||||||
io.to(walletId).emit('notification', args);
|
io.to(notification.walletId).emit('notification', notification);
|
||||||
|
};
|
||||||
|
|
||||||
|
WsApp._initBlockExplorerSocket = function(provider, network) {
|
||||||
|
var explorer = new BlockExplorer({
|
||||||
|
provider: provider,
|
||||||
|
network: network,
|
||||||
|
});
|
||||||
|
|
||||||
|
blockExplorerSockets[network] = explorer.initSocket();
|
||||||
|
};
|
||||||
|
|
||||||
|
WsApp.start = function(server) {
|
||||||
|
io = require('socket.io')(server);
|
||||||
|
|
||||||
|
WsApp._initBlockExplorerSocket('insight', 'testnet');
|
||||||
|
WsApp._initBlockExplorerSocket('insight', 'livenet');
|
||||||
|
|
||||||
|
WalletService.onNotification(function(serviceInstance, notification) {
|
||||||
|
if (!notification.walletId) return;
|
||||||
|
|
||||||
|
WsApp._sendNotification(notification);
|
||||||
});
|
});
|
||||||
|
|
||||||
io.on('connection', function(socket) {
|
io.on('connection', function(socket) {
|
||||||
|
@ -64,13 +108,13 @@ WsApp.start = function(server) {
|
||||||
socket.on('authorize', function(data) {
|
socket.on('authorize', function(data) {
|
||||||
if (data.message != socket.nonce) return WsApp.unauthorized();
|
if (data.message != socket.nonce) return WsApp.unauthorized();
|
||||||
|
|
||||||
WalletService.getInstanceWithAuth(data, function(err, res) {
|
WalletService.getInstanceWithAuth(data, function(err, service) {
|
||||||
if (err) return WsApp.unauthorized();
|
if (err) return WsApp.unauthorized();
|
||||||
|
|
||||||
socket.join(res.walletId);
|
socket.join(service.walletId);
|
||||||
socket.emit('authorized');
|
socket.emit('authorized');
|
||||||
|
|
||||||
WsApp.subscribeWallet(res);
|
WsApp.subscribeWallet(service);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue