fixes in networking
This commit is contained in:
parent
405f97d765
commit
6e07d64832
8 changed files with 87 additions and 56 deletions
|
|
@ -83,8 +83,11 @@ PublicKeyRing.prototype.getCopayerId = function(i, prefix) {
|
|||
buf = Buffer.concat([prefix, buf]);
|
||||
}
|
||||
return util.ripe160(buf).toString('hex');
|
||||
}
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.myCopayerId = function(i, prefix) {
|
||||
return this.getCopayerId(0,prefix);
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.registeredCopayers = function () {
|
||||
return this.copayersBIP32.length;
|
||||
|
|
|
|||
|
|
@ -97,6 +97,7 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
|
|||
};
|
||||
|
||||
Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
||||
console.log('_handleData: senderId:',senderId); //TODO
|
||||
|
||||
if (this.id !== data.walletId) {
|
||||
this.emit('badMessage',senderId);
|
||||
|
|
@ -136,19 +137,27 @@ Wallet.prototype._optsToObj = function () {
|
|||
};
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
Wallet.prototype.getPeerId = function(index) {
|
||||
// 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);
|
||||
//
|
||||
var idBuf;
|
||||
// TODO idBuf DISABLED FOR NOW
|
||||
//idBuf = new Buffer(this.id);
|
||||
return this.publicKeyRing.getCopayerId(index || 0, idBuf);
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.getMyPeerId = function() {
|
||||
return this.getPeerId(0);
|
||||
};
|
||||
|
||||
Wallet.prototype.netStart = function() {
|
||||
console.log('[Wallet.js.159:netStart:]'); //TODO
|
||||
var self = this;
|
||||
var net = this.network;
|
||||
net.removeAllListeners();
|
||||
|
|
@ -162,15 +171,17 @@ Wallet.prototype.netStart = function() {
|
|||
net.on('close', function() {
|
||||
self.emit('close');
|
||||
});
|
||||
|
||||
var myPeerId = self.getMyPeerId();
|
||||
var startOpts = {
|
||||
peerId: self.generatePeerId()
|
||||
}
|
||||
net.start(function(peerId) {
|
||||
peerId: myPeerId
|
||||
};
|
||||
net.start(function() {
|
||||
console.log('[Wallet.js.177] NET START: emit CREATED'); //TODO
|
||||
self.emit('created');
|
||||
var myId = self.generatePeerId();
|
||||
for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) {
|
||||
var otherPeerId = self.generatePeerId(i);
|
||||
if (otherPeerId !== myId) {
|
||||
var otherPeerId = self.getPeerId(i);
|
||||
if (otherPeerId !== myPeerId) {
|
||||
net.connectTo(otherPeerId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,10 +39,13 @@ WalletFactory.prototype.log = function(){
|
|||
|
||||
WalletFactory.prototype._checkRead = function(walletId) {
|
||||
var s = this.storage;
|
||||
var ret = s.get(walletId, 'publicKeyRing') &&
|
||||
s.get(walletId, 'txProposals') &&
|
||||
s.get(walletId, 'opts') &&
|
||||
s.get(walletId, 'privateKey')
|
||||
var ret =
|
||||
(
|
||||
s.get(walletId, 'publicKeyRing') &&
|
||||
s.get(walletId, 'txProposals') &&
|
||||
s.get(walletId, 'opts') &&
|
||||
s.get(walletId, 'privateKey')
|
||||
)?true:false;
|
||||
;
|
||||
return ret?true:false;
|
||||
};
|
||||
|
|
@ -53,17 +56,14 @@ WalletFactory.prototype.read = function(walletId) {
|
|||
|
||||
var s = this.storage;
|
||||
var opts = s.get(walletId, 'opts');
|
||||
|
||||
opts.id = walletId;
|
||||
opts.publicKeyRing = new PublicKeyRing.fromObj(s.get(walletId, 'publicKeyRing'));
|
||||
opts.txProposals = new TxProposals.fromObj(s.get(walletId, 'txProposals'));
|
||||
opts.privateKey = new PrivateKey.fromObj(s.get(walletId, 'privateKey'));
|
||||
|
||||
opts.storage = this.storage;
|
||||
opts.network = this.network;
|
||||
opts.blockchain = this.blockchain;
|
||||
opts.verbose = this.verbose;
|
||||
|
||||
var w = new Wallet(opts);
|
||||
|
||||
// JIC: Add our key
|
||||
|
|
@ -81,10 +81,13 @@ WalletFactory.prototype.read = function(walletId) {
|
|||
WalletFactory.prototype.create = function(opts) {
|
||||
var s = WalletFactory.storage;
|
||||
opts = opts || {};
|
||||
this.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
|
||||
this.log('### CREATING NEW WALLET.' +
|
||||
(opts.id ? ' USING ID: ' + opts.id : ' NEW ID') +
|
||||
(opts.privateKey ? ' USING PrivateKey: ' + opts.privateKey.getId() : ' NEW PrivateKey')
|
||||
);
|
||||
|
||||
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;
|
||||
|
|
@ -134,12 +137,19 @@ WalletFactory.prototype.remove = function(walletId) {
|
|||
};
|
||||
|
||||
|
||||
WalletFactory.prototype.connectTo = function(peerId, cb) {
|
||||
var self=this;
|
||||
WalletFactory.prototype.joinCreateSession = function(peerId, cb) {
|
||||
var self = this;
|
||||
|
||||
//Create our PrivateK
|
||||
var privateKey = new PrivateKey({ networkName: this.networkName });
|
||||
this.log('\t### PrivateKey Initialized');
|
||||
self.network.setPeerId(privateKey.getId());
|
||||
|
||||
self.network.start(function() {
|
||||
self.network.connectTo(peerId)
|
||||
self.network.connectTo(peerId);
|
||||
self.network.on('walletId', function(data) {
|
||||
return cb(data);
|
||||
data.opts.privateKey = privateKey;
|
||||
return cb(self.open(data.walletId, data.opts));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -182,10 +182,11 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
var self=this;
|
||||
var p = this.peer;
|
||||
|
||||
p.on('open', function(peerId) {
|
||||
self.peerId = peerId;
|
||||
self.connectedPeers = [peerId];
|
||||
return openCallback(peerId);
|
||||
p.on('open', function() {
|
||||
self.connectedPeers = [self.peerId];
|
||||
|
||||
console.log('[WebRTC.js.187] LENGTH', self.connectedPeers.length); //TODO
|
||||
return openCallback();
|
||||
});
|
||||
|
||||
p.on('error', function(err) {
|
||||
|
|
@ -210,23 +211,21 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
});
|
||||
};
|
||||
|
||||
Network.prototype.setPeerId = function(peerId) {
|
||||
if (this.started) {
|
||||
throw new Error ('network already started: can not change peerId')
|
||||
}
|
||||
this.peerId = peerId;
|
||||
};
|
||||
|
||||
|
||||
Network.prototype.start = function(openCallback, opts) {
|
||||
opts = opts || {};
|
||||
// Start PeerJS Peer
|
||||
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;
|
||||
}
|
||||
|
||||
if (this.started) return openCallback();
|
||||
|
||||
|
||||
opts = opts || {};
|
||||
opts.connectedPeers = opts.connectedPeers || [];
|
||||
this.peerId = this.peerId || opts.peerId;
|
||||
|
||||
|
|
@ -237,6 +236,8 @@ Network.prototype.start = function(openCallback, opts) {
|
|||
this.connectTo(otherPeerId);
|
||||
}
|
||||
this.started = true;
|
||||
|
||||
console.log('[WebRTC.js.237] started TRUE'); //TODO
|
||||
};
|
||||
|
||||
Network.prototype._sendToOne = function(peerId, data, cb) {
|
||||
|
|
@ -283,7 +284,7 @@ console.log('[WebRTC.js.258:peerId:]',peerId); //TODO
|
|||
Network.prototype.connectTo = function(peerId) {
|
||||
var self = this;
|
||||
|
||||
console.log('### STARTING TO CONNECT TO:' + peerId );
|
||||
console.log('### STARTING CONNECTION TO:' + peerId );
|
||||
|
||||
var dataConn = this.peer.connect(peerId, {
|
||||
serialization: 'none',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue