Merge pull request #156 from isocolsky/fix/notif

Fix/notif
This commit is contained in:
Matias Alejo Garcia 2015-03-30 12:12:37 -03:00
commit 62909c34db
7 changed files with 37 additions and 6 deletions

View File

@ -37,6 +37,7 @@ Notification.create = function(opts) {
x.id = _.padLeft(now, 14, '0') + _.padLeft(opts.ticker || 0, 4, '0');
x.type = opts.type || 'general';
x.data = opts.data;
x.walletId = opts.walletId;
x.creatorId = opts.creatorId;
return x;
@ -49,6 +50,7 @@ Notification.fromObj = function(obj) {
x.id = obj.id;
x.type = obj.type,
x.data = obj.data;
x.walletId = obj.walletId;
x.creatorId = obj.creatorId;
return x;

View File

@ -20,6 +20,7 @@ TxProposal.create = function(opts) {
var now = Date.now();
x.createdOn = Math.floor(now / 1000);
x.id = _.padLeft(now, 14, '0') + Uuid.v4();
x.walletId = opts.walletId;
x.creatorId = opts.creatorId;
x.toAddress = opts.toAddress;
x.amount = opts.amount;
@ -45,6 +46,7 @@ TxProposal.fromObj = function(obj) {
x.version = obj.version;
x.createdOn = obj.createdOn;
x.id = obj.id;
x.walletId = obj.walletId;
x.creatorId = obj.creatorId;
x.toAddress = obj.toAddress;
x.amount = obj.amount;

View File

@ -182,13 +182,16 @@ WalletService.prototype._notify = function(type, data) {
log.debug('Notification', type, data);
var walletId = self.walletId || data.walletId;
var copayerId = self.copayerId || data.copayerId;
$.checkState(walletId);
var n = Notification.create({
type: type,
data: data,
ticker: this.notifyTicker++,
creatorId: self.copayerId,
creatorId: copayerId,
walletId: walletId,
});
this.storage.storeNotification(walletId, n, function() {
self._emit('notification', n);
@ -351,13 +354,13 @@ WalletService.prototype._getBlockExplorer = function(provider, network) {
return this.blockExplorer;
switch (provider) {
default:;
default: ;
case 'insight':
switch (network) {
default:
case 'livenet':
case 'livenet':
url = 'https://insight.bitpay.com:443';
break;
break;
case 'testnet':
url = 'https://test-insight.bitpay.com:443'
break;
@ -580,6 +583,7 @@ WalletService.prototype.createTx = function(opts, cb) {
var changeAddress = wallet.createAddress(true);
var txp = TxProposal.create({
walletId: self.walletId,
creatorId: self.copayerId,
toAddress: opts.toAddress,
amount: opts.amount,

View File

@ -29,7 +29,10 @@ WsApp.start = function(server) {
var io = require('socket.io')(server);
WalletService.onNotification(function(serviceInstance, args) {
io.to(serviceInstance.walletId).emit('notification', args);
var room = serviceInstance.walletId || args.walletId;
if (room) {
io.to(room).emit('notification', args);
}
});
io.on('connection', function(socket) {

View File

@ -2,7 +2,7 @@
"name": "bitcore-wallet-service",
"description": "A service for Mutisig HD Bitcoin Wallets",
"author": "BitPay Inc",
"version": "0.0.15",
"version": "0.0.16",
"keywords": [
"bitcoin",
"copay",

View File

@ -800,6 +800,8 @@ describe('Copay server', function() {
server.createTx(txOpts, function(err, tx) {
should.not.exist(err);
should.exist(tx);
tx.walletId.should.equal(wallet.id);
tx.creatorId.should.equal(wallet.copayers[0].id);
tx.message.should.equal('some message');
tx.isAccepted().should.equal.false;
tx.isRejected().should.equal.false;
@ -1611,6 +1613,7 @@ describe('Copay server', function() {
should.not.exist(err);
var last = _.last(notifications);
last.type.should.equal('TxProposalFinallyAccepted');
last.walletId.should.equal(wallet.id);
last.creatorId.should.equal(wallet.copayers[1].id);
last.data.txProposalId.should.equal(txp.id);
done();
@ -1869,6 +1872,9 @@ describe('Copay server', function() {
should.not.exist(err);
var types = _.pluck(notifications, 'type');
types.should.deep.equal(['NewTxProposal', 'NewTxProposal', 'NewTxProposal', 'NewAddress']);
var walletIds = _.uniq(_.pluck(notifications, 'walletId'));
walletIds.length.should.equal(1);
walletIds[0].should.equal(wallet.id);
var creators = _.uniq(_.pluck(notifications, 'creatorId'));
creators.length.should.equal(1);
creators[0].should.equal(wallet.copayers[0].id);
@ -1902,6 +1908,19 @@ describe('Copay server', function() {
});
});
it('should contain walletId & creatorId on NewCopayer', function(done) {
server.getNotifications({
minTs: 0,
}, function(err, notifications) {
should.not.exist(err);
var newCopayer = notifications[0];
newCopayer.type.should.equal('NewCopayer');
newCopayer.walletId.should.equal(wallet.id);
newCopayer.creatorId.should.equal(wallet.copayers[0].id);
done();
});
});
it('should notify sign and acceptance', function(done) {
server.getPendingTxs({}, function(err, txs) {
helpers.stubBroadcastFail();

View File

@ -91,6 +91,7 @@ var aTXP = function() {
"version": "1.0.0",
"createdOn": 1423146231,
"id": "75c34f49-1ed6-255f-e9fd-0c71ae75ed1e",
"walletId": "1",
"creatorId": "1",
"toAddress": "18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7",
"amount": 50000000,