working towards refactoring network

This commit is contained in:
Manuel Araoz 2014-08-12 16:41:45 -04:00
parent bfd4bbe021
commit 3ebacd50cc
5 changed files with 70 additions and 57 deletions

View File

@ -89,6 +89,7 @@ Wallet.prototype.seedCopayer = function(pubKey) {
this.seededCopayerId = pubKey; this.seededCopayerId = pubKey;
}; };
// not being used now
Wallet.prototype.connectToAll = function() { Wallet.prototype.connectToAll = function() {
var all = this.publicKeyRing.getAllCopayerIds(); var all = this.publicKeyRing.getAllCopayerIds();
@ -317,7 +318,7 @@ Wallet.prototype._handleAddressBook = function(senderId, data, isInbound) {
Wallet.prototype._handleData = function(senderId, data, isInbound) { Wallet.prototype._handleData = function(senderId, data, isInbound) {
// TODO check message signature alert('data '+JSON.stringify(data));
if (data.type !== 'walletId' && this.id !== data.walletId) { if (data.type !== 'walletId' && this.id !== data.walletId) {
this.emit('badMessage', senderId); this.emit('badMessage', senderId);
@ -451,7 +452,6 @@ Wallet.prototype.netStart = function(callback) {
} }
net.start(startOpts, function() { net.start(startOpts, function() {
alert('start callback!');
self.emit('ready', net.getPeer()); self.emit('ready', net.getPeer());
setTimeout(function() { setTimeout(function() {
self.emit('publicKeyRingUpdated', true); self.emit('publicKeyRingUpdated', true);
@ -462,6 +462,8 @@ Wallet.prototype.netStart = function(callback) {
}); });
}; };
// not being used now
Wallet.prototype.scheduleConnect = function() { Wallet.prototype.scheduleConnect = function() {
var self = this; var self = this;
if (self.network.isOnline()) { if (self.network.isOnline()) {

View File

@ -255,7 +255,7 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
}); });
self.network.start(opts, function() { self.network.start(opts, function() {
self.network.connectTo(s.pubKey); self.network.greet(s.pubKey);
self.network.on('data', function(sender, data) { self.network.on('data', function(sender, data) {
if (data.type === 'walletId') { if (data.type === 'walletId') {

View File

@ -93,19 +93,6 @@ Network.prototype.connectedCopayers = function() {
return ret; return ret;
}; };
Network.prototype.connectToCopayers = function(copayerIds) {
var self = this;
var arrayDiff = Network._arrayDiff(copayerIds, self.connectedCopayers());
arrayDiff.forEach(function(copayerId) {
if (self.allowedCopayerIds && !self.allowedCopayerIds[copayerId]) {
self._deletePeer(self.peerFromCopayer(copayerId));
} else {
self.connectTo(copayerId);
}
});
};
Network.prototype._sendHello = function(copayerId) { Network.prototype._sendHello = function(copayerId) {
this.send(copayerId, { this.send(copayerId, {
type: 'hello', type: 'hello',
@ -193,9 +180,9 @@ Network.prototype.iterateNonce = function() {
Network.prototype._onMessage = function(enc) { Network.prototype._onMessage = function(enc) {
var key = this.getKey(); var key = this.getKey();
var sender = enc.pubkey;
try { try {
var prevnonce = this.networkNonces ? this.networkNonces[peerId] : undefined; var prevnonce = this.networkNonces ? this.networkNonces[sender] : undefined;
var opts = { var opts = {
prevnonce: prevnonce prevnonce: prevnonce
}; };
@ -204,33 +191,43 @@ Network.prototype._onMessage = function(enc) {
//if no error thrown in the last step, we can set the copayer's nonce //if no error thrown in the last step, we can set the copayer's nonce
if (!this.networkNonces) if (!this.networkNonces)
this.networkNonces = {}; this.networkNonces = {};
this.networkNonces[peerId] = decoded.nonce; this.networkNonces[sender] = decoded.nonce;
var payload = decoded.payload; var payload = decoded.payload;
} catch (e) { } catch (e) {
this._deletePeer(peerId); this._deletePeer(sender);
alert('quit 1');
return; return;
} }
if (this.allowedCopayerIds && !this.allowedCopayerIds[payload.copayerId]) { if (this.allowedCopayerIds && !this.allowedCopayerIds[payload.copayerId]) {
this._deletePeer(peerId); this._deletePeer(sender);
alert('quit 2');
return; return;
} }
if (!this.copayerForPeer[peerId] || (isInbound && !this.isInboundPeerAuth[peerId])) {
this._deletePeer(peerId); // TODO
/*
if (!this.copayerForPeer[sender] || (isInbound && !this.isInboundPeerAuth[sender])) {
this._deletePeer(sender);
alert('quit 3');
return; return;
} }
*/
var self = this; var self = this;
switch (payload.type) { switch (payload.type) {
case 'disconnect': case 'disconnect':
this._onClose(peerId); this._onClose(sender);
break;
case 'hello':
this._addConnectedCopayer(payload.copayerId);
break; break;
default: default:
this.emit('data', self.copayerForPeer[peerId], payload); this.emit('data', self.copayerForPeer[sender], payload);
} }
}; };
@ -239,23 +236,28 @@ Network.prototype._setupConnectionHandlers = function(cb) {
var self = this; var self = this;
self.socket.on('connect', function() { self.socket.on('connect', function() {
alert('CONNECTED!');
self.socket.on('disconnect', function() { self.socket.on('disconnect', function() {
alert('DISCONNECTED');
self.cleanUp(); self.cleanUp();
}); });
if (typeof cb === 'function') cb(); if (typeof cb === 'function') cb();
}); });
self.socket.on('message', self._onMessage); self.socket.on('message', self._onMessage.bind(self));
self.socket.on('error', self._handleError); self.socket.on('error', self._onError.bind(self));
}; };
Network.prototype._handleError = function(err) { Network.prototype._onError = function(err) {
console.log('RECV ERROR: ', err); console.log('RECV ERROR: ', err);
console.log(err.stack);
this.criticalError = err.message; this.criticalError = err.message;
}; };
Network.prototype.greet = function(copayerId) {
this._sendHello(copayerId);
var peerId = this.peerFromCopayer(copayerId);
this._addCopayerMap(peerId, copayerId);
};
Network.prototype._addCopayerMap = function(peerId, copayerId) { Network.prototype._addCopayerMap = function(peerId, copayerId) {
if (!this.copayerForPeer[peerId]) { if (!this.copayerForPeer[peerId]) {
if (Object.keys(this.copayerForPeer).length < this.maxPeers) { if (Object.keys(this.copayerForPeer).length < this.maxPeers) {
@ -264,8 +266,8 @@ Network.prototype._addCopayerMap = function(peerId, copayerId) {
} }
}; };
Network.prototype._setInboundPeerAuth = function(peerId, isAuthenticated) { Network.prototype._setInboundPeerAuth = function(peerId) {
this.isInboundPeerAuth[peerId] = isAuthenticated; this.isInboundPeerAuth[peerId] = true;
}; };
Network.prototype.setCopayerId = function(copayerId) { Network.prototype.setCopayerId = function(copayerId) {
@ -311,12 +313,12 @@ Network.prototype.start = function(opts, openCallback) {
} }
this.socket = io.connect(this.host + ':' + this.port, { this.socket = io.connect(this.host + ':' + this.port, {
//reconnection: false, reconnection: false,
}); });
this._setupConnectionHandlers(openCallback); this._setupConnectionHandlers(openCallback);
var pubkey = this.getKey().public.toString('hex'); var pubkey = this.getKey().public.toString('hex');
this.socket.emit('subscribe', pubkey); this.socket.emit('subscribe', pubkey);
this.socket.emit('sync'); //this.socket.emit('sync');
this.started = true; this.started = true;
//this.emit('serverError', self.criticalError); //this.emit('serverError', self.criticalError);
@ -333,7 +335,7 @@ Network.prototype.getPeer = function() {
Network.prototype.send = function(copayerIds, payload, cb) { Network.prototype.send = function(copayerIds, payload, cb) {
if (!payload) return cb(); preconditions.checkArgument(payload);
var self = this; var self = this;
if (!copayerIds) { if (!copayerIds) {
@ -353,9 +355,11 @@ Network.prototype.send = function(copayerIds, payload, cb) {
}; };
var copayerIdBuf = new Buffer(copayerId, 'hex'); var copayerIdBuf = new Buffer(copayerId, 'hex');
var message = AuthMessage.encode(copayerIdBuf, self.getKey(), payload, opts); var message = AuthMessage.encode(copayerIdBuf, self.getKey(), payload, opts);
console.log(JSON.stringify(payload));
console.log(JSON.stringify(message));
self.socket.emit('message', message); self.socket.emit('message', message);
if (++i === l && typeof cb === 'function') cb();
}); });
if (typeof cb === 'function') cb();
}; };

View File

@ -15,8 +15,7 @@
"mocha-lcov-reporter": "0.0.1", "mocha-lcov-reporter": "0.0.1",
"optimist": "^0.6.1", "optimist": "^0.6.1",
"preconditions": "^1.0.7", "preconditions": "^1.0.7",
"sinon": "1.9.1", "sinon": "1.9.1"
"socket.io-browserify": "^0.9.6"
}, },
"scripts": { "scripts": {
"shell": "node shell/scripts/launch.js", "shell": "node shell/scripts/launch.js",
@ -67,7 +66,7 @@
"mocha-lcov-reporter": "0.0.1", "mocha-lcov-reporter": "0.0.1",
"mock-fs": "^2.3.1", "mock-fs": "^2.3.1",
"node-cryptojs-aes": "0.4.0", "node-cryptojs-aes": "0.4.0",
"socket.io-client": "^1.0.6", "socket.io-client": "1.0.6",
"soop": "0.1.5", "soop": "0.1.5",
"travis-cov": "0.2.5", "travis-cov": "0.2.5",
"uglifyify": "1.2.3" "uglifyify": "1.2.3"

View File

@ -16,11 +16,6 @@ describe('Network / Async', function() {
describe('#Async constructor', function() { describe('#Async constructor', function() {
it('should set reconnect attempts', function() {
var n = new Async();
n.reconnectAttempts.should.equal(3);
});
it('should call cleanUp', function() { it('should call cleanUp', function() {
var save = Async.prototype.cleanUp; var save = Async.prototype.cleanUp;
Async.prototype.cleanUp = sinon.spy(); Async.prototype.cleanUp = sinon.spy();
@ -53,7 +48,6 @@ describe('Network / Async', function() {
}); });
}); });
describe('#send', function() { describe('#send', function() {
it('should call _sendToOne for a copayer', function(done) { it('should call _sendToOne for a copayer', function(done) {
@ -208,7 +202,9 @@ describe('Network / Async', function() {
var messagestr = JSON.stringify(message); var messagestr = JSON.stringify(message);
var messagebuf = new Buffer(messagestr); var messagebuf = new Buffer(messagestr);
var opts = {nonce: new Buffer('0000000000000001', 'hex')}; //message send with new nonce var opts = {
nonce: new Buffer('0000000000000001', 'hex')
}; //message send with new nonce
var encoded = n._encode(key2.public, key1, messagebuf, opts); var encoded = n._encode(key2.public, key1, messagebuf, opts);
var encodedstr = JSON.stringify(encoded); var encodedstr = JSON.stringify(encoded);
var encodeduint = new Buffer(encodedstr); var encodeduint = new Buffer(encodedstr);
@ -236,7 +232,9 @@ describe('Network / Async', function() {
var messagestr = JSON.stringify(message); var messagestr = JSON.stringify(message);
var messagebuf = new Buffer(messagestr); var messagebuf = new Buffer(messagestr);
var opts = {nonce: new Buffer('5000000000000002', 'hex')}; //message send with new nonce var opts = {
nonce: new Buffer('5000000000000002', 'hex')
}; //message send with new nonce
var encoded = n._encode(key2.public, key1, messagebuf, opts); var encoded = n._encode(key2.public, key1, messagebuf, opts);
var encodedstr = JSON.stringify(encoded); var encodedstr = JSON.stringify(encoded);
var encodeduint = new Buffer(encodedstr); var encodeduint = new Buffer(encodedstr);
@ -263,7 +261,9 @@ describe('Network / Async', function() {
var messagestr = JSON.stringify(message); var messagestr = JSON.stringify(message);
var messagebuf = new Buffer(messagestr); var messagebuf = new Buffer(messagestr);
var opts = {nonce: new Buffer('5000000000000002', 'hex')}; //message send with new nonce var opts = {
nonce: new Buffer('5000000000000002', 'hex')
}; //message send with new nonce
var encoded = n._encode(key2.public, key1, messagebuf, opts); var encoded = n._encode(key2.public, key1, messagebuf, opts);
var encodedstr = JSON.stringify(encoded); var encodedstr = JSON.stringify(encoded);
var encodeduint = new Buffer(encodedstr); var encodeduint = new Buffer(encodedstr);
@ -290,7 +290,9 @@ describe('Network / Async', function() {
var messagestr = JSON.stringify(message); var messagestr = JSON.stringify(message);
var messagebuf = new Buffer(messagestr); var messagebuf = new Buffer(messagestr);
var opts = {nonce: new Buffer('0000000000000001', 'hex')}; //message send with old nonce var opts = {
nonce: new Buffer('0000000000000001', 'hex')
}; //message send with old nonce
var encoded = n._encode(key2.public, key1, messagebuf, opts); var encoded = n._encode(key2.public, key1, messagebuf, opts);
var encodedstr = JSON.stringify(encoded); var encodedstr = JSON.stringify(encoded);
var encodeduint = new Buffer(encodedstr); var encodeduint = new Buffer(encodedstr);
@ -317,7 +319,9 @@ describe('Network / Async', function() {
var messagestr = JSON.stringify(message); var messagestr = JSON.stringify(message);
var messagebuf = new Buffer(messagestr); var messagebuf = new Buffer(messagestr);
var opts = {nonce: new Buffer('5000000000000001', 'hex')}; //message send with old nonce var opts = {
nonce: new Buffer('5000000000000001', 'hex')
}; //message send with old nonce
var encoded = n._encode(key2.public, key1, messagebuf, opts); var encoded = n._encode(key2.public, key1, messagebuf, opts);
var encodedstr = JSON.stringify(encoded); var encodedstr = JSON.stringify(encoded);
var encodeduint = new Buffer(encodedstr); var encodeduint = new Buffer(encodedstr);
@ -334,7 +338,7 @@ describe('Network / Async', function() {
}); });
describe('#setHexNonce', function() { describe('#setHexNonce', function() {
it('should set a nonce from a hex value', function() { it('should set a nonce from a hex value', function() {
var hex = '0000000000000000'; var hex = '0000000000000000';
var n = new Async(); var n = new Async();
@ -346,18 +350,20 @@ describe('Network / Async', function() {
}); });
describe('#setHexNonces', function() { describe('#setHexNonces', function() {
it('should set a nonce from a hex value', function() { it('should set a nonce from a hex value', function() {
var hex = '0000000000000000'; var hex = '0000000000000000';
var n = new Async(); var n = new Async();
n.setHexNonces({fakeid: hex}); n.setHexNonces({
fakeid: hex
});
n.getHexNonces().fakeid.should.equal(hex); n.getHexNonces().fakeid.should.equal(hex);
}); });
}); });
describe('#getHexNonce', function() { describe('#getHexNonce', function() {
it('should get a nonce hex value', function() { it('should get a nonce hex value', function() {
var hex = '0000000000000000'; var hex = '0000000000000000';
var n = new Async(); var n = new Async();
@ -368,11 +374,13 @@ describe('Network / Async', function() {
}); });
describe('#getHexNonces', function() { describe('#getHexNonces', function() {
it('should get a nonce from a hex value', function() { it('should get a nonce from a hex value', function() {
var hex = '0000000000000000'; var hex = '0000000000000000';
var n = new Async(); var n = new Async();
n.setHexNonces({fakeid: hex}); n.setHexNonces({
fakeid: hex
});
n.getHexNonces().fakeid.should.equal(hex); n.getHexNonces().fakeid.should.equal(hex);
}); });
@ -399,7 +407,7 @@ describe('Network / Async', function() {
var n = new Async(); var n = new Async();
n.iterateNonce(); n.iterateNonce();
var buf = new Buffer(4); var buf = new Buffer(4);
buf.writeUInt32BE(Math.floor(Date.now()/1000), 0); buf.writeUInt32BE(Math.floor(Date.now() / 1000), 0);
n.networkNonce[0].should.equal(buf[0]); n.networkNonce[0].should.equal(buf[0]);
}); });