mirror of https://github.com/BTCPrivate/copay.git
fixing stuff
This commit is contained in:
parent
7dc37a272a
commit
48dd8549ef
|
@ -5,7 +5,7 @@ var PublicKeyRing = require('./PublicKeyRing');
|
||||||
var PrivateKey = require('./PrivateKey');
|
var PrivateKey = require('./PrivateKey');
|
||||||
var Wallet = require('./Wallet');
|
var Wallet = require('./Wallet');
|
||||||
|
|
||||||
var WebRTC = module.exports.WebRTC = require('../network/WebRTC');
|
var Async = module.exports.Async = require('../network/Async');
|
||||||
var Insight = module.exports.Insight = require('../blockchain/Insight');
|
var Insight = module.exports.Insight = require('../blockchain/Insight');
|
||||||
var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('../storage/LocalEncrypted');
|
var StorageLocalEncrypted = module.exports.StorageLocalEncrypted = require('../storage/LocalEncrypted');
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ function WalletFactory(config, version) {
|
||||||
config = config || {};
|
config = config || {};
|
||||||
|
|
||||||
this.Storage = config.Storage || StorageLocalEncrypted;
|
this.Storage = config.Storage || StorageLocalEncrypted;
|
||||||
this.Network = config.Network || WebRTC;
|
this.Network = config.Network || Async;
|
||||||
this.Blockchain = config.Blockchain || Insight;
|
this.Blockchain = config.Blockchain || Insight;
|
||||||
|
|
||||||
this.storage = new this.Storage(config.storage);
|
this.storage = new this.Storage(config.storage);
|
||||||
|
|
|
@ -7,6 +7,7 @@ var AuthMessage = bitcore.AuthMessage;
|
||||||
var util = bitcore.util;
|
var util = bitcore.util;
|
||||||
var extend = require('util')._extend;
|
var extend = require('util')._extend;
|
||||||
var io = require('socket.io-client');
|
var io = require('socket.io-client');
|
||||||
|
var preconditions = require('preconditions').singleton();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Emits
|
* Emits
|
||||||
|
@ -26,8 +27,6 @@ function Network(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
this.host = opts.host || 'localhost';
|
this.host = opts.host || 'localhost';
|
||||||
this.port = opts.port || 3001;
|
this.port = opts.port || 3001;
|
||||||
this.retryDelay = opts.retryDelay || 3000;
|
|
||||||
this.reconnectAttempts = opts.reconnectAttempts || 3;
|
|
||||||
this.cleanUp();
|
this.cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +45,6 @@ Network.prototype.cleanUp = function() {
|
||||||
this.connections = {};
|
this.connections = {};
|
||||||
this.criticalErr = '';
|
this.criticalErr = '';
|
||||||
this.closing = 0;
|
this.closing = 0;
|
||||||
this.tries = 0;
|
|
||||||
this.removeAllListeners();
|
this.removeAllListeners();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -232,7 +230,7 @@ Network.prototype._onMessage = function(enc) {
|
||||||
this._onClose(peerId);
|
this._onClose(peerId);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.emit('data', self.copayerForPeer[peerId], payload, isInbound);
|
this.emit('data', self.copayerForPeer[peerId], payload);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -314,38 +312,22 @@ Network.prototype.start = function(opts, openCallback) {
|
||||||
if (!this.copayerId)
|
if (!this.copayerId)
|
||||||
this.setCopayerId(opts.copayerId);
|
this.setCopayerId(opts.copayerId);
|
||||||
|
|
||||||
var self = this;
|
if (this.connectedPeers.length > 0) return; // Already connected!
|
||||||
var setupPeer = function() {
|
if (this.socket) {
|
||||||
if (self.connectedPeers.length > 0) return; // Already connected!
|
this.socket.destroy();
|
||||||
if (self.socket) {
|
this.socket.removeAllListeners();
|
||||||
self.socket.destroy();
|
|
||||||
self.socket.removeAllListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self.criticalError && self.tries < self.reconnectAttempts) {
|
|
||||||
self.tries++;
|
|
||||||
self.opts.token = util.sha256(self.peerId).toString('hex');
|
|
||||||
self.socket = io.connect(self.host, {
|
|
||||||
port: self.port
|
|
||||||
});
|
|
||||||
self.socket.emit('subscribe', pubkey);
|
|
||||||
self.socket.emit('sync', ts);
|
|
||||||
self.started = true;
|
|
||||||
self._setupConnectionHandlers(self.socket, copayerId);
|
|
||||||
|
|
||||||
setTimeout(setupPeer, self.retryDelay); // Schedule retry
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (self.criticalError && self.criticalError.match(/taken/)) {
|
|
||||||
self.criticalError = ' Looks like you are already connected to this wallet please close all other Copay Wallets '
|
|
||||||
}
|
|
||||||
|
|
||||||
self.emit('serverError', self.criticalError);
|
|
||||||
self.cleanUp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tries = 0;
|
this.socket = io.connect(this.host, {
|
||||||
setupPeer();
|
port: this.port
|
||||||
|
});
|
||||||
|
this.socket.emit('subscribe', this.getKey().public.toString('hex'));
|
||||||
|
this.socket.emit('sync');
|
||||||
|
this.started = true;
|
||||||
|
this._setupConnectionHandlers(this.socket);
|
||||||
|
|
||||||
|
//this.emit('serverError', self.criticalError);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype.getOnlinePeerIDs = function() {
|
Network.prototype.getOnlinePeerIDs = function() {
|
||||||
|
|
|
@ -54,105 +54,6 @@ describe('Network / Async', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('#_setupPeerHandlers', function() {
|
|
||||||
var n = new Async();
|
|
||||||
n.peer = {};
|
|
||||||
var spy = n.peer.on = sinon.spy();
|
|
||||||
it('should setup handlers', function() {
|
|
||||||
n._setupPeerHandlers();
|
|
||||||
spy.calledWith('connection').should.equal(true);
|
|
||||||
spy.calledWith('open').should.equal(true);
|
|
||||||
spy.calledWith('error').should.equal(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#_handlePeerOpen', function() {
|
|
||||||
var n = new Async();
|
|
||||||
it('should call openCallback handler', function(done) {
|
|
||||||
n.peerId = 1;
|
|
||||||
n.copayerId = 2;
|
|
||||||
n._handlePeerOpen(function() {
|
|
||||||
n.connectedPeers.should.deep.equal([1]);
|
|
||||||
n.copayerForPeer.should.deep.equal({
|
|
||||||
1: 2
|
|
||||||
});
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#_handlePeerError', function() {
|
|
||||||
var log = console.log;
|
|
||||||
var n = new Async();
|
|
||||||
it('should call _checkAnyPeer on could not connect error', function() {
|
|
||||||
var save = n._checkAnyPeer;
|
|
||||||
var spy = n._checkAnyPeer = sinon.spy();
|
|
||||||
var logSpy = console.log = sinon.spy();
|
|
||||||
n._handlePeerError({
|
|
||||||
message: 'Could not connect to peer xxx'
|
|
||||||
});
|
|
||||||
console.log = log;
|
|
||||||
spy.called.should.equal(true);
|
|
||||||
logSpy.called.should.equal(true);
|
|
||||||
n._checkAnyPeer = save;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should call not call _checkAnyPeer other error', function() {
|
|
||||||
var save = n._checkAnyPeer;
|
|
||||||
var spy = n._checkAnyPeer = sinon.spy();
|
|
||||||
var otherMessage = 'Could connect to peer xxx';
|
|
||||||
var logSpy = console.log = sinon.spy();
|
|
||||||
n._handlePeerError({
|
|
||||||
message: otherMessage,
|
|
||||||
});
|
|
||||||
console.log = log;
|
|
||||||
spy.called.should.equal(false);
|
|
||||||
n.criticalError.should.equal(otherMessage);
|
|
||||||
logSpy.called.should.equal(true);
|
|
||||||
n._checkAnyPeer = save;
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
describe('#_encode', function() {
|
|
||||||
|
|
||||||
it('should encode data successfully', function() {
|
|
||||||
var n = new Async();
|
|
||||||
var data = new bitcore.Buffer('my data to encode');
|
|
||||||
var privkeystr = new bitcore.Buffer('test privkey');
|
|
||||||
var privkey = bitcore.util.sha256(privkeystr);
|
|
||||||
var key = new bitcore.Key();
|
|
||||||
key.private = privkey;
|
|
||||||
key.regenerateSync();
|
|
||||||
var encoded = n._encode(key.public, key, data);
|
|
||||||
should.exist(encoded);
|
|
||||||
encoded.sig.length.should.not.equal(0);
|
|
||||||
encoded.pubkey.length.should.not.equal(0);
|
|
||||||
encoded.encrypted.length.should.not.equal(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#_decode', function() {
|
|
||||||
|
|
||||||
it('should decode that which was encoded', function() {
|
|
||||||
var n = new Async();
|
|
||||||
var data = new bitcore.Buffer('my data to encrypt');
|
|
||||||
var privkeystr = new bitcore.Buffer('test privkey');
|
|
||||||
var privkey = bitcore.util.sha256(privkeystr);
|
|
||||||
var key = new bitcore.Key();
|
|
||||||
key.private = privkey;
|
|
||||||
key.regenerateSync();
|
|
||||||
var encoded = n._encode(key.public, key, data);
|
|
||||||
var decoded = n._decode(key, encoded);
|
|
||||||
encoded.sig.should.not.equal(0);
|
|
||||||
decoded.payload.toString().should.equal('my data to encrypt');
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#send', function() {
|
describe('#send', function() {
|
||||||
|
|
||||||
it('should call _sendToOne for a copayer', function(done) {
|
it('should call _sendToOne for a copayer', function(done) {
|
||||||
|
|
Loading…
Reference in New Issue