new WalletFactory
This commit is contained in:
parent
438ffe1ecc
commit
536fe90431
14 changed files with 462 additions and 349 deletions
|
|
@ -59,7 +59,7 @@ Insight.prototype.listUnspent = function(addresses, cb) {
|
|||
self._request(options, function(err, res) {
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
}
|
||||
callback();
|
||||
});
|
||||
}, function() {
|
||||
|
|
|
|||
|
|
@ -7,99 +7,134 @@ var coinUtil = bitcore.util;
|
|||
var buffertools = bitcore.buffertools;
|
||||
var Builder = bitcore.TransactionBuilder;
|
||||
var http = require('http');
|
||||
|
||||
var Storage = imports.Storage;
|
||||
var Network = imports.Network;
|
||||
var Blockchain = imports.Blockchain;
|
||||
|
||||
var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
||||
var copay = copay || require('../../../copay');
|
||||
|
||||
function Wallet(config) {
|
||||
this._startInterface(config);
|
||||
function Wallet(opts) {
|
||||
var self = this;
|
||||
|
||||
//required params
|
||||
['storage', 'network', 'blockchain',
|
||||
'requiredCopayers', 'totalCopayers', 'spendUnconfirmed',
|
||||
'publicKeyRing', 'txProposals', 'privateKey'
|
||||
].forEach( function(k){
|
||||
if (typeof opts[k] === 'undefined') throw new Error('missing key:' + k);
|
||||
self[k] = opts[k];
|
||||
});
|
||||
|
||||
this.id = opts.id || Wallet.getRandomId();
|
||||
this.publicKeyRing.walletId = this.id;
|
||||
this.txProposals.walletId = this.id;
|
||||
}
|
||||
|
||||
Wallet.parent=EventEmitter;
|
||||
Wallet.prototype.log = function(){
|
||||
if (!this.verbose) return;
|
||||
console.log(arguments);
|
||||
};
|
||||
|
||||
console.this.log(arguments);
|
||||
}
|
||||
Wallet.getRandomId = function() {
|
||||
var r = buffertools.toHex(coinUtil.generateNonce());
|
||||
return r;
|
||||
};
|
||||
|
||||
Wallet.prototype._startInterface = function(config) {
|
||||
this.storage = new Storage(config.storage);
|
||||
this.network = new Network(config.network);
|
||||
this.blockchain = new Blockchain(config.blockchain);
|
||||
Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
|
||||
this.openWalletId(data.walletId);
|
||||
this.log('RECV PUBLICKEYRING:',data);
|
||||
|
||||
this.networkName = config.networkName;
|
||||
this.requiredCopayers = config.wallet.requiredCopayers;
|
||||
this.totalCopayers = config.wallet.totalCopayers;
|
||||
var shouldSend = false;
|
||||
var recipients, pkr = this.publicKeyRing;
|
||||
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
|
||||
if (pkr.merge(inPKR, true) && !data.isBroadcast) {
|
||||
this.log('### BROADCASTING PKR');
|
||||
recipients = null;
|
||||
shouldSend = true;
|
||||
}
|
||||
else if (isInbound && !data.isBroadcast) {
|
||||
// always replying to connecting peer
|
||||
this.log('### REPLYING PKR TO:', senderId);
|
||||
recipients = senderId;
|
||||
shouldSend = true;
|
||||
}
|
||||
|
||||
if (shouldSend) {
|
||||
this.sendPublicKeyRing(recipients);
|
||||
}
|
||||
|
||||
this.store();
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.create = function(opts) {
|
||||
opts = opts || {};
|
||||
this.id = opts.id || Wallet.getRandomId();
|
||||
this.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
|
||||
Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
|
||||
this.openWalletId(data.walletId);
|
||||
this.log('RECV TXPROPOSAL:',data); //TODO
|
||||
|
||||
this.privateKey = new copay.PrivateKey({
|
||||
networkName: this.networkName
|
||||
});
|
||||
this.log('\t### PrivateKey Initialized');
|
||||
var shouldSend = false;
|
||||
var recipients;
|
||||
var inTxp = copay.TxProposals.fromObj(data.txProposals);
|
||||
var mergeInfo = this.txProposals.merge(inTxp, true);
|
||||
|
||||
this.publicKeyRing = new copay.PublicKeyRing({
|
||||
walletId: this.id,
|
||||
requiredCopayers: opts.requiredCopayers || this.requiredCopayers,
|
||||
totalCopayers: opts.totalCopayers || this.totalCopayers,
|
||||
networkName: this.networkName,
|
||||
});
|
||||
var addSeen = this.addSeenToTxProposals();
|
||||
if ((mergeInfo.merged && !data.isBroadcast) || addSeen) {
|
||||
this.log('### BROADCASTING txProposals. ' );
|
||||
recipients = null;
|
||||
shouldSend = true;
|
||||
}
|
||||
else if (isInbound && !data.isBroadcast) {
|
||||
// always replying to connecting peer
|
||||
this.log('### REPLYING txProposals TO:', senderId);
|
||||
recipients = senderId;
|
||||
shouldSend = true;
|
||||
}
|
||||
|
||||
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
|
||||
this.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.walletId);
|
||||
|
||||
this.txProposals = new copay.TxProposals({
|
||||
walletId: this.id,
|
||||
networkName: this.networkName,
|
||||
});
|
||||
this.log('\t### TxProposals Initialized');
|
||||
if (shouldSend)
|
||||
this.sendTxProposals(recipients);
|
||||
|
||||
this.store();
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype._checkLoad = function(walletId) {
|
||||
var ret = this.storage.get(walletId, 'publicKeyRing') &&
|
||||
this.storage.get(walletId, 'txProposals') &&
|
||||
this.storage.get(walletId, 'privateKey')
|
||||
;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Wallet.prototype.load = function(walletId) {
|
||||
if (! this._checkLoad(walletId)) return;
|
||||
|
||||
|
||||
this.id = walletId;
|
||||
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
|
||||
this.storage.get(this.id, 'publicKeyRing')
|
||||
);
|
||||
this.txProposals = new copay.TxProposals.fromObj(
|
||||
this.storage.get(this.id, 'txProposals')
|
||||
);
|
||||
this.privateKey = new copay.PrivateKey.fromObj(
|
||||
this.storage.get(this.id, 'privateKey')
|
||||
); //TODO secure
|
||||
|
||||
// JIC: Add our key
|
||||
try {
|
||||
this.publicKeyRing.addCopayer(
|
||||
this.privateKey.getBIP32().extendedPublicKeyString()
|
||||
);
|
||||
} catch (e) {
|
||||
this.log('NOT NECCESARY AN ERROR:', e); //TODO
|
||||
};
|
||||
Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
||||
switch(data.type) {
|
||||
case 'publicKeyRing':
|
||||
this._handlePublicKeyRing(senderId, data, isInbound);
|
||||
break;
|
||||
case 'txProposals':
|
||||
this._handleTxProposals(senderId, data, isInbound);
|
||||
break;
|
||||
case 'abort':
|
||||
this.emit('abort');
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
Wallet.prototype._handleNetworkChange = function(newPeer) {
|
||||
if (!newPeer) return;
|
||||
|
||||
this.log('#### Setting new PEER:', newPeer);
|
||||
this.sendPublicKeyRing(newPeer);
|
||||
this.sendTxProposals(newPeer);
|
||||
};
|
||||
|
||||
Wallet.prototype.netStart = function(cb) {
|
||||
var self = this;
|
||||
var net = this.network;
|
||||
net.on('networkChange', function() { self._handleNetworkChange() } );
|
||||
net.on('data', function() { self._handleData();});
|
||||
net.on('open', function() {}); // TODO
|
||||
net.on('close', function() {}); // TODO
|
||||
net.start(function(peerId) {
|
||||
self.emit('created');
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.store = function() {
|
||||
Wallet.factory.addWalletId(this.id);
|
||||
this.storage.set(this.id,'opts', {
|
||||
id: this.id,
|
||||
spendUnconfirmed: this.spendUnconfirmed,
|
||||
requiredCopayers: this.requiredCopayers,
|
||||
totalCopayers: this.totalCopayers,
|
||||
});
|
||||
this.storage.set(this.id,'publicKeyRing', this.publicKeyRing.toObj());
|
||||
this.storage.set(this.id,'txProposals', this.txProposals.toObj());
|
||||
this.storage.set(this.id,'privateKey', this.privateKey.toObj());
|
||||
|
|
@ -114,6 +149,7 @@ Wallet.prototype.sendTxProposals = function(recipients) {
|
|||
txProposals: this.txProposals.toObj(),
|
||||
walletId: this.id,
|
||||
});
|
||||
this.emit('txProposalsUpdated', this.txProposals);
|
||||
};
|
||||
|
||||
Wallet.prototype.sendPublicKeyRing = function(recipients) {
|
||||
|
|
@ -124,38 +160,68 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
|
|||
publicKeyRing: this.publicKeyRing.toObj(),
|
||||
walletId: this.id,
|
||||
});
|
||||
this.emit('publicKeyRingUpdated', this.publicKeyRing);
|
||||
};
|
||||
|
||||
Wallet.prototype.generateAddress = function() {
|
||||
var addr = this.publicKeyRing.generateAddress();
|
||||
this.store();
|
||||
|
||||
this.network.send(null, {
|
||||
type: 'publicKeyRing',
|
||||
publicKeyRing: this.publicKeyRing.toObj(),
|
||||
walletId: this.id
|
||||
});
|
||||
|
||||
this.sendPublicKeyRing();
|
||||
return addr;
|
||||
};
|
||||
|
||||
Wallet.prototype.getTxProposals = function() {
|
||||
var ret = [];
|
||||
this.txProposals.txps.forEach(function(txp) {
|
||||
var self= this;
|
||||
self.txProposals.txps.forEach(function(txp) {
|
||||
var i = {txp:txp};
|
||||
i.signedByUs = txp.signedBy[this.privateKey.id]?true:false;
|
||||
i.ntxid = txp.builder.build().getNormalizedHash();
|
||||
i.signedByUs = txp.signedBy[self.privateKey.id]?true:false;
|
||||
ret.push(i);
|
||||
});
|
||||
return ret;
|
||||
};
|
||||
|
||||
// TODO: this can be precalculated.
|
||||
Wallet.prototype._findTxByNtxid = function(ntxid) {
|
||||
var ret;
|
||||
var l = this.txProposals.txps.length;
|
||||
var id = ntxid.toString('hex');
|
||||
for(var i=0; i<l; i++) {
|
||||
var txp = this.txProposals.txps[i];
|
||||
var id2 = txp.builder.build().getNormalizedHash().toString('hex');
|
||||
if (id === id2 ) {
|
||||
ret = txp;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.sign = function(ntxid) {
|
||||
var txp = this._findTxByNtxid(ntxid);
|
||||
if (!txp) return;
|
||||
|
||||
var pkr = this.publicKeyRing;
|
||||
var keys = this.privateKey.getAll(pkr.addressIndex, pkr.changeAddressIndex);
|
||||
var ret = txp.builder.sign(keys);
|
||||
|
||||
if (ret.signaturesAdded) {
|
||||
txp.signedBy[this.privateKey.id] = Date.now();
|
||||
w.store();
|
||||
this.sendTxProposals();
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.addSeenToTxProposals = function() {
|
||||
var ret=false;
|
||||
var self=this;
|
||||
|
||||
this.txProposals.txps.forEach(function(txp) {
|
||||
if (!txp.seenBy[this.privateKey.id]) {
|
||||
txp.seenBy[this.privateKey.id] = Date.now();
|
||||
if (!txp.seenBy[self.privateKey.id]) {
|
||||
txp.seenBy[self.privateKey.id] = Date.now();
|
||||
ret = true;
|
||||
}
|
||||
});
|
||||
|
|
@ -181,7 +247,33 @@ Wallet.prototype.listUnspent = function(cb) {
|
|||
this.blockchain.listUnspent(this.getAddressesStr(), cb);
|
||||
};
|
||||
|
||||
Wallet.prototype.createTx = function(toAddress, amountSatStr, utxos, opts) {
|
||||
|
||||
Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
|
||||
var self = this;
|
||||
if (typeof opts === 'function') {
|
||||
cb = opts;
|
||||
opts = {};
|
||||
}
|
||||
opts = opts || {};
|
||||
|
||||
if (typeof opts.spendUnconfirmed === 'undefined') {
|
||||
opts.spendUnconfirmed = this.spendUnconfirmed;
|
||||
}
|
||||
|
||||
if (!opts.remainderOut) {
|
||||
opts.remainderOut={ address: this.publicKeyRing.generateAddress(true).toString()};
|
||||
}
|
||||
|
||||
self.listUnspent(function(utxos) {
|
||||
// TODO check enough funds, etc.
|
||||
self.createTxSync(toAddress, amountSatStr, utxos, opts);
|
||||
self.store();
|
||||
self.sendTxProposals();
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
|
||||
var pkr = this.publicKeyRing;
|
||||
var priv = this.privateKey;
|
||||
opts = opts || {};
|
||||
|
|
@ -216,65 +308,14 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, utxos, opts) {
|
|||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.sign = function(txp) {
|
||||
Wallet.prototype.connectTo = function(peerId) {
|
||||
this.network.connectTo(peerId);
|
||||
};
|
||||
|
||||
// // HERE? not sure
|
||||
// Wallet.prototype.cleanPeers = function() {
|
||||
// this.storage.remove('peerData');
|
||||
// };
|
||||
//
|
||||
|
||||
Wallet.getRandomId = function() {
|
||||
var r = buffertools.toHex(coinUtil.generateNonce());
|
||||
return r;
|
||||
};
|
||||
|
||||
/*
|
||||
* WalletFactory
|
||||
*
|
||||
*/
|
||||
|
||||
var WalletFactory = function() {
|
||||
this.storage = Storage.default();
|
||||
};
|
||||
|
||||
WalletFactory.prototype.create = function(config, opts) {
|
||||
var w = new Wallet(config);
|
||||
w.create(opts);
|
||||
w.store();
|
||||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.get = function(config, walletId) {
|
||||
return Wallet.read(config, walletId);
|
||||
};
|
||||
|
||||
WalletFactory.prototype.remove = function(walletId) {
|
||||
// TODO remove wallet contents, not only the id (Wallet.remove?)
|
||||
this._delWalletId(walletId);
|
||||
};
|
||||
|
||||
WalletFactory.prototype.addWalletId = function(walletId) {
|
||||
var ids = this.getWalletIds();
|
||||
if (ids.indexOf(walletId) !== -1) return;
|
||||
ids.push(walletId);
|
||||
this.storage.setGlobal('walletIds', ids);
|
||||
};
|
||||
|
||||
WalletFactory.prototype._delWalletId = function(walletId) {
|
||||
var ids = this.getWalletIds();
|
||||
var index = ids.indexOf(walletId);
|
||||
if (index === -1) return;
|
||||
ids.splice(index, 1); // removes walletId
|
||||
this.storage.setGlobal('walletIds', ids);
|
||||
};
|
||||
|
||||
WalletFactory.prototype.getWalletIds = function() {
|
||||
var ids = this.storage.getGlobal('walletIds');
|
||||
return ids || [];
|
||||
};
|
||||
|
||||
Wallet.factory = new WalletFactory();
|
||||
;
|
||||
|
||||
module.exports = require('soop')(Wallet);
|
||||
|
|
|
|||
145
js/models/core/WalletFactory.js
Normal file
145
js/models/core/WalletFactory.js
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
'use strict';
|
||||
|
||||
var imports = require('soop').imports();
|
||||
var Storage = imports.Storage;
|
||||
var Network = imports.Network;
|
||||
var Blockchain = imports.Blockchain;
|
||||
|
||||
var TxProposals = require('./TxProposals');
|
||||
var PublicKeyRing = require('./PublicKeyRing');
|
||||
var PrivateKey = require('./PrivateKey');
|
||||
var Wallet = require('./Wallet');
|
||||
|
||||
/*
|
||||
* WalletFactory
|
||||
*
|
||||
*
|
||||
* var wallet = WF.read(config,walletId); -> always go to storage
|
||||
* var wallet = WF.create(config,walletId); -> create wallets, with the given ID (or random is not given)
|
||||
*
|
||||
* var wallet = WF.open(config,walletId); -> try to read walletId, if fails, create a new wallet with that id
|
||||
*/
|
||||
|
||||
function WalletFactory(config) {
|
||||
var self = this;
|
||||
this.storage = new Storage(config.storage);
|
||||
this.network = new Network(config.network);
|
||||
this.blockchain = new Blockchain(config.blockchain);
|
||||
|
||||
this.networkName = config.networkName;
|
||||
this.verbose = config.verbose;
|
||||
this.walletDefaults = config.wallet;
|
||||
}
|
||||
|
||||
WalletFactory.prototype.log = function(){
|
||||
if (!this.verbose) return;
|
||||
console.log(arguments);
|
||||
};
|
||||
|
||||
|
||||
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')
|
||||
;
|
||||
return ret;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.read = function(walletId) {
|
||||
if (! this._checkRead(walletId)) return false;
|
||||
|
||||
var s = this.storage;
|
||||
var opts = s.get(walletId, 'opts');
|
||||
|
||||
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;
|
||||
|
||||
var w = new Wallet(opts);
|
||||
|
||||
// JIC: Add our key
|
||||
try {
|
||||
w.publicKeyRing.addCopayer(
|
||||
w.privateKey.getBIP32().extendedPublicKeyString()
|
||||
);
|
||||
} catch (e) {
|
||||
this.log('NOT NECCESARY AN ERROR:', e); //TODO
|
||||
}
|
||||
this.log('### WALLET OPENED:', w.id);
|
||||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.create = function(opts) {
|
||||
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.getBIP32().extendedPublicKeyString());
|
||||
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 = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
|
||||
opts.requiredCopayers = requiredCopayers;
|
||||
opts.totalCopayers = totalCopayers;
|
||||
var w = new Wallet(opts);
|
||||
w.store();
|
||||
this.addWalletId(w.id);
|
||||
return w;
|
||||
};
|
||||
|
||||
WalletFactory.prototype.open = function(walletId) {
|
||||
if(!WalletFactory.read(walletId)) {
|
||||
WalletFactory.create({id: walletId});
|
||||
}
|
||||
};
|
||||
|
||||
WalletFactory.prototype.getWalletIds = function() {
|
||||
var ids = this.storage.getGlobal('walletIds');
|
||||
return ids || [];
|
||||
}
|
||||
|
||||
WalletFactory.prototype._delWalletId = function(walletId) {
|
||||
var ids = this.getWalletIds();
|
||||
var index = ids.indexOf(walletId);
|
||||
if (index === -1) return;
|
||||
ids.splice(index, 1); // removes walletId
|
||||
this.storage.setGlobal('walletIds', ids);
|
||||
};
|
||||
|
||||
WalletFactory.prototype.remove = function(walletId) {
|
||||
WalletFactory._delWalletId(walletId);
|
||||
// TODO remove wallet contents, not only the id (Wallet.remove?)
|
||||
};
|
||||
|
||||
WalletFactory.prototype.addWalletId = function(walletId) {
|
||||
var ids = this.getWalletIds();
|
||||
if (ids.indexOf(walletId) !== -1) return;
|
||||
ids.push(walletId);
|
||||
this.storage.setGlobal('walletIds', ids);
|
||||
};
|
||||
|
||||
module.exports = require('soop')(WalletFactory);
|
||||
|
|
@ -131,8 +131,7 @@ Network.prototype._addPeer = function(peerId, isInbound) {
|
|||
}
|
||||
};
|
||||
|
||||
Network.prototype._setupConnectionHandlers = function(
|
||||
dataConn, isInbound, openCallback, closeCallback) {
|
||||
Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
|
||||
|
||||
var self=this;
|
||||
|
||||
|
|
@ -144,7 +143,7 @@ Network.prototype._setupConnectionHandlers = function(
|
|||
|
||||
self._addPeer(dataConn.peer, isInbound);
|
||||
self._notify( isInbound ? dataConn.peer : null);
|
||||
if (typeof openCallback === 'function') openCallback();
|
||||
this.emit('open');
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -161,7 +160,7 @@ Network.prototype._setupConnectionHandlers = function(
|
|||
console.log('### CLOSE RECV FROM:', dataConn.peer);
|
||||
|
||||
self._onClose(dataConn.peer);
|
||||
if (typeof closeCallback === 'function') closeCallback();
|
||||
this.emit('close');
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -174,7 +173,6 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
var self=this;
|
||||
var p = this.peer;
|
||||
|
||||
|
||||
p.on('open', function(peerId) {
|
||||
self.peerId = peerId;
|
||||
self.connectedPeers = [peerId];
|
||||
|
|
@ -218,8 +216,6 @@ Network.prototype.start = function(openCallback) {
|
|||
|
||||
Network.prototype._sendToOne = function(peerId, data, cb) {
|
||||
if (peerId !== this.peerId) {
|
||||
console.log('[WebRTC.js.222:peerId:]',peerId, data); //TODO
|
||||
|
||||
var conns = this.peer.connections[peerId];
|
||||
|
||||
if (conns) {
|
||||
|
|
@ -257,7 +253,7 @@ Network.prototype.send = function(peerIds, data, cb) {
|
|||
self._sendToOne(peerIds, data, cb);
|
||||
};
|
||||
|
||||
Network.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
|
||||
Network.prototype.connectTo = function(peerId) {
|
||||
var self = this;
|
||||
|
||||
console.log('### STARTING TO CONNECT TO:' + peerId );
|
||||
|
|
@ -269,7 +265,7 @@ Network.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
|
|||
metadata: { message: 'hi copayer!' }
|
||||
});
|
||||
|
||||
self._setupConnectionHandlers(dataConn, false, openCallback, closeCallback);
|
||||
self._setupConnectionHandlers(dataConn, false);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue