fix test 8

This commit is contained in:
Manuel Araoz 2014-08-19 14:22:13 -04:00
parent ed7916f1be
commit 372d230e17
2 changed files with 36 additions and 21 deletions

View File

@ -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;
};

View File

@ -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