From ef45a97e5bb862d6018c4e2bcfd54c5a8a35adff Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 30 Mar 2015 11:24:33 -0300 Subject: [PATCH 1/5] add walletId to notification model --- lib/model/notification.js | 2 ++ lib/server.js | 7 ++++--- test/integration/server.js | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) 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/server.js b/lib/server.js index 6a2ebaa..5c190f8 100644 --- a/lib/server.js +++ b/lib/server.js @@ -189,6 +189,7 @@ WalletService.prototype._notify = function(type, data) { data: data, ticker: this.notifyTicker++, creatorId: self.copayerId, + walletId: walletId, }); this.storage.storeNotification(walletId, n, function() { self._emit('notification', n); @@ -351,13 +352,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; diff --git a/test/integration/server.js b/test/integration/server.js index c57b926..705827b 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1611,6 +1611,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 +1870,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); From 4e840328bd1f135b8622f9346ce311ebe42d7db0 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 30 Mar 2015 11:29:19 -0300 Subject: [PATCH 2/5] add walletId to txProposal model --- lib/model/txproposal.js | 2 ++ lib/server.js | 1 + test/integration/server.js | 2 ++ test/models/txproposal.js | 1 + 4 files changed, 6 insertions(+) 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 5c190f8..090d1c9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -581,6 +581,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/test/integration/server.js b/test/integration/server.js index 705827b..fa02405 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; 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, From 0376768144f40b8536bca0d3670fb505c8cd5e64 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 30 Mar 2015 11:33:38 -0300 Subject: [PATCH 3/5] improve notification broadcast --- lib/wsapp.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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) { From 4fc8faa4832ae52178a8d13dff5b46189127e49b Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 30 Mar 2015 11:46:00 -0300 Subject: [PATCH 4/5] correctly assign copayerId to notification --- lib/server.js | 4 +++- lib/wsapp.js | 3 +++ test/integration/server.js | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index 090d1c9..4fcc19a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -182,13 +182,15 @@ 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() { diff --git a/lib/wsapp.js b/lib/wsapp.js index 8196e03..dd382b2 100644 --- a/lib/wsapp.js +++ b/lib/wsapp.js @@ -30,6 +30,9 @@ WsApp.start = function(server) { WalletService.onNotification(function(serviceInstance, args) { var room = serviceInstance.walletId || args.walletId; + console.log('*** [wsapp.js ln33] args:', args); // TODO + console.log('*** [wsapp.js ln33] room:', room); // TODO + if (room) { io.to(room).emit('notification', args); } diff --git a/test/integration/server.js b/test/integration/server.js index fa02405..b3d1c30 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1908,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(); From 50aa9fabff81226be288702c22c0bde397475a11 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 30 Mar 2015 11:49:32 -0300 Subject: [PATCH 5/5] v0.0.16 --- lib/wsapp.js | 3 --- package.json | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/wsapp.js b/lib/wsapp.js index dd382b2..8196e03 100644 --- a/lib/wsapp.js +++ b/lib/wsapp.js @@ -30,9 +30,6 @@ WsApp.start = function(server) { WalletService.onNotification(function(serviceInstance, args) { var room = serviceInstance.walletId || args.walletId; - console.log('*** [wsapp.js ln33] args:', args); // TODO - console.log('*** [wsapp.js ln33] room:', room); // TODO - if (room) { io.to(room).emit('notification', args); } 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",