diff --git a/js/models/network/Async.js b/js/models/network/Async.js index 29c021cdb..f348f2d7b 100644 --- a/js/models/network/Async.js +++ b/js/models/network/Async.js @@ -179,22 +179,28 @@ Network.prototype.iterateNonce = function() { return this.networkNonce; }; -Network.prototype._onMessage = function(enc) { +Network.prototype.decode = function(enc) { + var sender = enc.pubkey; var key = this.getKey(); + var prevnonce = this.networkNonces ? this.networkNonces[sender] : undefined; + var opts = { + prevnonce: prevnonce + }; + var decoded = AuthMessage.decode(key, enc, opts); + + //if no error thrown in the last step, we can set the copayer's nonce + if (!this.networkNonces) + this.networkNonces = {}; + this.networkNonces[sender] = decoded.nonce; + + var payload = decoded.payload; + return payload; +}; + +Network.prototype._onMessage = function(enc) { var sender = enc.pubkey; try { - var prevnonce = this.networkNonces ? this.networkNonces[sender] : undefined; - var opts = { - prevnonce: prevnonce - }; - var decoded = AuthMessage.decode(key, enc, opts); - - //if no error thrown in the last step, we can set the copayer's nonce - if (!this.networkNonces) - this.networkNonces = {}; - this.networkNonces[sender] = decoded.nonce; - - var payload = decoded.payload; + var payload = this.decode(enc); } catch (e) { this._deletePeer(sender); return; @@ -359,18 +365,22 @@ Network.prototype.send = function(copayerIds, payload, cb) { //console.log('sending ' + JSON.stringify(payload)); copayerIds.forEach(function(copayerId) { //console.log('\t to ' + copayerId); - self.iterateNonce(); - var opts = { - nonce: self.networkNonce - }; - var copayerIdBuf = new Buffer(copayerId, 'hex'); - var message = AuthMessage.encode(copayerIdBuf, self.getKey(), payload, opts); + var message = self.encode(copayerId, payload); self.socket.emit('message', message); }); if (typeof cb === 'function') cb(); }; +Network.prototype.encode = function(copayerId, payload) { + this.iterateNonce(); + var opts = { + nonce: this.networkNonce + }; + var copayerIdBuf = new Buffer(copayerId, 'hex'); + var message = AuthMessage.encode(copayerIdBuf, this.getKey(), payload, opts); +}; + Network.prototype.isOnline = function() { return !!this.socket; }; diff --git a/test/test.network.Async.js b/test/test.network.Async.js index cf73279de..f6860d7eb 100644 --- a/test/test.network.Async.js +++ b/test/test.network.Async.js @@ -127,6 +127,7 @@ describe('Network / Async', function() { it('should not reject data sent from a peer with hijacked pubkey', function() { var n = createN(); n.privkey = key2.private.toString('hex'); + n.key = null; var message = { type: 'hello', @@ -151,6 +152,7 @@ describe('Network / Async', function() { it('should reject data sent from a peer with hijacked pubkey', function() { var n = createN(); n.privkey = key2.private.toString('hex'); + n.key = null; var message = { type: 'hello', @@ -177,8 +179,7 @@ describe('Network / Async', function() { it('should not reject data sent from a peer with no previously set nonce but who is setting one now', function() { var n = createN(); n.privkey = key2.private.toString('hex'); - //n.networkNonces = {}; - //n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('0000000000000001', 'hex'); //previously used nonce + n.key = null; var message = { type: 'hello', @@ -207,6 +208,7 @@ describe('Network / Async', function() { it('should not reject data sent from a peer with a really big new nonce', function() { var n = createN(); n.privkey = key2.private.toString('hex'); + n.key = null; n.networkNonces = {}; n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('5000000000000001', 'hex'); //previously used nonce @@ -236,6 +238,7 @@ describe('Network / Async', function() { it('should not reject data sent from a peer with a really big new nonce', function() { var n = createN(); n.privkey = key2.private.toString('hex'); + n.key = false; n.networkNonces = {}; n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('5000000000000001', 'hex'); //previously used nonce @@ -265,6 +268,7 @@ describe('Network / Async', function() { it('should reject data sent from a peer with an outdated nonce', function() { var n = createN(); n.privkey = key2.private.toString('hex'); + n.key = null; n.networkNonces = {}; n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('0000000000000002', 'hex'); //previously used nonce @@ -294,6 +298,7 @@ describe('Network / Async', function() { it('should reject data sent from a peer with a really big outdated nonce', function() { var n = createN(); n.privkey = key2.private.toString('hex'); + n.key = null; n.networkNonces = {}; n.networkNonces[(new bitcore.SIN(key1.public)).toString()] = new Buffer('5000000000000002', 'hex'); //previously used nonce