Merge pull request #95 from maraoz/feature/auto-peer-discovery
auto peer discovery
This commit is contained in:
commit
0a188532b9
14 changed files with 148 additions and 106 deletions
|
|
@ -61,7 +61,7 @@
|
|||
<span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()==1">
|
||||
One key is
|
||||
</span>
|
||||
missing. Ask your copayers to join your session: <b>{{$root.wallet.network.peerId}}</b>
|
||||
missing. Share this secret with your other copayers for them to join your wallet: <b>{{$root.wallet.network.peerId}}</b>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -81,8 +81,9 @@
|
|||
<div ng-show="!loading">
|
||||
<div class="row">
|
||||
<div class="large-6 columns">
|
||||
<h3>Join a Network Wallet</h3>
|
||||
<input type="text" class="form-control" placeholder="Peer ID" ng-model="connectionId" autofocus>
|
||||
<h3>Join Wallet Creation</h3>
|
||||
<input type="text" class="form-control" placeholder="Paste secret here"
|
||||
ng-model="connectionId" autofocus>
|
||||
</div>
|
||||
<div class="large-3 columns">
|
||||
<button class="button primary expand round" type="button" ng-click="join(connectionId)">Join</button>
|
||||
|
|
@ -109,7 +110,7 @@
|
|||
<div ng-show="walletIds.length>0">
|
||||
<div class="row">
|
||||
<div class="large-6 columns">
|
||||
<h3>Open a Existing Wallet</h3>
|
||||
<h3>Open Existing Wallet</h3>
|
||||
<select class="form-control" ng-model="selectedWalletId" ng-options="walletId for walletId in walletIds">
|
||||
</select>
|
||||
</div>
|
||||
|
|
|
|||
20
js/config.js
20
js/config.js
|
|
@ -3,13 +3,13 @@
|
|||
var config = {
|
||||
networkName: 'testnet',
|
||||
network: {
|
||||
key: 'lwjd5qra8257b9',
|
||||
// key: 'lwjd5qra8257b9',
|
||||
// This is for running local peerJs with params: ./peerjs -p 10009 -k 'sdfjhwefh'
|
||||
// key: 'sdfjhwefh',
|
||||
// host: 'localhost',
|
||||
// port: 10009,
|
||||
// path: '/',
|
||||
maxPeers: 3,
|
||||
key: 'sdfjhwefh',
|
||||
host: '192.168.1.100',
|
||||
port: 10009,
|
||||
path: '/',
|
||||
maxPeers: 10,
|
||||
debug: 3,
|
||||
},
|
||||
limits: {
|
||||
|
|
@ -23,12 +23,12 @@ var config = {
|
|||
verbose: 1,
|
||||
},
|
||||
blockchain: {
|
||||
host: 'localhost',
|
||||
port: 3001
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
},
|
||||
socket: {
|
||||
host: 'localhost',
|
||||
port: 3001
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
},
|
||||
verbose: 1,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ angular.module('copay.setup').controller('SetupController',
|
|||
};
|
||||
var w = walletFactory.create(opts);
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
};
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -17,12 +17,11 @@ angular.module('copay.signin').controller('SigninController',
|
|||
$location.path('setup');
|
||||
};
|
||||
|
||||
$scope.open = function(walletId) {
|
||||
$scope.open = function(walletId, opts) {
|
||||
$scope.loading = true;
|
||||
|
||||
var w = walletFactory.open(walletId);
|
||||
var w = walletFactory.open(walletId, opts);
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
};
|
||||
|
||||
$scope.join = function(cid) {
|
||||
|
|
@ -31,9 +30,8 @@ angular.module('copay.signin').controller('SigninController',
|
|||
controllerUtils.onError($scope);
|
||||
$rootScope.$digest();
|
||||
});
|
||||
walletFactory.connectTo(cid, function(w) {
|
||||
controllerUtils.setupUxHandlers(w);
|
||||
w.netStart();
|
||||
walletFactory.connectTo(cid, function(data) {
|
||||
$scope.open(data.walletId, data.opts);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -16,13 +16,17 @@ function PrivateKey(opts) {
|
|||
var init = opts.extendedPrivateKeyString || this.network.name;
|
||||
this.bip = opts.BIP32 || new BIP32(init);
|
||||
this.privateKeyCache = opts.privateKeyCache || {};
|
||||
this._calcId();
|
||||
};
|
||||
|
||||
PrivateKey.prototype._calcId = function() {
|
||||
this.id = util.ripe160(this.bip.extendedPublicKey).toString('hex');
|
||||
PrivateKey.prototype.getId = function(prefix) {
|
||||
var buf = this.bip.extendedPublicKey;
|
||||
if (prefix) {
|
||||
buf = Buffer.concat([prefix, buf]);
|
||||
}
|
||||
return util.ripe160(buf).toString('hex');
|
||||
};
|
||||
|
||||
|
||||
PrivateKey.fromObj = function(obj) {
|
||||
return new PrivateKey(obj);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ var Address = bitcore.Address;
|
|||
var Script = bitcore.Script;
|
||||
var coinUtil = bitcore.util;
|
||||
var Transaction = bitcore.Transaction;
|
||||
var util = bitcore.util;
|
||||
|
||||
var Storage = imports.Storage || require('../storage/Base.js');
|
||||
var storage = Storage.default();
|
||||
|
|
@ -76,6 +77,14 @@ PublicKeyRing.prototype.serialize = function () {
|
|||
return JSON.stringify(this.toObj());
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getCopayerId = function(i, prefix) {
|
||||
var buf = this.copayersBIP32[i].extendedPublicKey;
|
||||
if (prefix) {
|
||||
buf = Buffer.concat([prefix, buf]);
|
||||
}
|
||||
return util.ripe160(buf).toString('hex');
|
||||
}
|
||||
|
||||
|
||||
PublicKeyRing.prototype.registeredCopayers = function () {
|
||||
return this.copayersBIP32.length;
|
||||
|
|
@ -225,12 +234,12 @@ PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
|
|||
if (
|
||||
this.requiredCopayers && inPKR.requiredCopayers &&
|
||||
(this.requiredCopayers !== inPKR.requiredCopayers))
|
||||
throw new Error('inPRK requiredCopayers mismatch');
|
||||
throw new Error('inPRK requiredCopayers mismatch '+this.requiredCopayers+'!='+inPKR.requiredCopayers);
|
||||
|
||||
if (
|
||||
this.totalCopayers && inPKR.totalCopayers &&
|
||||
(this.totalCopayers !== inPKR.totalCopayers))
|
||||
throw new Error('inPRK requiredCopayers mismatch');
|
||||
throw new Error('inPRK totalCopayers mismatch'+this.totalCopayers+'!='+inPKR.requiredCopayers);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,9 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
|
|||
var shouldSend = false;
|
||||
var recipients, pkr = this.publicKeyRing;
|
||||
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
|
||||
if (pkr.merge(inPKR, true) && !data.isBroadcast) {
|
||||
|
||||
var hasChanged = pkr.merge(inPKR, true);
|
||||
if (hasChanged && !data.isBroadcast) {
|
||||
this.log('### BROADCASTING PKR');
|
||||
recipients = null;
|
||||
shouldSend = true;
|
||||
|
|
@ -133,6 +135,19 @@ Wallet.prototype._optsToObj = function () {
|
|||
return obj;
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.generatePeerId = function(index) {
|
||||
var idBuf = new Buffer(this.id);
|
||||
if (typeof index === 'undefined') {
|
||||
// return my own peerId
|
||||
var gen = this.privateKey.getId(idBuf);
|
||||
return gen;
|
||||
}
|
||||
// return peer number 'index' peerId
|
||||
return this.publicKeyRing.getCopayerId(index, idBuf);
|
||||
|
||||
};
|
||||
|
||||
Wallet.prototype.netStart = function() {
|
||||
var self = this;
|
||||
var net = this.network;
|
||||
|
|
@ -141,15 +156,25 @@ Wallet.prototype.netStart = function() {
|
|||
net.on('data', self._handleData.bind(self) );
|
||||
net.on('open', function() {}); // TODO
|
||||
net.on('openError', function() {
|
||||
this.log('[Wallet.js.132:openError:] GOT openError'); //TODO
|
||||
self.log('[Wallet.js.132:openError:] GOT openError'); //TODO
|
||||
self.emit('openError');
|
||||
});
|
||||
net.on('close', function() {
|
||||
self.emit('close');
|
||||
});
|
||||
var startOpts = {
|
||||
peerId: self.generatePeerId()
|
||||
}
|
||||
net.start(function(peerId) {
|
||||
self.emit('created');
|
||||
});
|
||||
var myId = self.generatePeerId();
|
||||
for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) {
|
||||
var otherPeerId = self.generatePeerId(i);
|
||||
if (otherPeerId !== myId) {
|
||||
net.connectTo(otherPeerId);
|
||||
}
|
||||
}
|
||||
}, startOpts);
|
||||
};
|
||||
|
||||
Wallet.prototype.store = function(isSync) {
|
||||
|
|
@ -207,6 +232,7 @@ Wallet.prototype.sendWalletId = function(recipients) {
|
|||
this.network.send(recipients, {
|
||||
type: 'walletId',
|
||||
walletId: this.id,
|
||||
opts: this._optsToObj()
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -236,7 +262,7 @@ Wallet.prototype.getTxProposals = function() {
|
|||
self.txProposals.txps.forEach(function(txp) {
|
||||
var i = {txp:txp};
|
||||
i.ntxid = txp.builder.build().getNormalizedHash();
|
||||
i.signedByUs = txp.signedBy[self.privateKey.id]?true:false;
|
||||
i.signedByUs = txp.signedBy[self.privateKey.getId()]?true:false;
|
||||
ret.push(i);
|
||||
});
|
||||
return ret;
|
||||
|
|
@ -267,7 +293,7 @@ Wallet.prototype.sign = function(ntxid) {
|
|||
var ret = txp.builder.sign(keys);
|
||||
|
||||
if (ret.signaturesAdded) {
|
||||
txp.signedBy[this.privateKey.id] = Date.now();
|
||||
txp.signedBy[this.privateKey.getId()] = Date.now();
|
||||
this.log('[Wallet.js.230:ret:]',ret); //TODO
|
||||
if (ret.isFullySigned) {
|
||||
this.log('[Wallet.js.231] BROADCASTING TX!!!'); //TODO
|
||||
|
|
@ -294,8 +320,8 @@ Wallet.prototype.addSeenToTxProposals = function() {
|
|||
var self=this;
|
||||
|
||||
this.txProposals.txps.forEach(function(txp) {
|
||||
if (!txp.seenBy[self.privateKey.id]) {
|
||||
txp.seenBy[self.privateKey.id] = Date.now();
|
||||
if (!txp.seenBy[self.privateKey.getId()]) {
|
||||
txp.seenBy[self.privateKey.getId()] = Date.now();
|
||||
ret = true;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -115,48 +115,12 @@ WalletFactory.prototype.create = function(opts) {
|
|||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.open = function(walletId) {
|
||||
var w = this.read(walletId) || this.create({
|
||||
id: walletId,
|
||||
verbose: this.verbose,
|
||||
});
|
||||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.openRemote = function(peedId) {
|
||||
var s = WalletFactory.storage;
|
||||
opts = opts || {};
|
||||
this.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
|
||||
|
||||
opts.privateKey = opts.privateKey || new PrivateKey({ networkName: this.networkName });
|
||||
this.log('\t### PrivateKey Initialized');
|
||||
|
||||
var requiredCopayers = opts.requiredCopayers || this.walletDefaults.requiredCopayers;
|
||||
var totalCopayers = opts.totalCopayers || this.walletDefaults.totalCopayers;
|
||||
|
||||
opts.publicKeyRing = opts.publicKeyRing || new PublicKeyRing({
|
||||
networkName: this.networkName,
|
||||
requiredCopayers: requiredCopayers,
|
||||
totalCopayers: totalCopayers,
|
||||
});
|
||||
opts.publicKeyRing.addCopayer(opts.privateKey.getExtendedPublicKeyString());
|
||||
this.log('\t### PublicKeyRing Initialized');
|
||||
|
||||
opts.txProposals = opts.txProposals || new TxProposals({
|
||||
networkName: this.networkName,
|
||||
});
|
||||
this.log('\t### TxProposals Initialized');
|
||||
|
||||
opts.storage = this.storage;
|
||||
opts.network = this.network;
|
||||
opts.blockchain = this.blockchain;
|
||||
|
||||
opts.spendUnconfirmed = typeof opts.spendUnconfirmed === undefined
|
||||
?this.walletDefaults.spendUnconfirmed : opts.spendUnconfirmed;
|
||||
|
||||
opts.requiredCopayers = requiredCopayers;
|
||||
opts.totalCopayers = totalCopayers;
|
||||
var w = new Wallet(opts);
|
||||
WalletFactory.prototype.open = function(walletId, opts) {
|
||||
this.log('Opening walletId:' + walletId);
|
||||
opts = opts || {};
|
||||
opts.id = walletId;
|
||||
opts.verbose = this.verbose;
|
||||
var w = this.read(walletId) || this.create(opts);
|
||||
w.store();
|
||||
return w;
|
||||
};
|
||||
|
|
@ -174,9 +138,8 @@ WalletFactory.prototype.connectTo = function(peerId, cb) {
|
|||
var self=this;
|
||||
self.network.start(function() {
|
||||
self.network.connectTo(peerId)
|
||||
self.network.on('walletId', function(walletId) {
|
||||
self.log('Opening walletId:' + walletId);
|
||||
return cb(self.open(walletId));
|
||||
self.network.on('walletId', function(data) {
|
||||
return cb(data);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
|
||||
|
||||
function Peer(id) {
|
||||
this.id = id;
|
||||
};
|
||||
|
||||
|
||||
module.exports = require('soop')(Peer);
|
||||
|
|
@ -21,7 +21,7 @@ function Network(opts) {
|
|||
this.peerId = opts.peerId;
|
||||
this.apiKey = opts.apiKey || 'lwjd5qra8257b9';
|
||||
this.debug = opts.debug || 3;
|
||||
this.maxPeers = opts.maxPeers || 5;
|
||||
this.maxPeers = opts.maxPeers || 10;
|
||||
this.opts = { key: opts.key };
|
||||
|
||||
// For using your own peerJs server
|
||||
|
|
@ -29,6 +29,7 @@ function Network(opts) {
|
|||
if (opts[k]) self.opts[k]=opts[k];
|
||||
});
|
||||
this.connectedPeers = [];
|
||||
this.started = false;
|
||||
}
|
||||
|
||||
Network.parent=EventEmitter;
|
||||
|
|
@ -64,7 +65,6 @@ Network._arrayPushOnce = function(el, array) {
|
|||
Network._arrayRemove = function(el, array) {
|
||||
var pos = array.indexOf(el);
|
||||
if (pos >= 0) array.splice(pos, 1);
|
||||
|
||||
return array;
|
||||
};
|
||||
|
||||
|
|
@ -105,7 +105,7 @@ Network.prototype._onData = function(data, isInbound) {
|
|||
this._onClose(obj.sender);
|
||||
break;
|
||||
case 'walletId':
|
||||
this.emit('walletId', obj.data.walletId);
|
||||
this.emit('walletId', obj.data);
|
||||
break;
|
||||
default:
|
||||
this.emit('data', obj.sender, obj.data, isInbound);
|
||||
|
|
@ -123,8 +123,6 @@ Network.prototype._sendPeers = function(peerIds) {
|
|||
Network.prototype._addPeer = function(peerId, isInbound) {
|
||||
|
||||
var hasChanged = Network._arrayPushOnce(peerId, this.connectedPeers);
|
||||
|
||||
|
||||
if (isInbound && hasChanged) {
|
||||
this._sendPeers(); //broadcast peer list
|
||||
}
|
||||
|
|
@ -187,15 +185,12 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
p.on('open', function(peerId) {
|
||||
self.peerId = peerId;
|
||||
self.connectedPeers = [peerId];
|
||||
self._notifyNetworkChange();
|
||||
return openCallback(peerId);
|
||||
});
|
||||
|
||||
p.on('error', function(err) {
|
||||
console.log('### PEER ERROR:', err);
|
||||
self.peer.disconnect();
|
||||
self.peer.destroy();
|
||||
self.peer = null;
|
||||
//self.disconnect(null, true); // force disconnect
|
||||
self._checkAnyPeer();
|
||||
});
|
||||
|
||||
|
|
@ -215,12 +210,33 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
});
|
||||
};
|
||||
|
||||
Network.prototype.start = function(openCallback) {
|
||||
Network.prototype.start = function(openCallback, opts) {
|
||||
opts = opts || {};
|
||||
// Start PeerJS Peer
|
||||
if (this.peer) return openCallback(); // This is for connectTo-> peer is started before
|
||||
var self = this;
|
||||
if (this.started) {
|
||||
// network already started, restarting network layer
|
||||
opts.connectedPeers = this.connectedPeers;
|
||||
Network._arrayRemove(this.peerId, opts.connectedPeers);
|
||||
this.disconnect(function() {
|
||||
self.start(openCallback, opts);
|
||||
}, true); // fast disconnect
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
opts = opts || {};
|
||||
opts.connectedPeers = opts.connectedPeers || [];
|
||||
this.peerId = this.peerId || opts.peerId;
|
||||
|
||||
this.peer = new Peer(this.peerId, this.opts);
|
||||
this._setupPeerHandlers(openCallback);
|
||||
for (var i = 0; i<opts.connectedPeers.length; i++) {
|
||||
var otherPeerId = opts.connectedPeers[i];
|
||||
this.connectTo(otherPeerId);
|
||||
}
|
||||
this.started = true;
|
||||
};
|
||||
|
||||
Network.prototype._sendToOne = function(peerId, data, cb) {
|
||||
|
|
@ -279,11 +295,12 @@ Network.prototype.connectTo = function(peerId) {
|
|||
};
|
||||
|
||||
|
||||
Network.prototype.disconnect = function(cb) {
|
||||
Network.prototype.disconnect = function(cb, forced) {
|
||||
var self = this;
|
||||
self.closing = 1;
|
||||
this.send(null, { type: 'disconnect' }, function() {
|
||||
var cleanUp = function() {
|
||||
self.connectedPeers = [];
|
||||
self.started = false;
|
||||
self.peerId = null;
|
||||
if (self.peer) {
|
||||
self.peer.disconnect();
|
||||
|
|
@ -292,7 +309,12 @@ Network.prototype.disconnect = function(cb) {
|
|||
}
|
||||
self.closing = 0;
|
||||
if (typeof cb === 'function') cb();
|
||||
});
|
||||
};
|
||||
if (!forced) {
|
||||
this.send(null, { type: 'disconnect' }, cleanUp);
|
||||
} else {
|
||||
cleanUp();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = require('soop')(Network);
|
||||
|
|
|
|||
|
|
@ -43,6 +43,8 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro
|
|||
});
|
||||
w.on('openError', root.onErrorDigest);
|
||||
w.on('close', root.onErrorDigest);
|
||||
|
||||
w.netStart();
|
||||
};
|
||||
|
||||
root.handleTransactionByAddress = function(scope, cb) {
|
||||
|
|
|
|||
27
test/mocks/FakeNetwork.js
Normal file
27
test/mocks/FakeNetwork.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
var imports = require('soop').imports();
|
||||
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
||||
|
||||
function Network(opts) {
|
||||
}
|
||||
|
||||
Network.parent=EventEmitter;
|
||||
|
||||
Network.prototype.start = function(openCallback, opts) {
|
||||
// start! :D
|
||||
};
|
||||
|
||||
Network.prototype.send = function(peerIds, data, cb) {
|
||||
// send! c:
|
||||
};
|
||||
|
||||
Network.prototype.connectTo = function(peerId) {
|
||||
// connect C:
|
||||
};
|
||||
|
||||
|
||||
Network.prototype.disconnect = function(cb) {
|
||||
// disconect :c
|
||||
};
|
||||
|
||||
module.exports = require('soop')(Network);
|
||||
|
|
@ -69,16 +69,15 @@ describe('PrivateKey model', function() {
|
|||
|
||||
it('should calculate .id', function () {
|
||||
var w1 = new PrivateKey(config);
|
||||
should.exist(w1.id);
|
||||
w1.id.length.should.equal(40);
|
||||
should.exist(w1.getId());
|
||||
w1.getId().length.should.equal(40);
|
||||
});
|
||||
it('fromObj toObj roundtrip', function () {
|
||||
var w1 = new PrivateKey(config);
|
||||
var w2 = PrivateKey.fromObj(w1.toObj());
|
||||
|
||||
w2.toObj().extendedPrivateKeyString.should.equal(w1.toObj().extendedPrivateKeyString);
|
||||
w2.id.should.equal(w1.id);
|
||||
|
||||
w2.getId().should.equal(w1.getId());
|
||||
|
||||
JSON.stringify(w2.get(1,1).storeObj()).should
|
||||
.equal(JSON.stringify(w1.get(1,1).storeObj()));
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@
|
|||
var chai = chai || require('chai');
|
||||
var should = chai.should();
|
||||
|
||||
var WebRTC = require('../js/models/network/WebRTC');
|
||||
var FakeNetwork = require('./mocks/FakeNetwork');
|
||||
var Insight = require('../js/models/blockchain/Insight');
|
||||
var FakeStorage = require('./mocks/FakeStorage');
|
||||
|
||||
var WalletFactory = typeof copay === 'undefined' ? require('soop').load('../js/models/core/WalletFactory',{
|
||||
Network: WebRTC,
|
||||
Network: FakeNetwork,
|
||||
Blockchain: Insight,
|
||||
Storage: FakeStorage,
|
||||
}) : copay.WalletFactory;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue