automatic peer discovery

This commit is contained in:
Manuel Araoz 2014-04-18 18:25:51 -03:00
commit 4ba65dd4e1
9 changed files with 103 additions and 44 deletions

View file

@ -21,7 +21,7 @@ function PrivateKey(opts) {
PrivateKey.prototype.getId = function(prefix) {
var buf = this.bip.extendedPublicKey;
if (prefix) {
buf = Buffer.concat([prefix, this.bip.extendedPublicKey]);
buf = Buffer.concat([prefix, buf]);
}
return util.ripe160(buf).toString('hex');
};

View file

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

View file

@ -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;
@ -115,8 +117,11 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
Wallet.prototype._handleNetworkChange = function(newPeer) {
if (newPeer) {
this.log('#### Setting new PEER:', newPeer);
console.log('sending wallet id');
this.sendWalletId(newPeer);
console.log('sending pubkeyring');
this.sendPublicKeyRing(newPeer);
console.log('sending tx proposals');
this.sendTxProposals(newPeer);
}
this.emit('refresh');
@ -134,10 +139,16 @@ Wallet.prototype._optsToObj = function () {
};
Wallet.prototype.generatePeerId = function() {
var gen = this.privateKey.getId(new Buffer(this.id));
console.log(gen);
return gen;
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() {
@ -148,17 +159,26 @@ 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: this.generatePeerId()
peerId: self.generatePeerId()
}
console.log('STARTING NETWORK WITH PEER ID: '+startOpts.peerId);
net.start(function(peerId) {
self.emit('created');
console.log('CREATEEEEEEEEEEEEEEEEEEEEEEd');
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);
};
@ -217,6 +237,7 @@ Wallet.prototype.sendWalletId = function(recipients) {
this.network.send(recipients, {
type: 'walletId',
walletId: this.id,
opts: this._optsToObj()
});
};

View file

@ -75,7 +75,6 @@ WalletFactory.prototype.read = function(walletId) {
this.log('NOT NECCESARY AN ERROR:', e); //TODO
}
this.log('### WALLET OPENED:', w.id);
w.netStart();
return w;
};
@ -113,16 +112,16 @@ WalletFactory.prototype.create = function(opts) {
opts.totalCopayers = totalCopayers;
var w = new Wallet(opts);
w.store();
w.netStart();
return w;
};
WalletFactory.prototype.open = function(walletId) {
WalletFactory.prototype.open = function(walletId, opts) {
this.log('Opening walletId:' + walletId);
var w = this.read(walletId) || this.create({
id: walletId,
verbose: this.verbose,
});
opts = opts || {};
opts.id = walletId;
opts.verbose = this.verbose;
var w = this.read(walletId) || this.create(opts);
w.store();
return w;
};
@ -139,8 +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) {
return cb(walletId);
self.network.on('walletId', function(data) {
return cb(data);
});
});
};