mirror of https://github.com/BTCPrivate/copay.git
peerId hashing WIP2
This commit is contained in:
parent
88d758a793
commit
c1881d5fbb
20
index.html
20
index.html
|
@ -57,8 +57,14 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div ng-if='$root.wallet && !$root.wallet.publicKeyRing.isComplete() && !loading' data-alert class="alert-box info round" >
|
<div ng-if='$root.wallet && !$root.wallet.publicKeyRing.isComplete() && !loading' data-alert class="alert-box warn round size-18" >
|
||||||
<i class="fi-alert size-18"></i>
|
|
||||||
|
|
||||||
|
<div style="float:left; margin:0 15px">
|
||||||
|
<i class="fi-alert size-60"></i>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="size-18">
|
||||||
Note: Your wallet is not complete yet.
|
Note: Your wallet is not complete yet.
|
||||||
<span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()>1">
|
<span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()>1">
|
||||||
{{$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers() }} keys are
|
{{$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers() }} keys are
|
||||||
|
@ -66,7 +72,15 @@
|
||||||
<span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()==1">
|
<span ng-show="$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers()==1">
|
||||||
One key is
|
One key is
|
||||||
</span>
|
</span>
|
||||||
missing. Share this secret with your other copayers for them to join your wallet: <b>{{$root.wallet.getMyPeerId()}}</b>
|
missing.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="size-18">
|
||||||
|
Share this secret with your other copayers for them to join your wallet <br>
|
||||||
|
</div>
|
||||||
|
<div class="size-24">
|
||||||
|
<b>{{$root.wallet.getMyCopayerId()}}</b>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,6 @@ PrivateKey.prototype.getId = function(prefix) {
|
||||||
return hash.substring(0, hash.length/2);
|
return hash.substring(0, hash.length/2);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
PrivateKey.fromObj = function(obj) {
|
PrivateKey.fromObj = function(obj) {
|
||||||
return new PrivateKey(obj);
|
return new PrivateKey(obj);
|
||||||
};
|
};
|
||||||
|
|
|
@ -122,10 +122,10 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype._handleNetworkChange = function(newPeerId) {
|
Wallet.prototype._handleNetworkChange = function(newCopayerId) {
|
||||||
if (newPeerId) {
|
if (newPeerId) {
|
||||||
this.log('#### Setting new PEER:', newPeerId);
|
this.log('#### Setting new PEER:', newCopayerId);
|
||||||
this.sendWalletId(newPeerId);
|
this.sendWalletId(newCopayerId);
|
||||||
}
|
}
|
||||||
this.emit('refresh');
|
this.emit('refresh');
|
||||||
};
|
};
|
||||||
|
@ -143,13 +143,13 @@ Wallet.prototype._optsToObj = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype.getPeerId = function(index) {
|
Wallet.prototype.getCopayerId = function(index) {
|
||||||
return this.publicKeyRing.getCopayerId(index || 0);
|
return this.publicKeyRing.getCopayerId(index || 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype.getMyPeerId = function() {
|
Wallet.prototype.getMyCopayerId = function() {
|
||||||
return this.getPeerId(0);
|
return this.getCopayerId(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.netStart = function() {
|
Wallet.prototype.netStart = function() {
|
||||||
|
@ -167,19 +167,21 @@ Wallet.prototype.netStart = function() {
|
||||||
self.emit('close');
|
self.emit('close');
|
||||||
});
|
});
|
||||||
|
|
||||||
var myPeerId = self.getMyPeerId();
|
var myId = self.getMyCopayerId();
|
||||||
var startOpts = {
|
var startOpts = {
|
||||||
peerId: myPeerId
|
copayerId: myId
|
||||||
};
|
};
|
||||||
net.start(function() {
|
net.start(function() {
|
||||||
self.emit('created');
|
self.emit('created');
|
||||||
for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) {
|
for (var i=0; i<self.publicKeyRing.registeredCopayers(); i++) {
|
||||||
var otherPeerId = self.getPeerId(i);
|
var otherId = self.getCopayerId(i);
|
||||||
if (otherPeerId !== myPeerId) {
|
if (otherId !== myId) {
|
||||||
net.connectTo(otherPeerId);
|
net.connectTo(otherId);
|
||||||
|
}
|
||||||
|
if (self.firstCopayerId){
|
||||||
|
self.sendWalletReady(self.firstCopayerId);
|
||||||
|
self.firstCopayerId = null;
|
||||||
}
|
}
|
||||||
self.sendWalletReady(self.firstPeerId);
|
|
||||||
self.firstPeerId = null;
|
|
||||||
self.emit('refresh');
|
self.emit('refresh');
|
||||||
}
|
}
|
||||||
}, startOpts);
|
}, startOpts);
|
||||||
|
@ -279,8 +281,8 @@ Wallet.prototype.getTxProposals = function() {
|
||||||
var ret = [];
|
var ret = [];
|
||||||
for(var k in this.txProposals.txps) {
|
for(var k in this.txProposals.txps) {
|
||||||
var i = this.txProposals.getTxProposal(k);
|
var i = this.txProposals.getTxProposal(k);
|
||||||
i.signedByUs = i.signedBy[this.getMyPeerId()]?true:false;
|
i.signedByUs = i.signedBy[this.getMyCopayerId()]?true:false;
|
||||||
i.rejectedByUs = i.rejectedBy[this.getMyPeerId()]?true:false;
|
i.rejectedByUs = i.rejectedBy[this.getMyCopayerId()]?true:false;
|
||||||
if (this.totalCopayers-i.rejectCount < this.requiredCopayers)
|
if (this.totalCopayers-i.rejectCount < this.requiredCopayers)
|
||||||
i.finallyRejected=true;
|
i.finallyRejected=true;
|
||||||
|
|
||||||
|
@ -291,7 +293,7 @@ Wallet.prototype.getTxProposals = function() {
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype.reject = function(ntxid) {
|
Wallet.prototype.reject = function(ntxid) {
|
||||||
var myId=this.getMyPeerId();
|
var myId=this.getMyCopayerId();
|
||||||
var txp = this.txProposals.txps[ntxid];
|
var txp = this.txProposals.txps[ntxid];
|
||||||
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
|
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
|
||||||
|
|
||||||
|
@ -303,7 +305,7 @@ Wallet.prototype.reject = function(ntxid) {
|
||||||
|
|
||||||
Wallet.prototype.sign = function(ntxid) {
|
Wallet.prototype.sign = function(ntxid) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var myId=this.getMyPeerId();
|
var myId=this.getMyCopayerId();
|
||||||
var txp = self.txProposals.txps[ntxid];
|
var txp = self.txProposals.txps[ntxid];
|
||||||
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
|
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
|
||||||
|
|
||||||
|
@ -350,7 +352,7 @@ Wallet.prototype.sendTx = function(ntxid, cb) {
|
||||||
|
|
||||||
Wallet.prototype.addSeenToTxProposals = function() {
|
Wallet.prototype.addSeenToTxProposals = function() {
|
||||||
var ret=false;
|
var ret=false;
|
||||||
var myId=this.getMyPeerId();
|
var myId=this.getMyCopayerId();
|
||||||
|
|
||||||
for(var k in this.txProposals.txps) {
|
for(var k in this.txProposals.txps) {
|
||||||
var txp = this.txProposals.txps[k];
|
var txp = this.txProposals.txps[k];
|
||||||
|
@ -489,7 +491,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
|
||||||
if (priv) {
|
if (priv) {
|
||||||
b.sign( priv.getAll(pkr.addressIndex, pkr.changeAddressIndex) );
|
b.sign( priv.getAll(pkr.addressIndex, pkr.changeAddressIndex) );
|
||||||
}
|
}
|
||||||
var myId = this.getMyPeerId();
|
var myId = this.getMyCopayerId();
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
|
|
||||||
var me = {};
|
var me = {};
|
||||||
|
|
|
@ -139,21 +139,21 @@ WalletFactory.prototype.remove = function(walletId) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
WalletFactory.prototype.joinCreateSession = function(peerId, cb) {
|
WalletFactory.prototype.joinCreateSession = function(copayerId, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
//Create our PrivateK
|
//Create our PrivateK
|
||||||
var privateKey = new PrivateKey({ networkName: this.networkName });
|
var privateKey = new PrivateKey({ networkName: this.networkName });
|
||||||
this.log('\t### PrivateKey Initialized');
|
this.log('\t### PrivateKey Initialized');
|
||||||
self.network.setPeerId(privateKey.getId());
|
self.network.setCopayerId(privateKey.getId());
|
||||||
|
|
||||||
self.network.start(function() {
|
self.network.start(function() {
|
||||||
self.network.connectTo(peerId);
|
self.network.connectTo(copayerId);
|
||||||
self.network.on('data', function(sender, data) {
|
self.network.on('data', function(sender, data) {
|
||||||
if (data.type ==='walletId') {
|
if (data.type ==='walletId') {
|
||||||
data.opts.privateKey = privateKey;
|
data.opts.privateKey = privateKey;
|
||||||
var w = self.open(data.walletId, data.opts);
|
var w = self.open(data.walletId, data.opts);
|
||||||
w.firstPeerId = peerId;
|
w.firstCopayerId = copayerId;
|
||||||
return cb(w);
|
return cb(w);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
||||||
|
var bitcore = require('bitcore');
|
||||||
|
var util = bitcore.util;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Emits
|
* Emits
|
||||||
|
@ -11,7 +13,6 @@ var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
||||||
* when an unknown data type arrives
|
* when an unknown data type arrives
|
||||||
*
|
*
|
||||||
* Provides
|
* Provides
|
||||||
* send(toPeerIds, {data}, cb?)
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ function Network(opts) {
|
||||||
this.maxPeers = opts.maxPeers || 10;
|
this.maxPeers = opts.maxPeers || 10;
|
||||||
this.opts = { key: opts.key };
|
this.opts = { key: opts.key };
|
||||||
this.connections = {};
|
this.connections = {};
|
||||||
|
this.copayerForPeer = {};
|
||||||
|
|
||||||
// For using your own peerJs server
|
// For using your own peerJs server
|
||||||
['port', 'host', 'path', 'debug'].forEach(function(k) {
|
['port', 'host', 'path', 'debug'].forEach(function(k) {
|
||||||
|
@ -69,6 +71,16 @@ Network._arrayRemove = function(el, array) {
|
||||||
return array;
|
return array;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Network.prototype.connectedCopayers = function() {
|
||||||
|
var ret =[];
|
||||||
|
for(var i in this.connectedPeers){
|
||||||
|
var copayerId =this.copayerForPeer[this.connectedPeers[i]];
|
||||||
|
if (copayerId) ret.push(copayerId);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
|
||||||
Network.prototype._onClose = function(peerId) {
|
Network.prototype._onClose = function(peerId) {
|
||||||
console.log('[WebRTC.js.72:_onClose:]');
|
console.log('[WebRTC.js.72:_onClose:]');
|
||||||
|
|
||||||
|
@ -77,9 +89,22 @@ console.log('[WebRTC.js.72:_onClose:]');
|
||||||
this._notifyNetworkChange();
|
this._notifyNetworkChange();
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype._connectToPeers = function(peerIds) {
|
Network.prototype._connectToCopayers = function(copayerIds) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var ret = false;
|
var ret = false;
|
||||||
|
|
||||||
|
var peerIds = [];
|
||||||
|
for(var i in copayerIds) {
|
||||||
|
if (!copayerIds[i]) continue;
|
||||||
|
|
||||||
|
var peerId = this.peerFromCopayer(copayerIds[i]);
|
||||||
|
peerIds.push(peerId);
|
||||||
|
|
||||||
|
this.copayerForPeer[peerId] = copayerIds[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[WebRTC.js.102] copayerForPeer:', this.copayerForPeer); //TODO
|
||||||
|
|
||||||
var arrayDiff1= Network._arrayDiff(peerIds, this.connectedPeers);
|
var arrayDiff1= Network._arrayDiff(peerIds, this.connectedPeers);
|
||||||
var arrayDiff = Network._arrayDiff(arrayDiff1, [this.peerId]);
|
var arrayDiff = Network._arrayDiff(arrayDiff1, [this.peerId]);
|
||||||
arrayDiff.forEach(function(peerId) {
|
arrayDiff.forEach(function(peerId) {
|
||||||
|
@ -90,7 +115,35 @@ Network.prototype._connectToPeers = function(peerIds) {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype._onData = function(data, isInbound) {
|
Network.prototype._sendCopayerId = function(copayerId) {
|
||||||
|
console.log('#### SENDING COPAYERID TO TO ', copayerId);
|
||||||
|
this.send(copayerId, {
|
||||||
|
type: 'copayerId',
|
||||||
|
copayerId: this.copayerId,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Network.prototype._sendCopayers = function(copayerIds) {
|
||||||
|
console.log('#### SENDING PEER LIST: ', this.connectedPeers,this.connectedCopayers(), ' TO ', copayerIds?copayerIds: 'ALL');
|
||||||
|
this.send(copayerIds, {
|
||||||
|
type: 'copayers',
|
||||||
|
copayers: this.connectedCopayers(),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Network.prototype._addCopayer = function(copayerId, isInbound) {
|
||||||
|
var hasChanged = Network._arrayPushOnce(this.peerFromCopayer(copayerId), this.connectedPeers);
|
||||||
|
if (isInbound && hasChanged) {
|
||||||
|
this._sendCopayers(); //broadcast peer list
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (isInbound) {
|
||||||
|
this._sendCopayers(copayerId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Network.prototype._onData = function(data, isInbound, peerId) {
|
||||||
var obj;
|
var obj;
|
||||||
try {
|
try {
|
||||||
obj = JSON.parse(data);
|
obj = JSON.parse(data);
|
||||||
|
@ -99,40 +152,41 @@ Network.prototype._onData = function(data, isInbound) {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender, obj.data);
|
console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender, obj.data);
|
||||||
|
var self=this;
|
||||||
|
|
||||||
|
if(obj.data.type === 'copayerId') {
|
||||||
|
if (this.peerFromCopayer(obj.data.copayerId) === peerId) {
|
||||||
|
console.log('#### Peer sent the right copayerId. Setting it up.'); //TODO
|
||||||
|
this.copayerForPeer[peerId]=obj.data.copayerId;
|
||||||
|
this._addCopayer(obj.data.copayerId, isInbound);
|
||||||
|
this._notifyNetworkChange( isInbound ? obj.data.copayerId : null);
|
||||||
|
this.emit('open');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log('### RECEIVED WRONG COPAYER ID FROM:', peerId); //TODO
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.copayerForPeer[peerId]) {
|
||||||
|
console.log('### Discarting message from unknow peer: ', peerId); //TODO
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch(obj.data.type) {
|
switch(obj.data.type) {
|
||||||
case 'peerList':
|
case 'copayers':
|
||||||
this._connectToPeers(obj.data.peers);
|
this._connectToCopayers(obj.data.copayers);
|
||||||
this._notifyNetworkChange();
|
this._notifyNetworkChange();
|
||||||
break;
|
break;
|
||||||
case 'disconnect':
|
case 'disconnect':
|
||||||
this._onClose(obj.sender);
|
this._onClose(obj.sender);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.emit('data', obj.sender, obj.data, isInbound);
|
this.emit('data', self.copayerForPeer[obj.sender], obj.data, isInbound);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype._sendPeers = function(peerIds) {
|
|
||||||
console.log('#### SENDING PEER LIST: ', this.connectedPeers, ' TO ', peerIds?peerIds: 'ALL');
|
|
||||||
this.send(peerIds, {
|
|
||||||
type: 'peerList',
|
|
||||||
peers: this.connectedPeers,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Network.prototype._addPeer = function(peerId, isInbound) {
|
|
||||||
|
|
||||||
var hasChanged = Network._arrayPushOnce(peerId, this.connectedPeers);
|
|
||||||
if (isInbound && hasChanged) {
|
|
||||||
this._sendPeers(); //broadcast peer list
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (isInbound) {
|
|
||||||
this._sendPeers(peerId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Network.prototype._checkAnyPeer = function() {
|
Network.prototype._checkAnyPeer = function() {
|
||||||
if (!this.connectedPeers.length) {
|
if (!this.connectedPeers.length) {
|
||||||
|
@ -148,17 +202,17 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
|
||||||
if (!Network._inArray(dataConn.peer, self.connectedPeers)) {
|
if (!Network._inArray(dataConn.peer, self.connectedPeers)) {
|
||||||
self.connections[dataConn.peer] = dataConn;
|
self.connections[dataConn.peer] = dataConn;
|
||||||
|
|
||||||
console.log('### DATA CONNECTION READY TO: ADDING PEER: %s (inbound: %s)',
|
console.log('### DATA CONNECTION READY: ADDING PEER: %s (inbound: %s)',
|
||||||
dataConn.peer, isInbound);
|
dataConn.peer, isInbound);
|
||||||
|
|
||||||
self._addPeer(dataConn.peer, isInbound);
|
// The outbount send its copayerID
|
||||||
self._notifyNetworkChange( isInbound ? dataConn.peer : null);
|
if(!isInbound)
|
||||||
this.emit('open');
|
self._sendCopayerId(self.copayerForPeer[dataConn.peer]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
dataConn.on('data', function(data) {
|
dataConn.on('data', function(data) {
|
||||||
self._onData(data, isInbound);
|
self._onData(data, isInbound, dataConn.peer);
|
||||||
});
|
});
|
||||||
|
|
||||||
dataConn.on('error', function(e) {
|
dataConn.on('error', function(e) {
|
||||||
|
@ -177,9 +231,9 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype._notifyNetworkChange = function(newPeer) {
|
Network.prototype._notifyNetworkChange = function(newCopayerId) {
|
||||||
console.log('[WebRTC.js.164:_notifyNetworkChange:]', newPeer); //TODO
|
console.log('[WebRTC.js.164:_notifyNetworkChange:]', newCopayerId); //TODO
|
||||||
this.emit('networkChange', newPeer);
|
this.emit('networkChange', newCopayerId);
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype._setupPeerHandlers = function(openCallback) {
|
Network.prototype._setupPeerHandlers = function(openCallback) {
|
||||||
|
@ -213,24 +267,29 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype.setPeerId = function(peerId) {
|
Network.prototype.setCopayerId = function(copayerId) {
|
||||||
if (this.started) {
|
if (this.started) {
|
||||||
throw new Error ('network already started: can not change peerId')
|
throw new Error ('network already started: can not change peerId')
|
||||||
}
|
}
|
||||||
this.peerId = peerId;
|
this.copayerId = copayerId;
|
||||||
|
this.peerId = this.peerFromCopayer(this.copayerId);
|
||||||
|
|
||||||
|
console.log('[WebRTC.js.275] setCopayerId', copayerId, this.peerId ); //TODO
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Network.prototype.peerFromCopayer = function(hex) {
|
||||||
|
return util.sha256(new Buffer(hex,'hex')).toString('hex');
|
||||||
|
};
|
||||||
|
|
||||||
Network.prototype.start = function(openCallback, opts) {
|
Network.prototype.start = function(openCallback, opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
// Start PeerJS Peer
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (this.started) return openCallback();
|
if (this.started) return openCallback();
|
||||||
|
|
||||||
opts.connectedPeers = opts.connectedPeers || [];
|
opts.connectedPeers = opts.connectedPeers || [];
|
||||||
this.peerId = this.peerId || opts.peerId;
|
|
||||||
|
|
||||||
|
|
||||||
|
this.setCopayerId(this.copayerId || opts.copayerId);
|
||||||
this.peer = new Peer(this.peerId, this.opts);
|
this.peer = new Peer(this.peerId, this.opts);
|
||||||
this._setupPeerHandlers(openCallback);
|
this._setupPeerHandlers(openCallback);
|
||||||
for (var i = 0; i<opts.connectedPeers.length; i++) {
|
for (var i = 0; i<opts.connectedPeers.length; i++) {
|
||||||
|
@ -238,11 +297,12 @@ Network.prototype.start = function(openCallback, opts) {
|
||||||
this.connectTo(otherPeerId);
|
this.connectTo(otherPeerId);
|
||||||
}
|
}
|
||||||
this.started = true;
|
this.started = true;
|
||||||
|
|
||||||
console.log('[WebRTC.js.237] started TRUE'); //TODO
|
console.log('[WebRTC.js.237] started TRUE'); //TODO
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype._sendToOne = function(peerId, data, cb) {
|
Network.prototype._sendToOne = function(copayerId, data, cb) {
|
||||||
|
|
||||||
|
var peerId = this.peerFromCopayer(copayerId);
|
||||||
if (peerId !== this.peerId) {
|
if (peerId !== this.peerId) {
|
||||||
var dataConn = this.connections[peerId];
|
var dataConn = this.connections[peerId];
|
||||||
if (dataConn) {
|
if (dataConn) {
|
||||||
|
@ -259,36 +319,34 @@ console.log('[WebRTC.js.255] WARN: NO CONNECTION TO:', peerId); //TODO
|
||||||
if (typeof cb === 'function') cb();
|
if (typeof cb === 'function') cb();
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype.send = function(peerIds, data, cb) {
|
Network.prototype.send = function(copayerIds, data, cb) {
|
||||||
var self=this;
|
var self=this;
|
||||||
if (!peerIds) {
|
if (!copayerIds) {
|
||||||
peerIds = this.connectedPeers;
|
copayerIds = this.connectedCopayers();
|
||||||
data.isBroadcast = 1;
|
data.isBroadcast = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(peerIds)) {
|
if (Array.isArray(copayerIds)) {
|
||||||
var l = peerIds.length;
|
var l = copayerIds.length;
|
||||||
var i = 0;
|
var i = 0;
|
||||||
peerIds.forEach(function(peerId) {
|
copayerIds.forEach(function(copayerId) {
|
||||||
console.log('[WebRTC.js.258:peerId:]',peerId); //TODO
|
self._sendToOne(copayerId, data, function () {
|
||||||
self._sendToOne(peerId, data, function () {
|
|
||||||
if (++i === l && typeof cb === 'function') cb();
|
if (++i === l && typeof cb === 'function') cb();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (typeof peerIds === 'string')
|
else if (typeof copayerIds === 'string')
|
||||||
self._sendToOne(peerIds, data, cb);
|
self._sendToOne(copayerIds, data, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
Network.prototype.connectTo = function(peerId) {
|
Network.prototype.connectTo = function(copayerId) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var peerId = this.peerFromCopayer(copayerId);
|
||||||
console.log('### STARTING CONNECTION TO:' + peerId );
|
this.copayerForPeer[peerId] = copayerId;
|
||||||
|
console.log('### STARTING CONNECTION TO:', peerId, copayerId);
|
||||||
var dataConn = this.peer.connect(peerId, {
|
var dataConn = this.peer.connect(peerId, {
|
||||||
serialization: 'none',
|
serialization: 'none',
|
||||||
reliable: true,
|
reliable: true,
|
||||||
metadata: { message: 'hi copayer!' }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self._setupConnectionHandlers(dataConn, false);
|
self._setupConnectionHandlers(dataConn, false);
|
||||||
|
|
Loading…
Reference in New Issue