diff --git a/lib/model/notification.js b/lib/model/notification.js index 2cffa09..4e45c0f 100644 --- a/lib/model/notification.js +++ b/lib/model/notification.js @@ -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; diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 025ada5..a20599e 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -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; diff --git a/lib/server.js b/lib/server.js index 6a2ebaa..4fcc19a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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, diff --git a/lib/wsapp.js b/lib/wsapp.js index af7698f..8196e03 100644 --- a/lib/wsapp.js +++ b/lib/wsapp.js @@ -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) { diff --git a/package.json b/package.json index b538c9b..6313f7d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/integration/server.js b/test/integration/server.js index c57b926..b3d1c30 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -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(); diff --git a/test/models/txproposal.js b/test/models/txproposal.js index 9b864f3..142291e 100644 --- a/test/models/txproposal.js +++ b/test/models/txproposal.js @@ -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,